We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
ec2_customer_gateway: bgp_asn is not required
### Summary

The ec2_customer_gateway module has incorrect documentation for the bgp_asn parameter.

It says the ASN must be passed when state=present, but the code defaults to 25000 if the parameter is absent.  See the ensure_cgw_present() method:

```
    def ensure_cgw_present(self, bgp_asn, ip_address):
        if not bgp_asn:
            bgp_asn = 65000
        response = self.ec2.create_customer_gateway(
            DryRun=False,
            Type='ipsec.1',
            PublicIp=ip_address,
            BgpAsn=bgp_asn,
        )
        return response

### Issue Type

Documentation Report

### Component Name

ec2_customer_gateway

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.12.4]
  config file = None
  configured module search path = ['/home/neil/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/neil/.local/share/virtualenvs/community.aws-uRL047Ho/lib/python3.10/site-packages/ansible
  ansible collection location = /home/neil/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/neil/.local/share/virtualenvs/community.aws-uRL047Ho/bin/ansible
  python version = 3.10.1 (main, Jan 10 2022, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)]
  jinja version = 3.1.1
  libyaml = True
```


### Collection Versions

```console (paste below)
$ ansible-galaxy collection list
```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed

```


### OS / Environment

main branch, as of 2022-04-18.

### Additional Information

Suggested rewording:

```
options:
  bgp_asn:
    description:
      - Border Gateway Protocol (BGP) Autonomous System Number (ASN), defaults to 25000.
    type: int
```

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `plugins/modules/ec2_customer_gateway.py`
Content:
```
1 #!/usr/bin/python
2 #
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4 
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7 
8 
9 DOCUMENTATION = '''
10 ---
11 module: ec2_customer_gateway
12 version_added: 1.0.0
13 short_description: Manage an AWS customer gateway
14 description:
15     - Manage an AWS customer gateway.
16 author: Michael Baydoun (@MichaelBaydoun)
17 notes:
18     - You cannot create more than one customer gateway with the same IP address. If you run an identical request more than one time, the
19       first request creates the customer gateway, and subsequent requests return information about the existing customer gateway. The subsequent
20       requests do not create new customer gateway resources.
21     - Return values contain customer_gateway and customer_gateways keys which are identical dicts. You should use
22       customer_gateway. See U(https://github.com/ansible/ansible-modules-extras/issues/2773) for details.
23 options:
24   bgp_asn:
25     description:
26       - Border Gateway Protocol (BGP) Autonomous System Number (ASN), required when I(state=present).
27     type: int
28   ip_address:
29     description:
30       - Internet-routable IP address for customers gateway, must be a static address.
31     required: true
32     type: str
33   name:
34     description:
35       - Name of the customer gateway.
36     required: true
37     type: str
38   routing:
39     description:
40       - The type of routing.
41     choices: ['static', 'dynamic']
42     default: dynamic
43     type: str
44   state:
45     description:
46       - Create or terminate the Customer Gateway.
47     default: present
48     choices: [ 'present', 'absent' ]
49     type: str
50 extends_documentation_fragment:
51 - amazon.aws.aws
52 - amazon.aws.ec2
53 
54 '''
55 
56 EXAMPLES = '''
57 - name: Create Customer Gateway
58   community.aws.ec2_customer_gateway:
59     bgp_asn: 12345
60     ip_address: 1.2.3.4
61     name: IndianapolisOffice
62     region: us-east-1
63   register: cgw
64 
65 - name: Delete Customer Gateway
66   community.aws.ec2_customer_gateway:
67     ip_address: 1.2.3.4
68     name: IndianapolisOffice
69     state: absent
70     region: us-east-1
71   register: cgw
72 '''
73 
74 RETURN = '''
75 gateway.customer_gateways:
76     description: details about the gateway that was created.
77     returned: success
78     type: complex
79     contains:
80         bgp_asn:
81             description: The Border Gateway Autonomous System Number.
82             returned: when exists and gateway is available.
83             sample: 65123
84             type: str
85         customer_gateway_id:
86             description: gateway id assigned by amazon.
87             returned: when exists and gateway is available.
88             sample: cgw-cb6386a2
89             type: str
90         ip_address:
91             description: ip address of your gateway device.
92             returned: when exists and gateway is available.
93             sample: 1.2.3.4
94             type: str
95         state:
96             description: state of gateway.
97             returned: when gateway exists and is available.
98             sample: available
99             type: str
100         tags:
101             description: Any tags on the gateway.
102             returned: when gateway exists and is available, and when tags exist.
103             type: list
104         type:
105             description: encryption type.
106             returned: when gateway exists and is available.
107             sample: ipsec.1
108             type: str
109 '''
110 
111 try:
112     import botocore
113 except ImportError:
114     pass  # Handled by AnsibleAWSModule
115 
116 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
117 
118 from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
119 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
120 
121 
122 class Ec2CustomerGatewayManager:
123 
124     def __init__(self, module):
125         self.module = module
126 
127         try:
128             self.ec2 = module.client('ec2')
129         except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
130             module.fail_json_aws(e, msg='Failed to connect to AWS')
131 
132     @AWSRetry.jittered_backoff(delay=2, max_delay=30, retries=6, catch_extra_error_codes=['IncorrectState'])
133     def ensure_cgw_absent(self, gw_id):
134         response = self.ec2.delete_customer_gateway(
135             DryRun=False,
136             CustomerGatewayId=gw_id
137         )
138         return response
139 
140     def ensure_cgw_present(self, bgp_asn, ip_address):
141         if not bgp_asn:
142             bgp_asn = 65000
143         response = self.ec2.create_customer_gateway(
144             DryRun=False,
145             Type='ipsec.1',
146             PublicIp=ip_address,
147             BgpAsn=bgp_asn,
148         )
149         return response
150 
151     def tag_cgw_name(self, gw_id, name):
152         response = self.ec2.create_tags(
153             DryRun=False,
154             Resources=[
155                 gw_id,
156             ],
157             Tags=[
158                 {
159                     'Key': 'Name',
160                     'Value': name
161                 },
162             ]
163         )
164         return response
165 
166     def describe_gateways(self, ip_address):
167         response = self.ec2.describe_customer_gateways(
168             DryRun=False,
169             Filters=[
170                 {
171                     'Name': 'state',
172                     'Values': [
173                         'available',
174                     ]
175                 },
176                 {
177                     'Name': 'ip-address',
178                     'Values': [
179                         ip_address,
180                     ]
181                 }
182             ]
183         )
184         return response
185 
186 
187 def main():
188     argument_spec = dict(
189         bgp_asn=dict(required=False, type='int'),
190         ip_address=dict(required=True),
191         name=dict(required=True),
192         routing=dict(default='dynamic', choices=['dynamic', 'static']),
193         state=dict(default='present', choices=['present', 'absent']),
194     )
195 
196     module = AnsibleAWSModule(
197         argument_spec=argument_spec,
198         supports_check_mode=True,
199         required_if=[
200             ('routing', 'dynamic', ['bgp_asn'])
201         ]
202     )
203 
204     gw_mgr = Ec2CustomerGatewayManager(module)
205 
206     name = module.params.get('name')
207 
208     existing = gw_mgr.describe_gateways(module.params['ip_address'])
209 
210     results = dict(changed=False)
211     if module.params['state'] == 'present':
212         if existing['CustomerGateways']:
213             existing['CustomerGateway'] = existing['CustomerGateways'][0]
214             results['gateway'] = existing
215             if existing['CustomerGateway']['Tags']:
216                 tag_array = existing['CustomerGateway']['Tags']
217                 for key, value in enumerate(tag_array):
218                     if value['Key'] == 'Name':
219                         current_name = value['Value']
220                         if current_name != name:
221                             results['name'] = gw_mgr.tag_cgw_name(
222                                 results['gateway']['CustomerGateway']['CustomerGatewayId'],
223                                 module.params['name'],
224                             )
225                             results['changed'] = True
226         else:
227             if not module.check_mode:
228                 results['gateway'] = gw_mgr.ensure_cgw_present(
229                     module.params['bgp_asn'],
230                     module.params['ip_address'],
231                 )
232                 results['name'] = gw_mgr.tag_cgw_name(
233                     results['gateway']['CustomerGateway']['CustomerGatewayId'],
234                     module.params['name'],
235                 )
236             results['changed'] = True
237 
238     elif module.params['state'] == 'absent':
239         if existing['CustomerGateways']:
240             existing['CustomerGateway'] = existing['CustomerGateways'][0]
241             results['gateway'] = existing
242             if not module.check_mode:
243                 results['gateway'] = gw_mgr.ensure_cgw_absent(
244                     existing['CustomerGateway']['CustomerGatewayId']
245                 )
246             results['changed'] = True
247 
248     pretty_results = camel_dict_to_snake_dict(results)
249     module.exit_json(**pretty_results)
250 
251 
252 if __name__ == '__main__':
253     main()
254
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/plugins/modules/ec2_customer_gateway.py b/plugins/modules/ec2_customer_gateway.py
--- a/plugins/modules/ec2_customer_gateway.py
+++ b/plugins/modules/ec2_customer_gateway.py
@@ -23,7 +23,8 @@
 options:
   bgp_asn:
     description:
-      - Border Gateway Protocol (BGP) Autonomous System Number (ASN), required when I(state=present).
+      - Border Gateway Protocol (BGP) Autonomous System Number (ASN).
+      - Defaults to C(65000) if not specified when I(state=present).
     type: int
   ip_address:
     description:

{"golden_diff": "diff --git a/plugins/modules/ec2_customer_gateway.py b/plugins/modules/ec2_customer_gateway.py\n--- a/plugins/modules/ec2_customer_gateway.py\n+++ b/plugins/modules/ec2_customer_gateway.py\n@@ -23,7 +23,8 @@\n options:\n   bgp_asn:\n     description:\n-      - Border Gateway Protocol (BGP) Autonomous System Number (ASN), required when I(state=present).\n+      - Border Gateway Protocol (BGP) Autonomous System Number (ASN).\n+      - Defaults to C(65000) if not specified when I(state=present).\n     type: int\n   ip_address:\n     description:\n", "issue": "ec2_customer_gateway: bgp_asn is not required\n### Summary\n\nThe ec2_customer_gateway module has incorrect documentation for the bgp_asn parameter.\r\n\r\nIt says the ASN must be passed when state=present, but the code defaults to 25000 if the parameter is absent.  See the ensure_cgw_present() method:\r\n\r\n```\r\n    def ensure_cgw_present(self, bgp_asn, ip_address):\r\n        if not bgp_asn:\r\n            bgp_asn = 65000\r\n        response = self.ec2.create_customer_gateway(\r\n            DryRun=False,\r\n            Type='ipsec.1',\r\n            PublicIp=ip_address,\r\n            BgpAsn=bgp_asn,\r\n        )\r\n        return response\n\n### Issue Type\n\nDocumentation Report\n\n### Component Name\n\nec2_customer_gateway\n\n### Ansible Version\n\n```console (paste below)\r\n$ ansible --version\r\nansible [core 2.12.4]\r\n  config file = None\r\n  configured module search path = ['/home/neil/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']\r\n  ansible python module location = /home/neil/.local/share/virtualenvs/community.aws-uRL047Ho/lib/python3.10/site-packages/ansible\r\n  ansible collection location = /home/neil/.ansible/collections:/usr/share/ansible/collections\r\n  executable location = /home/neil/.local/share/virtualenvs/community.aws-uRL047Ho/bin/ansible\r\n  python version = 3.10.1 (main, Jan 10 2022, 00:00:00) [GCC 11.2.1 20211203 (Red Hat 11.2.1-7)]\r\n  jinja version = 3.1.1\r\n  libyaml = True\r\n```\r\n\n\n### Collection Versions\n\n```console (paste below)\r\n$ ansible-galaxy collection list\r\n```\r\n\n\n### Configuration\n\n```console (paste below)\r\n$ ansible-config dump --only-changed\r\n\r\n```\r\n\n\n### OS / Environment\n\nmain branch, as of 2022-04-18.\n\n### Additional Information\n\nSuggested rewording:\r\n\r\n```\r\noptions:\r\n  bgp_asn:\r\n    description:\r\n      - Border Gateway Protocol (BGP) Autonomous System Number (ASN), defaults to 25000.\r\n    type: int\r\n```\n\n### Code of Conduct\n\n- [X] I agree to follow the Ansible Code of Conduct\n", "before_files": [{"content": "#!/usr/bin/python\n#\n# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)\n\nfrom __future__ import absolute_import, division, print_function\n__metaclass__ = type\n\n\nDOCUMENTATION = '''\n---\nmodule: ec2_customer_gateway\nversion_added: 1.0.0\nshort_description: Manage an AWS customer gateway\ndescription:\n    - Manage an AWS customer gateway.\nauthor: Michael Baydoun (@MichaelBaydoun)\nnotes:\n    - You cannot create more than one customer gateway with the same IP address. If you run an identical request more than one time, the\n      first request creates the customer gateway, and subsequent requests return information about the existing customer gateway. The subsequent\n      requests do not create new customer gateway resources.\n    - Return values contain customer_gateway and customer_gateways keys which are identical dicts. You should use\n      customer_gateway. See U(https://github.com/ansible/ansible-modules-extras/issues/2773) for details.\noptions:\n  bgp_asn:\n    description:\n      - Border Gateway Protocol (BGP) Autonomous System Number (ASN), required when I(state=present).\n    type: int\n  ip_address:\n    description:\n      - Internet-routable IP address for customers gateway, must be a static address.\n    required: true\n    type: str\n  name:\n    description:\n      - Name of the customer gateway.\n    required: true\n    type: str\n  routing:\n    description:\n      - The type of routing.\n    choices: ['static', 'dynamic']\n    default: dynamic\n    type: str\n  state:\n    description:\n      - Create or terminate the Customer Gateway.\n    default: present\n    choices: [ 'present', 'absent' ]\n    type: str\nextends_documentation_fragment:\n- amazon.aws.aws\n- amazon.aws.ec2\n\n'''\n\nEXAMPLES = '''\n- name: Create Customer Gateway\n  community.aws.ec2_customer_gateway:\n    bgp_asn: 12345\n    ip_address: 1.2.3.4\n    name: IndianapolisOffice\n    region: us-east-1\n  register: cgw\n\n- name: Delete Customer Gateway\n  community.aws.ec2_customer_gateway:\n    ip_address: 1.2.3.4\n    name: IndianapolisOffice\n    state: absent\n    region: us-east-1\n  register: cgw\n'''\n\nRETURN = '''\ngateway.customer_gateways:\n    description: details about the gateway that was created.\n    returned: success\n    type: complex\n    contains:\n        bgp_asn:\n            description: The Border Gateway Autonomous System Number.\n            returned: when exists and gateway is available.\n            sample: 65123\n            type: str\n        customer_gateway_id:\n            description: gateway id assigned by amazon.\n            returned: when exists and gateway is available.\n            sample: cgw-cb6386a2\n            type: str\n        ip_address:\n            description: ip address of your gateway device.\n            returned: when exists and gateway is available.\n            sample: 1.2.3.4\n            type: str\n        state:\n            description: state of gateway.\n            returned: when gateway exists and is available.\n            sample: available\n            type: str\n        tags:\n            description: Any tags on the gateway.\n            returned: when gateway exists and is available, and when tags exist.\n            type: list\n        type:\n            description: encryption type.\n            returned: when gateway exists and is available.\n            sample: ipsec.1\n            type: str\n'''\n\ntry:\n    import botocore\nexcept ImportError:\n    pass  # Handled by AnsibleAWSModule\n\nfrom ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict\n\nfrom ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule\nfrom ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry\n\n\nclass Ec2CustomerGatewayManager:\n\n    def __init__(self, module):\n        self.module = module\n\n        try:\n            self.ec2 = module.client('ec2')\n        except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:\n            module.fail_json_aws(e, msg='Failed to connect to AWS')\n\n    @AWSRetry.jittered_backoff(delay=2, max_delay=30, retries=6, catch_extra_error_codes=['IncorrectState'])\n    def ensure_cgw_absent(self, gw_id):\n        response = self.ec2.delete_customer_gateway(\n            DryRun=False,\n            CustomerGatewayId=gw_id\n        )\n        return response\n\n    def ensure_cgw_present(self, bgp_asn, ip_address):\n        if not bgp_asn:\n            bgp_asn = 65000\n        response = self.ec2.create_customer_gateway(\n            DryRun=False,\n            Type='ipsec.1',\n            PublicIp=ip_address,\n            BgpAsn=bgp_asn,\n        )\n        return response\n\n    def tag_cgw_name(self, gw_id, name):\n        response = self.ec2.create_tags(\n            DryRun=False,\n            Resources=[\n                gw_id,\n            ],\n            Tags=[\n                {\n                    'Key': 'Name',\n                    'Value': name\n                },\n            ]\n        )\n        return response\n\n    def describe_gateways(self, ip_address):\n        response = self.ec2.describe_customer_gateways(\n            DryRun=False,\n            Filters=[\n                {\n                    'Name': 'state',\n                    'Values': [\n                        'available',\n                    ]\n                },\n                {\n                    'Name': 'ip-address',\n                    'Values': [\n                        ip_address,\n                    ]\n                }\n            ]\n        )\n        return response\n\n\ndef main():\n    argument_spec = dict(\n        bgp_asn=dict(required=False, type='int'),\n        ip_address=dict(required=True),\n        name=dict(required=True),\n        routing=dict(default='dynamic', choices=['dynamic', 'static']),\n        state=dict(default='present', choices=['present', 'absent']),\n    )\n\n    module = AnsibleAWSModule(\n        argument_spec=argument_spec,\n        supports_check_mode=True,\n        required_if=[\n            ('routing', 'dynamic', ['bgp_asn'])\n        ]\n    )\n\n    gw_mgr = Ec2CustomerGatewayManager(module)\n\n    name = module.params.get('name')\n\n    existing = gw_mgr.describe_gateways(module.params['ip_address'])\n\n    results = dict(changed=False)\n    if module.params['state'] == 'present':\n        if existing['CustomerGateways']:\n            existing['CustomerGateway'] = existing['CustomerGateways'][0]\n            results['gateway'] = existing\n            if existing['CustomerGateway']['Tags']:\n                tag_array = existing['CustomerGateway']['Tags']\n                for key, value in enumerate(tag_array):\n                    if value['Key'] == 'Name':\n                        current_name = value['Value']\n                        if current_name != name:\n                            results['name'] = gw_mgr.tag_cgw_name(\n                                results['gateway']['CustomerGateway']['CustomerGatewayId'],\n                                module.params['name'],\n                            )\n                            results['changed'] = True\n        else:\n            if not module.check_mode:\n                results['gateway'] = gw_mgr.ensure_cgw_present(\n                    module.params['bgp_asn'],\n                    module.params['ip_address'],\n                )\n                results['name'] = gw_mgr.tag_cgw_name(\n                    results['gateway']['CustomerGateway']['CustomerGatewayId'],\n                    module.params['name'],\n                )\n            results['changed'] = True\n\n    elif module.params['state'] == 'absent':\n        if existing['CustomerGateways']:\n            existing['CustomerGateway'] = existing['CustomerGateways'][0]\n            results['gateway'] = existing\n            if not module.check_mode:\n                results['gateway'] = gw_mgr.ensure_cgw_absent(\n                    existing['CustomerGateway']['CustomerGatewayId']\n                )\n            results['changed'] = True\n\n    pretty_results = camel_dict_to_snake_dict(results)\n    module.exit_json(**pretty_results)\n\n\nif __name__ == '__main__':\n    main()\n", "path": "plugins/modules/ec2_customer_gateway.py"}], "after_files": [{"content": "#!/usr/bin/python\n#\n# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)\n\nfrom __future__ import absolute_import, division, print_function\n__metaclass__ = type\n\n\nDOCUMENTATION = '''\n---\nmodule: ec2_customer_gateway\nversion_added: 1.0.0\nshort_description: Manage an AWS customer gateway\ndescription:\n    - Manage an AWS customer gateway.\nauthor: Michael Baydoun (@MichaelBaydoun)\nnotes:\n    - You cannot create more than one customer gateway with the same IP address. If you run an identical request more than one time, the\n      first request creates the customer gateway, and subsequent requests return information about the existing customer gateway. The subsequent\n      requests do not create new customer gateway resources.\n    - Return values contain customer_gateway and customer_gateways keys which are identical dicts. You should use\n      customer_gateway. See U(https://github.com/ansible/ansible-modules-extras/issues/2773) for details.\noptions:\n  bgp_asn:\n    description:\n      - Border Gateway Protocol (BGP) Autonomous System Number (ASN).\n      - Defaults to C(65000) if not specified when I(state=present).\n    type: int\n  ip_address:\n    description:\n      - Internet-routable IP address for customers gateway, must be a static address.\n    required: true\n    type: str\n  name:\n    description:\n      - Name of the customer gateway.\n    required: true\n    type: str\n  routing:\n    description:\n      - The type of routing.\n    choices: ['static', 'dynamic']\n    default: dynamic\n    type: str\n  state:\n    description:\n      - Create or terminate the Customer Gateway.\n    default: present\n    choices: [ 'present', 'absent' ]\n    type: str\nextends_documentation_fragment:\n- amazon.aws.aws\n- amazon.aws.ec2\n\n'''\n\nEXAMPLES = '''\n- name: Create Customer Gateway\n  community.aws.ec2_customer_gateway:\n    bgp_asn: 12345\n    ip_address: 1.2.3.4\n    name: IndianapolisOffice\n    region: us-east-1\n  register: cgw\n\n- name: Delete Customer Gateway\n  community.aws.ec2_customer_gateway:\n    ip_address: 1.2.3.4\n    name: IndianapolisOffice\n    state: absent\n    region: us-east-1\n  register: cgw\n'''\n\nRETURN = '''\ngateway.customer_gateways:\n    description: details about the gateway that was created.\n    returned: success\n    type: complex\n    contains:\n        bgp_asn:\n            description: The Border Gateway Autonomous System Number.\n            returned: when exists and gateway is available.\n            sample: 65123\n            type: str\n        customer_gateway_id:\n            description: gateway id assigned by amazon.\n            returned: when exists and gateway is available.\n            sample: cgw-cb6386a2\n            type: str\n        ip_address:\n            description: ip address of your gateway device.\n            returned: when exists and gateway is available.\n            sample: 1.2.3.4\n            type: str\n        state:\n            description: state of gateway.\n            returned: when gateway exists and is available.\n            sample: available\n            type: str\n        tags:\n            description: Any tags on the gateway.\n            returned: when gateway exists and is available, and when tags exist.\n            type: list\n        type:\n            description: encryption type.\n            returned: when gateway exists and is available.\n            sample: ipsec.1\n            type: str\n'''\n\ntry:\n    import botocore\nexcept ImportError:\n    pass  # Handled by AnsibleAWSModule\n\nfrom ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict\n\nfrom ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule\nfrom ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry\n\n\nclass Ec2CustomerGatewayManager:\n\n    def __init__(self, module):\n        self.module = module\n\n        try:\n            self.ec2 = module.client('ec2')\n        except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:\n            module.fail_json_aws(e, msg='Failed to connect to AWS')\n\n    @AWSRetry.jittered_backoff(delay=2, max_delay=30, retries=6, catch_extra_error_codes=['IncorrectState'])\n    def ensure_cgw_absent(self, gw_id):\n        response = self.ec2.delete_customer_gateway(\n            DryRun=False,\n            CustomerGatewayId=gw_id\n        )\n        return response\n\n    def ensure_cgw_present(self, bgp_asn, ip_address):\n        if not bgp_asn:\n            bgp_asn = 65000\n        response = self.ec2.create_customer_gateway(\n            DryRun=False,\n            Type='ipsec.1',\n            PublicIp=ip_address,\n            BgpAsn=bgp_asn,\n        )\n        return response\n\n    def tag_cgw_name(self, gw_id, name):\n        response = self.ec2.create_tags(\n            DryRun=False,\n            Resources=[\n                gw_id,\n            ],\n            Tags=[\n                {\n                    'Key': 'Name',\n                    'Value': name\n                },\n            ]\n        )\n        return response\n\n    def describe_gateways(self, ip_address):\n        response = self.ec2.describe_customer_gateways(\n            DryRun=False,\n            Filters=[\n                {\n                    'Name': 'state',\n                    'Values': [\n                        'available',\n                    ]\n                },\n                {\n                    'Name': 'ip-address',\n                    'Values': [\n                        ip_address,\n                    ]\n                }\n            ]\n        )\n        return response\n\n\ndef main():\n    argument_spec = dict(\n        bgp_asn=dict(required=False, type='int'),\n        ip_address=dict(required=True),\n        name=dict(required=True),\n        routing=dict(default='dynamic', choices=['dynamic', 'static']),\n        state=dict(default='present', choices=['present', 'absent']),\n    )\n\n    module = AnsibleAWSModule(\n        argument_spec=argument_spec,\n        supports_check_mode=True,\n        required_if=[\n            ('routing', 'dynamic', ['bgp_asn'])\n        ]\n    )\n\n    gw_mgr = Ec2CustomerGatewayManager(module)\n\n    name = module.params.get('name')\n\n    existing = gw_mgr.describe_gateways(module.params['ip_address'])\n\n    results = dict(changed=False)\n    if module.params['state'] == 'present':\n        if existing['CustomerGateways']:\n            existing['CustomerGateway'] = existing['CustomerGateways'][0]\n            results['gateway'] = existing\n            if existing['CustomerGateway']['Tags']:\n                tag_array = existing['CustomerGateway']['Tags']\n                for key, value in enumerate(tag_array):\n                    if value['Key'] == 'Name':\n                        current_name = value['Value']\n                        if current_name != name:\n                            results['name'] = gw_mgr.tag_cgw_name(\n                                results['gateway']['CustomerGateway']['CustomerGatewayId'],\n                                module.params['name'],\n                            )\n                            results['changed'] = True\n        else:\n            if not module.check_mode:\n                results['gateway'] = gw_mgr.ensure_cgw_present(\n                    module.params['bgp_asn'],\n                    module.params['ip_address'],\n                )\n                results['name'] = gw_mgr.tag_cgw_name(\n                    results['gateway']['CustomerGateway']['CustomerGatewayId'],\n                    module.params['name'],\n                )\n            results['changed'] = True\n\n    elif module.params['state'] == 'absent':\n        if existing['CustomerGateways']:\n            existing['CustomerGateway'] = existing['CustomerGateways'][0]\n            results['gateway'] = existing\n            if not module.check_mode:\n                results['gateway'] = gw_mgr.ensure_cgw_absent(\n                    existing['CustomerGateway']['CustomerGatewayId']\n                )\n            results['changed'] = True\n\n    pretty_results = camel_dict_to_snake_dict(results)\n    module.exit_json(**pretty_results)\n\n\nif __name__ == '__main__':\n    main()\n", "path": "plugins/modules/ec2_customer_gateway.py"}]}