| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: ec2_carrier_gateway |
| 10 |
version_added: 6.0.0 |
| 11 |
short_description: Manage an AWS VPC Carrier gateway |
| 12 |
description: |
| 13 |
- Manage an AWS VPC Carrier gateway. |
| 14 |
author: |
| 15 |
- "Marco Braga (@mtulio)" |
| 16 |
options: |
| 17 |
vpc_id: |
| 18 |
description: |
| 19 |
- The VPC ID for the VPC in which to manage the Carrier Gateway. |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
carrier_gateway_id: |
| 23 |
description: |
| 24 |
- The Carrier Gateway ID to manage the Carrier Gateway. |
| 25 |
required: false |
| 26 |
type: str |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create or terminate the Carrier Gateway. |
| 30 |
default: present |
| 31 |
choices: [ 'present', 'absent' ] |
| 32 |
type: str |
| 33 |
extends_documentation_fragment: |
| 34 |
- amazon.aws.common.modules |
| 35 |
- amazon.aws.region.modules |
| 36 |
- amazon.aws.tags |
| 37 |
- amazon.aws.boto3 |
| 38 |
""" |
| 39 |
|
| 40 |
EXAMPLES = r""" |
| 41 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 42 |
|
| 43 |
# Ensure that the VPC has an Carrier Gateway. |
| 44 |
# The Carrier Gateway ID can be accessed via {{cagw.carrier_gateway_id}} for use in setting up Route tables etc. |
| 45 |
- name: Create Carrier gateway |
| 46 |
community.aws.ec2_carrier_gateway: |
| 47 |
vpc_id: vpc-abcdefgh |
| 48 |
state: present |
| 49 |
register: cagw |
| 50 |
|
| 51 |
- name: Create Carrier gateway with tags |
| 52 |
community.aws.ec2_carrier_gateway: |
| 53 |
vpc_id: vpc-abcdefgh |
| 54 |
state: present |
| 55 |
tags: |
| 56 |
Tag1: tag1 |
| 57 |
Tag2: tag2 |
| 58 |
register: cagw |
| 59 |
|
| 60 |
- name: Delete Carrier gateway |
| 61 |
community.aws.ec2_carrier_gateway: |
| 62 |
vpc_id: vpc-abcdefgh |
| 63 |
carrier_gateway_id: "cagw-123" |
| 64 |
state: absent |
| 65 |
register: vpc_cagw_delete |
| 66 |
""" |
| 67 |
|
| 68 |
RETURN = r""" |
| 69 |
changed: |
| 70 |
description: If any changes have been made to the Carrier Gateway. |
| 71 |
type: bool |
| 72 |
returned: always |
| 73 |
sample: |
| 74 |
changed: false |
| 75 |
carrier_gateway_id: |
| 76 |
description: The unique identifier for the Carrier Gateway. |
| 77 |
type: str |
| 78 |
returned: I(state=present) |
| 79 |
sample: |
| 80 |
carrier_gateway_id: "cagw-XXXXXXXX" |
| 81 |
tags: |
| 82 |
description: The tags associated the Carrier Gateway. |
| 83 |
type: dict |
| 84 |
returned: I(state=present) |
| 85 |
sample: |
| 86 |
tags: |
| 87 |
"Ansible": "Test" |
| 88 |
vpc_id: |
| 89 |
description: The VPC ID associated with the Carrier Gateway. |
| 90 |
type: str |
| 91 |
returned: I(state=present) |
| 92 |
sample: |
| 93 |
vpc_id: "vpc-XXXXXXXX" |
| 94 |
""" |
| 95 |
|
| 96 |
try: |
| 97 |
import botocore |
| 98 |
except ImportError: |
| 99 |
pass # caught by AnsibleAWSModule |
| 100 |
|
| 101 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 102 |
|
| 103 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_message |
| 104 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags |
| 105 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 106 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 107 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 108 |
|
| 109 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 110 |
|
| 111 |
|
| 112 |
@AWSRetry.jittered_backoff(retries=10, delay=10) |
| 113 |
def describe_cagws_with_backoff(connection, **params): |
| 114 |
paginator = connection.get_paginator("describe_carrier_gateways") |
| 115 |
return paginator.paginate(**params).build_full_result()["CarrierGateways"] |
| 116 |
|
| 117 |
|
| 118 |
class AnsibleEc2Cagw: |
| 119 |
def __init__(self, module, results): |
| 120 |
self._module = module |
| 121 |
self._results = results |
| 122 |
self._connection = self._module.client("ec2", retry_decorator=AWSRetry.jittered_backoff()) |
| 123 |
self._check_mode = self._module.check_mode |
| 124 |
|
| 125 |
def process(self): |
| 126 |
vpc_id = self._module.params.get("vpc_id") |
| 127 |
state = self._module.params.get("state", "present") |
| 128 |
tags = self._module.params.get("tags") |
| 129 |
purge_tags = self._module.params.get("purge_tags") |
| 130 |
|
| 131 |
if state == "present": |
| 132 |
self.ensure_cagw_present(vpc_id, tags, purge_tags) |
| 133 |
elif state == "absent": |
| 134 |
self.ensure_cagw_absent(vpc_id) |
| 135 |
|
| 136 |
def get_matching_cagw(self, vpc_id, carrier_gateway_id=None): |
| 137 |
""" |
| 138 |
Returns the carrier gateway found. |
| 139 |
Parameters: |
| 140 |
vpc_id (str): VPC ID |
| 141 |
carrier_gateway_id (str): Carrier Gateway ID, if specified |
| 142 |
Returns: |
| 143 |
cagw (dict): dict of cagw found, None if none found |
| 144 |
""" |
| 145 |
filters = ansible_dict_to_boto3_filter_list({"vpc-id": vpc_id}) |
| 146 |
try: |
| 147 |
if not carrier_gateway_id: |
| 148 |
cagws = describe_cagws_with_backoff( |
| 149 |
self._connection, |
| 150 |
Filters=filters, |
| 151 |
) |
| 152 |
else: |
| 153 |
cagws = describe_cagws_with_backoff( |
| 154 |
self._connection, |
| 155 |
CarrierGatewayIds=[carrier_gateway_id], |
| 156 |
) |
| 157 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 158 |
self._module.fail_json_aws(e) |
| 159 |
|
| 160 |
cagw = None |
| 161 |
if len(cagws) > 1: |
| 162 |
self._module.fail_json(msg=f"EC2 returned more than one Carrier Gateway for VPC {vpc_id}, aborting") |
| 163 |
elif cagws: |
| 164 |
cagw = camel_dict_to_snake_dict(cagws[0]) |
| 165 |
|
| 166 |
return cagw |
| 167 |
|
| 168 |
@staticmethod |
| 169 |
def get_cagw_info(cagw, vpc_id): |
| 170 |
return { |
| 171 |
"carrier_gateway_id": cagw["carrier_gateway_id"], |
| 172 |
"tags": boto3_tag_list_to_ansible_dict(cagw["tags"]), |
| 173 |
"vpc_id": vpc_id, |
| 174 |
} |
| 175 |
|
| 176 |
def ensure_cagw_absent(self, vpc_id): |
| 177 |
cagw = self.get_matching_cagw(vpc_id) |
| 178 |
if cagw is None: |
| 179 |
return self._results |
| 180 |
|
| 181 |
if self._check_mode: |
| 182 |
self._results["changed"] = True |
| 183 |
return self._results |
| 184 |
|
| 185 |
try: |
| 186 |
self._results["changed"] = True |
| 187 |
self._connection.delete_carrier_gateway( |
| 188 |
aws_retry=True, |
| 189 |
CarrierGatewayId=cagw["carrier_gateway_id"], |
| 190 |
) |
| 191 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 192 |
self._module.fail_json_aws(e, msg="Unable to delete Carrier Gateway") |
| 193 |
|
| 194 |
return self._results |
| 195 |
|
| 196 |
def ensure_cagw_present(self, vpc_id, tags, purge_tags): |
| 197 |
cagw = self.get_matching_cagw(vpc_id) |
| 198 |
|
| 199 |
if cagw is None: |
| 200 |
if self._check_mode: |
| 201 |
self._results["changed"] = True |
| 202 |
self._results["carrier_gateway_id"] = None |
| 203 |
return self._results |
| 204 |
|
| 205 |
try: |
| 206 |
response = self._connection.create_carrier_gateway(VpcId=vpc_id, aws_retry=True) |
| 207 |
cagw = camel_dict_to_snake_dict(response["CarrierGateway"]) |
| 208 |
self._results["changed"] = True |
| 209 |
except is_boto3_error_message("You must be opted into a wavelength zone to create a carrier gateway."): |
| 210 |
self._module.fail_json(msg="You must be opted into a wavelength zone to create a carrier gateway") |
| 211 |
except botocore.exceptions.WaiterError as e: |
| 212 |
self._module.fail_json_aws(e, msg="No Carrier Gateway exists.") |
| 213 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 214 |
self._module.fail_json_aws(e, msg="Unable to create Carrier Gateway") |
| 215 |
|
| 216 |
# Modify tags |
| 217 |
self._results["changed"] |= ensure_ec2_tags( |
| 218 |
self._connection, |
| 219 |
self._module, |
| 220 |
cagw["carrier_gateway_id"], |
| 221 |
resource_type="carrier-gateway", |
| 222 |
tags=tags, |
| 223 |
purge_tags=purge_tags, |
| 224 |
retry_codes="InvalidCarrierGatewayID.NotFound", |
| 225 |
) |
| 226 |
|
| 227 |
# Update cagw |
| 228 |
cagw = self.get_matching_cagw(vpc_id, carrier_gateway_id=cagw["carrier_gateway_id"]) |
| 229 |
cagw_info = self.get_cagw_info(cagw, vpc_id) |
| 230 |
self._results.update(cagw_info) |
| 231 |
|
| 232 |
return self._results |
| 233 |
|
| 234 |
|
| 235 |
def main(): |
| 236 |
argument_spec = dict( |
| 237 |
carrier_gateway_id=dict(required=False), |
| 238 |
vpc_id=dict(required=True), |
| 239 |
state=dict(default="present", choices=["present", "absent"]), |
| 240 |
tags=dict(required=False, type="dict", aliases=["resource_tags"]), |
| 241 |
purge_tags=dict(default=True, type="bool"), |
| 242 |
) |
| 243 |
|
| 244 |
module = AnsibleAWSModule( |
| 245 |
argument_spec=argument_spec, |
| 246 |
required_one_of=[["vpc_id", "carrier_gateway_id"]], |
| 247 |
supports_check_mode=True, |
| 248 |
) |
| 249 |
results = dict( |
| 250 |
changed=False, |
| 251 |
) |
| 252 |
cagw_manager = AnsibleEc2Cagw(module=module, results=results) |
| 253 |
cagw_manager.process() |
| 254 |
|
| 255 |
module.exit_json(**results) |
| 256 |
|
| 257 |
|
| 258 |
if __name__ == "__main__": |
| 259 |
main() |