Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/ec2_transit_gateway_info.py

raw
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_info
9 short_description: Retrieve information about EC2 Transit Gateways in AWS
10 version_added: 1.0.0
11 version_added_collection: community.aws
12 description:
13 - Gather information about EC2 Transit Gateways in AWS.
14 author:
15 - "Bob Boldin (@BobBoldin)"
16 options:
17 transit_gateway_ids:
18 description:
19 - A list of Transit Gateway IDs for which to gather information.
20 aliases: [transit_gateway_id]
21 type: list
22 elements: str
23 default: []
24 filters:
25 description:
26 - A dictionary of filters to apply to the query. Each key-value pair represents a filter key and its corresponding value.
27 - For a complete list of available filters,
28 refer to the AWS documentation U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeTransitGateways.html).
29 type: dict
30 default: {}
31 extends_documentation_fragment:
32 - amazon.aws.common.modules
33 - amazon.aws.region.modules
34 - amazon.aws.boto3
35 """
36
37 EXAMPLES = r"""
38 # Note: These examples do not set authentication details, see the AWS Guide for details.
39
40 - name: Gather info about all transit gateways
41 amazon.aws.ec2_transit_gateway_info:
42
43 - name: Gather info about a particular transit gateway using filter transit gateway ID
44 amazon.aws.ec2_transit_gateway_info:
45 filters:
46 transit-gateway-id: tgw-02c42332e6b7da829
47
48 - name: Gather info about a particular transit gateway using multiple option filters
49 amazon.aws.ec2_transit_gateway_info:
50 filters:
51 options.dns-support: enable
52 options.vpn-ecmp-support: enable
53
54 - name: Gather info about multiple transit gateways using module param
55 amazon.aws.ec2_transit_gateway_info:
56 transit_gateway_ids:
57 - tgw-02c42332e6b7da829
58 - tgw-03c53443d5a8cb716
59 """
60
61 RETURN = r"""
62 transit_gateways:
63 description:
64 - Transit gateways that match the provided filters.
65 - Each element consists of a dict with all the information related to that transit gateway.
66 returned: on success
67 type: list
68 elements: dict
69 contains:
70 creation_time:
71 description: The creation time.
72 returned: always
73 type: str
74 sample: "2019-02-05T16:19:58+00:00"
75 description:
76 description: The description of the transit gateway.
77 returned: always
78 type: str
79 sample: "A transit gateway"
80 options:
81 description: A dictionary of the transit gateway options.
82 returned: always
83 type: dict
84 contains:
85 amazon_side_asn:
86 description:
87 - A private Autonomous System Number (ASN) for the Amazon ide of a BGP session.
88 - The range is 64512 to 65534 for 16-bit ASNs and 4200000000 to 4294967294 for 32-bit ASNs.
89 returned: always
90 type: int
91 sample: 64512
92 auto_accept_shared_attachments:
93 description: Indicates whether attachment requests are automatically accepted.
94 returned: always
95 type: str
96 sample: "enable"
97 default_route_table_association:
98 description: Indicates whether resource attachments are automatically associated with the default association route table.
99 returned: always
100 type: str
101 sample: "disable"
102 association_default_route_table_id:
103 description: The ID of the default association route table.
104 returned: when present
105 type: str
106 sample: "tgw-rtb-0fd332c911223344"
107 default_route_table_propagation:
108 description: Indicates whether resource attachments automatically propagate routes to the default propagation route table.
109 returned: always
110 type: str
111 sample: "disable"
112 dns_support:
113 description: Indicates whether DNS support is enabled.
114 returned: always
115 type: str
116 sample: "enable"
117 multicast_support:
118 description: Indicates whether Multicast support is enabled.
119 returned: always
120 type: str
121 sample: "enable"
122 version_added: 7.3.0
123 propagation_default_route_table_id:
124 description: The ID of the default propagation route table.
125 returned: when present
126 type: str
127 sample: "rtb-11223344"
128 vpn_ecmp_support:
129 description: Indicates whether Equal Cost Multipath Protocol support is enabled.
130 returned: always
131 type: str
132 sample: "enable"
133 owner_id:
134 description: The AWS account number ID which owns the transit gateway.
135 returned: always
136 type: str
137 sample: "123456789012"
138 state:
139 description: The state of the transit gateway.
140 returned: always
141 type: str
142 sample: "available"
143 tags:
144 description: A dict of tags associated with the transit gateway.
145 returned: always
146 type: dict
147 sample: {
148 "Name": "A sample TGW",
149 "Env": "Dev"
150 }
151 transit_gateway_arn:
152 description: The Amazon Resource Name (ARN) of the transit gateway.
153 returned: always
154 type: str
155 sample: "arn:aws:ec2:us-west-2:123456789012:transit-gateway/tgw-02c42332e6b7da829"
156 transit_gateway_id:
157 description: The ID of the transit gateway.
158 returned: always
159 type: str
160 sample: "tgw-02c42332e6b7da829"
161 """
162
163 from typing import Any
164 from typing import Dict
165 from typing import List
166
167 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
168
169 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error
170 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_transit_gateways
171 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
172 from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
173 from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list
174
175
176 def get_transit_gateway_response(module: AnsibleAWSModule, connection) -> Dict[str, Any]:
177 """
178 Get transit gateway response from AWS.
179
180 module : AnsibleAWSModule object
181 connection : boto3 client connection object
182 :return: Response from describe_transit_gateways call
183 """
184 filters = ansible_dict_to_boto3_filter_list(module.params["filters"])
185 transit_gateway_ids = module.params["transit_gateway_ids"]
186
187 params = {}
188 if transit_gateway_ids:
189 params["TransitGatewayIds"] = transit_gateway_ids
190 if filters:
191 params["Filters"] = filters
192
193 result = describe_ec2_transit_gateways(connection, **params)
194 return result
195
196
197 def extract_transit_gateway_info(transit_gateway: Dict[str, Any]) -> Dict[str, Any]:
198 """
199 Extract and transform transit gateway information.
200
201 transit_gateway : The transit gateway data from AWS
202 :return: Transformed transit gateway information
203 """
204 tgw_data = camel_dict_to_snake_dict(transit_gateway, ignore_list=["Tags"])
205 tgw_data["tags"] = boto3_tag_list_to_ansible_dict(transit_gateway.get("Tags", []))
206 return tgw_data
207
208
209 def describe_transit_gateways(module: AnsibleAWSModule, connection) -> List[Dict[str, Any]]:
210 """
211 Describe transit gateways.
212
213 module : AnsibleAWSModule object
214 connection : boto3 client connection object
215 :return: List of transit gateways
216 """
217 response = get_transit_gateway_response(module, connection)
218 return [extract_transit_gateway_info(tgw) for tgw in response]
219
220
221 def setup_module_object() -> AnsibleAWSModule:
222 """
223 Merge argument spec and create Ansible module object.
224 :return: Ansible module object
225 """
226 argument_spec = dict(
227 transit_gateway_ids=dict(type="list", default=[], elements="str", aliases=["transit_gateway_id"]),
228 filters=dict(type="dict", default={}),
229 )
230
231 module = AnsibleAWSModule(
232 argument_spec=argument_spec,
233 supports_check_mode=True,
234 )
235
236 return module
237
238
239 def main():
240 module = setup_module_object()
241 results = {"changed": False}
242
243 connection = module.client("ec2")
244 try:
245 transit_gateways = describe_transit_gateways(module, connection)
246 results["transit_gateways"] = transit_gateways
247 except AnsibleEC2Error as e:
248 module.fail_json_aws(e)
249
250 module.exit_json(**results)
251
252
253 if __name__ == "__main__":
254 main()