From db76089eaacf06fe2a25b57e6a662b2c1e2ad83d Mon Sep 17 00:00:00 2001 From: Herby Gillot Date: Thu, 11 Jun 2015 14:01:40 +1000 Subject: [PATCH] Allow VPC igw to be specified by gateway_id: "igw" --- .../cloud/amazon/ec2_vpc_route_table.py | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/extras/cloud/amazon/ec2_vpc_route_table.py b/lib/ansible/modules/extras/cloud/amazon/ec2_vpc_route_table.py index 491751e23d..dc21d9607e 100644 --- a/lib/ansible/modules/extras/cloud/amazon/ec2_vpc_route_table.py +++ b/lib/ansible/modules/extras/cloud/amazon/ec2_vpc_route_table.py @@ -45,7 +45,9 @@ options: description: - List of routes in the route table. Routes are specified''' ''' as dicts containing the keys 'dest' and one of 'gateway_id',''' -''' 'instance_id', 'interface_id', or 'vpc_peering_connection'. +''' 'instance_id', 'interface_id', or 'vpc_peering_connection'. ''' +''' If 'gateway_id' is specified, you can refer to the VPC's IGW ''' +''' by using the value "igw". required: true aliases: [] subnets: @@ -168,6 +170,10 @@ class AnsibleRouteTableException(Exception): pass +class AnsibleIgwSearchException(AnsibleRouteTableException): + pass + + class AnsibleTagCreationException(AnsibleRouteTableException): pass @@ -236,6 +242,29 @@ def find_subnets(vpc_conn, vpc_id, identified_subnets): return subnets_by_id + subnets_by_cidr + subnets_by_name +def find_igw(vpc_conn, vpc_id): + """ + Finds the Internet gateway for the given VPC ID. + + Raises an AnsibleIgwSearchException if either no IGW can be found, or more + than one found for the given VPC. + + Note that this function is duplicated in other ec2 modules, and should + potentially be moved into potentially be moved into a shared module_utils + """ + igw = vpc_conn.get_all_internet_gateways( + filters={'attachment.vpc-id': vpc_id}) + + if not igw: + return AnsibleIgwSearchException('No IGW found for VPC "{0}"'. + format(vpc_id)) + elif len(igw) == 1: + return igw[0].id + else: + raise AnsibleIgwSearchException('Multiple IGWs found for VPC "{0}"'. + format(vpc_id)) + + def get_resource_tags(vpc_conn, resource_id): return dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': resource_id})) @@ -525,6 +554,11 @@ def main(): for route_spec in routes: rename_key(route_spec, 'dest', 'destination_cidr_block') + if 'gateway_id' in route_spec and route_spec['gateway_id'] and \ + route_spec['gateway_id'].lower() == 'igw': + igw = find_igw(vpc_conn, vpc_id) + route_spec['gateway_id'] = igw + subnets = module.params.get('subnets') state = module.params.get('state', 'present')