예제 #1
0
def get_route_table_info(connection, module, route_table):
    result = get_route_table_by_id(connection, module, route_table['RouteTableId'])
    try:
        result['Tags'] = describe_ec2_tags(connection, module, route_table['RouteTableId'])
    except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
        module.fail_json_aws(e, msg="Couldn't get tags for route table")
    result = camel_dict_to_snake_dict(result, ignore_list=['Tags'])
    # backwards compatibility
    result['id'] = result['route_table_id']
    return result
예제 #2
0
def get_route_table_by_tags(connection, module, vpc_id, tags):
    count = 0
    route_table = None
    filters = ansible_dict_to_boto3_filter_list({'vpc-id': vpc_id})
    try:
        route_tables = describe_route_tables_with_backoff(connection, Filters=filters)
    except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
        module.fail_json_aws(e, msg="Couldn't get route table")
    for table in route_tables:
        this_tags = describe_ec2_tags(connection, module, table['RouteTableId'])
        if tags_match(tags, this_tags):
            route_table = table
            count += 1

    if count > 1:
        module.fail_json(msg="Tags provided do not identify a unique route table")
    else:
        return route_table
예제 #3
0
def ensure_route_table_present(connection, module):

    lookup = module.params.get('lookup')
    propagating_vgw_ids = module.params.get('propagating_vgw_ids')
    purge_routes = module.params.get('purge_routes')
    purge_subnets = module.params.get('purge_subnets')
    purge_tags = module.params.get('purge_tags')
    route_table_id = module.params.get('route_table_id')
    subnets = module.params.get('subnets')
    tags = module.params.get('tags')
    vpc_id = module.params.get('vpc_id')
    routes = create_route_spec(connection, module, vpc_id)

    changed = False
    tags_valid = False

    if lookup == 'tag':
        if tags is not None:
            try:
                route_table = get_route_table_by_tags(connection, module,
                                                      vpc_id, tags)
            except (botocore.exceptions.ClientError,
                    botocore.exceptions.BotoCoreError) as e:
                module.fail_json_aws(
                    e, msg="Error finding route table with lookup 'tag'")
        else:
            route_table = None
    elif lookup == 'id':
        try:
            route_table = get_route_table_by_id(connection, module,
                                                route_table_id)
        except (botocore.exceptions.ClientError,
                botocore.exceptions.BotoCoreError) as e:
            module.fail_json_aws(
                e, msg="Error finding route table with lookup 'id'")

    # If no route table returned then create new route table
    if route_table is None:
        changed = True
        if not module.check_mode:
            try:
                route_table = connection.create_route_table(
                    aws_retry=True, VpcId=vpc_id)['RouteTable']
                # try to wait for route table to be present before moving on
                get_waiter(connection, 'route_table_exists').wait(
                    RouteTableIds=[route_table['RouteTableId']], )
            except botocore.exceptions.WaiterError as e:
                module.fail_json_aws(
                    e, msg='Timeout waiting for route table creation')
            except (botocore.exceptions.ClientError,
                    botocore.exceptions.BotoCoreError) as e:
                module.fail_json_aws(e, msg="Error creating route table")
        else:
            route_table = {
                "id": "rtb-xxxxxxxx",
                "route_table_id": "rtb-xxxxxxxx",
                "vpc_id": vpc_id
            }
            module.exit_json(changed=changed, route_table=route_table)

    if routes is not None:
        result = ensure_routes(connection=connection,
                               module=module,
                               route_table=route_table,
                               route_specs=routes,
                               propagating_vgw_ids=propagating_vgw_ids,
                               check_mode=module.check_mode,
                               purge_routes=purge_routes)
        changed = changed or result['changed']

    if propagating_vgw_ids is not None:
        result = ensure_propagation(connection=connection,
                                    module=module,
                                    route_table=route_table,
                                    propagating_vgw_ids=propagating_vgw_ids,
                                    check_mode=module.check_mode)
        changed = changed or result['changed']

    if not tags_valid and tags is not None:
        changed |= ensure_ec2_tags(
            connection,
            module,
            route_table['RouteTableId'],
            tags=tags,
            purge_tags=purge_tags,
            retry_codes=['InvalidRouteTableID.NotFound'])
        route_table['Tags'] = describe_ec2_tags(connection, module,
                                                route_table['RouteTableId'])

    if subnets is not None:
        associated_subnets = find_subnets(connection, module, vpc_id, subnets)

        result = ensure_subnet_associations(connection=connection,
                                            module=module,
                                            route_table=route_table,
                                            subnets=associated_subnets,
                                            check_mode=module.check_mode,
                                            purge_subnets=purge_subnets)
        changed = changed or result['changed']

    if changed:
        # pause to allow route table routes/subnets/associations to be updated before exiting with final state
        sleep(5)
    module.exit_json(changed=changed,
                     route_table=get_route_table_info(connection, module,
                                                      route_table))