| 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_connection |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Creates, deletes, modifies a DirectConnect connection |
| 12 |
description: |
| 13 |
- Create, update, or delete a Direct Connect connection between a network and a specific AWS Direct Connect location. |
| 14 |
- Upon creation the connection may be added to a link aggregation group or established as a standalone connection. |
| 15 |
- The connection may later be associated or disassociated with a link aggregation group. |
| 16 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_direct_connect_connection). |
| 17 |
The usage did not change. |
| 18 |
author: |
| 19 |
- "Sloane Hertel (@s-hertel)" |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- The state of the Direct Connect connection. |
| 24 |
choices: |
| 25 |
- present |
| 26 |
- absent |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
name: |
| 30 |
description: |
| 31 |
- The name of the Direct Connect connection. This is required to create a |
| 32 |
new connection. |
| 33 |
- One of I(connection_id) or I(name) must be specified. |
| 34 |
type: str |
| 35 |
connection_id: |
| 36 |
description: |
| 37 |
- The ID of the Direct Connect connection. |
| 38 |
- Modifying attributes of a connection with I(forced_update) will result in a new Direct Connect connection ID. |
| 39 |
- One of I(connection_id) or I(name) must be specified. |
| 40 |
type: str |
| 41 |
location: |
| 42 |
description: |
| 43 |
- Where the Direct Connect connection is located. |
| 44 |
- Required when I(state=present). |
| 45 |
type: str |
| 46 |
bandwidth: |
| 47 |
description: |
| 48 |
- The bandwidth of the Direct Connect connection. |
| 49 |
- Required when I(state=present). |
| 50 |
choices: |
| 51 |
- 1Gbps |
| 52 |
- 10Gbps |
| 53 |
type: str |
| 54 |
link_aggregation_group: |
| 55 |
description: |
| 56 |
- The ID of the link aggregation group you want to associate with the connection. |
| 57 |
- This is optional when a stand-alone connection is desired. |
| 58 |
type: str |
| 59 |
forced_update: |
| 60 |
description: |
| 61 |
- To modify I(bandwidth) or I(location) the connection needs to be deleted and recreated. |
| 62 |
- By default this will not happen. This option must be explicitly set to C(true) to change I(bandwith) or I(location). |
| 63 |
type: bool |
| 64 |
default: false |
| 65 |
extends_documentation_fragment: |
| 66 |
- amazon.aws.common.modules |
| 67 |
- amazon.aws.region.modules |
| 68 |
- amazon.aws.boto3 |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
|
| 73 |
# create a Direct Connect connection |
| 74 |
- community.aws.directconnect_connection: |
| 75 |
name: ansible-test-connection |
| 76 |
state: present |
| 77 |
location: EqDC2 |
| 78 |
link_aggregation_group: dxlag-xxxxxxxx |
| 79 |
bandwidth: 1Gbps |
| 80 |
register: dc |
| 81 |
|
| 82 |
# disassociate the LAG from the connection |
| 83 |
- community.aws.directconnect_connection: |
| 84 |
state: present |
| 85 |
connection_id: dc.connection.connection_id |
| 86 |
location: EqDC2 |
| 87 |
bandwidth: 1Gbps |
| 88 |
|
| 89 |
# replace the connection with one with more bandwidth |
| 90 |
- community.aws.directconnect_connection: |
| 91 |
state: present |
| 92 |
name: ansible-test-connection |
| 93 |
location: EqDC2 |
| 94 |
bandwidth: 10Gbps |
| 95 |
forced_update: true |
| 96 |
|
| 97 |
# delete the connection |
| 98 |
- community.aws.directconnect_connection: |
| 99 |
state: absent |
| 100 |
name: ansible-test-connection |
| 101 |
""" |
| 102 |
|
| 103 |
RETURN = r""" |
| 104 |
connection: |
| 105 |
description: The attributes of the direct connect connection. |
| 106 |
type: complex |
| 107 |
returned: I(state=present) |
| 108 |
contains: |
| 109 |
aws_device: |
| 110 |
description: The endpoint which the physical connection terminates on. |
| 111 |
returned: when the requested state is no longer 'requested' |
| 112 |
type: str |
| 113 |
sample: EqDC2-12pmo7hemtz1z |
| 114 |
bandwidth: |
| 115 |
description: The bandwidth of the connection. |
| 116 |
returned: always |
| 117 |
type: str |
| 118 |
sample: 1Gbps |
| 119 |
connection_id: |
| 120 |
description: The ID of the connection. |
| 121 |
returned: always |
| 122 |
type: str |
| 123 |
sample: dxcon-ffy9ywed |
| 124 |
connection_name: |
| 125 |
description: The name of the connection. |
| 126 |
returned: always |
| 127 |
type: str |
| 128 |
sample: ansible-test-connection |
| 129 |
connection_state: |
| 130 |
description: The state of the connection. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: pending |
| 134 |
loa_issue_time: |
| 135 |
description: The issue time of the connection's Letter of Authorization - Connecting Facility Assignment. |
| 136 |
returned: when the LOA-CFA has been issued (the connection state will no longer be 'requested') |
| 137 |
type: str |
| 138 |
sample: '2018-03-20T17:36:26-04:00' |
| 139 |
location: |
| 140 |
description: The location of the connection. |
| 141 |
returned: always |
| 142 |
type: str |
| 143 |
sample: EqDC2 |
| 144 |
owner_account: |
| 145 |
description: The account that owns the direct connect connection. |
| 146 |
returned: always |
| 147 |
type: str |
| 148 |
sample: '123456789012' |
| 149 |
region: |
| 150 |
description: The region in which the connection exists. |
| 151 |
returned: always |
| 152 |
type: str |
| 153 |
sample: us-east-1 |
| 154 |
""" |
| 155 |
|
| 156 |
import traceback |
| 157 |
|
| 158 |
try: |
| 159 |
from botocore.exceptions import BotoCoreError |
| 160 |
from botocore.exceptions import ClientError |
| 161 |
except ImportError: |
| 162 |
pass # handled by imported AnsibleAWSModule |
| 163 |
|
| 164 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 165 |
|
| 166 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError |
| 167 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import associate_connection_and_lag |
| 168 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import delete_connection |
| 169 |
from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import disassociate_connection_and_lag |
| 170 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 171 |
|
| 172 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 173 |
|
| 174 |
retry_params = {"retries": 10, "delay": 5, "backoff": 1.2, "catch_extra_error_codes": ["DirectConnectClientException"]} |
| 175 |
|
| 176 |
|
| 177 |
def connection_status(client, connection_id): |
| 178 |
return connection_exists(client, connection_id=connection_id, connection_name=None, verify=False) |
| 179 |
|
| 180 |
|
| 181 |
def connection_exists(client, connection_id=None, connection_name=None, verify=True): |
| 182 |
params = {} |
| 183 |
if connection_id: |
| 184 |
params["connectionId"] = connection_id |
| 185 |
try: |
| 186 |
response = AWSRetry.jittered_backoff(**retry_params)(client.describe_connections)(**params) |
| 187 |
except (BotoCoreError, ClientError) as e: |
| 188 |
if connection_id: |
| 189 |
msg = f"Failed to describe DirectConnect ID {connection_id}" |
| 190 |
else: |
| 191 |
msg = "Failed to describe DirectConnect connections" |
| 192 |
raise DirectConnectError(msg=msg, last_traceback=traceback.format_exc(), exception=e) |
| 193 |
|
| 194 |
match = [] |
| 195 |
connection = [] |
| 196 |
|
| 197 |
# look for matching connections |
| 198 |
|
| 199 |
if len(response.get("connections", [])) == 1 and connection_id: |
| 200 |
if response["connections"][0]["connectionState"] != "deleted": |
| 201 |
match.append(response["connections"][0]["connectionId"]) |
| 202 |
connection.extend(response["connections"]) |
| 203 |
|
| 204 |
for conn in response.get("connections", []): |
| 205 |
if connection_name == conn["connectionName"] and conn["connectionState"] != "deleted": |
| 206 |
match.append(conn["connectionId"]) |
| 207 |
connection.append(conn) |
| 208 |
|
| 209 |
# verifying if the connections exists; if true, return connection identifier, otherwise return False |
| 210 |
if verify and len(match) == 1: |
| 211 |
return match[0] |
| 212 |
elif verify: |
| 213 |
return False |
| 214 |
# not verifying if the connection exists; just return current connection info |
| 215 |
elif len(connection) == 1: |
| 216 |
return {"connection": connection[0]} |
| 217 |
return {"connection": {}} |
| 218 |
|
| 219 |
|
| 220 |
def create_connection(client, location, bandwidth, name, lag_id): |
| 221 |
if not name: |
| 222 |
raise DirectConnectError(msg="Failed to create a Direct Connect connection: name required.") |
| 223 |
params = { |
| 224 |
"location": location, |
| 225 |
"bandwidth": bandwidth, |
| 226 |
"connectionName": name, |
| 227 |
} |
| 228 |
if lag_id: |
| 229 |
params["lagId"] = lag_id |
| 230 |
|
| 231 |
try: |
| 232 |
connection = AWSRetry.jittered_backoff(**retry_params)(client.create_connection)(**params) |
| 233 |
except (BotoCoreError, ClientError) as e: |
| 234 |
raise DirectConnectError( |
| 235 |
msg=f"Failed to create DirectConnect connection {name}", |
| 236 |
last_traceback=traceback.format_exc(), |
| 237 |
exception=e, |
| 238 |
) |
| 239 |
return connection["connectionId"] |
| 240 |
|
| 241 |
|
| 242 |
def changed_properties(current_status, location, bandwidth): |
| 243 |
current_bandwidth = current_status["bandwidth"] |
| 244 |
current_location = current_status["location"] |
| 245 |
|
| 246 |
return current_bandwidth != bandwidth or current_location != location |
| 247 |
|
| 248 |
|
| 249 |
@AWSRetry.jittered_backoff(**retry_params) |
| 250 |
def update_associations(client, latest_state, connection_id, lag_id): |
| 251 |
changed = False |
| 252 |
if "lagId" in latest_state and lag_id != latest_state["lagId"]: |
| 253 |
disassociate_connection_and_lag(client, connection_id, lag_id=latest_state["lagId"]) |
| 254 |
changed = True |
| 255 |
if (changed and lag_id) or (lag_id and "lagId" not in latest_state): |
| 256 |
associate_connection_and_lag(client, connection_id, lag_id) |
| 257 |
changed = True |
| 258 |
return changed |
| 259 |
|
| 260 |
|
| 261 |
def ensure_present(client, connection_id, connection_name, location, bandwidth, lag_id, forced_update): |
| 262 |
# the connection is found; get the latest state and see if it needs to be updated |
| 263 |
if connection_id: |
| 264 |
latest_state = connection_status(client, connection_id=connection_id)["connection"] |
| 265 |
if changed_properties(latest_state, location, bandwidth) and forced_update: |
| 266 |
ensure_absent(client, connection_id) |
| 267 |
return ensure_present( |
| 268 |
client=client, |
| 269 |
connection_id=None, |
| 270 |
connection_name=connection_name, |
| 271 |
location=location, |
| 272 |
bandwidth=bandwidth, |
| 273 |
lag_id=lag_id, |
| 274 |
forced_update=forced_update, |
| 275 |
) |
| 276 |
elif update_associations(client, latest_state, connection_id, lag_id): |
| 277 |
return True, connection_id |
| 278 |
|
| 279 |
# no connection found; create a new one |
| 280 |
else: |
| 281 |
return True, create_connection(client, location, bandwidth, connection_name, lag_id) |
| 282 |
|
| 283 |
return False, connection_id |
| 284 |
|
| 285 |
|
| 286 |
@AWSRetry.jittered_backoff(**retry_params) |
| 287 |
def ensure_absent(client, connection_id): |
| 288 |
changed = False |
| 289 |
if connection_id: |
| 290 |
delete_connection(client, connection_id) |
| 291 |
changed = True |
| 292 |
|
| 293 |
return changed |
| 294 |
|
| 295 |
|
| 296 |
def main(): |
| 297 |
argument_spec = dict( |
| 298 |
state=dict(required=True, choices=["present", "absent"]), |
| 299 |
name=dict(), |
| 300 |
location=dict(), |
| 301 |
bandwidth=dict(choices=["1Gbps", "10Gbps"]), |
| 302 |
link_aggregation_group=dict(), |
| 303 |
connection_id=dict(), |
| 304 |
forced_update=dict(type="bool", default=False), |
| 305 |
) |
| 306 |
|
| 307 |
module = AnsibleAWSModule( |
| 308 |
argument_spec=argument_spec, |
| 309 |
required_one_of=[("connection_id", "name")], |
| 310 |
required_if=[("state", "present", ("location", "bandwidth"))], |
| 311 |
) |
| 312 |
|
| 313 |
connection = module.client("directconnect") |
| 314 |
|
| 315 |
state = module.params.get("state") |
| 316 |
try: |
| 317 |
connection_id = connection_exists( |
| 318 |
connection, connection_id=module.params.get("connection_id"), connection_name=module.params.get("name") |
| 319 |
) |
| 320 |
if not connection_id and module.params.get("connection_id"): |
| 321 |
module.fail_json( |
| 322 |
msg=f"The Direct Connect connection {module.params['connection_id']} does not exist.", |
| 323 |
) |
| 324 |
|
| 325 |
if state == "present": |
| 326 |
changed, connection_id = ensure_present( |
| 327 |
connection, |
| 328 |
connection_id=connection_id, |
| 329 |
connection_name=module.params.get("name"), |
| 330 |
location=module.params.get("location"), |
| 331 |
bandwidth=module.params.get("bandwidth"), |
| 332 |
lag_id=module.params.get("link_aggregation_group"), |
| 333 |
forced_update=module.params.get("forced_update"), |
| 334 |
) |
| 335 |
response = connection_status(connection, connection_id) |
| 336 |
elif state == "absent": |
| 337 |
changed = ensure_absent(connection, connection_id) |
| 338 |
response = {} |
| 339 |
except DirectConnectError as e: |
| 340 |
if e.last_traceback: |
| 341 |
module.fail_json( |
| 342 |
msg=e.msg, |
| 343 |
exception=e.last_traceback, |
| 344 |
**camel_dict_to_snake_dict(e.exception.response), |
| 345 |
) |
| 346 |
else: |
| 347 |
module.fail_json(msg=e.msg) |
| 348 |
|
| 349 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(response)) |
| 350 |
|
| 351 |
|
| 352 |
if __name__ == "__main__": |
| 353 |
main() |