Debian Codegraph @

ansible_collections/community/aws/plugins/modules/directconnect_confirm_connection.py

raw
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_confirm_connection
10 short_description: Confirms the creation of a hosted DirectConnect connection
11 description:
12 - Confirms the creation of a hosted DirectConnect, which requires approval before it can be used.
13 - DirectConnect connections that require approval would be in the C(ordering).
14 - After confirmation, they will move to the C(pending) state and finally the C(available) state.
15 - Prior to release 5.0.0 this module was called C(community.aws.aws_direct_connect_confirm_connection).
16 The usage did not change.
17 author:
18 - "Matt Traynham (@mtraynham)"
19 options:
20 name:
21 description:
22 - The name of the Direct Connect connection.
23 - One of I(connection_id) or I(name) must be specified.
24 type: str
25 connection_id:
26 description:
27 - The ID of the Direct Connect connection.
28 - One of I(connection_id) or I(name) must be specified.
29 type: str
30 extends_documentation_fragment:
31 - amazon.aws.common.modules
32 - amazon.aws.region.modules
33 - amazon.aws.boto3
34 """
35
36 EXAMPLES = r"""
37
38 # confirm a Direct Connect by name
39 - name: confirm the connection id
40 community.aws.directconnect_confirm_connection:
41 name: my_host_direct_connect
42
43 # confirm a Direct Connect by connection_id
44 - name: confirm the connection id
45 community.aws.directconnect_confirm_connection:
46 connection_id: dxcon-xxxxxxxx
47 """
48
49 RETURN = r"""
50
51 connection_state:
52 description: The state of the connection.
53 returned: always
54 type: str
55 sample: pending
56 """
57
58 import traceback
59
60 try:
61 from botocore.exceptions import BotoCoreError
62 from botocore.exceptions import ClientError
63 except ImportError:
64 pass # handled by imported AnsibleAWSModule
65
66 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
67
68 from ansible_collections.amazon.aws.plugins.module_utils.direct_connect import DirectConnectError
69 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
70
71 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
72
73 retry_params = {"retries": 10, "delay": 5, "backoff": 1.2, "catch_extra_error_codes": ["DirectConnectClientException"]}
74
75
76 @AWSRetry.jittered_backoff(**retry_params)
77 def describe_connections(client, params):
78 return client.describe_connections(**params)
79
80
81 def find_connection_id(client, connection_id=None, connection_name=None):
82 params = {}
83 if connection_id:
84 params["connectionId"] = connection_id
85 try:
86 response = describe_connections(client, params)
87 except (BotoCoreError, ClientError) as e:
88 if connection_id:
89 msg = f"Failed to describe DirectConnect ID {connection_id}"
90 else:
91 msg = "Failed to describe DirectConnect connections"
92 raise DirectConnectError(
93 msg=msg,
94 last_traceback=traceback.format_exc(),
95 exception=e,
96 )
97
98 match = []
99 if len(response.get("connections", [])) == 1 and connection_id:
100 if response["connections"][0]["connectionState"] != "deleted":
101 match.append(response["connections"][0]["connectionId"])
102
103 for conn in response.get("connections", []):
104 if connection_name == conn["connectionName"] and conn["connectionState"] != "deleted":
105 match.append(conn["connectionId"])
106
107 if len(match) == 1:
108 return match[0]
109 else:
110 raise DirectConnectError(msg="Could not find a valid DirectConnect connection")
111
112
113 def get_connection_state(client, connection_id):
114 try:
115 response = describe_connections(client, dict(connectionId=connection_id))
116 return response["connections"][0]["connectionState"]
117 except (BotoCoreError, ClientError, IndexError) as e:
118 raise DirectConnectError(
119 msg=f"Failed to describe DirectConnect connection {connection_id} state",
120 last_traceback=traceback.format_exc(),
121 exception=e,
122 )
123
124
125 def main():
126 argument_spec = dict(connection_id=dict(), name=dict())
127 module = AnsibleAWSModule(
128 argument_spec=argument_spec,
129 mutually_exclusive=[["connection_id", "name"]],
130 required_one_of=[["connection_id", "name"]],
131 )
132 client = module.client("directconnect")
133
134 connection_id = module.params["connection_id"]
135 connection_name = module.params["name"]
136
137 changed = False
138 connection_state = None
139 try:
140 connection_id = find_connection_id(client, connection_id, connection_name)
141 connection_state = get_connection_state(client, connection_id)
142 if connection_state == "ordering":
143 client.confirm_connection(connectionId=connection_id)
144 changed = True
145 connection_state = get_connection_state(client, connection_id)
146 except DirectConnectError as e:
147 if e.last_traceback:
148 module.fail_json(msg=e.msg, exception=e.last_traceback, **camel_dict_to_snake_dict(e.exception.response))
149 else:
150 module.fail_json(msg=e.msg)
151
152 module.exit_json(changed=changed, connection_state=connection_state)
153
154
155 if __name__ == "__main__":
156 main()