| 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 |
module: ec2_transit_gateway_vpc_attachment |
| 9 |
short_description: Create and delete AWS Transit Gateway VPC attachments |
| 10 |
version_added: 4.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
description: |
| 13 |
- Creates, Deletes and Updates AWS Transit Gateway VPC Attachments. |
| 14 |
options: |
| 15 |
transit_gateway: |
| 16 |
description: |
| 17 |
- The ID of the Transit Gateway that the attachment belongs to. |
| 18 |
- When creating a new attachment, O(transit_gateway) must be provided. |
| 19 |
- At least one of O(name), O(transit_gateway) and O(id) must be provided. |
| 20 |
- O(transit_gateway) is an immutable setting and can not be updated on an |
| 21 |
existing attachment. |
| 22 |
type: str |
| 23 |
required: false |
| 24 |
aliases: ["transit_gateway_id"] |
| 25 |
id: |
| 26 |
description: |
| 27 |
- The ID of the Transit Gateway Attachment. |
| 28 |
- When O(id) is not set, a search using O(transit_gateway) and O(name) will be |
| 29 |
performed. If multiple results are returned, the module will fail. |
| 30 |
- At least one of O(name), O(transit_gateway) and O(id) must be provided. |
| 31 |
type: str |
| 32 |
required: false |
| 33 |
aliases: ["attachment_id"] |
| 34 |
name: |
| 35 |
description: |
| 36 |
- The V(Name) tag of the Transit Gateway attachment. |
| 37 |
- Providing both O(id) and O(name) will set the V(Name) tag on an existing |
| 38 |
attachment the matching O(id). |
| 39 |
- Setting the V(Name) tag in O(tags) will also result in the V(Name) tag being |
| 40 |
updated. |
| 41 |
- At least one of O(name), O(transit_gateway) and O(id) must be provided. |
| 42 |
type: str |
| 43 |
required: false |
| 44 |
state: |
| 45 |
description: |
| 46 |
- Create or remove the Transit Gateway attachment. |
| 47 |
type: str |
| 48 |
required: false |
| 49 |
choices: ["present", "absent"] |
| 50 |
default: 'present' |
| 51 |
subnets: |
| 52 |
description: |
| 53 |
- The ID of the subnets in which to create the transit gateway VPC attachment. |
| 54 |
- Required when creating a new attachment. |
| 55 |
type: list |
| 56 |
elements: str |
| 57 |
required: false |
| 58 |
purge_subnets: |
| 59 |
description: |
| 60 |
- If O(purge_subnets=true), existing subnets will be removed from the |
| 61 |
attachment as necessary to match exactly what is defined by O(subnets). |
| 62 |
type: bool |
| 63 |
required: false |
| 64 |
default: true |
| 65 |
dns_support: |
| 66 |
description: |
| 67 |
- Whether DNS support is enabled. |
| 68 |
type: bool |
| 69 |
required: false |
| 70 |
ipv6_support: |
| 71 |
description: |
| 72 |
- Whether IPv6 support is enabled. |
| 73 |
type: bool |
| 74 |
required: false |
| 75 |
appliance_mode_support: |
| 76 |
description: |
| 77 |
- Whether the attachment is configured for appliance mode. |
| 78 |
- When appliance mode is enabled, Transit Gateway, using 4-tuples of an |
| 79 |
IP packet, selects a single Transit Gateway ENI in the Appliance VPC |
| 80 |
for the life of a flow to send traffic to. |
| 81 |
type: bool |
| 82 |
required: false |
| 83 |
wait: |
| 84 |
description: |
| 85 |
- Whether to wait for the Transit Gateway attachment to reach the |
| 86 |
C(Available) or C(Deleted) state before the module returns. |
| 87 |
type: bool |
| 88 |
required: false |
| 89 |
default: true |
| 90 |
wait_timeout: |
| 91 |
description: |
| 92 |
- Maximum time, in seconds, to wait for the Transit Gateway attachment |
| 93 |
to reach the expected state. |
| 94 |
- Defaults to 600 seconds. |
| 95 |
type: int |
| 96 |
default: 600 |
| 97 |
required: false |
| 98 |
author: |
| 99 |
- Mark Chappell (@tremble) |
| 100 |
- Alina Buzachis (@alinabuzachis) |
| 101 |
extends_documentation_fragment: |
| 102 |
- amazon.aws.common.modules |
| 103 |
- amazon.aws.region.modules |
| 104 |
- amazon.aws.tags |
| 105 |
- amazon.aws.boto3 |
| 106 |
""" |
| 107 |
|
| 108 |
EXAMPLES = r""" |
| 109 |
- name: Create a Transit Gateway attachment |
| 110 |
amazon.aws.ec2_transit_gateway_vpc_attachment: |
| 111 |
state: "present" |
| 112 |
transit_gateway: "tgw-123456789abcdef01" |
| 113 |
name: "AnsibleTest-1" |
| 114 |
subnets: |
| 115 |
- "subnet-00000000000000000" |
| 116 |
- "subnet-11111111111111111" |
| 117 |
- "subnet-22222222222222222" |
| 118 |
ipv6_support: true |
| 119 |
purge_subnets: true |
| 120 |
dns_support: true |
| 121 |
appliance_mode_support: true |
| 122 |
tags: |
| 123 |
TestTag: "changed data in Test Tag" |
| 124 |
|
| 125 |
- name: Set sub options on a Transit Gateway attachment |
| 126 |
amazon.aws.ec2_transit_gateway_vpc_attachment: |
| 127 |
state: "present" |
| 128 |
id: "tgw-attach-0c0c5fd0b0f01d1c9" |
| 129 |
name: "AnsibleTest-1" |
| 130 |
ipv6_support: true |
| 131 |
purge_subnets: false |
| 132 |
dns_support: false |
| 133 |
appliance_mode_support: true |
| 134 |
|
| 135 |
- name: Delete the transit gateway |
| 136 |
amazon.aws.ec2_transit_gateway_vpc_attachment: |
| 137 |
state: "absent" |
| 138 |
id: "tgw-attach-0c0c5fd0b0f01d1c9" |
| 139 |
""" |
| 140 |
|
| 141 |
RETURN = r""" |
| 142 |
attachments: |
| 143 |
description: The attributes of the Transit Gateway attachments. |
| 144 |
type: list |
| 145 |
elements: dict |
| 146 |
returned: success |
| 147 |
contains: |
| 148 |
creation_time: |
| 149 |
description: |
| 150 |
- An ISO 8601 date time stamp of when the attachment was created. |
| 151 |
type: str |
| 152 |
returned: success |
| 153 |
sample: "2022-03-10T16:40:26+00:00" |
| 154 |
options: |
| 155 |
description: |
| 156 |
- Additional VPC attachment options. |
| 157 |
type: dict |
| 158 |
returned: success |
| 159 |
contains: |
| 160 |
appliance_mode_support: |
| 161 |
description: |
| 162 |
- Indicates whether appliance mode support is enabled. |
| 163 |
type: str |
| 164 |
returned: success |
| 165 |
sample: "enable" |
| 166 |
dns_support: |
| 167 |
description: |
| 168 |
- Indicates whether DNS support is enabled. |
| 169 |
type: str |
| 170 |
returned: success |
| 171 |
sample: "disable" |
| 172 |
ipv6_support: |
| 173 |
description: |
| 174 |
- Indicates whether IPv6 support is disabled. |
| 175 |
type: str |
| 176 |
returned: success |
| 177 |
sample: "disable" |
| 178 |
security_group_referencing_support: |
| 179 |
description: |
| 180 |
- Indicated weather security group referencing support is disabled. |
| 181 |
type: str |
| 182 |
returned: success |
| 183 |
sample: "enable" |
| 184 |
state: |
| 185 |
description: |
| 186 |
- The state of the attachment. |
| 187 |
type: str |
| 188 |
returned: success |
| 189 |
sample: "deleting" |
| 190 |
subnet_ids: |
| 191 |
description: |
| 192 |
- The IDs of the subnets in use by the attachment. |
| 193 |
type: list |
| 194 |
elements: str |
| 195 |
returned: success |
| 196 |
sample: ["subnet-0123456789abcdef0", "subnet-11111111111111111"] |
| 197 |
tags: |
| 198 |
description: |
| 199 |
- A dictionary representing the resource tags. |
| 200 |
type: dict |
| 201 |
returned: success |
| 202 |
transit_gateway_attachment_id: |
| 203 |
description: |
| 204 |
- The ID of the attachment. |
| 205 |
type: str |
| 206 |
returned: success |
| 207 |
sample: "tgw-attach-0c0c5fd0b0f01d1c9" |
| 208 |
transit_gateway_id: |
| 209 |
description: |
| 210 |
- The ID of the transit gateway that the attachment is connected to. |
| 211 |
type: str |
| 212 |
returned: success |
| 213 |
sample: "tgw-0123456789abcdef0" |
| 214 |
vpc_id: |
| 215 |
description: |
| 216 |
- The ID of the VPC that the attachment is connected to. |
| 217 |
type: str |
| 218 |
returned: success |
| 219 |
sample: "vpc-0123456789abcdef0" |
| 220 |
vpc_owner_id: |
| 221 |
description: |
| 222 |
- The ID of the account that the VPC belongs to. |
| 223 |
type: str |
| 224 |
returned: success |
| 225 |
sample: "1234567890122" |
| 226 |
""" |
| 227 |
|
| 228 |
from typing import NoReturn |
| 229 |
|
| 230 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 231 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import boto3_resource_to_ansible_dict |
| 232 |
from ansible_collections.amazon.aws.plugins.module_utils.transitgateway import TransitGatewayVpcAttachmentManager |
| 233 |
from ansible_collections.amazon.aws.plugins.module_utils.transitgateway import find_existing_attachment |
| 234 |
from ansible_collections.amazon.aws.plugins.module_utils.transitgateway import get_states |
| 235 |
from ansible_collections.amazon.aws.plugins.module_utils.transitgateway import subnets_to_vpc |
| 236 |
|
| 237 |
|
| 238 |
def handle_vpc_attachments(client, module: AnsibleAWSModule) -> NoReturn: |
| 239 |
""" |
| 240 |
Handle the creation, modification, or deletion of VPC attachments |
| 241 |
based on the parameters provided in the Ansible module. |
| 242 |
|
| 243 |
Args: |
| 244 |
client: The AWS client to interact with EC2 services. |
| 245 |
module: An instance of AnsibleAWSModule. |
| 246 |
|
| 247 |
Returns: |
| 248 |
NoReturn: The function exits by calling module.exit_json() |
| 249 |
with the results of the operation. |
| 250 |
""" |
| 251 |
attach_id = module.params.get("id", None) |
| 252 |
attachment = None |
| 253 |
|
| 254 |
if not attach_id: |
| 255 |
filters = {} |
| 256 |
if module.params.get("transit_gateway"): |
| 257 |
filters["transit-gateway-id"] = module.params["transit_gateway"] |
| 258 |
if module.params.get("name"): |
| 259 |
filters["tag:Name"] = module.params["name"] |
| 260 |
if module.params.get("subnets"): |
| 261 |
vpc_id = subnets_to_vpc(client, module, module.params["subnets"]) |
| 262 |
filters["vpc-id"] = vpc_id |
| 263 |
|
| 264 |
# Attachments lurk in a 'deleted' state, for a while, ignore them so we |
| 265 |
# can reuse the names |
| 266 |
filters["state"] = get_states() |
| 267 |
|
| 268 |
attachment = find_existing_attachment(client, module, filters=filters) |
| 269 |
if attachment: |
| 270 |
attach_id = attachment["TransitGatewayAttachmentId"] |
| 271 |
else: |
| 272 |
attachment = find_existing_attachment(client, module, attachment_id=attach_id) |
| 273 |
|
| 274 |
manager = TransitGatewayVpcAttachmentManager(client, module, attachment, attachment_id=attach_id) |
| 275 |
|
| 276 |
if module.params["state"] == "absent": |
| 277 |
manager.delete_attachment() |
| 278 |
else: |
| 279 |
manager.create_or_modify_attachment() |
| 280 |
|
| 281 |
results = dict( |
| 282 |
changed=manager.changed, |
| 283 |
attachments=[manager.updated], |
| 284 |
) |
| 285 |
if manager.changed: |
| 286 |
results["diff"] = dict( |
| 287 |
before=boto3_resource_to_ansible_dict(manager.existing), |
| 288 |
after=manager.updated, |
| 289 |
) |
| 290 |
|
| 291 |
module.exit_json(**results) |
| 292 |
|
| 293 |
|
| 294 |
def main(): |
| 295 |
argument_spec = dict( |
| 296 |
state=dict(type="str", required=False, default="present", choices=["absent", "present"]), |
| 297 |
transit_gateway=dict(type="str", required=False, aliases=["transit_gateway_id"]), |
| 298 |
id=dict(type="str", required=False, aliases=["attachment_id"]), |
| 299 |
name=dict(type="str", required=False), |
| 300 |
subnets=dict(type="list", elements="str", required=False), |
| 301 |
purge_subnets=dict(type="bool", required=False, default=True), |
| 302 |
tags=dict(type="dict", required=False, aliases=["resource_tags"]), |
| 303 |
purge_tags=dict(type="bool", required=False, default=True), |
| 304 |
appliance_mode_support=dict(type="bool", required=False), |
| 305 |
dns_support=dict(type="bool", required=False), |
| 306 |
ipv6_support=dict(type="bool", required=False), |
| 307 |
wait=dict(type="bool", required=False, default=True), |
| 308 |
wait_timeout=dict(type="int", default=600, required=False), |
| 309 |
) |
| 310 |
|
| 311 |
one_of = [ |
| 312 |
["id", "transit_gateway", "name"], |
| 313 |
] |
| 314 |
|
| 315 |
module = AnsibleAWSModule( |
| 316 |
argument_spec=argument_spec, |
| 317 |
supports_check_mode=True, |
| 318 |
required_one_of=one_of, |
| 319 |
) |
| 320 |
|
| 321 |
client = module.client("ec2") |
| 322 |
|
| 323 |
handle_vpc_attachments(client, module) |
| 324 |
|
| 325 |
|
| 326 |
if __name__ == "__main__": |
| 327 |
main() |