| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 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: directconnect_link_aggregation_group |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage Direct Connect LAG bundles |
| 12 |
description: |
| 13 |
- Create, delete, or modify a Direct Connect link aggregation group. |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_direct_connect_link_aggregation_group). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- "Sloane Hertel (@s-hertel)" |
| 18 |
options: |
| 19 |
state: |
| 20 |
description: |
| 21 |
- The state of the Direct Connect link aggregation group. |
| 22 |
choices: |
| 23 |
- present |
| 24 |
- absent |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the Direct Connect link aggregation group. |
| 30 |
type: str |
| 31 |
link_aggregation_group_id: |
| 32 |
description: |
| 33 |
- The ID of the Direct Connect link aggregation group. |
| 34 |
type: str |
| 35 |
num_connections: |
| 36 |
description: |
| 37 |
- The number of connections with which to initialize the link aggregation group. |
| 38 |
type: int |
| 39 |
min_links: |
| 40 |
description: |
| 41 |
- The minimum number of physical connections that must be operational for the LAG itself to be operational. |
| 42 |
type: int |
| 43 |
location: |
| 44 |
description: |
| 45 |
- The location of the link aggregation group. |
| 46 |
type: str |
| 47 |
bandwidth: |
| 48 |
description: |
| 49 |
- The bandwidth of the link aggregation group. |
| 50 |
type: str |
| 51 |
force_delete: |
| 52 |
description: |
| 53 |
- This allows the minimum number of links to be set to 0, any hosted connections disassociated, |
| 54 |
and any virtual interfaces associated to the LAG deleted. |
| 55 |
type: bool |
| 56 |
default: false |
| 57 |
connection_id: |
| 58 |
description: |
| 59 |
- A connection ID to link with the link aggregation group upon creation. |
| 60 |
type: str |
| 61 |
delete_with_disassociation: |
| 62 |
description: |
| 63 |
- To be used with I(state=absent) to delete connections after disassociating them with the LAG. |
| 64 |
type: bool |
| 65 |
default: false |
| 66 |
wait: |
| 67 |
description: |
| 68 |
- Whether or not to wait for the operation to complete. |
| 69 |
- May be useful when waiting for virtual interfaces to be deleted. |
| 70 |
- The time to wait can be controlled by setting I(wait_timeout). |
| 71 |
type: bool |
| 72 |
default: false |
| 73 |
wait_timeout: |
| 74 |
description: |
| 75 |
- The duration in seconds to wait if I(wait=true). |
| 76 |
default: 120 |
| 77 |
type: int |
| 78 |
extends_documentation_fragment: |
| 79 |
- amazon.aws.common.modules |
| 80 |
- amazon.aws.region.modules |
| 81 |
- amazon.aws.boto3 |
| 82 |
""" |
| 83 |
|
| 84 |
EXAMPLES = r""" |
| 85 |
|
| 86 |
# create a Direct Connect connection |
| 87 |
- community.aws.directconnect_link_aggregation_group: |
| 88 |
state: present |
| 89 |
location: EqDC2 |
| 90 |
lag_id: dxlag-xxxxxxxx |
| 91 |
bandwidth: 1Gbps |
| 92 |
""" |
| 93 |
|
| 94 |
RETURN = r""" |
| 95 |
changed: |
| 96 |
type: str |
| 97 |
description: Whether or not the LAG has changed. |
| 98 |
returned: always |
| 99 |
aws_device: |
| 100 |
type: str |
| 101 |
description: The AWS Direct Connection endpoint that hosts the LAG. |
| 102 |
sample: "EqSe2-1bwfvazist2k0" |
| 103 |
returned: when I(state=present) |
| 104 |
connections: |
| 105 |
type: list |
| 106 |
description: A list of connections bundled by this LAG. |
| 107 |
sample: |
| 108 |
"connections": [ |
| 109 |
{ |
| 110 |
"aws_device": "EqSe2-1bwfvazist2k0", |
| 111 |
"bandwidth": "1Gbps", |
| 112 |
"connection_id": "dxcon-fgzjah5a", |
| 113 |
"connection_name": "Requested Connection 1 for Lag dxlag-fgtoh97h", |
| 114 |
"connection_state": "down", |
| 115 |
"lag_id": "dxlag-fgnsp4rq", |
| 116 |
"location": "EqSe2", |
| 117 |
"owner_account": "123456789012", |
| 118 |
"region": "us-west-2" |
| 119 |
} |
| 120 |
] |
| 121 |
returned: when I(state=present) |
| 122 |
connections_bandwidth: |
| 123 |
type: str |
| 124 |
description: The individual bandwidth of the physical connections bundled by the LAG. |
| 125 |
sample: "1Gbps" |
| 126 |
returned: when I(state=present) |
| 127 |
lag_id: |
| 128 |
type: str |
| 129 |
description: Unique identifier for the link aggregation group. |
| 130 |
sample: "dxlag-fgnsp4rq" |
| 131 |
returned: when I(state=present) |
| 132 |
lag_name: |
| 133 |
type: str |
| 134 |
description: User-provided name for the link aggregation group. |
| 135 |
returned: when I(state=present) |
| 136 |
lag_state: |
| 137 |
type: str |
| 138 |
description: State of the LAG. |
| 139 |
sample: "pending" |
| 140 |
returned: when I(state=present) |
| 141 |
location: |
| 142 |
type: str |
| 143 |
description: Where the connection is located. |
| 144 |
sample: "EqSe2" |
| 145 |
returned: when I(state=present) |
| 146 |
minimum_links: |
| 147 |
type: int |
| 148 |
description: The minimum number of physical connections that must be operational for the LAG itself to be operational. |
| 149 |
returned: when I(state=present) |
| 150 |
number_of_connections: |
| 151 |
type: int |
| 152 |
description: The number of physical connections bundled by the LAG. |
| 153 |
returned: when I(state=present) |
| 154 |
owner_account: |
| 155 |
type: str |
| 156 |
description: Owner account ID of the LAG. |
| 157 |
returned: when I(state=present) |
| 158 |
region: |
| 159 |
type: str |
| 160 |
description: The region in which the LAG exists. |
| 161 |
returned: when I(state=present) |
| 162 |
""" |
| 163 |
|
| 164 |
import time |
| 165 |
import traceback |
| 166 |
|
| 167 |
try: |
| 168 |
import botocore |
| 169 |
except ImportError: |
| 170 |
pass # Handled by AnsibleAWSModule |
| 171 |
|
| 172 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 173 |
|
| 174 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError |
| 175 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import delete_connection |
| 176 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import delete_virtual_interface |
| 177 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import disassociate_connection_and_lag |
| 178 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 179 |
|
| 180 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 181 |
|
| 182 |
|
| 183 |
def lag_status(client, lag_id): |
| 184 |
return lag_exists(client, lag_id=lag_id, lag_name=None, verify=False) |
| 185 |
|
| 186 |
|
| 187 |
def lag_exists(client, lag_id=None, lag_name=None, verify=True): |
| 188 |
"""If verify=True, returns the LAG ID or None |
| 189 |
If verify=False, returns the LAG's data (or an empty dict) |
| 190 |
""" |
| 191 |
try: |
| 192 |
if lag_id: |
| 193 |
response = client.describe_lags(lagId=lag_id) |
| 194 |
else: |
| 195 |
response = client.describe_lags() |
| 196 |
except botocore.exceptions.ClientError as e: |
| 197 |
if lag_id and verify: |
| 198 |
return False |
| 199 |
elif lag_id: |
| 200 |
return {} |
| 201 |
else: |
| 202 |
failed_op = "Failed to describe DirectConnect link aggregation groups." |
| 203 |
raise DirectConnectError(msg=failed_op, last_traceback=traceback.format_exc(), exception=e) |
| 204 |
|
| 205 |
match = [] # List of LAG IDs that are exact matches |
| 206 |
lag = [] # List of LAG data that are exact matches |
| 207 |
|
| 208 |
# look for matching connections |
| 209 |
if len(response.get("lags", [])) == 1 and lag_id: |
| 210 |
if response["lags"][0]["lagState"] != "deleted": |
| 211 |
match.append(response["lags"][0]["lagId"]) |
| 212 |
lag.append(response["lags"][0]) |
| 213 |
else: |
| 214 |
for each in response.get("lags", []): |
| 215 |
if each["lagState"] != "deleted": |
| 216 |
if not lag_id: |
| 217 |
if lag_name == each["lagName"]: |
| 218 |
match.append(each["lagId"]) |
| 219 |
else: |
| 220 |
match.append(each["lagId"]) |
| 221 |
|
| 222 |
# verifying if the connections exists; if true, return connection identifier, otherwise return False |
| 223 |
if verify and len(match) == 1: |
| 224 |
return match[0] |
| 225 |
elif verify: |
| 226 |
return False |
| 227 |
|
| 228 |
# not verifying if the connection exists; just return current connection info |
| 229 |
else: |
| 230 |
if len(lag) == 1: |
| 231 |
return lag[0] |
| 232 |
else: |
| 233 |
return {} |
| 234 |
|
| 235 |
|
| 236 |
def create_lag(client, num_connections, location, bandwidth, name, connection_id): |
| 237 |
if not name: |
| 238 |
raise DirectConnectError( |
| 239 |
msg="Failed to create a Direct Connect link aggregation group: name required.", |
| 240 |
last_traceback=None, |
| 241 |
exception="", |
| 242 |
) |
| 243 |
|
| 244 |
parameters = dict( |
| 245 |
numberOfConnections=num_connections, location=location, connectionsBandwidth=bandwidth, lagName=name |
| 246 |
) |
| 247 |
if connection_id: |
| 248 |
parameters.update(connectionId=connection_id) |
| 249 |
try: |
| 250 |
lag = client.create_lag(**parameters) |
| 251 |
except botocore.exceptions.ClientError as e: |
| 252 |
raise DirectConnectError( |
| 253 |
msg=f"Failed to create DirectConnect link aggregation group {name}", |
| 254 |
last_traceback=traceback.format_exc(), |
| 255 |
exception=e, |
| 256 |
) |
| 257 |
|
| 258 |
return lag["lagId"] |
| 259 |
|
| 260 |
|
| 261 |
def delete_lag(client, lag_id): |
| 262 |
try: |
| 263 |
client.delete_lag(lagId=lag_id) |
| 264 |
except botocore.exceptions.ClientError as e: |
| 265 |
raise DirectConnectError( |
| 266 |
msg=f"Failed to delete Direct Connect link aggregation group {lag_id}.", |
| 267 |
last_traceback=traceback.format_exc(), |
| 268 |
exception=e, |
| 269 |
) |
| 270 |
|
| 271 |
|
| 272 |
@AWSRetry.jittered_backoff(retries=5, delay=2, backoff=2.0, catch_extra_error_codes=["DirectConnectClientException"]) |
| 273 |
def _update_lag(client, lag_id, lag_name, min_links): |
| 274 |
params = {} |
| 275 |
if min_links: |
| 276 |
params.update(minimumLinks=min_links) |
| 277 |
if lag_name: |
| 278 |
params.update(lagName=lag_name) |
| 279 |
|
| 280 |
client.update_lag(lagId=lag_id, **params) |
| 281 |
|
| 282 |
|
| 283 |
def update_lag(client, lag_id, lag_name, min_links, num_connections, wait, wait_timeout): |
| 284 |
start = time.time() |
| 285 |
|
| 286 |
if min_links and min_links > num_connections: |
| 287 |
raise DirectConnectError( |
| 288 |
msg=f"The number of connections {num_connections} must be greater than the minimum number of links {min_links} to update the LAG {lag_id}", |
| 289 |
last_traceback=None, |
| 290 |
exception=None, |
| 291 |
) |
| 292 |
|
| 293 |
while True: |
| 294 |
try: |
| 295 |
_update_lag(client, lag_id, lag_name, min_links) |
| 296 |
except botocore.exceptions.ClientError as e: |
| 297 |
if wait and time.time() - start <= wait_timeout: |
| 298 |
continue |
| 299 |
msg = f"Failed to update Direct Connect link aggregation group {lag_id}." |
| 300 |
if "MinimumLinks cannot be set higher than the number of connections" in e.response["Error"]["Message"]: |
| 301 |
msg += f"Unable to set the min number of links to {min_links} while the LAG connections are being requested" |
| 302 |
raise DirectConnectError(msg=msg, last_traceback=traceback.format_exc(), exception=e) |
| 303 |
else: |
| 304 |
break |
| 305 |
|
| 306 |
|
| 307 |
def lag_changed(current_status, name, min_links): |
| 308 |
"""Determines if a modifiable link aggregation group attribute has been modified.""" |
| 309 |
return (name and name != current_status["lagName"]) or (min_links and min_links != current_status["minimumLinks"]) |
| 310 |
|
| 311 |
|
| 312 |
def ensure_present( |
| 313 |
client, num_connections, lag_id, lag_name, location, bandwidth, connection_id, min_links, wait, wait_timeout |
| 314 |
): |
| 315 |
exists = lag_exists(client, lag_id, lag_name) |
| 316 |
if not exists and lag_id: |
| 317 |
raise DirectConnectError( |
| 318 |
msg=f"The Direct Connect link aggregation group {lag_id} does not exist.", |
| 319 |
last_traceback=None, |
| 320 |
exception="", |
| 321 |
) |
| 322 |
|
| 323 |
# the connection is found; get the latest state and see if it needs to be updated |
| 324 |
if exists: |
| 325 |
lag_id = exists |
| 326 |
latest_state = lag_status(client, lag_id) |
| 327 |
if lag_changed(latest_state, lag_name, min_links): |
| 328 |
update_lag(client, lag_id, lag_name, min_links, num_connections, wait, wait_timeout) |
| 329 |
return True, lag_id |
| 330 |
return False, lag_id |
| 331 |
|
| 332 |
# no connection found; create a new one |
| 333 |
else: |
| 334 |
lag_id = create_lag(client, num_connections, location, bandwidth, lag_name, connection_id) |
| 335 |
update_lag(client, lag_id, lag_name, min_links, num_connections, wait, wait_timeout) |
| 336 |
return True, lag_id |
| 337 |
|
| 338 |
|
| 339 |
def describe_virtual_interfaces(client, lag_id): |
| 340 |
try: |
| 341 |
response = client.describe_virtual_interfaces(connectionId=lag_id) |
| 342 |
except botocore.exceptions.ClientError as e: |
| 343 |
raise DirectConnectError( |
| 344 |
msg=f"Failed to describe any virtual interfaces associated with LAG: {lag_id}", |
| 345 |
last_traceback=traceback.format_exc(), |
| 346 |
exception=e, |
| 347 |
) |
| 348 |
return response.get("virtualInterfaces", []) |
| 349 |
|
| 350 |
|
| 351 |
def get_connections_and_virtual_interfaces(client, lag_id): |
| 352 |
virtual_interfaces = describe_virtual_interfaces(client, lag_id) |
| 353 |
connections = lag_status(client, lag_id=lag_id).get("connections", []) |
| 354 |
return virtual_interfaces, connections |
| 355 |
|
| 356 |
|
| 357 |
def disassociate_vis(client, lag_id, virtual_interfaces): |
| 358 |
for vi in virtual_interfaces: |
| 359 |
delete_virtual_interface(client, vi["virtualInterfaceId"]) |
| 360 |
try: |
| 361 |
client.delete_virtual_interface(virtualInterfaceId=vi["virtualInterfaceId"]) |
| 362 |
except botocore.exceptions.ClientError as e: |
| 363 |
raise DirectConnectError( |
| 364 |
msg=f"Could not delete virtual interface {vi} to delete link aggregation group {lag_id}.", |
| 365 |
last_traceback=traceback.format_exc(), |
| 366 |
exception=e, |
| 367 |
) |
| 368 |
|
| 369 |
|
| 370 |
def ensure_absent(client, lag_id, lag_name, force_delete, delete_with_disassociation, wait, wait_timeout): |
| 371 |
lag_id = lag_exists(client, lag_id, lag_name) |
| 372 |
if not lag_id: |
| 373 |
return False |
| 374 |
|
| 375 |
latest_status = lag_status(client, lag_id) |
| 376 |
|
| 377 |
# determine the associated connections and virtual interfaces to disassociate |
| 378 |
virtual_interfaces, connections = get_connections_and_virtual_interfaces(client, lag_id) |
| 379 |
|
| 380 |
# If min_links is not 0, there are associated connections, or if there are virtual interfaces, ask for force_delete |
| 381 |
if any((latest_status["minimumLinks"], virtual_interfaces, connections)) and not force_delete: |
| 382 |
raise DirectConnectError( |
| 383 |
msg=( |
| 384 |
"There are a minimum number of links, hosted connections, or associated virtual interfaces for LAG" |
| 385 |
f" {lag_id}. To force deletion of the LAG use delete_force: True (if the LAG has virtual interfaces" |
| 386 |
" they will be deleted). Optionally, to ensure hosted connections are deleted after disassociation use" |
| 387 |
" delete_with_disassociation: True and wait: True (as Virtual Interfaces may take a few moments to" |
| 388 |
" delete)" |
| 389 |
), |
| 390 |
last_traceback=None, |
| 391 |
exception=None, |
| 392 |
) |
| 393 |
|
| 394 |
# update min_links to be 0 so we can remove the LAG |
| 395 |
update_lag(client, lag_id, None, 0, len(connections), wait, wait_timeout) |
| 396 |
|
| 397 |
# if virtual_interfaces and not delete_vi_with_disassociation: Raise failure; can't delete while vi attached |
| 398 |
for connection in connections: |
| 399 |
disassociate_connection_and_lag(client, connection["connectionId"], lag_id) |
| 400 |
if delete_with_disassociation: |
| 401 |
delete_connection(client, connection["connectionId"]) |
| 402 |
|
| 403 |
for vi in virtual_interfaces: |
| 404 |
delete_virtual_interface(client, vi["virtualInterfaceId"]) |
| 405 |
|
| 406 |
start_time = time.time() |
| 407 |
while True: |
| 408 |
try: |
| 409 |
delete_lag(client, lag_id) |
| 410 |
except DirectConnectError as e: |
| 411 |
if ( |
| 412 |
("until its Virtual Interfaces are deleted" in e.exception) |
| 413 |
and (time.time() - start_time < wait_timeout) |
| 414 |
and wait |
| 415 |
): |
| 416 |
continue |
| 417 |
else: |
| 418 |
return True |
| 419 |
|
| 420 |
|
| 421 |
def main(): |
| 422 |
argument_spec = dict( |
| 423 |
state=dict(required=True, choices=["present", "absent"]), |
| 424 |
name=dict(), |
| 425 |
link_aggregation_group_id=dict(), |
| 426 |
num_connections=dict(type="int"), |
| 427 |
min_links=dict(type="int"), |
| 428 |
location=dict(), |
| 429 |
bandwidth=dict(), |
| 430 |
connection_id=dict(), |
| 431 |
delete_with_disassociation=dict(type="bool", default=False), |
| 432 |
force_delete=dict(type="bool", default=False), |
| 433 |
wait=dict(type="bool", default=False), |
| 434 |
wait_timeout=dict(type="int", default=120), |
| 435 |
) |
| 436 |
|
| 437 |
module = AnsibleAWSModule( |
| 438 |
argument_spec=argument_spec, |
| 439 |
required_one_of=[("link_aggregation_group_id", "name")], |
| 440 |
required_if=[("state", "present", ("location", "bandwidth"))], |
| 441 |
) |
| 442 |
|
| 443 |
try: |
| 444 |
connection = module.client("directconnect") |
| 445 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 446 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 447 |
|
| 448 |
state = module.params.get("state") |
| 449 |
response = {} |
| 450 |
try: |
| 451 |
if state == "present": |
| 452 |
changed, lag_id = ensure_present( |
| 453 |
connection, |
| 454 |
num_connections=module.params.get("num_connections"), |
| 455 |
lag_id=module.params.get("link_aggregation_group_id"), |
| 456 |
lag_name=module.params.get("name"), |
| 457 |
location=module.params.get("location"), |
| 458 |
bandwidth=module.params.get("bandwidth"), |
| 459 |
connection_id=module.params.get("connection_id"), |
| 460 |
min_links=module.params.get("min_links"), |
| 461 |
wait=module.params.get("wait"), |
| 462 |
wait_timeout=module.params.get("wait_timeout"), |
| 463 |
) |
| 464 |
response = lag_status(connection, lag_id) |
| 465 |
elif state == "absent": |
| 466 |
changed = ensure_absent( |
| 467 |
connection, |
| 468 |
lag_id=module.params.get("link_aggregation_group_id"), |
| 469 |
lag_name=module.params.get("name"), |
| 470 |
force_delete=module.params.get("force_delete"), |
| 471 |
delete_with_disassociation=module.params.get("delete_with_disassociation"), |
| 472 |
wait=module.params.get("wait"), |
| 473 |
wait_timeout=module.params.get("wait_timeout"), |
| 474 |
) |
| 475 |
except DirectConnectError as e: |
| 476 |
if e.last_traceback: |
| 477 |
module.fail_json(msg=e.msg, exception=e.last_traceback, **camel_dict_to_snake_dict(e.exception)) |
| 478 |
else: |
| 479 |
module.fail_json(msg=e.msg) |
| 480 |
|
| 481 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(response)) |
| 482 |
|
| 483 |
|
| 484 |
if __name__ == "__main__": |
| 485 |
main() |