| 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: directconnect_gateway |
| 9 |
author: |
| 10 |
- Gobin Sougrakpam (@gobins) |
| 11 |
version_added: 1.0.0 |
| 12 |
short_description: Manage AWS Direct Connect gateway |
| 13 |
description: |
| 14 |
- Creates AWS Direct Connect Gateway. |
| 15 |
- Deletes AWS Direct Connect Gateway. |
| 16 |
- Attaches Virtual Gateways to Direct Connect Gateway. |
| 17 |
- Detaches Virtual Gateways to Direct Connect Gateway. |
| 18 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_direct_connect_gateway). |
| 19 |
The usage did not change. |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- Set I(state=present) to ensure a resource is created. |
| 24 |
- Set I(state=absent) to remove a resource. |
| 25 |
default: present |
| 26 |
choices: [ "present", "absent"] |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of the Direct Connect Gateway to be created or deleted. |
| 31 |
type: str |
| 32 |
amazon_asn: |
| 33 |
description: |
| 34 |
- The Amazon side ASN. |
| 35 |
- Required when I(state=present). |
| 36 |
type: str |
| 37 |
direct_connect_gateway_id: |
| 38 |
description: |
| 39 |
- The ID of an existing Direct Connect Gateway. |
| 40 |
- Required when I(state=absent). |
| 41 |
type: str |
| 42 |
virtual_gateway_id: |
| 43 |
description: |
| 44 |
- The VPN gateway ID of an existing virtual gateway. |
| 45 |
type: str |
| 46 |
wait_timeout: |
| 47 |
description: |
| 48 |
- How long to wait for the association to be deleted. |
| 49 |
type: int |
| 50 |
default: 320 |
| 51 |
extends_documentation_fragment: |
| 52 |
- amazon.aws.common.modules |
| 53 |
- amazon.aws.region.modules |
| 54 |
- amazon.aws.boto3 |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
- name: Create a new direct connect gateway attached to virtual private gateway |
| 59 |
community.aws.directconnect_gateway: |
| 60 |
state: present |
| 61 |
name: my-dx-gateway |
| 62 |
amazon_asn: 7224 |
| 63 |
virtual_gateway_id: vpg-12345 |
| 64 |
register: created_dxgw |
| 65 |
|
| 66 |
- name: Create a new unattached dxgw |
| 67 |
community.aws.directconnect_gateway: |
| 68 |
state: present |
| 69 |
name: my-dx-gateway |
| 70 |
amazon_asn: 7224 |
| 71 |
register: created_dxgw |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
result: |
| 76 |
description: |
| 77 |
- The attributes of the Direct Connect Gateway |
| 78 |
type: complex |
| 79 |
returned: I(state=present) |
| 80 |
contains: |
| 81 |
amazon_side_asn: |
| 82 |
description: ASN on the amazon side. |
| 83 |
type: str |
| 84 |
direct_connect_gateway_id: |
| 85 |
description: The ID of the direct connect gateway. |
| 86 |
type: str |
| 87 |
direct_connect_gateway_name: |
| 88 |
description: The name of the direct connect gateway. |
| 89 |
type: str |
| 90 |
direct_connect_gateway_state: |
| 91 |
description: The state of the direct connect gateway. |
| 92 |
type: str |
| 93 |
owner_account: |
| 94 |
description: The AWS account ID of the owner of the direct connect gateway. |
| 95 |
type: str |
| 96 |
""" |
| 97 |
|
| 98 |
import time |
| 99 |
|
| 100 |
try: |
| 101 |
import botocore |
| 102 |
except ImportError: |
| 103 |
pass # Handled by AnsibleAWSModule |
| 104 |
|
| 105 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 106 |
|
| 107 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 108 |
|
| 109 |
|
| 110 |
def dx_gateway_info(client, gateway_id, module): |
| 111 |
try: |
| 112 |
resp = client.describe_direct_connect_gateways( |
| 113 |
directConnectGatewayId=gateway_id, |
| 114 |
) |
| 115 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 116 |
module.fail_json_aws(e, msg="Failed to fetch gateway information.") |
| 117 |
if resp["directConnectGateways"]: |
| 118 |
return resp["directConnectGateways"][0] |
| 119 |
|
| 120 |
|
| 121 |
def wait_for_status(client, module, gateway_id, virtual_gateway_id, status): |
| 122 |
polling_increment_secs = 15 |
| 123 |
max_retries = 3 |
| 124 |
status_achieved = False |
| 125 |
|
| 126 |
for x in range(0, max_retries): |
| 127 |
try: |
| 128 |
response = check_dxgw_association( |
| 129 |
client, |
| 130 |
module, |
| 131 |
gateway_id=gateway_id, |
| 132 |
virtual_gateway_id=virtual_gateway_id, |
| 133 |
) |
| 134 |
if response["directConnectGatewayAssociations"]: |
| 135 |
if response["directConnectGatewayAssociations"][0]["associationState"] == status: |
| 136 |
status_achieved = True |
| 137 |
break |
| 138 |
time.sleep(polling_increment_secs) |
| 139 |
else: |
| 140 |
status_achieved = True |
| 141 |
break |
| 142 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 143 |
module.fail_json_aws(e, msg="Failed while waiting for gateway association.") |
| 144 |
|
| 145 |
result = response |
| 146 |
return status_achieved, result |
| 147 |
|
| 148 |
|
| 149 |
def associate_direct_connect_gateway(client, module, gateway_id): |
| 150 |
params = dict() |
| 151 |
params["virtual_gateway_id"] = module.params.get("virtual_gateway_id") |
| 152 |
try: |
| 153 |
response = client.create_direct_connect_gateway_association( |
| 154 |
directConnectGatewayId=gateway_id, |
| 155 |
virtualGatewayId=params["virtual_gateway_id"], |
| 156 |
) |
| 157 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 158 |
module.fail_json_aws(e, "Failed to associate gateway") |
| 159 |
|
| 160 |
status_achieved, dxgw = wait_for_status(client, module, gateway_id, params["virtual_gateway_id"], "associating") |
| 161 |
if not status_achieved: |
| 162 |
module.fail_json(msg="Error waiting for dxgw to attach to vpg - please check the AWS console") |
| 163 |
|
| 164 |
result = response |
| 165 |
return result |
| 166 |
|
| 167 |
|
| 168 |
def delete_association(client, module, gateway_id, virtual_gateway_id): |
| 169 |
try: |
| 170 |
response = client.delete_direct_connect_gateway_association( |
| 171 |
directConnectGatewayId=gateway_id, |
| 172 |
virtualGatewayId=virtual_gateway_id, |
| 173 |
) |
| 174 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 175 |
module.fail_json_aws(e, msg="Failed to delete gateway association.") |
| 176 |
|
| 177 |
status_achieved, dxgw = wait_for_status(client, module, gateway_id, virtual_gateway_id, "disassociating") |
| 178 |
if not status_achieved: |
| 179 |
module.fail_json(msg="Error waiting for dxgw to detach from vpg - please check the AWS console") |
| 180 |
|
| 181 |
result = response |
| 182 |
return result |
| 183 |
|
| 184 |
|
| 185 |
def create_dx_gateway(client, module): |
| 186 |
params = dict() |
| 187 |
params["name"] = module.params.get("name") |
| 188 |
params["amazon_asn"] = module.params.get("amazon_asn") |
| 189 |
try: |
| 190 |
response = client.create_direct_connect_gateway( |
| 191 |
directConnectGatewayName=params["name"], |
| 192 |
amazonSideAsn=int(params["amazon_asn"]), |
| 193 |
) |
| 194 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 195 |
module.fail_json_aws(e, msg="Failed to create direct connect gateway.") |
| 196 |
|
| 197 |
result = response |
| 198 |
return result |
| 199 |
|
| 200 |
|
| 201 |
def find_dx_gateway(client, module, gateway_id=None): |
| 202 |
params = dict() |
| 203 |
gateways = list() |
| 204 |
if gateway_id is not None: |
| 205 |
params["directConnectGatewayId"] = gateway_id |
| 206 |
while True: |
| 207 |
try: |
| 208 |
resp = client.describe_direct_connect_gateways(**params) |
| 209 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 210 |
module.fail_json_aws(e, msg="Failed to describe gateways") |
| 211 |
gateways.extend(resp["directConnectGateways"]) |
| 212 |
if "nextToken" in resp: |
| 213 |
params["nextToken"] = resp["nextToken"] |
| 214 |
else: |
| 215 |
break |
| 216 |
if gateways != []: |
| 217 |
count = 0 |
| 218 |
for gateway in gateways: |
| 219 |
if module.params.get("name") == gateway["directConnectGatewayName"]: |
| 220 |
count += 1 |
| 221 |
return gateway |
| 222 |
return None |
| 223 |
|
| 224 |
|
| 225 |
def check_dxgw_association(client, module, gateway_id, virtual_gateway_id=None): |
| 226 |
try: |
| 227 |
if virtual_gateway_id is None: |
| 228 |
resp = client.describe_direct_connect_gateway_associations( |
| 229 |
directConnectGatewayId=gateway_id, |
| 230 |
) |
| 231 |
else: |
| 232 |
resp = client.describe_direct_connect_gateway_associations( |
| 233 |
directConnectGatewayId=gateway_id, |
| 234 |
virtualGatewayId=virtual_gateway_id, |
| 235 |
) |
| 236 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 237 |
module.fail_json_aws(e, msg="Failed to check gateway association") |
| 238 |
return resp |
| 239 |
|
| 240 |
|
| 241 |
def ensure_present(client, module): |
| 242 |
# If an existing direct connect gateway matches our args |
| 243 |
# then a match is considered to have been found and we will not create another dxgw. |
| 244 |
|
| 245 |
changed = False |
| 246 |
params = dict() |
| 247 |
result = dict() |
| 248 |
params["name"] = module.params.get("name") |
| 249 |
params["amazon_asn"] = module.params.get("amazon_asn") |
| 250 |
params["virtual_gateway_id"] = module.params.get("virtual_gateway_id") |
| 251 |
|
| 252 |
# check if a gateway matching our module args already exists |
| 253 |
existing_dxgw = find_dx_gateway(client, module) |
| 254 |
|
| 255 |
if existing_dxgw is not None and existing_dxgw["directConnectGatewayState"] != "deleted": |
| 256 |
gateway_id = existing_dxgw["directConnectGatewayId"] |
| 257 |
# if a gateway_id was provided, check if it is attach to the DXGW |
| 258 |
if params["virtual_gateway_id"]: |
| 259 |
resp = check_dxgw_association( |
| 260 |
client, module, gateway_id=gateway_id, virtual_gateway_id=params["virtual_gateway_id"] |
| 261 |
) |
| 262 |
if not resp["directConnectGatewayAssociations"]: |
| 263 |
# attach the dxgw to the supplied virtual_gateway_id |
| 264 |
associate_direct_connect_gateway(client, module, gateway_id) |
| 265 |
changed = True |
| 266 |
# if params['virtual_gateway_id'] is not provided, check the dxgw is attached to a VPG. If so, detach it. |
| 267 |
else: |
| 268 |
existing_dxgw = find_dx_gateway(client, module) |
| 269 |
|
| 270 |
resp = check_dxgw_association(client, module, gateway_id=gateway_id) |
| 271 |
if resp["directConnectGatewayAssociations"]: |
| 272 |
for association in resp["directConnectGatewayAssociations"]: |
| 273 |
if association["associationState"] not in ["disassociating", "disassociated"]: |
| 274 |
delete_association( |
| 275 |
client, |
| 276 |
module, |
| 277 |
gateway_id=gateway_id, |
| 278 |
virtual_gateway_id=association["virtualGatewayId"], |
| 279 |
) |
| 280 |
else: |
| 281 |
# create a new dxgw |
| 282 |
new_dxgw = create_dx_gateway(client, module) |
| 283 |
changed = True |
| 284 |
gateway_id = new_dxgw["directConnectGateway"]["directConnectGatewayId"] |
| 285 |
|
| 286 |
# if a vpc-id was supplied, attempt to attach it to the dxgw |
| 287 |
if params["virtual_gateway_id"]: |
| 288 |
associate_direct_connect_gateway(client, module, gateway_id) |
| 289 |
resp = check_dxgw_association( |
| 290 |
client, |
| 291 |
module, |
| 292 |
gateway_id=gateway_id, |
| 293 |
) |
| 294 |
if resp["directConnectGatewayAssociations"]: |
| 295 |
changed = True |
| 296 |
|
| 297 |
result = dx_gateway_info(client, gateway_id, module) |
| 298 |
return changed, result |
| 299 |
|
| 300 |
|
| 301 |
def ensure_absent(client, module): |
| 302 |
# If an existing direct connect gateway matches our args |
| 303 |
# then a match is considered to have been found and we will not create another dxgw. |
| 304 |
|
| 305 |
changed = False |
| 306 |
dx_gateway_id = module.params.get("direct_connect_gateway_id") |
| 307 |
existing_dxgw = find_dx_gateway(client, module, dx_gateway_id) |
| 308 |
if existing_dxgw is not None: |
| 309 |
resp = check_dxgw_association(client, module, gateway_id=dx_gateway_id) |
| 310 |
if resp["directConnectGatewayAssociations"]: |
| 311 |
for association in resp["directConnectGatewayAssociations"]: |
| 312 |
if association["associationState"] not in ["disassociating", "disassociated"]: |
| 313 |
delete_association( |
| 314 |
client, |
| 315 |
module, |
| 316 |
gateway_id=dx_gateway_id, |
| 317 |
virtual_gateway_id=association["virtualGatewayId"], |
| 318 |
) |
| 319 |
# wait for deleting association |
| 320 |
timeout = time.time() + module.params.get("wait_timeout") |
| 321 |
while time.time() < timeout: |
| 322 |
resp = check_dxgw_association(client, module, gateway_id=dx_gateway_id) |
| 323 |
if resp["directConnectGatewayAssociations"] != []: |
| 324 |
time.sleep(15) |
| 325 |
else: |
| 326 |
break |
| 327 |
|
| 328 |
try: |
| 329 |
resp = client.delete_direct_connect_gateway( |
| 330 |
directConnectGatewayId=dx_gateway_id, |
| 331 |
) |
| 332 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 333 |
module.fail_json_aws(e, msg="Failed to delete gateway") |
| 334 |
return changed |
| 335 |
|
| 336 |
|
| 337 |
def main(): |
| 338 |
argument_spec = dict( |
| 339 |
state=dict(default="present", choices=["present", "absent"]), |
| 340 |
name=dict(), |
| 341 |
amazon_asn=dict(), |
| 342 |
virtual_gateway_id=dict(), |
| 343 |
direct_connect_gateway_id=dict(), |
| 344 |
wait_timeout=dict(type="int", default=320), |
| 345 |
) |
| 346 |
required_if = [("state", "present", ["name", "amazon_asn"]), ("state", "absent", ["direct_connect_gateway_id"])] |
| 347 |
module = AnsibleAWSModule( |
| 348 |
argument_spec=argument_spec, |
| 349 |
required_if=required_if, |
| 350 |
) |
| 351 |
|
| 352 |
state = module.params.get("state") |
| 353 |
|
| 354 |
try: |
| 355 |
client = module.client("directconnect") |
| 356 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 357 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 358 |
|
| 359 |
if state == "present": |
| 360 |
(changed, results) = ensure_present(client, module) |
| 361 |
elif state == "absent": |
| 362 |
changed = ensure_absent(client, module) |
| 363 |
results = {} |
| 364 |
|
| 365 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(results)) |
| 366 |
|
| 367 |
|
| 368 |
if __name__ == "__main__": |
| 369 |
main() |