Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 Gu 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_vpnsite
14 version_added: '1.5.0'
15 short_description: Manage Azure VpnSite instance
16 description:
17 - Create, update and delete instance of Azure VpnSite.
18 options:
19 resource_group:
20 description:
21 - The resource group name of the VpnSite.
22 required: true
23 type: str
24 location:
25 description:
26 - The location of the VpnSite
27 type: str
28 name:
29 description:
30 - The name of the VpnSite.
31 required: true
32 type: str
33 virtual_wan:
34 description:
35 - The VirtualWAN to which the vpnSite belongs.
36 type: dict
37 suboptions:
38 id:
39 description:
40 - The resource ID of the related virtual wan.
41 type: str
42 device_properties:
43 description:
44 - The device properties.
45 type: dict
46 suboptions:
47 device_vendor:
48 description:
49 - Name of the device Vendor.
50 type: str
51 device_model:
52 description:
53 - Model of the device.
54 type: str
55 link_speed_in_mbps:
56 description:
57 - Link speed.
58 type: int
59 ip_address:
60 description:
61 - The ip-address for the vpn-site.
62 type: str
63 site_key:
64 description:
65 - The key for vpn-site that can be used for connections.
66 type: str
67 address_space:
68 description:
69 - The AddressSpace that contains an array of IP address ranges.
70 type: dict
71 suboptions:
72 address_prefixes:
73 description:
74 - A list of address blocks reserved for this virtual network in CIDR notation.
75 type: list
76 elements: str
77 bgp_properties:
78 description:
79 - The set of bgp properties.
80 type: dict
81 suboptions:
82 asn:
83 description:
84 - The BGP speaker's ASN.
85 type: int
86 bgp_peering_address:
87 description:
88 - The BGP peering address and BGP identifier of this BGP speaker.
89 type: str
90 peer_weight:
91 description:
92 - The weight added to routes learned from this BGP speaker.
93 type: int
94 bgp_peering_addresses:
95 description:
96 - BGP peering address with IP configuration ID for virtual network gateway.
97 type: list
98 elements: dict
99 suboptions:
100 ipconfiguration_id:
101 description:
102 - The ID of IP configuration which belongs to gateway.
103 type: str
104 default_bgp_ip_addresses:
105 description:
106 - The list of default BGP peering addresses which belong to IP configuration.
107 type: list
108 elements: str
109 custom_bgp_ip_addresses:
110 description:
111 - The list of custom BGP peering addresses which belong to IP configuration.
112 type: list
113 elements: str
114 tunnel_ip_addresses:
115 description:
116 - The list of tunnel public IP addresses which belong to IP configuration.
117 type: list
118 elements: str
119 is_security_site:
120 description:
121 - IsSecuritySite flag.
122 type: bool
123 vpn_site_links:
124 description:
125 - List of all vpn site links.
126 type: list
127 elements: dict
128 suboptions:
129 name:
130 description:
131 - The name of the resource that is unique within a resource group.
132 - This name can be used to access the resource.
133 type: str
134 link_properties:
135 description:
136 - The link provider properties.
137 type: dict
138 suboptions:
139 link_provider_name:
140 description:
141 - Name of the link provider.
142 type: str
143 link_speed_in_mbps:
144 description:
145 - Link speed.
146 type: int
147 ip_address:
148 description:
149 - The IP address for the vpn site link.
150 type: str
151 fqdn:
152 description:
153 - FQDN of vpn-site-link.
154 type: str
155 bgp_properties:
156 description:
157 - The set of bgp properties.
158 type: dict
159 suboptions:
160 asn:
161 description:
162 - The BGP speaker's ASN.
163 type: int
164 bgp_peering_address:
165 description:
166 - The BGP peering address and BGP identifier of this BGP speaker.
167 type: str
168 o365_policy:
169 description:
170 - Office365 Policy.
171 type: dict
172 suboptions:
173 break_out_categories:
174 description:
175 - Office365 breakout categories.
176 type: dict
177 suboptions:
178 allow:
179 description:
180 - Flag to control allow category.
181 type: bool
182 optimize:
183 description:
184 - Flag to control optimize category.
185 type: bool
186 default:
187 description:
188 - Flag to control default category.
189 type: bool
190 state:
191 description:
192 - Assert the state of the VpnSite.
193 - Use C(present) to create or update an VpnSite and C(absent) to delete it.
194 default: present
195 type: str
196 choices:
197 - absent
198 - present
199 extends_documentation_fragment:
200 - azure.azcollection.azure
201 - azure.azcollection.azure_tags
202 author:
203 - Fred-Sun (@Fred-Sun)
204
205 '''
206
207 EXAMPLES = '''
208 - name: Create VpnSite
209 azure_rm_vpnsite:
210 resource_group: myResourceGroup
211 name: vpnSite_name
212
213 - name: Delete Vpn Site
214 azure_rm_vpnsite:
215 resource_group: myResourceGroup
216 name: vpnSite_name
217 '''
218
219 RETURN = '''
220 state:
221 description:
222 - Current state of the vpn site.
223 type: complex
224 returned: success
225 contains:
226 id:
227 description:
228 - Resource ID.
229 returned: always
230 type: str
231 sample: /subscriptions/xxx-xxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/vpnSites/vpn_site_name
232 name:
233 description:
234 - Resource name.
235 returned: always
236 type: str
237 sample: vpn_site_name
238 type:
239 description:
240 - Resource type.
241 returned: always
242 type: str
243 sample: Microsoft.Network/vpnSites
244 location:
245 description:
246 - Resource location.
247 returned: always
248 type: str
249 sample: eastus
250 tags:
251 description:
252 - Resource tags.
253 returned: always
254 type: dict
255 sample: { 'key1': 'value1'}
256 etag:
257 description:
258 - A unique read-only string that changes whenever the resource is updated.
259 returned: always
260 type: str
261 sample: 8d7415fe-d92c-4331-92ea-460aadfb9648
262 virtual_wan:
263 description:
264 - The VirtualWAN to which the vpnSite belongs.
265 returned: always
266 type: complex
267 contains:
268 id:
269 description:
270 - Resource ID.
271 returned: always
272 type: str
273 sample: /subscriptions/xxx-xxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/virtualWans/virtualwan_name
274 device_properties:
275 description:
276 - The device properties.
277 returned: always
278 type: complex
279 contains:
280 device_vendor:
281 description:
282 - Name of the device Vendor.
283 returned: always
284 type: str
285 sample: {"link_speed_in_mbps": 0}
286 provisioning_state:
287 description:
288 - The provisioning state of the VPN site resource.
289 returned: always
290 type: str
291 sample: "Succeeded"
292 is_security_site:
293 description:
294 - IsSecuritySite flag.
295 returned: always
296 type: bool
297 sample: false
298 '''
299
300 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
301 try:
302 from azure.core.exceptions import ResourceNotFoundError
303 from azure.core.polling import LROPoller
304 except ImportError:
305 # This is handled in azure_rm_common
306 pass
307
308
309 class Actions:
310 NoAction, Create, Update, Delete, Update_tags = range(5)
311
312
313 class AzureRMVpnSite(AzureRMModuleBaseExt):
314 def __init__(self):
315 self.module_arg_spec = dict(
316 resource_group=dict(
317 type='str',
318 required=True
319 ),
320 name=dict(
321 type='str',
322 required=True
323 ),
324 location=dict(
325 type='str'
326 ),
327 virtual_wan=dict(
328 type='dict',
329 options=dict(
330 id=dict(
331 type='str',
332 )
333 )
334 ),
335 device_properties=dict(
336 type='dict',
337 options=dict(
338 device_vendor=dict(
339 type='str',
340 ),
341 device_model=dict(
342 type='str',
343 ),
344 link_speed_in_mbps=dict(
345 type='int',
346 )
347 )
348 ),
349 ip_address=dict(
350 type='str',
351 ),
352 site_key=dict(
353 type='str',
354 no_log=True,
355 ),
356 address_space=dict(
357 type='dict',
358 options=dict(
359 address_prefixes=dict(
360 type='list',
361 elements='str'
362 )
363 )
364 ),
365 bgp_properties=dict(
366 type='dict',
367 options=dict(
368 asn=dict(
369 type='int',
370 ),
371 bgp_peering_address=dict(
372 type='str',
373 ),
374 peer_weight=dict(
375 type='int',
376 ),
377 bgp_peering_addresses=dict(
378 type='list',
379 elements='dict',
380 options=dict(
381 ipconfiguration_id=dict(
382 type='str',
383 ),
384 default_bgp_ip_addresses=dict(
385 type='list',
386 elements='str'
387 ),
388 custom_bgp_ip_addresses=dict(
389 type='list',
390 elements='str'
391 ),
392 tunnel_ip_addresses=dict(
393 type='list',
394 elements='str'
395 )
396 )
397 )
398 )
399 ),
400 is_security_site=dict(
401 type='bool',
402 ),
403 vpn_site_links=dict(
404 type='list',
405 elements='dict',
406 options=dict(
407 name=dict(
408 type='str',
409 ),
410 link_properties=dict(
411 type='dict',
412 options=dict(
413 link_provider_name=dict(
414 type='str',
415 ),
416 link_speed_in_mbps=dict(
417 type='int',
418 )
419 )
420 ),
421 ip_address=dict(
422 type='str',
423 ),
424 fqdn=dict(
425 type='str',
426 ),
427 bgp_properties=dict(
428 type='dict',
429 options=dict(
430 asn=dict(
431 type='int',
432 ),
433 bgp_peering_address=dict(
434 type='str',
435 )
436 )
437 )
438 )
439 ),
440 o365_policy=dict(
441 type='dict',
442 options=dict(
443 break_out_categories=dict(
444 type='dict',
445 options=dict(
446 allow=dict(
447 type='bool',
448 ),
449 optimize=dict(
450 type='bool',
451 ),
452 default=dict(
453 type='bool',
454 )
455 )
456 )
457 )
458 ),
459 state=dict(
460 type='str',
461 default='present',
462 choices=['present', 'absent']
463 )
464 )
465
466 self.resource_group = None
467 self.name = None
468 self.location = None
469 self.body = {}
470
471 self.results = dict(changed=False)
472 self.state = None
473 self.to_do = Actions.NoAction
474
475 super(AzureRMVpnSite, self).__init__(derived_arg_spec=self.module_arg_spec,
476 supports_check_mode=True,
477 supports_tags=True)
478
479 def exec_module(self, **kwargs):
480 for key in list(self.module_arg_spec.keys()) + ['tags']:
481 if hasattr(self, key):
482 setattr(self, key, kwargs[key])
483 elif kwargs[key] is not None:
484 self.body[key] = kwargs[key]
485
486 self.inflate_parameters(self.module_arg_spec, self.body, 0)
487
488 resource_group = self.get_resource_group(self.resource_group)
489 if self.location is None:
490 # Set default location
491 self.location = resource_group.location
492 self.body['location'] = self.location
493
494 old_response = None
495 response = None
496
497 old_response = self.get_resource()
498
499 if not old_response:
500 if self.state == 'present':
501 self.to_do = Actions.Create
502 else:
503 if self.state == 'absent':
504 self.to_do = Actions.Delete
505 else:
506 if self.body.get('virtual_wan') is not None and self.body['virtual_wan'] != old_response.get('virtual_wan'):
507 self.to_do = Actions.Update
508 for key in self.body.keys():
509 if key == 'address_space':
510 if old_response.get('address_space') is None or\
511 len(self.body['address_space']['address_prefixes']) > len(old_response['address_space']['address_prefixes']) or\
512 not all(key in old_response['address_space']['address_prefixes'] for key in self.body['address_space']['address_prefixes']):
513 self.to_do = Actions.Update
514 elif key == 'device_properties':
515 if old_response.get('device_properties') is None or\
516 not all(self.body['device_properties'][key] == old_response['device_properties'].get(key)
517 for key in self.body['device_properties'].keys()):
518 self.to_do = Actions.Update
519 elif key == 'o365_policy':
520 if old_response.get('o365_policy') is None or\
521 not all(self.body['o365_policy']['break_out_categories'][key] == old_response['o365_policy']['break_out_categories'].get(key)
522 for key in self.body['o365_policy']['break_out_categories'].keys()):
523 self.to_do = Actions.Update
524 elif key == 'vpn_site_links':
525 if old_response.get('vpn_site_links') is None or\
526 not all(self.body['vpn_site_links'][key] == old_response['vpn_site_links'].get(key)
527 for key in self.body['vpn_site_links'].keys()):
528 self.to_do = Actions.Update
529 elif key == 'bgp_properties':
530 if old_response.get('bgp_properties') is None:
531 self.to_do = Actions.Update
532 else:
533 for item in self.body['bgp_properties'].keys():
534 if item != 'bgp_peering_addresses' and item != 'peer_weight':
535 if self.body['bgp_properties'][item] != old_response['bgp_properties'].get(item):
536 self.to_do = Actions.Update
537 else:
538 if self.body['bgp_properties'].get('bgp_peering_addresses') is not None:
539 bgp_address = old_response['bgp_properties']['bgp_peering_addresses']
540 if old_response['bgp_properties'].get('bgp_peering_addresses') is None or\
541 not all(self.body['bgp_properties']['bgp_peering_addresses'][value] == bgp_address.get(value)
542 for value in ['ipconfiguration_id', 'custom_bgp_ip_addresses']):
543 self.to_do = Actions.Update
544
545 elif self.body[key] != old_response.get(key):
546 self.to_do = Actions.Update
547
548 update_tags, self.tags = self.update_tags(old_response.get('tags'))
549 if update_tags:
550 self.to_do = Actions.Update_tags
551
552 if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
553 self.results['changed'] = True
554 if self.check_mode:
555 return self.results
556 response = self.create_update_resource()
557 elif self.to_do == Actions.Update_tags:
558 self.results['changed'] = True
559 if self.check_mode:
560 return self.results
561 response = self.update_resource_tags(dict(tags=self.tags))
562 elif self.to_do == Actions.Delete:
563 self.results['changed'] = True
564 if self.check_mode:
565 return self.results
566 self.delete_resource()
567 else:
568 self.results['changed'] = False
569 response = old_response
570
571 if response is not None:
572 self.results['state'] = response
573 return self.results
574
575 def update_resource_tags(self, tags_parameters):
576 try:
577 response = self.network_client.vpn_sites.update_tags(resource_group_name=self.resource_group,
578 vpn_site_name=self.name,
579 vpn_site_parameters=tags_parameters)
580 if isinstance(response, LROPoller):
581 response = self.get_poller_result(response)
582 except Exception as exc:
583 self.log('Error attempting to update the VpnSite instance tags.')
584 self.fail('Error updating the VpnSite instance: {0}'.format(str(exc)))
585 return response.as_dict()
586
587 def create_update_resource(self):
588 try:
589 response = self.network_client.vpn_sites.begin_create_or_update(resource_group_name=self.resource_group,
590 vpn_site_name=self.name,
591 vpn_site_parameters=self.body)
592 if isinstance(response, LROPoller):
593 response = self.get_poller_result(response)
594 except Exception as exc:
595 self.log('Error attempting to create the VpnSite instance.')
596 self.fail('Error creating the VpnSite instance: {0}'.format(str(exc)))
597 return response.as_dict()
598
599 def delete_resource(self):
600 try:
601 response = self.network_client.vpn_sites.begin_delete(resource_group_name=self.resource_group,
602 vpn_site_name=self.name)
603 except Exception as e:
604 self.log('Error attempting to delete the VpnSite instance.')
605 self.fail('Error deleting the VpnSite instance: {0}'.format(str(e)))
606
607 return True
608
609 def get_resource(self):
610 try:
611 response = self.network_client.vpn_sites.get(resource_group_name=self.resource_group,
612 vpn_site_name=self.name)
613 except ResourceNotFoundError as e:
614 return False
615 return response.as_dict()
616
617
618 def main():
619 AzureRMVpnSite()
620
621
622 if __name__ == '__main__':
623 main()