From c19c9e83f1ece592abf7b5be776f1a52bc2c8e6b Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Tue, 12 Jul 2022 19:56:22 -0300 Subject: [PATCH 01/22] feat(module/vpc-cagw): Add Carrier Gateway modules ``` $ ansible localhost -m ec2_vpc_cagw_info localhost | SUCCESS => { "carrier_gateways": [ { "carrier_gateway_id": "cagw-037df45cae5362d59", "tags": { "Name": "test1-54dsl-vpc-cagw" }, "vpc_id": "vpc-069cabb60c7e7fc6d" } ], "changed": false } $ ansible localhost -m ec2_vpc_cagw -a "state=absent vpc_id=vpc-069cabb60c7e7fc6d carrier_gateway_id=cagw-037df45cae5362d59" localhost | CHANGED => { "changed": true } $ ansible localhost -m ec2_vpc_cagw_info localhost | SUCCESS => { "carrier_gateways": [], "changed": false } $ ansible localhost -m ec2_vpc_cagw -a "vpc_id=vpc-069cabb60c7e7fc6d" localhost | CHANGED => { "carrier_gateway_id": "cagw-095f998ebdcb5ef86", "changed": true, "tags": {}, "vpc_id": "vpc-069cabb60c7e7fc6d" } $ ansible localhost -m ec2_vpc_cagw_info localhost | SUCCESS => { "carrier_gateways": [ { "carrier_gateway_id": "cagw-095f998ebdcb5ef86", "tags": {}, "vpc_id": "vpc-069cabb60c7e7fc6d" } ], "changed": false } ``` --- plugins/modules/ec2_vpc_cagw.py | 243 +++++++++++++++++++++++++++ plugins/modules/ec2_vpc_cagw_info.py | 166 ++++++++++++++++++ 2 files changed, 409 insertions(+) create mode 100644 plugins/modules/ec2_vpc_cagw.py create mode 100644 plugins/modules/ec2_vpc_cagw_info.py diff --git a/plugins/modules/ec2_vpc_cagw.py b/plugins/modules/ec2_vpc_cagw.py new file mode 100644 index 00000000000..56d70a41a5e --- /dev/null +++ b/plugins/modules/ec2_vpc_cagw.py @@ -0,0 +1,243 @@ +#!/usr/bin/python +# Copyright: Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = ''' +--- +module: ec2_vpc_cagw +version_added: 1.0.0 +short_description: Manage an AWS VPC Carrier gateway +description: + - Manage an AWS VPC Carrier gateway +author: "Marco Braga (@mtulio)" +options: + vpc_id: + description: + - The VPC ID for the VPC in which to manage the Carrier Gateway. + required: true + type: str + state: + description: + - Create or terminate the CAGW + default: present + choices: [ 'present', 'absent' ] + type: str +notes: +- Support for I(purge_tags) was added in release 1.3.0. +extends_documentation_fragment: +- amazon.aws.aws +- amazon.aws.ec2 +- amazon.aws.tags +''' + +EXAMPLES = ''' +# Note: These examples do not set authentication details, see the AWS Guide for details. + +# Ensure that the VPC has an Carrier Gateway. +# The Carrier Gateway ID is can be accessed via {{cagw.carrier_gateway_id}} for use in setting up NATs etc. +- name: Create Carrier gateway + community.aws.ec2_vpc_cagw: + vpc_id: vpc-abcdefgh + state: present + register: cagw + +- name: Create Carrier gateway with tags + community.aws.ec2_vpc_cagw: + vpc_id: vpc-abcdefgh + state: present + tags: + Tag1: tag1 + Tag2: tag2 + register: cagw + +- name: Delete Carrier gateway + community.aws.ec2_vpc_cagw: + state: absent + vpc_id: vpc-abcdefgh + register: vpc_cagw_delete +''' + +RETURN = ''' +changed: + description: If any changes have been made to the Carrier Gateway. + type: bool + returned: always + sample: + changed: false +carrier_gateway_id: + description: The unique identifier for the Carrier Gateway. + type: str + returned: I(state=present) + sample: + carrier_gateway_id: "cagw-XXXXXXXX" +tags: + description: The tags associated the Carrier Gateway. + type: dict + returned: I(state=present) + sample: + tags: + "Ansible": "Test" +vpc_id: + description: The VPC ID associated with the Carrier Gateway. + type: str + returned: I(state=present) + sample: + vpc_id: "vpc-XXXXXXXX" +''' + +try: + import botocore +except ImportError: + pass # caught by AnsibleAWSModule + +from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list +from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict + +@AWSRetry.jittered_backoff(retries=10, delay=10) +def describe_cagws_with_backoff(connection, **params): + paginator = connection.get_paginator('describe_carrier_gateways') + return paginator.paginate(**params).build_full_result()['CarrierGateways'] + + +class AnsibleEc2Cagw(): + + def __init__(self, module, results): + self._module = module + self._results = results + self._connection = self._module.client( + 'ec2', retry_decorator=AWSRetry.jittered_backoff() + ) + self._check_mode = self._module.check_mode + + def process(self): + vpc_id = self._module.params.get('vpc_id') + state = self._module.params.get('state', 'present') + tags = self._module.params.get('tags') + purge_tags = self._module.params.get('purge_tags') + + if state == 'present': + self.ensure_cagw_present(vpc_id, tags, purge_tags) + elif state == 'absent': + self.ensure_cagw_absent(vpc_id) + + def get_matching_cagw(self, vpc_id, carrier_gateway_id=None): + ''' + Returns the carrier gateway found. + Parameters: + vpc_id (str): VPC ID + carrier_gateway_id (str): Carrier Gateway ID, if specified + Returns: + cagw (dict): dict of cagw found, None if none found + ''' + filters = ansible_dict_to_boto3_filter_list({'vpc-id': vpc_id}) + try: + if not carrier_gateway_id: + cagws = describe_cagws_with_backoff(self._connection, Filters=filters) + else: + cagws = describe_cagws_with_backoff(self._connection, CarrierGatewayIds=[carrier_gateway_id]) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + self._module.fail_json_aws(e) + + cagw = None + if len(cagws) > 1: + self._module.fail_json( + msg='EC2 returned more than one Carrier Gateway for VPC {0}, aborting' + .format(vpc_id)) + elif cagws: + cagw = camel_dict_to_snake_dict(cagws[0]) + + return cagw + + @staticmethod + def get_cagw_info(cagw, vpc_id): + return { + 'carrier_gateway_id': cagw['carrier_gateway_id'], + 'tags': boto3_tag_list_to_ansible_dict(cagw['tags']), + 'vpc_id': vpc_id + } + + def ensure_cagw_absent(self, vpc_id): + cagw = self.get_matching_cagw(vpc_id) + if cagw is None: + return self._results + + if self._check_mode: + self._results['changed'] = True + return self._results + + try: + self._results['changed'] = True + self._connection.delete_carrier_gateway( + aws_retry=True, + CarrierGatewayId=cagw['carrier_gateway_id'] + ) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + self._module.fail_json_aws(e, msg="Unable to delete Carrier Gateway") + + return self._results + + def ensure_cagw_present(self, vpc_id, tags, purge_tags): + cagw = self.get_matching_cagw(vpc_id) + + if cagw is None: + if self._check_mode: + self._results['changed'] = True + self._results['carrier_gateway_id'] = None + return self._results + + try: + response = self._connection.create_carrier_gateway(VpcId=vpc_id, aws_retry=True) + cagw = camel_dict_to_snake_dict(response['CarrierGateway']) + self._results['changed'] = True + except botocore.exceptions.WaiterError as e: + self._module.fail_json_aws(e, msg="No Carrier Gateway exists.") + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + self._module.fail_json_aws(e, msg='Unable to create Carrier Gateway') + + # Modify tags + self._results['changed'] |= ensure_ec2_tags( + self._connection, self._module, cagw['carrier_gateway_id'], + resource_type='carrier-gateway', tags=tags, purge_tags=purge_tags, + retry_codes='InvalidCarrierGatewayID.NotFound' + ) + + # Update cagw + cagw = self.get_matching_cagw(vpc_id, carrier_gateway_id=cagw['carrier_gateway_id']) + cagw_info = self.get_cagw_info(cagw, vpc_id) + self._results.update(cagw_info) + + return self._results + + +def main(): + argument_spec = dict( + carrier_gateway_id=dict(required=False), + vpc_id=dict(required=False), + state=dict(default='present', choices=['present', 'absent']), + tags=dict(required=False, type='dict', aliases=['resource_tags']), + purge_tags=dict(default=True, type='bool'), + ) + + module = AnsibleAWSModule( + argument_spec=argument_spec, + supports_check_mode=True, + ) + results = dict( + changed=False + ) + cagw_manager = AnsibleEc2Cagw(module=module, results=results) + cagw_manager.process() + + module.exit_json(**results) + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/ec2_vpc_cagw_info.py b/plugins/modules/ec2_vpc_cagw_info.py new file mode 100644 index 00000000000..4ad4feba0f1 --- /dev/null +++ b/plugins/modules/ec2_vpc_cagw_info.py @@ -0,0 +1,166 @@ +#!/usr/bin/python +# Copyright: Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = r''' +--- +module: ec2_vpc_cagw_info +version_added: 1.0.0 +short_description: Gather information about carrier gateways in AWS +description: + - Gather information about carrier gateways in AWS. +author: "Marco Braga (@mtulio)" +options: + filters: + description: + - A dict of filters to apply. Each dict item consists of a filter key and a filter value. + See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeCarrierGateways.html) for possible filters. + type: dict + carrier_gateway_ids: + description: + - Get details of specific Carrier Gateway ID. Provide this value as a list. + type: list + elements: str + convert_tags: + description: + - Convert tags from boto3 format (list of dictionaries) to the standard dictionary format. + - Prior to release 4.0.0 this defaulted to C(False). + default: True + type: bool + version_added: 1.3.0 +extends_documentation_fragment: +- amazon.aws.aws +- amazon.aws.ec2 + +''' + +EXAMPLES = r''' +# # Note: These examples do not set authentication details, see the AWS Guide for details. + +- name: Gather information about all Carrier Gateways for an account or profile + amazon.aws.ec2_vpc_cagw_info: + region: ap-southeast-2 + profile: production + register: cagw_info + +- name: Gather information about a filtered list of Carrier Gateways + amazon.aws.ec2_vpc_cagw_info: + region: ap-southeast-2 + profile: production + filters: + "tag:Name": "cagw-123" + register: cagw_info + +- name: Gather information about a specific carrier gateway by CarrierGatewayId + amazon.aws.ec2_vpc_cagw_info: + region: ap-southeast-2 + profile: production + carrier_gateway_ids: cagw-c1231234 + register: cagw_info +''' + +RETURN = r''' +changed: + description: True if listing the carrier gateways succeeds. + type: bool + returned: always + sample: "false" +carrier_gateways: + description: The carrier gateways for the account. + returned: always + type: complex + contains: + vpc_id: + description: The ID of the VPC. + returned: I(state=present) + type: str + sample: vpc-02123b67 + carrier_gateway_id: + description: The ID of the carrier gateway. + returned: I(state=present) + type: str + sample: cagw-2123634d + tags: + description: Any tags assigned to the carrier gateway. + returned: I(state=present) + type: dict + sample: + tags: + "Ansible": "Test" +''' + +try: + import botocore +except ImportError: + pass # Handled by AnsibleAWSModule + +from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict + + +def get_carrier_gateway_info(carrier_gateway, convert_tags): + if convert_tags: + tags = boto3_tag_list_to_ansible_dict(carrier_gateway['Tags']) + ignore_list = ["Tags"] + else: + tags = carrier_gateway['Tags'] + ignore_list = [] + carrier_gateway_info = {'CarrierGatewayId': carrier_gateway['CarrierGatewayId'], + 'VpcId': carrier_gateway['VpcId'], + 'Tags': tags} + + carrier_gateway_info = camel_dict_to_snake_dict(carrier_gateway_info, ignore_list=ignore_list) + return carrier_gateway_info + + +def list_carrier_gateways(connection, module): + params = dict() + + params['Filters'] = ansible_dict_to_boto3_filter_list(module.params.get('filters')) + convert_tags = module.params.get('convert_tags') + + if module.params.get("carrier_gateway_ids"): + params['CarrierGatewayIds'] = module.params.get("carrier_gateway_ids") + + try: + all_carrier_gateways = connection.describe_carrier_gateways(aws_retry=True, **params) + except is_boto3_error_code('InvalidCarrierGatewayID.NotFound'): + module.fail_json('CarrierGateway not found') + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except + module.fail_json_aws(e, 'Unable to describe carrier gateways') + + return [get_carrier_gateway_info(cagw, convert_tags) + for cagw in all_carrier_gateways['CarrierGateways']] + + +def main(): + argument_spec = dict( + filters=dict(type='dict', default=dict()), + carrier_gateway_ids=dict(type='list', default=None, elements='str'), + convert_tags=dict(type='bool', default=True), + ) + + module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) + + # Validate Requirements + try: + connection = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff()) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg='Failed to connect to AWS') + + # call your function here + results = list_carrier_gateways(connection, module) + + module.exit_json(carrier_gateways=results) + + +if __name__ == '__main__': + main() From 73c816aed2f0cedcc35313e3793524c5e9c9944a Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Wed, 13 Jul 2022 12:52:46 -0300 Subject: [PATCH 02/22] Update plugins/modules/ec2_vpc_cagw.py Co-authored-by: Mark Chappell --- plugins/modules/ec2_vpc_cagw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/ec2_vpc_cagw.py b/plugins/modules/ec2_vpc_cagw.py index 56d70a41a5e..331e292f968 100644 --- a/plugins/modules/ec2_vpc_cagw.py +++ b/plugins/modules/ec2_vpc_cagw.py @@ -9,7 +9,7 @@ DOCUMENTATION = ''' --- module: ec2_vpc_cagw -version_added: 1.0.0 +version_added: 5.0.0 short_description: Manage an AWS VPC Carrier gateway description: - Manage an AWS VPC Carrier gateway From 2a2a0c3f551ca65b5756ef70f751aef356899cb8 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Wed, 13 Jul 2022 13:03:10 -0300 Subject: [PATCH 03/22] Apply suggestions from code review Co-authored-by: Mark Chappell --- plugins/modules/ec2_vpc_cagw.py | 17 +++++++++-------- plugins/modules/ec2_vpc_cagw_info.py | 23 +++++++---------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/plugins/modules/ec2_vpc_cagw.py b/plugins/modules/ec2_vpc_cagw.py index 331e292f968..e7eb25830b7 100644 --- a/plugins/modules/ec2_vpc_cagw.py +++ b/plugins/modules/ec2_vpc_cagw.py @@ -12,8 +12,9 @@ version_added: 5.0.0 short_description: Manage an AWS VPC Carrier gateway description: - - Manage an AWS VPC Carrier gateway -author: "Marco Braga (@mtulio)" + - Manage an AWS VPC Carrier gateway. +author: + - "Marco Braga (@mtulio)" options: vpc_id: description: @@ -22,16 +23,14 @@ type: str state: description: - - Create or terminate the CAGW + - Create or terminate the Carrier Gateway. default: present choices: [ 'present', 'absent' ] type: str -notes: -- Support for I(purge_tags) was added in release 1.3.0. extends_documentation_fragment: -- amazon.aws.aws -- amazon.aws.ec2 -- amazon.aws.tags + - amazon.aws.aws + - amazon.aws.ec2 + - amazon.aws.tags ''' EXAMPLES = ''' @@ -101,6 +100,7 @@ from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict + @AWSRetry.jittered_backoff(retries=10, delay=10) def describe_cagws_with_backoff(connection, **params): paginator = connection.get_paginator('describe_carrier_gateways') @@ -228,6 +228,7 @@ def main(): module = AnsibleAWSModule( argument_spec=argument_spec, + requires_one_of=[['vpc_id', 'carrier_gateway_id']], supports_check_mode=True, ) results = dict( diff --git a/plugins/modules/ec2_vpc_cagw_info.py b/plugins/modules/ec2_vpc_cagw_info.py index 4ad4feba0f1..47b6d99dec4 100644 --- a/plugins/modules/ec2_vpc_cagw_info.py +++ b/plugins/modules/ec2_vpc_cagw_info.py @@ -9,11 +9,12 @@ DOCUMENTATION = r''' --- module: ec2_vpc_cagw_info -version_added: 1.0.0 +version_added: 5.0.0 short_description: Gather information about carrier gateways in AWS description: - - Gather information about carrier gateways in AWS. -author: "Marco Braga (@mtulio)" + - Gather information about carrier gateways in AWS. +author: + - "Marco Braga (@mtulio)" options: filters: description: @@ -22,19 +23,12 @@ type: dict carrier_gateway_ids: description: - - Get details of specific Carrier Gateway ID. Provide this value as a list. + - Get details of specific Carrier Gateway ID. type: list elements: str - convert_tags: - description: - - Convert tags from boto3 format (list of dictionaries) to the standard dictionary format. - - Prior to release 4.0.0 this defaulted to C(False). - default: True - type: bool - version_added: 1.3.0 extends_documentation_fragment: -- amazon.aws.aws -- amazon.aws.ec2 + - amazon.aws.aws + - amazon.aws.ec2 ''' @@ -44,13 +38,11 @@ - name: Gather information about all Carrier Gateways for an account or profile amazon.aws.ec2_vpc_cagw_info: region: ap-southeast-2 - profile: production register: cagw_info - name: Gather information about a filtered list of Carrier Gateways amazon.aws.ec2_vpc_cagw_info: region: ap-southeast-2 - profile: production filters: "tag:Name": "cagw-123" register: cagw_info @@ -58,7 +50,6 @@ - name: Gather information about a specific carrier gateway by CarrierGatewayId amazon.aws.ec2_vpc_cagw_info: region: ap-southeast-2 - profile: production carrier_gateway_ids: cagw-c1231234 register: cagw_info ''' From ebabe783156ee655bf8d4426968c7af37145b659 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Wed, 13 Jul 2022 13:09:00 -0300 Subject: [PATCH 04/22] Update plugins/modules/ec2_vpc_cagw.py Co-authored-by: Mark Chappell --- plugins/modules/ec2_vpc_cagw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/ec2_vpc_cagw.py b/plugins/modules/ec2_vpc_cagw.py index e7eb25830b7..f374233d63a 100644 --- a/plugins/modules/ec2_vpc_cagw.py +++ b/plugins/modules/ec2_vpc_cagw.py @@ -8,7 +8,7 @@ DOCUMENTATION = ''' --- -module: ec2_vpc_cagw +module: ec2_carrier_gateway version_added: 5.0.0 short_description: Manage an AWS VPC Carrier gateway description: From 34669278efc909fdcb508db177f1627d1c75d982 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Thu, 14 Jul 2022 17:04:17 -0300 Subject: [PATCH 05/22] feat(module/vpc-cagw): renaming modules to ec2_carrier_gateway* --- .../{ec2_vpc_cagw.py => ec2_carrier_gateway.py} | 16 +++++++++++----- ..._cagw_info.py => ec2_carrier_gateway_info.py} | 14 +++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) rename plugins/modules/{ec2_vpc_cagw.py => ec2_carrier_gateway.py} (95%) rename plugins/modules/{ec2_vpc_cagw_info.py => ec2_carrier_gateway_info.py} (96%) diff --git a/plugins/modules/ec2_vpc_cagw.py b/plugins/modules/ec2_carrier_gateway.py similarity index 95% rename from plugins/modules/ec2_vpc_cagw.py rename to plugins/modules/ec2_carrier_gateway.py index f374233d63a..61a809a49a6 100644 --- a/plugins/modules/ec2_vpc_cagw.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -21,6 +21,11 @@ - The VPC ID for the VPC in which to manage the Carrier Gateway. required: true type: str + carrier_gateway_id: + description: + - The Carrier Gateway ID to manage the Carrier Gateway. + required: false + type: str state: description: - Create or terminate the Carrier Gateway. @@ -37,15 +42,15 @@ # Note: These examples do not set authentication details, see the AWS Guide for details. # Ensure that the VPC has an Carrier Gateway. -# The Carrier Gateway ID is can be accessed via {{cagw.carrier_gateway_id}} for use in setting up NATs etc. +# The Carrier Gateway ID can be accessed via {{cagw.carrier_gateway_id}} for use in setting up Route tables etc. - name: Create Carrier gateway - community.aws.ec2_vpc_cagw: + community.aws.ec2_carrier_gateway: vpc_id: vpc-abcdefgh state: present register: cagw - name: Create Carrier gateway with tags - community.aws.ec2_vpc_cagw: + community.aws.ec2_carrier_gateway: vpc_id: vpc-abcdefgh state: present tags: @@ -54,9 +59,10 @@ register: cagw - name: Delete Carrier gateway - community.aws.ec2_vpc_cagw: - state: absent + community.aws.ec2_carrier_gateway: vpc_id: vpc-abcdefgh + carrier_gateway_id: "cagw-123" + state: absent register: vpc_cagw_delete ''' diff --git a/plugins/modules/ec2_vpc_cagw_info.py b/plugins/modules/ec2_carrier_gateway_info.py similarity index 96% rename from plugins/modules/ec2_vpc_cagw_info.py rename to plugins/modules/ec2_carrier_gateway_info.py index 47b6d99dec4..3d66345e37d 100644 --- a/plugins/modules/ec2_vpc_cagw_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -6,9 +6,9 @@ __metaclass__ = type -DOCUMENTATION = r''' +DOCUMENTATION = ''' --- -module: ec2_vpc_cagw_info +module: ec2_carrier_gateway_info version_added: 5.0.0 short_description: Gather information about carrier gateways in AWS description: @@ -32,29 +32,29 @@ ''' -EXAMPLES = r''' +EXAMPLES = ''' # # Note: These examples do not set authentication details, see the AWS Guide for details. - name: Gather information about all Carrier Gateways for an account or profile - amazon.aws.ec2_vpc_cagw_info: + community.aws.ec2_carrier_gateway_info: region: ap-southeast-2 register: cagw_info - name: Gather information about a filtered list of Carrier Gateways - amazon.aws.ec2_vpc_cagw_info: + community.aws.ec2_carrier_gateway_info: region: ap-southeast-2 filters: "tag:Name": "cagw-123" register: cagw_info - name: Gather information about a specific carrier gateway by CarrierGatewayId - amazon.aws.ec2_vpc_cagw_info: + community.aws.ec2_carrier_gateway_info: region: ap-southeast-2 carrier_gateway_ids: cagw-c1231234 register: cagw_info ''' -RETURN = r''' +RETURN = ''' changed: description: True if listing the carrier gateways succeeds. type: bool From aba622abb3ef720c023bd64168bcb02604fc2616 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Thu, 14 Jul 2022 17:11:35 -0300 Subject: [PATCH 06/22] feat(module/ec2-cagw): add integration tests --- .../targets/ec2_carrier_gateway/aliases | 9 + .../ec2_carrier_gateway/defaults/main.yml | 3 + .../targets/ec2_carrier_gateway/meta/main.yml | 1 + .../ec2_carrier_gateway/tasks/main.yml | 200 ++++++++++++++++ .../ec2_carrier_gateway/tasks/tags.yml | 215 ++++++++++++++++++ 5 files changed, 428 insertions(+) create mode 100644 tests/integration/targets/ec2_carrier_gateway/aliases create mode 100644 tests/integration/targets/ec2_carrier_gateway/defaults/main.yml create mode 100644 tests/integration/targets/ec2_carrier_gateway/meta/main.yml create mode 100644 tests/integration/targets/ec2_carrier_gateway/tasks/main.yml create mode 100644 tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml diff --git a/tests/integration/targets/ec2_carrier_gateway/aliases b/tests/integration/targets/ec2_carrier_gateway/aliases new file mode 100644 index 00000000000..318034a776f --- /dev/null +++ b/tests/integration/targets/ec2_carrier_gateway/aliases @@ -0,0 +1,9 @@ +# reason: missing-policy +# To test Carrier Gateway in the VPC, the Wavelength subnet +# group should be enabled on the AWS Account. +unsupported + +cloud/aws + +ecs_carrier_gateway +ecs_carrier_gateway_info diff --git a/tests/integration/targets/ec2_carrier_gateway/defaults/main.yml b/tests/integration/targets/ec2_carrier_gateway/defaults/main.yml new file mode 100644 index 00000000000..2e8c38f8847 --- /dev/null +++ b/tests/integration/targets/ec2_carrier_gateway/defaults/main.yml @@ -0,0 +1,3 @@ +--- +vpc_name: '{{ resource_prefix }}-ec2-vpc-cagw' +cagw_name: '{{ resource_prefix }}-ec2-vpc-cagw' diff --git a/tests/integration/targets/ec2_carrier_gateway/meta/main.yml b/tests/integration/targets/ec2_carrier_gateway/meta/main.yml new file mode 100644 index 00000000000..32cf5dda7ed --- /dev/null +++ b/tests/integration/targets/ec2_carrier_gateway/meta/main.yml @@ -0,0 +1 @@ +dependencies: [] diff --git a/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml new file mode 100644 index 00000000000..73f3a58f0b2 --- /dev/null +++ b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml @@ -0,0 +1,200 @@ +--- +- name: 'ec2_carrier_gateway integration tests' + collections: + - community.aws + module_defaults: + group/aws: + aws_access_key: '{{ aws_access_key }}' + aws_secret_key: '{{ aws_secret_key }}' + security_token: '{{ security_token | default(omit) }}' + region: '{{ aws_region }}' + block: + + # ============================================================ + - debug: msg="Setting up test dependencies" + + - name: create a VPC + ec2_vpc_net: + name: "{{ vpc_name }}-{{ item }}" + state: present + cidr_block: "{{ vpc_cidr }}" + tags: + Description: "Created by ansible-test for CAGW tests" + register: vpc_result + loop: [1, 2] + + - name: use set fact for vpc ids + set_fact: + vpc_id_1: '{{ vpc_result.results.0.vpc.id }}' + vpc_id_2: '{{ vpc_result.results.1.vpc.id }}' + + # ============================================================ + - debug: msg="Running tests" + + - name: create carrier gateway and attach it to vpc + ec2_carrier_gateway: + state: present + vpc_id: '{{ vpc_id_1 }}' + name: "{{ cagw_name }}" + register: cagw + + - name: use set fact for cagw ids + set_fact: + cagw_id: '{{ cagw.carrier_gateway_id }}' + + - assert: + that: + - cagw.changed + - cagw.vpc_id == vpc_id_1 + - cagw.tags.Name == cagw_name + + - name: test idempotence + ec2_carrier_gateway: + state: present + vpc_id: '{{ vpc_id_1 }}' + name: "{{ cagw_name }}" + register: cagw + + - assert: + that: + - not cagw.changed + - cagw.carrier_gateway_id == cagw_id + + # ============================================================TODO + - name: attach carrier gateway to the other VPC + ec2_carrier_gateway: + state: present + vpc_id: '{{ vpc_id_2 }}' + name: "{{ cagw_name }}" + register: cagw + + - assert: + that: + - cagw.changed + - cagw.carrier_gateway_id == cagw_id + - cagw.vpc_id == vpc_id_2 + + # ============================================================ + + - name: get VPC CAGW facts by ID (CHECK) + ec2_carrier_gateway_info: + carrier_gateway_id: ['{{ cagw_id }}'] + register: cagw_info + check_mode: True + + - name: verify expected facts + vars: + cagw_details: '{{ cagw_info.carrier_gateways[0] }}' + assert: + that: + - cagw_info.carrier_gateways | length == 1 + - '"carrier_gateway_id" in cagw_details' + - '"tags" in cagw_details' + - '"vpc_id" in cagw_details' + - cagw_details.carrier_gateway_id == cagw_id + - '"Name" in cagw_details.tags' + - cagw_details.tags.Name == cagw_name + + - name: get VPC CAGW facts by Tag + ec2_carrier_gateway_info: + filters: + "tag:Name": "{{ cagw_name }}" + register: cagw_info + + - name: verify expected facts + vars: + cagw_details: '{{ cagw_info.virtual_gateways[0] }}' + assert: + that: + - cagw_info.virtual_gateways | length == 1 + - '"carrier_gateway_id" in cagw_details' + - '"state" in cagw_details' + - '"tags" in cagw_details' + - cagw_details.carrier_gateway_id == cagw_id + - '"Name" in cagw_details.tags' + - cagw_details.tags.Name == cagw_name + + + # ============================================================ + + - name: get all CAGWs + ec2_carrier_gateway_info: + register: cagw_info + + - name: verify test CAGW is in the results + vars: + cagw_id_list: '{{ cagw_info.carrier_gateways | map(attribute="carrier_gateway_id") | list }}' + assert: + that: + - cagw_id in cagw_id_list + + # ============================================================TODO + + - name: detach vpn gateway + ec2_carrier_gateway: + state: present + name: "{{ cagw_name }}" + register: cagw + + - assert: + that: + - cagw.changed + - not cagw.vpc_id + + - name: test idempotence + ec2_carrier_gateway: + state: present + name: "{{ cagw_name }}" + register: cagw + + - assert: + that: + - not cagw.changed + + # ============================================================ + + - include_tasks: 'tags.yml' + + # ============================================================ + + - name: delete carrier gateway + ec2_carrier_gateway: + state: absent + name: "{{ cagw_name }}" + register: cagw + + - assert: + that: + - cagw.changed + + - name: test idempotence + ec2_carrier_gateway: + state: absent + name: "{{ cagw_name }}" + register: cagw + + - assert: + that: + - not cagw.changed + + always: + + - debug: msg="Removing test dependencies" + + - name: delete carrier gateway + ec2_carrier_gateway: + state: absent + vpn_gateway_id: '{{ cagw.carrier_gateway_id }}' + ignore_errors: true + + - name: delete vpc + ec2_vpc_net: + name: "{{ vpc_name }}-{{ item }}" + state: absent + cidr_block: "{{ vpc_cidr }}" + loop: [1, 2] + register: result + retries: 10 + delay: 5 + until: result is not failed + ignore_errors: true diff --git a/tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml b/tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml new file mode 100644 index 00000000000..34707bcc708 --- /dev/null +++ b/tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml @@ -0,0 +1,215 @@ +- vars: + first_tags: + 'Key with Spaces': Value with spaces + CamelCaseKey: CamelCaseValue + pascalCaseKey: pascalCaseValue + snake_case_key: snake_case_value + second_tags: + 'New Key with Spaces': Value with spaces + NewCamelCaseKey: CamelCaseValue + newPascalCaseKey: pascalCaseValue + new_snake_case_key: snake_case_value + third_tags: + 'Key with Spaces': Value with spaces + CamelCaseKey: CamelCaseValue + pascalCaseKey: pascalCaseValue + snake_case_key: snake_case_value + 'New Key with Spaces': Updated Value with spaces + final_tags: + 'Key with Spaces': Value with spaces + CamelCaseKey: CamelCaseValue + pascalCaseKey: pascalCaseValue + snake_case_key: snake_case_value + 'New Key with Spaces': Updated Value with spaces + NewCamelCaseKey: CamelCaseValue + newPascalCaseKey: pascalCaseValue + new_snake_case_key: snake_case_value + name_tags: + Name: '{{ cagw_name }}' + module_defaults: + ec2_carrier_gateway: + name: '{{ cagw_name }}' + ec2_carrier_gateway_info: + vpn_gateway_ids: ['{{ cagw_id }}'] + block: + + # ============================================================ + + - name: add tags + ec2_carrier_gateway: + tags: '{{ first_tags }}' + state: 'present' + register: tag_cagw + + - name: get VPC CAGW facts + ec2_carrier_gateway_info: {} + register: tag_cagw_info + + - name: verify the tags were added + assert: + that: + - tag_cagw is changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == ( first_tags | combine(name_tags) ) + + - name: add tags - IDEMPOTENCY + ec2_carrier_gateway: + tags: '{{ first_tags }}' + state: 'present' + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: {} + register: tag_carrier_gateway_info + + - name: verify no change + assert: + that: + - tag_cagw is not changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == ( first_tags | combine(name_tags) ) + + # ============================================================ + + - name: get VPC CAGW facts by filter + ec2_carrier_gateway_info: + filters: + 'tag:Name': '{{ cagw_name }}' + vpn_gateway_ids: '{{ omit }}' + register: tag_cagw_info + + - name: assert the facts are the same as before + assert: + that: + - tag_cagw_info.carrier_gateways | length == 1 + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + + # ============================================================ + + - name: modify tags with purge + ec2_carrier_gateway: + tags: '{{ second_tags }}' + state: 'present' + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: + register: tag_cagw_info + + - name: verify the tags were added + assert: + that: + - tag_cagw is changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == ( second_tags | combine(name_tags) ) + + - name: modify tags with purge - IDEMPOTENCY + ec2_carrier_gateway: + tags: '{{ second_tags }}' + state: 'present' + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: + register: tag_cagw_info + + - name: verify no change + assert: + that: + - tag_cagw is not changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == ( second_tags | combine(name_tags) ) + + # ============================================================ + + - name: modify tags without purge + ec2_carrier_gateway: + tags: '{{ third_tags }}' + state: 'present' + purge_tags: False + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: + register: tag_cagw_info + + - name: verify the tags were added + assert: + that: + - tag_cagw is changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == ( final_tags | combine(name_tags) ) + + - name: modify tags without purge - IDEMPOTENCY + ec2_carrier_gateway: + tags: '{{ third_tags }}' + state: 'present' + purge_tags: False + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: + register: tag_cagw_info + + - name: verify no change + assert: + that: + - tag_cagw is not changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == ( final_tags | combine(name_tags) ) + + # ============================================================ + + - name: No change to tags without setting tags + ec2_carrier_gateway: + state: 'present' + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: + register: tag_cagw_info + + - name: verify the tags were added + assert: + that: + - tag_cagw is not changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == ( final_tags | combine(name_tags) ) + + # ============================================================ + + - name: remove non name tags + ec2_carrier_gateway: + tags: {} + state: 'present' + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: + register: tag_cagw_info + + - name: verify the tags were added + assert: + that: + - tag_cagw is changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == name_tags + + - name: remove non name tags - IDEMPOTENCY + ec2_carrier_gateway: + tags: {} + state: 'present' + register: tag_cagw + - name: get VPC CAGW facts + ec2_carrier_gateway_info: + register: tag_cagw_info + + - name: verify no change + assert: + that: + - tag_cagw is not changed + - tag_cagw.carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].carrier_gateway_id == cagw_id + - tag_cagw_info.carrier_gateways[0].tags == name_tags From 89193c323f811f7d87f9e2a3c0637f95486d3dc7 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Mon, 18 Jul 2022 23:47:09 -0300 Subject: [PATCH 07/22] feat(module/ec2-cagw): setting vpc_id as required on the docs --- plugins/modules/ec2_carrier_gateway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index 61a809a49a6..833cad8cfe1 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -226,7 +226,7 @@ def ensure_cagw_present(self, vpc_id, tags, purge_tags): def main(): argument_spec = dict( carrier_gateway_id=dict(required=False), - vpc_id=dict(required=False), + vpc_id=dict(required=True), state=dict(default='present', choices=['present', 'absent']), tags=dict(required=False, type='dict', aliases=['resource_tags']), purge_tags=dict(default=True, type='bool'), From 4edb501fba9fea56de3a78f55494dd1b96fd6b76 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Mon, 18 Jul 2022 23:57:03 -0300 Subject: [PATCH 08/22] feat(module/ec2-cagw): remove convert_tags dep --- plugins/modules/ec2_carrier_gateway_info.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/plugins/modules/ec2_carrier_gateway_info.py b/plugins/modules/ec2_carrier_gateway_info.py index 3d66345e37d..06c4c2aa0c8 100644 --- a/plugins/modules/ec2_carrier_gateway_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -97,13 +97,9 @@ from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict -def get_carrier_gateway_info(carrier_gateway, convert_tags): - if convert_tags: - tags = boto3_tag_list_to_ansible_dict(carrier_gateway['Tags']) - ignore_list = ["Tags"] - else: - tags = carrier_gateway['Tags'] - ignore_list = [] +def get_carrier_gateway_info(carrier_gateway): + tags = carrier_gateway['Tags'] + ignore_list = [] carrier_gateway_info = {'CarrierGatewayId': carrier_gateway['CarrierGatewayId'], 'VpcId': carrier_gateway['VpcId'], 'Tags': tags} @@ -116,7 +112,6 @@ def list_carrier_gateways(connection, module): params = dict() params['Filters'] = ansible_dict_to_boto3_filter_list(module.params.get('filters')) - convert_tags = module.params.get('convert_tags') if module.params.get("carrier_gateway_ids"): params['CarrierGatewayIds'] = module.params.get("carrier_gateway_ids") @@ -128,7 +123,7 @@ def list_carrier_gateways(connection, module): except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except module.fail_json_aws(e, 'Unable to describe carrier gateways') - return [get_carrier_gateway_info(cagw, convert_tags) + return [get_carrier_gateway_info(cagw) for cagw in all_carrier_gateways['CarrierGateways']] @@ -136,7 +131,6 @@ def main(): argument_spec = dict( filters=dict(type='dict', default=dict()), carrier_gateway_ids=dict(type='list', default=None, elements='str'), - convert_tags=dict(type='bool', default=True), ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) From f3d5d4f4243aa9ad4e1bd07731ba6d80210040e9 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Tue, 19 Jul 2022 00:04:12 -0300 Subject: [PATCH 09/22] feat(module/ec2-cagw): CI compliant --- plugins/modules/ec2_carrier_gateway_info.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/modules/ec2_carrier_gateway_info.py b/plugins/modules/ec2_carrier_gateway_info.py index 06c4c2aa0c8..57c9fbe7afe 100644 --- a/plugins/modules/ec2_carrier_gateway_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -104,15 +104,16 @@ def get_carrier_gateway_info(carrier_gateway): 'VpcId': carrier_gateway['VpcId'], 'Tags': tags} - carrier_gateway_info = camel_dict_to_snake_dict(carrier_gateway_info, ignore_list=ignore_list) + carrier_gateway_info = camel_dict_to_snake_dict(carrier_gateway_info, + ignore_list=ignore_list) return carrier_gateway_info def list_carrier_gateways(connection, module): + params = dict() params['Filters'] = ansible_dict_to_boto3_filter_list(module.params.get('filters')) - if module.params.get("carrier_gateway_ids"): params['CarrierGatewayIds'] = module.params.get("carrier_gateway_ids") From e3aa34291a9324e4f549fb57ae0bb403a1d9bd06 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Tue, 19 Jul 2022 00:32:28 -0300 Subject: [PATCH 10/22] feat(module/ec2-cagw): revertig requires_one_of --- plugins/modules/ec2_carrier_gateway.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index 833cad8cfe1..115ee29ab99 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -234,7 +234,6 @@ def main(): module = AnsibleAWSModule( argument_spec=argument_spec, - requires_one_of=[['vpc_id', 'carrier_gateway_id']], supports_check_mode=True, ) results = dict( From 2749ff35dad79adc0e9d28992c8d5fb0c4819df7 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Tue, 19 Jul 2022 01:05:47 -0300 Subject: [PATCH 11/22] feat(module/ec2-cagw): fix E127 --- plugins/modules/ec2_carrier_gateway_info.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/ec2_carrier_gateway_info.py b/plugins/modules/ec2_carrier_gateway_info.py index 57c9fbe7afe..8ccf8e0cddc 100644 --- a/plugins/modules/ec2_carrier_gateway_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -101,8 +101,8 @@ def get_carrier_gateway_info(carrier_gateway): tags = carrier_gateway['Tags'] ignore_list = [] carrier_gateway_info = {'CarrierGatewayId': carrier_gateway['CarrierGatewayId'], - 'VpcId': carrier_gateway['VpcId'], - 'Tags': tags} + 'VpcId': carrier_gateway['VpcId'], + 'Tags': tags} carrier_gateway_info = camel_dict_to_snake_dict(carrier_gateway_info, ignore_list=ignore_list) From eca4bc974f7ce0d0a5eb3ddc0ad8ff06681ce648 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Fri, 29 Jul 2022 12:07:32 -0300 Subject: [PATCH 12/22] fix(module/ec2-cagw/tests): correct alias w/ prefix mod name --- tests/integration/targets/ec2_carrier_gateway/aliases | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/ec2_carrier_gateway/aliases b/tests/integration/targets/ec2_carrier_gateway/aliases index 318034a776f..913237649c9 100644 --- a/tests/integration/targets/ec2_carrier_gateway/aliases +++ b/tests/integration/targets/ec2_carrier_gateway/aliases @@ -5,5 +5,5 @@ unsupported cloud/aws -ecs_carrier_gateway -ecs_carrier_gateway_info +ec2_carrier_gateway +ec2_carrier_gateway_info From d309518d160ff2a5dd395de27c47697f424c5157 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Fri, 29 Jul 2022 12:07:58 -0300 Subject: [PATCH 13/22] fix(module/ec2-cagw/tests): remove tests with second vpc --- .../ec2_carrier_gateway/tasks/main.yml | 40 +------------------ 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml index 73f3a58f0b2..077b1e8bb7e 100644 --- a/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml +++ b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml @@ -21,12 +21,11 @@ tags: Description: "Created by ansible-test for CAGW tests" register: vpc_result - loop: [1, 2] + loop: [1] - name: use set fact for vpc ids set_fact: vpc_id_1: '{{ vpc_result.results.0.vpc.id }}' - vpc_id_2: '{{ vpc_result.results.1.vpc.id }}' # ============================================================ - debug: msg="Running tests" @@ -60,20 +59,6 @@ - not cagw.changed - cagw.carrier_gateway_id == cagw_id - # ============================================================TODO - - name: attach carrier gateway to the other VPC - ec2_carrier_gateway: - state: present - vpc_id: '{{ vpc_id_2 }}' - name: "{{ cagw_name }}" - register: cagw - - - assert: - that: - - cagw.changed - - cagw.carrier_gateway_id == cagw_id - - cagw.vpc_id == vpc_id_2 - # ============================================================ - name: get VPC CAGW facts by ID (CHECK) @@ -128,29 +113,6 @@ that: - cagw_id in cagw_id_list - # ============================================================TODO - - - name: detach vpn gateway - ec2_carrier_gateway: - state: present - name: "{{ cagw_name }}" - register: cagw - - - assert: - that: - - cagw.changed - - not cagw.vpc_id - - - name: test idempotence - ec2_carrier_gateway: - state: present - name: "{{ cagw_name }}" - register: cagw - - - assert: - that: - - not cagw.changed - # ============================================================ - include_tasks: 'tags.yml' From 83541ecc42cf6faf2e3b29797ab42d9c7b485e0a Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Fri, 29 Jul 2022 12:40:49 -0300 Subject: [PATCH 14/22] fix(module/ec2-cagw/tests): add requires_one_of --- plugins/modules/ec2_carrier_gateway.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index 115ee29ab99..34e298b38d5 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -234,6 +234,7 @@ def main(): module = AnsibleAWSModule( argument_spec=argument_spec, + required_one_of=[['vpc_id', 'carrier_gateway_id']], supports_check_mode=True, ) results = dict( From b0b0dd15872fe7de8e7b4fcc15943081528251dc Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Fri, 29 Jul 2022 13:22:17 -0300 Subject: [PATCH 15/22] fix(module/ec2-cagw/tests): improve the opt-in requirement err --- plugins/modules/ec2_carrier_gateway.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index 34e298b38d5..30698c5e679 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -100,6 +100,7 @@ pass # caught by AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_message from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags @@ -203,6 +204,8 @@ def ensure_cagw_present(self, vpc_id, tags, purge_tags): response = self._connection.create_carrier_gateway(VpcId=vpc_id, aws_retry=True) cagw = camel_dict_to_snake_dict(response['CarrierGateway']) self._results['changed'] = True + except is_boto3_error_message("You must be opted into a wavelength zone to create a carrier gateway.") as e: + self._module.fail_json(msg="You must be opted into a wavelength zone to create a carrier gateway") except botocore.exceptions.WaiterError as e: self._module.fail_json_aws(e, msg="No Carrier Gateway exists.") except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: From 67809fc125bf53f13d9b7d8ceae33ede35959b1f Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Sat, 8 Oct 2022 01:34:37 -0300 Subject: [PATCH 16/22] fix(module/ec2-cagw/tests): add check_mode=true --- .../targets/ec2_carrier_gateway/tasks/main.yml | 5 +++++ .../targets/ec2_carrier_gateway/tasks/tags.yml | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml index 077b1e8bb7e..1435a6ccd1d 100644 --- a/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml +++ b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml @@ -36,6 +36,7 @@ vpc_id: '{{ vpc_id_1 }}' name: "{{ cagw_name }}" register: cagw + check_mode: true - name: use set fact for cagw ids set_fact: @@ -53,6 +54,7 @@ vpc_id: '{{ vpc_id_1 }}' name: "{{ cagw_name }}" register: cagw + check_mode: true - assert: that: @@ -124,6 +126,7 @@ state: absent name: "{{ cagw_name }}" register: cagw + check_mode: true - assert: that: @@ -134,6 +137,7 @@ state: absent name: "{{ cagw_name }}" register: cagw + check_mode: true - assert: that: @@ -148,6 +152,7 @@ state: absent vpn_gateway_id: '{{ cagw.carrier_gateway_id }}' ignore_errors: true + check_mode: true - name: delete vpc ec2_vpc_net: diff --git a/tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml b/tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml index 34707bcc708..07104daa712 100644 --- a/tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml +++ b/tests/integration/targets/ec2_carrier_gateway/tasks/tags.yml @@ -40,6 +40,7 @@ tags: '{{ first_tags }}' state: 'present' register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: {} @@ -58,6 +59,7 @@ tags: '{{ first_tags }}' state: 'present' register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: {} register: tag_carrier_gateway_info @@ -93,6 +95,7 @@ tags: '{{ second_tags }}' state: 'present' register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: register: tag_cagw_info @@ -110,6 +113,7 @@ tags: '{{ second_tags }}' state: 'present' register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: register: tag_cagw_info @@ -130,6 +134,7 @@ state: 'present' purge_tags: False register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: register: tag_cagw_info @@ -148,6 +153,7 @@ state: 'present' purge_tags: False register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: register: tag_cagw_info @@ -166,6 +172,7 @@ ec2_carrier_gateway: state: 'present' register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: register: tag_cagw_info @@ -185,6 +192,7 @@ tags: {} state: 'present' register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: register: tag_cagw_info @@ -202,6 +210,7 @@ tags: {} state: 'present' register: tag_cagw + check_mode: true - name: get VPC CAGW facts ec2_carrier_gateway_info: register: tag_cagw_info From 53a2eebca83f6475714ff1e7427498b372be5a0d Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Wed, 28 Dec 2022 17:08:20 -0300 Subject: [PATCH 17/22] fix(module/ec2-cagw/tests): filter args on doc spec --- plugins/modules/ec2_carrier_gateway_info.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/modules/ec2_carrier_gateway_info.py b/plugins/modules/ec2_carrier_gateway_info.py index 8ccf8e0cddc..3151c6978af 100644 --- a/plugins/modules/ec2_carrier_gateway_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -20,10 +20,13 @@ description: - A dict of filters to apply. Each dict item consists of a filter key and a filter value. See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeCarrierGateways.html) for possible filters. + required: false + default: {} type: dict carrier_gateway_ids: description: - Get details of specific Carrier Gateway ID. + required: false type: list elements: str extends_documentation_fragment: @@ -130,8 +133,8 @@ def list_carrier_gateways(connection, module): def main(): argument_spec = dict( - filters=dict(type='dict', default=dict()), - carrier_gateway_ids=dict(type='list', default=None, elements='str'), + carrier_gateway_ids=dict(default=None, elements='str', type='list'), + filters=dict(default={}, type='dict') ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) @@ -142,7 +145,6 @@ def main(): except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg='Failed to connect to AWS') - # call your function here results = list_carrier_gateways(connection, module) module.exit_json(carrier_gateways=results) From 5eab234b696c4b6ac4277ed5845efa5332539a97 Mon Sep 17 00:00:00 2001 From: Marco Braga Date: Thu, 30 Mar 2023 01:34:19 -0300 Subject: [PATCH 18/22] Update plugins/modules/ec2_carrier_gateway.py Co-authored-by: Markus Bergholz --- plugins/modules/ec2_carrier_gateway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index 30698c5e679..49d9b93ca9a 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -9,7 +9,7 @@ DOCUMENTATION = ''' --- module: ec2_carrier_gateway -version_added: 5.0.0 +version_added: 6.0.0 short_description: Manage an AWS VPC Carrier gateway description: - Manage an AWS VPC Carrier gateway. From c437922fec6ff5affb56aa10315040b07f6bd48f Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 30 Mar 2023 09:21:49 +0200 Subject: [PATCH 19/22] headers --- plugins/modules/ec2_carrier_gateway.py | 23 +++++++++--------- plugins/modules/ec2_carrier_gateway_info.py | 26 ++++++++++----------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index 49d9b93ca9a..bc96863f047 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -1,12 +1,10 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- + # Copyright: Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import absolute_import, division, print_function -__metaclass__ = type - - -DOCUMENTATION = ''' +DOCUMENTATION = r""" --- module: ec2_carrier_gateway version_added: 6.0.0 @@ -33,12 +31,13 @@ choices: [ 'present', 'absent' ] type: str extends_documentation_fragment: - - amazon.aws.aws - - amazon.aws.ec2 + - amazon.aws.common.modules + - amazon.aws.region.modules - amazon.aws.tags -''' + - amazon.aws.boto3 +""" -EXAMPLES = ''' +EXAMPLES = r""" # Note: These examples do not set authentication details, see the AWS Guide for details. # Ensure that the VPC has an Carrier Gateway. @@ -64,9 +63,9 @@ carrier_gateway_id: "cagw-123" state: absent register: vpc_cagw_delete -''' +""" -RETURN = ''' +RETURN = r""" changed: description: If any changes have been made to the Carrier Gateway. type: bool @@ -92,7 +91,7 @@ returned: I(state=present) sample: vpc_id: "vpc-XXXXXXXX" -''' +""" try: import botocore diff --git a/plugins/modules/ec2_carrier_gateway_info.py b/plugins/modules/ec2_carrier_gateway_info.py index 3151c6978af..6e6e745af4a 100644 --- a/plugins/modules/ec2_carrier_gateway_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -1,15 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- + # Copyright: Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import absolute_import, division, print_function -__metaclass__ = type - - -DOCUMENTATION = ''' +DOCUMENTATION = r""" --- module: ec2_carrier_gateway_info -version_added: 5.0.0 +version_added: 6.0.0 short_description: Gather information about carrier gateways in AWS description: - Gather information about carrier gateways in AWS. @@ -30,12 +28,12 @@ type: list elements: str extends_documentation_fragment: - - amazon.aws.aws - - amazon.aws.ec2 - -''' + - amazon.aws.common.modules + - amazon.aws.region.modules + - amazon.aws.boto3 +""" -EXAMPLES = ''' +EXAMPLES = r""" # # Note: These examples do not set authentication details, see the AWS Guide for details. - name: Gather information about all Carrier Gateways for an account or profile @@ -55,9 +53,9 @@ region: ap-southeast-2 carrier_gateway_ids: cagw-c1231234 register: cagw_info -''' +""" -RETURN = ''' +RETURN = r""" changed: description: True if listing the carrier gateways succeeds. type: bool @@ -85,7 +83,7 @@ sample: tags: "Ansible": "Test" -''' +""" try: import botocore From 11afa8f7404a6c8274a1a4e822c276e2f3e949ae Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 30 Mar 2023 09:40:13 +0200 Subject: [PATCH 20/22] black --- plugins/modules/ec2_carrier_gateway.py | 91 +++++++++++---------- plugins/modules/ec2_carrier_gateway_info.py | 47 ++++++----- 2 files changed, 75 insertions(+), 63 deletions(-) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index bc96863f047..d8178924a1f 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -109,54 +109,57 @@ @AWSRetry.jittered_backoff(retries=10, delay=10) def describe_cagws_with_backoff(connection, **params): - paginator = connection.get_paginator('describe_carrier_gateways') - return paginator.paginate(**params).build_full_result()['CarrierGateways'] + paginator = connection.get_paginator("describe_carrier_gateways") + return paginator.paginate(**params).build_full_result()["CarrierGateways"] -class AnsibleEc2Cagw(): - +class AnsibleEc2Cagw: def __init__(self, module, results): self._module = module self._results = results - self._connection = self._module.client( - 'ec2', retry_decorator=AWSRetry.jittered_backoff() - ) + self._connection = self._module.client("ec2", retry_decorator=AWSRetry.jittered_backoff()) self._check_mode = self._module.check_mode def process(self): - vpc_id = self._module.params.get('vpc_id') - state = self._module.params.get('state', 'present') - tags = self._module.params.get('tags') - purge_tags = self._module.params.get('purge_tags') + vpc_id = self._module.params.get("vpc_id") + state = self._module.params.get("state", "present") + tags = self._module.params.get("tags") + purge_tags = self._module.params.get("purge_tags") - if state == 'present': + if state == "present": self.ensure_cagw_present(vpc_id, tags, purge_tags) - elif state == 'absent': + elif state == "absent": self.ensure_cagw_absent(vpc_id) def get_matching_cagw(self, vpc_id, carrier_gateway_id=None): - ''' + """ Returns the carrier gateway found. Parameters: vpc_id (str): VPC ID carrier_gateway_id (str): Carrier Gateway ID, if specified Returns: cagw (dict): dict of cagw found, None if none found - ''' - filters = ansible_dict_to_boto3_filter_list({'vpc-id': vpc_id}) + """ + filters = ansible_dict_to_boto3_filter_list({"vpc-id": vpc_id}) try: if not carrier_gateway_id: - cagws = describe_cagws_with_backoff(self._connection, Filters=filters) + cagws = describe_cagws_with_backoff( + self._connection, + Filters=filters, + ) else: - cagws = describe_cagws_with_backoff(self._connection, CarrierGatewayIds=[carrier_gateway_id]) + cagws = describe_cagws_with_backoff( + self._connection, + CarrierGatewayIds=[carrier_gateway_id], + ) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: self._module.fail_json_aws(e) cagw = None if len(cagws) > 1: self._module.fail_json( - msg='EC2 returned more than one Carrier Gateway for VPC {0}, aborting' - .format(vpc_id)) + msg=f"EC2 returned more than one Carrier Gateway for VPC {vpc_id}, aborting" + ) elif cagws: cagw = camel_dict_to_snake_dict(cagws[0]) @@ -165,9 +168,9 @@ def get_matching_cagw(self, vpc_id, carrier_gateway_id=None): @staticmethod def get_cagw_info(cagw, vpc_id): return { - 'carrier_gateway_id': cagw['carrier_gateway_id'], - 'tags': boto3_tag_list_to_ansible_dict(cagw['tags']), - 'vpc_id': vpc_id + "carrier_gateway_id": cagw["carrier_gateway_id"], + "tags": boto3_tag_list_to_ansible_dict(cagw["tags"]), + "vpc_id": vpc_id, } def ensure_cagw_absent(self, vpc_id): @@ -176,14 +179,14 @@ def ensure_cagw_absent(self, vpc_id): return self._results if self._check_mode: - self._results['changed'] = True + self._results["changed"] = True return self._results try: - self._results['changed'] = True + self._results["changed"] = True self._connection.delete_carrier_gateway( aws_retry=True, - CarrierGatewayId=cagw['carrier_gateway_id'] + CarrierGatewayId=cagw["carrier_gateway_id"], ) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: self._module.fail_json_aws(e, msg="Unable to delete Carrier Gateway") @@ -195,30 +198,34 @@ def ensure_cagw_present(self, vpc_id, tags, purge_tags): if cagw is None: if self._check_mode: - self._results['changed'] = True - self._results['carrier_gateway_id'] = None + self._results["changed"] = True + self._results["carrier_gateway_id"] = None return self._results try: response = self._connection.create_carrier_gateway(VpcId=vpc_id, aws_retry=True) - cagw = camel_dict_to_snake_dict(response['CarrierGateway']) - self._results['changed'] = True + cagw = camel_dict_to_snake_dict(response["CarrierGateway"]) + self._results["changed"] = True except is_boto3_error_message("You must be opted into a wavelength zone to create a carrier gateway.") as e: self._module.fail_json(msg="You must be opted into a wavelength zone to create a carrier gateway") except botocore.exceptions.WaiterError as e: self._module.fail_json_aws(e, msg="No Carrier Gateway exists.") except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - self._module.fail_json_aws(e, msg='Unable to create Carrier Gateway') + self._module.fail_json_aws(e, msg="Unable to create Carrier Gateway") # Modify tags - self._results['changed'] |= ensure_ec2_tags( - self._connection, self._module, cagw['carrier_gateway_id'], - resource_type='carrier-gateway', tags=tags, purge_tags=purge_tags, - retry_codes='InvalidCarrierGatewayID.NotFound' + self._results["changed"] |= ensure_ec2_tags( + self._connection, + self._module, + cagw["carrier_gateway_id"], + resource_type="carrier-gateway", + tags=tags, + purge_tags=purge_tags, + retry_codes="InvalidCarrierGatewayID.NotFound", ) # Update cagw - cagw = self.get_matching_cagw(vpc_id, carrier_gateway_id=cagw['carrier_gateway_id']) + cagw = self.get_matching_cagw(vpc_id, carrier_gateway_id=cagw["carrier_gateway_id"]) cagw_info = self.get_cagw_info(cagw, vpc_id) self._results.update(cagw_info) @@ -229,18 +236,18 @@ def main(): argument_spec = dict( carrier_gateway_id=dict(required=False), vpc_id=dict(required=True), - state=dict(default='present', choices=['present', 'absent']), - tags=dict(required=False, type='dict', aliases=['resource_tags']), - purge_tags=dict(default=True, type='bool'), + state=dict(default="present", choices=["present", "absent"]), + tags=dict(required=False, type="dict", aliases=["resource_tags"]), + purge_tags=dict(default=True, type="bool"), ) module = AnsibleAWSModule( argument_spec=argument_spec, - required_one_of=[['vpc_id', 'carrier_gateway_id']], + required_one_of=[["vpc_id", "carrier_gateway_id"]], supports_check_mode=True, ) results = dict( - changed=False + changed=False, ) cagw_manager = AnsibleEc2Cagw(module=module, results=results) cagw_manager.process() @@ -248,5 +255,5 @@ def main(): module.exit_json(**results) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/plugins/modules/ec2_carrier_gateway_info.py b/plugins/modules/ec2_carrier_gateway_info.py index 6e6e745af4a..43b88df9877 100644 --- a/plugins/modules/ec2_carrier_gateway_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -99,54 +99,59 @@ def get_carrier_gateway_info(carrier_gateway): - tags = carrier_gateway['Tags'] + tags = carrier_gateway["Tags"] ignore_list = [] - carrier_gateway_info = {'CarrierGatewayId': carrier_gateway['CarrierGatewayId'], - 'VpcId': carrier_gateway['VpcId'], - 'Tags': tags} + carrier_gateway_info = { + "CarrierGatewayId": carrier_gateway["CarrierGatewayId"], + "VpcId": carrier_gateway["VpcId"], + "Tags": tags, + } - carrier_gateway_info = camel_dict_to_snake_dict(carrier_gateway_info, - ignore_list=ignore_list) + carrier_gateway_info = camel_dict_to_snake_dict(carrier_gateway_info, ignore_list=ignore_list) return carrier_gateway_info def list_carrier_gateways(connection, module): - params = dict() - params['Filters'] = ansible_dict_to_boto3_filter_list(module.params.get('filters')) + params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) if module.params.get("carrier_gateway_ids"): - params['CarrierGatewayIds'] = module.params.get("carrier_gateway_ids") + params["CarrierGatewayIds"] = module.params.get("carrier_gateway_ids") try: all_carrier_gateways = connection.describe_carrier_gateways(aws_retry=True, **params) - except is_boto3_error_code('InvalidCarrierGatewayID.NotFound'): - module.fail_json('CarrierGateway not found') - except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except - module.fail_json_aws(e, 'Unable to describe carrier gateways') + except is_boto3_error_code("InvalidCarrierGatewayID.NotFound"): + module.fail_json("CarrierGateway not found") + except ( + botocore.exceptions.ClientError, + botocore.exceptions.BotoCoreError, + ) as e: # pylint: disable=duplicate-except + module.fail_json_aws(e, "Unable to describe carrier gateways") - return [get_carrier_gateway_info(cagw) - for cagw in all_carrier_gateways['CarrierGateways']] + return [get_carrier_gateway_info(cagw) for cagw in all_carrier_gateways["CarrierGateways"]] def main(): argument_spec = dict( - carrier_gateway_ids=dict(default=None, elements='str', type='list'), - filters=dict(default={}, type='dict') + carrier_gateway_ids=dict(default=None, elements="str", type="list"), + filters=dict(default={}, type="dict"), ) - module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) + module = AnsibleAWSModule( + argument_spec=argument_spec, + supports_check_mode=True, + ) # Validate Requirements try: - connection = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff()) + connection = module.client("ec2", retry_decorator=AWSRetry.jittered_backoff()) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - module.fail_json_aws(e, msg='Failed to connect to AWS') + module.fail_json_aws(e, msg="Failed to connect to AWS") results = list_carrier_gateways(connection, module) module.exit_json(carrier_gateways=results) -if __name__ == '__main__': +if __name__ == "__main__": main() From fea9e9ecf85b26cf1a6dfbace142cfb71ccfa63d Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 30 Mar 2023 09:47:54 +0200 Subject: [PATCH 21/22] Update imports --- plugins/modules/ec2_carrier_gateway.py | 12 +++++++----- plugins/modules/ec2_carrier_gateway_info.py | 16 +++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/plugins/modules/ec2_carrier_gateway.py b/plugins/modules/ec2_carrier_gateway.py index d8178924a1f..3458170e393 100644 --- a/plugins/modules/ec2_carrier_gateway.py +++ b/plugins/modules/ec2_carrier_gateway.py @@ -98,13 +98,15 @@ except ImportError: pass # caught by AnsibleAWSModule -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule -from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_message -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict + +from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_message +from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict +from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list + +from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule @AWSRetry.jittered_backoff(retries=10, delay=10) diff --git a/plugins/modules/ec2_carrier_gateway_info.py b/plugins/modules/ec2_carrier_gateway_info.py index 43b88df9877..43d77d59aa6 100644 --- a/plugins/modules/ec2_carrier_gateway_info.py +++ b/plugins/modules/ec2_carrier_gateway_info.py @@ -90,16 +90,18 @@ except ImportError: pass # Handled by AnsibleAWSModule -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule -from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list -from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict + +from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code +from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry +from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict +from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list + +from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule def get_carrier_gateway_info(carrier_gateway): - tags = carrier_gateway["Tags"] + tags = boto3_tag_list_to_ansible_dict(carrier_gateway["Tags"]) ignore_list = [] carrier_gateway_info = { "CarrierGatewayId": carrier_gateway["CarrierGatewayId"], From 3616de5f906169fa9ecb640dc1934f1912324da2 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 30 Mar 2023 10:02:47 +0200 Subject: [PATCH 22/22] fix cgw deletion in integration test --- tests/integration/targets/ec2_carrier_gateway/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml index 1435a6ccd1d..1ec10c5c18c 100644 --- a/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml +++ b/tests/integration/targets/ec2_carrier_gateway/tasks/main.yml @@ -150,7 +150,7 @@ - name: delete carrier gateway ec2_carrier_gateway: state: absent - vpn_gateway_id: '{{ cagw.carrier_gateway_id }}' + carrier_gateway_id: '{{ cagw.carrier_gateway_id }}' ignore_errors: true check_mode: true