Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualnetworkgatewayconnection.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_virtualnetworkgatewayconnection
14
15 version_added: "2.7.0"
16
17 short_description: Manage Azure Virtual Network Gateway Connection in a resource group
18
19 description:
20 - Create, update or delete Azure Virtual Network Gateway Connection in a resource group
21
22 options:
23 resource_group:
24 description:
25 - The network gateway connection's resource group.
26 type: str
27 required: true
28 name:
29 description:
30 - The name of the network gateway connection.
31 type: str
32 required: true
33 location:
34 description:
35 - The location of the network gateway connection.
36 type: str
37 local_network_gateway2:
38 description:
39 - The reference to local network gateway resource.
40 type: str
41 virtual_network_gateway1:
42 description:
43 - The reference to virtual network gateway resource.
44 type: str
45 virtual_network_gateway2:
46 description:
47 - The reference to virtual network gateway resource.
48 type: str
49 authorization_key:
50 description:
51 - The authorizationKey.
52 type: str
53 connection_type:
54 description:
55 - Gateway connection type.
56 type: str
57 choices:
58 - IPsec
59 - Vnet2Vnet
60 - ExpressRoute
61 - VPNClient
62 connection_protocol:
63 description:
64 - Connection protocol used for this connection.
65 choices:
66 - IKEv2
67 - IKEv1
68 type: str
69 routing_weight:
70 description:
71 - The routing weight.
72 type: int
73 dpd_timeout_seconds:
74 description:
75 - The dead peer detection timeout of this connection in seconds.
76 type: int
77 shared_key:
78 description:
79 - The IPSec shared key.
80 type: str
81 enable_bgp:
82 description:
83 - EnableBgp flag.
84 type: bool
85 use_local_azure_ip_address:
86 description:
87 - Use private local Azure IP for the connection.
88 type: bool
89 use_policy_based_traffic_selectors:
90 description:
91 - Enable policy-based traffic selectors.
92 type: bool
93 express_route_gateway_bypass:
94 description:
95 - Bypass ExpressRoute Gateway for data forwarding.
96 type: bool
97 state:
98 description:
99 - Use C(present) to create or update a virtual network gateway connection.
100 - Use C(absent) to delete the virtual network gateway connection.
101 type: str
102 default: present
103 choices:
104 - absent
105 - present
106 extends_documentation_fragment:
107 - azure.azcollection.azure
108 - azure.azcollection.azure_tags
109
110 author:
111 - xuzhang3 (@xuzhang3)
112 - Fred Sun (@Fred-sun)
113 '''
114
115 EXAMPLES = '''
116 - name: Create a new virtual network gateway connection
117 azure_rm_virtualnetworkgatewayconnection:
118 resource_group: "{{ resource_group }}"
119 name: "new{{ rpfx }}"
120 virtual_network_gateway1: "{{ virtual_network_gateway1 }}"
121 virtual_network_gateway2: "{{ virtual_network_gateway2 }}"
122 local_network_gateway2: "{{ local_network_gateway2 }}"
123 authorization_key: Password@0329
124 connection_type: Vnet2Vnet
125 connection_protocol: IKEv2
126 routing_weight: 1
127 dpd_timeout_seconds: 60
128 enable_bgp: false
129 use_local_azure_ip_address: false
130 use_policy_based_traffic_selectors: false
131 express_route_gateway_bypass: false
132 tags:
133 key1: value1
134
135 - name: Delete network gateway connection
136 virtualnetworkgatewayconnection:
137 resource_group: "{{ resource_group }}"
138 name: "localgateway-name"
139 state: absent
140 '''
141
142 RETURN = '''
143 state:
144 description:
145 - Current state of the Azure Local Network Gateway resource.
146 returned: always
147 type: complex
148 contains:
149 id:
150 description:
151 - The resource ID.
152 type: str
153 returned: always
154 sample: "/subscriptions/xxx-xxx/resourceGroups/rg/providers/Microsoft.Network/connections/fredvnet"
155 authorization_key:
156 description:
157 - The authorizationKey.
158 type: str
159 returned: always
160 sample: "308201E806092A864********************78B9FADDAC2D"
161 connection_mode:
162 description:
163 - The connection mode for this connection.
164 type: str
165 returned: always
166 sample: Default
167 connection_protocol:
168 description:
169 - Connection protocol used for this connection.
170 type: str
171 returned: always
172 sample: IKEv2
173 connection_type:
174 description:
175 - Gateway connection type.
176 type: str
177 returned: always
178 sample: Vnet2Vnet
179 dpd_timeout_seconds:
180 description:
181 - The dead peer detection timeout of this connection in seconds.
182 type: int
183 returned: always
184 sample: 45
185 enable_bgp:
186 description:
187 - EnableBgp flag.
188 type: bool
189 returned: always
190 sample: false
191 etag:
192 description:
193 - A unique read-only string that changes whenever the resource is updated.
194 type: str
195 returned: always
196 sample: 58b6f0c2-7d7c-4666-a62e-6416b4e00c83
197 express_route_gateway_bypass:
198 description:
199 - Bypass ExpressRoute Gateway for data forwarding.
200 type: bool
201 returned: always
202 sample: false
203 ipsec_policies:
204 description:
205 - The IPSec Policies to be considered by this connection.
206 type: list
207 returned: always
208 sample: []
209 location:
210 description:
211 - The resource location.
212 type: str
213 returned: always
214 sample: eastus
215 name:
216 description:
217 - The resource name.
218 type: str
219 returned: always
220 sample: fredvnetconnection
221 provisioning_state:
222 description:
223 - The provisioning state of the virtual network gateway connection resource.
224 type: str
225 returned: always
226 sample: Succeeded
227 resource_group:
228 description:
229 - The resource group of the virtual network gateway connection.
230 type: str
231 returned: always
232 sample: rg
233 routing_weight:
234 description:
235 - The routing weight.
236 type: int
237 returned: always
238 sample: 0
239 shared_key:
240 description:
241 - The IPSec shared key.
242 type: str
243 returned: always
244 sample: null
245 tags:
246 description:
247 - The resource tags.
248 type: dict
249 returned: always
250 sample: {'key1': 'value1'}
251 traffic_selector_policies:
252 description:
253 - The Traffic Selector Policies to be considered by this connection.
254 type: list
255 returned: always
256 sample: []
257 use_local_azure_ip_address:
258 description:
259 - Use private local Azure IP for the connection.
260 type: bool
261 returned: always
262 sample: false
263 use_policy_based_traffic_selectors:
264 description:
265 - Enable policy-based traffic selectors.
266 type: bool
267 returned: always
268 sample: false
269 local_network_gateway2:
270 description:
271 - The reference to local network gateway resource.
272 type: complex
273 returned: when-used
274 contains:
275 id:
276 description:
277 - The ID of the local network gateway resource.
278 type: str
279 returned: always
280 sample: /subscriptions/xxx-xxx/resourceGroups/rg/providers/Microsoft.Network/localNetworkGateways/rpfx001
281 virtual_network_gateway1:
282 description:
283 - The reference to virtual network gateway resource.
284 type: complex
285 returned: always
286 contains:
287 id:
288 description:
289 - The ID of the virtul network gateway resource.
290 type: str
291 returned: always
292 sample: /subscriptions/xxx-xxx/resourceGroups/rg/providers/Microsoft.Network/virtualNetworkGateways/fredvng
293 virtual_network_gateway2:
294 description:
295 - The reference to virtual network gateway resource.
296 type: complex
297 returned: when-used
298 contains:
299 id:
300 description:
301 - The ID of the virtul network gateway resource.
302 type: str
303 returned: always
304 sample: /subscriptions/xxx-xxx/resourceGroups/rg/providers/Microsoft.Network/virtualNetworkGateways/fredvng02
305
306 '''
307
308 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
309 try:
310 from azure.core.exceptions import HttpResponseError
311 from azure.core.polling import LROPoller
312 except Exception:
313 # handled in azure_rm_common
314 pass
315
316
317 class AzureRMVirutalNetworkGatewayConnection(AzureRMModuleBase):
318 """Utility class to get Azure Kubernetes Service Credentials facts"""
319
320 def __init__(self):
321
322 self.module_arg_spec = dict(
323 resource_group=dict(type='str', required=True),
324 name=dict(type='str', required=True),
325 state=dict(type='str', default='present', choices=['present', 'absent']),
326 location=dict(type='str'),
327 virtual_network_gateway1=dict(type='str', required=False),
328 virtual_network_gateway2=dict(type='str'),
329 local_network_gateway2=dict(type='str'),
330 authorization_key=dict(type='str', no_log=True),
331 connection_type=dict(type='str', choices=["IPsec", "Vnet2Vnet", "ExpressRoute", "VPNClient"]),
332 connection_protocol=dict(type='str', choices=["IKEv2", "IKEv1"]),
333 routing_weight=dict(type='int'),
334 dpd_timeout_seconds=dict(type='int'),
335 shared_key=dict(type='str', no_log=True),
336 enable_bgp=dict(type='bool'),
337 use_local_azure_ip_address=dict(type='bool'),
338 use_policy_based_traffic_selectors=dict(type='bool'),
339 express_route_gateway_bypass=dict(type='bool'),
340 )
341
342 self.name = None
343 self.resource_group = None
344 self.location = None
345 self.state = None
346 self.virtual_network_gateway1 = None
347 self.virtual_network_gateway2 = None
348 self.local_network_gateway2 = None
349 self.authorization_key = None
350 self.connection_type = None
351 self.connection_protocol = None
352 self.routing_weight = None
353 self.dpd_timeout_seconds = None
354 self.shared_key = None
355 self.enable_bgp = None
356 self.use_local_azure_ip_address = None
357 self.use_policy_based_traffic_selectors = None
358 self.express_route_gateway_bypass = None
359
360 required_if = [('connection_type', 'IPsec', ['name', 'local_network_gateway2']),
361 ('connection_type', 'Vnet2Vnet', ['name', 'virtual_network_gateway2'])]
362
363 self.results = dict(
364 changed=False,
365 state=dict(),
366 )
367
368 super(AzureRMVirutalNetworkGatewayConnection, self).__init__(derived_arg_spec=self.module_arg_spec,
369 supports_check_mode=True,
370 supports_tags=True,
371 required_if=required_if,
372 facts_module=False)
373
374 def exec_module(self, **kwargs):
375
376 for key in list(self.module_arg_spec) + ['tags']:
377 setattr(self, key, kwargs[key])
378
379 if not self.location:
380 # Set default location
381 resource_group = self.get_resource_group(self.resource_group)
382 self.location = resource_group.location
383
384 old_response = self.get_local_network_gateway()
385 changed = False
386 update_tags = False
387 response = None
388
389 if self.state == 'present':
390 if old_response is not None:
391 if self.dpd_timeout_seconds and (self.dpd_timeout_seconds != old_response['dpd_timeout_seconds']):
392 changed = True
393 elif bool(self.use_local_azure_ip_address) != bool(old_response['use_local_azure_ip_address']):
394 changed = True
395 elif bool(self.enable_bgp) != bool(old_response['enable_bgp']):
396 changed = True
397 elif bool(self.use_policy_based_traffic_selectors) != bool(old_response['use_policy_based_traffic_selectors']):
398 changed = True
399 elif self.routing_weight and (self.routing_weight != old_response['routing_weight']):
400 changed = True
401 elif bool(self.express_route_gateway_bypass) != bool(old_response['express_route_gateway_bypass']):
402 changed = True
403 else:
404 changed = True
405
406 if changed:
407 if not self.check_mode:
408 response = self.create_or_update_network_gateway_connection()
409 if old_response is not None:
410 update_tags, new_tags = self.update_tags(old_response.get('tags'))
411 if update_tags:
412 if not self.check_mode:
413 response = self.update_vngwc_tags(new_tags)
414 changed = True
415 else:
416 if not self.check_mode:
417 if old_response is not None:
418 self.delete_local_network_gateway()
419 changed = True
420 response = None
421 else:
422 changed = True
423
424 if response is None:
425 response = old_response
426 self.results['state'] = response
427 self.results['changed'] = changed
428 return self.results
429
430 def get_local_network_gateway(self):
431 """Gets the specified network gateway connection in a resource group"""
432 response = None
433 try:
434 response = self.network_client.virtual_network_gateway_connections.get(self.resource_group, self.name)
435 except HttpResponseError as ec:
436 self.log("Gets the specified network gateway connection in a resource group Failed, Exception as {0}".format(ec))
437 return None
438 return self.format_response(response)
439
440 def create_or_update_network_gateway_connection(self):
441 """Create or Update network gateway connection"""
442 response = None
443 try:
444 body = dict(location=self.location,
445 virtual_network_gateway1=dict(id=self.virtual_network_gateway1) if self.virtual_network_gateway1 else None,
446 virtual_network_gateway2=dict(id=self.virtual_network_gateway2) if self.virtual_network_gateway2 else None,
447 local_network_gateway2=dict(id=self.local_network_gateway2) if self.local_network_gateway2 else None,
448 authorization_key=self.authorization_key,
449 connection_type=self.connection_type,
450 connection_protocol=self.connection_protocol,
451 routing_weight=self.routing_weight,
452 dpd_timeout_seconds=self.dpd_timeout_seconds,
453 shared_key=self.shared_key,
454 enable_bgp=self.enable_bgp,
455 use_local_azure_ip_address=self.use_local_azure_ip_address,
456 use_policy_based_traffic_selector=self.use_policy_based_traffic_selectors,
457 express_route_gateway_bypass=self.express_route_gateway_bypass,
458 tags=self.tags)
459 except Exception as ec:
460 pass
461 try:
462 response = self.network_client.virtual_network_gateway_connections.begin_create_or_update(self.resource_group, self.name, body)
463 if isinstance(response, LROPoller):
464 response = self.get_poller_result(response)
465 except HttpResponseError as ec:
466 self.fail("Create or Updated a network gateway connection in a resource group Failed, Exception as {0}".format(ec))
467
468 return self.format_response(response)
469
470 def update_vngwc_tags(self, tags):
471 """Updates a network gateway connection tags"""
472 response = None
473 try:
474 response = self.network_client.virtual_network_gateway_connections.begin_update_tags(self.resource_group, self.name, dict(tags=tags))
475 if isinstance(response, LROPoller):
476 response = self.get_poller_result(response)
477 except HttpResponseError as ec:
478 self.fail("Update a network gateway connection tags Failed, Exception as {0}".format(ec))
479 return self.format_response(response)
480
481 def delete_local_network_gateway(self):
482 """Deletes the specified network gateway connection"""
483 try:
484 poller = self.network_client.virtual_network_gateway_connections.begin_delete(self.resource_group, self.name)
485 self.get_poller_result(poller)
486 except HttpResponseError as ec:
487 self.fail("Deletes the specified network gateway connection Failed, Exception as {0}".format(ec))
488 return None
489
490 def format_response(self, vngwconn):
491 result = dict(
492 resource_group=self.resource_group,
493 authorization_key=vngwconn.authorization_key,
494 connection_mode=vngwconn.connection_mode,
495 connection_protocol=vngwconn.connection_protocol,
496 connection_type=vngwconn.connection_type,
497 dpd_timeout_seconds=vngwconn.dpd_timeout_seconds,
498 etag=vngwconn.etag,
499 enable_bgp=vngwconn.enable_bgp,
500 express_route_gateway_bypass=vngwconn.express_route_gateway_bypass,
501 id=vngwconn.id,
502 ipsec_policies=vngwconn.ipsec_policies,
503 location=vngwconn.location,
504 name=vngwconn.name,
505 provisioning_state=vngwconn.provisioning_state,
506 routing_weight=vngwconn.routing_weight,
507 tags=vngwconn.tags,
508 traffic_selector_policies=vngwconn.traffic_selector_policies,
509 shared_key=vngwconn.shared_key,
510 use_local_azure_ip_address=vngwconn.use_local_azure_ip_address,
511 use_policy_based_traffic_selectors=vngwconn.use_policy_based_traffic_selectors,
512 virtual_network_gateway2=None,
513 local_network_gateway2=None,
514 virtual_network_gateway1=None,
515 )
516 if vngwconn.local_network_gateway2:
517 result['local_network_gateway2'] = dict(id=vngwconn.local_network_gateway2.id)
518 if vngwconn.virtual_network_gateway2:
519 result['virtual_network_gateway2'] = dict(id=vngwconn.virtual_network_gateway2.id)
520 if vngwconn.virtual_network_gateway1:
521 result['virtual_network_gateway1'] = dict(id=vngwconn.virtual_network_gateway1.id)
522 return result
523
524
525 def main():
526 """Main module execution code path"""
527
528 AzureRMVirutalNetworkGatewayConnection()
529
530
531 if __name__ == '__main__':
532 main()