Skip to content

Commit

Permalink
Merge pull request ansible-collections#537 from tremble/ec2_vpc_endpo…
Browse files Browse the repository at this point in the history
…int_info/retries

Update ec2_vpc_endpoint_info AWS retries

Reviewed-by: https://github.com/apps/ansible-zuul
  • Loading branch information
ansible-zuul[bot] authored Apr 12, 2021
2 parents 1cd3938 + 6c19542 commit 8612c78
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/537-ec2_vpc_endpoint_info-retries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- ec2_vpc_endpoint_info - use boto3 paginator when fetching services (https://github.com/ansible-collections/community.aws/pull/537).
- ec2_vpc_endpoint_info - ensure paginated endpoint description is retried on common AWS failures (https://github.com/ansible-collections/community.aws/pull/537).
34 changes: 20 additions & 14 deletions plugins/modules/ec2_vpc_endpoint_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,42 @@
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list


@AWSRetry.exponential_backoff()
@AWSRetry.jittered_backoff()
def _describe_endpoints(client, **params):
paginator = client.get_paginator('describe_vpc_endpoints')
return paginator.paginate(**params).build_full_result()


@AWSRetry.jittered_backoff()
def _describe_endpoint_services(client, **params):
paginator = client.get_paginator('describe_vpc_endpoint_services')
return paginator.paginate(**params).build_full_result()


def get_supported_services(client, module):
results = list()
params = dict()
while True:
response = client.describe_vpc_endpoint_services(**params)
results.extend(response['ServiceNames'])
if 'NextToken' in response:
params['NextToken'] = response['NextToken']
else:
break
try:
services = _describe_endpoint_services(client)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Failed to get endpoint servicess")

results = list(services['ServiceNames'])
return dict(service_names=results)


@AWSRetry.exponential_backoff()
def get_endpoints(client, module):
results = list()
params = dict()
params['Filters'] = ansible_dict_to_boto3_filter_list(module.params.get('filters'))
if module.params.get('vpc_endpoint_ids'):
params['VpcEndpointIds'] = module.params.get('vpc_endpoint_ids')
try:
paginator = client.get_paginator('describe_vpc_endpoints')
results = paginator.paginate(**params).build_full_result()['VpcEndpoints']

results = _describe_endpoints(client, **params)['VpcEndpoints']
results = normalize_boto3_result(results)
except is_boto3_error_code('InvalidVpcEndpointId.NotFound'):
module.exit_json(msg='VpcEndpoint {0} does not exist'.format(module.params.get('vpc_endpoint_ids')), vpc_endpoints=[])
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Failed to get endpoints")

return dict(vpc_endpoints=[camel_dict_to_snake_dict(result) for result in results])


Expand Down
13 changes: 9 additions & 4 deletions tests/integration/targets/ec2_vpc_endpoint/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@
route_table_ids:
- '{{ rtb_igw_id }}'
tags:
testPrefix: '{{ resource_prefix }}'
camelCase: "helloWorld"
PascalCase: "HelloWorld"
snake_case: "hello_world"
Expand All @@ -547,7 +548,8 @@
assert:
that:
- tag_vpc_endpoint.changed
- tag_vpc_endpoint.result.tags | length == 5
- tag_vpc_endpoint.result.tags | length == 6
- endpoint_tags["testPrefix"] == resource_prefix
- endpoint_tags["camelCase"] == "helloWorld"
- endpoint_tags["PascalCase"] == "HelloWorld"
- endpoint_tags["snake_case"] == "hello_world"
Expand All @@ -560,8 +562,8 @@
ec2_vpc_endpoint_info:
query: endpoints
filters:
"tag:camelCase":
- "helloWorld"
"tag:testPrefix":
- "{{ resource_prefix }}"
register: tag_result

- name: Assert tag lookup found endpoint
Expand All @@ -582,6 +584,7 @@
route_table_ids:
- '{{ rtb_igw_id }}'
tags:
testPrefix: '{{ resource_prefix }}'
camelCase: "helloWorld"
PascalCase: "HelloWorld"
snake_case: "hello_world"
Expand All @@ -603,6 +606,7 @@
route_table_ids:
- '{{ rtb_igw_id }}'
tags:
testPrefix: '{{ resource_prefix }}'
camelCase: "helloWorld"
PascalCase: "HelloWorld"
snake_case: "hello_world"
Expand Down Expand Up @@ -649,7 +653,8 @@
assert:
that:
- add_tag_vpc_endpoint.changed
- add_tag_vpc_endpoint.result.tags | length == 6
- add_tag_vpc_endpoint.result.tags | length == 7
- endpoint_tags["testPrefix"] == resource_prefix
- endpoint_tags["camelCase"] == "helloWorld"
- endpoint_tags["PascalCase"] == "HelloWorld"
- endpoint_tags["snake_case"] == "hello_world"
Expand Down

0 comments on commit 8612c78

Please sign in to comment.