Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 XiuxiSun, (@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_virtualhub
14 version_added: '1.10.0'
15 short_description: Manage Azure VirtualHub instance
16 description:
17 - Create, update and delete instance of Azure VirtualHub.
18 options:
19 resource_group:
20 description:
21 - The resource group name of the VirtualHub.
22 required: true
23 type: str
24 location:
25 description:
26 - The location of the VirtualHub.
27 type: str
28 name:
29 description:
30 - The name of the VirtualHub.
31 required: true
32 type: str
33 virtual_wan:
34 description:
35 - The VirtualWAN to which the VirtualHub belongs.
36 type: dict
37 suboptions:
38 id:
39 description:
40 - Resource ID.
41 type: str
42 vpn_gateway:
43 description:
44 - The VpnGateway associated with this VirtualHub.
45 type: dict
46 suboptions:
47 id:
48 description:
49 - Resource ID.
50 type: str
51 p2_s_vpn_gateway:
52 description:
53 - The P2SVpnGateway associated with this VirtualHub.
54 type: dict
55 suboptions:
56 id:
57 description:
58 - Resource ID.
59 type: str
60 express_route_gateway:
61 description:
62 - The expressRouteGateway associated with this VirtualHub.
63 type: dict
64 suboptions:
65 id:
66 description:
67 - Resource ID.
68 type: str
69 azure_firewall:
70 description:
71 - The azureFirewall associated with this VirtualHub.
72 type: dict
73 suboptions:
74 id:
75 description:
76 - Resource ID.
77 type: str
78 security_partner_provider:
79 description:
80 - The securityPartnerProvider associated with this VirtualHub.
81 type: dict
82 suboptions:
83 id:
84 description:
85 - Resource ID.
86 type: str
87 address_prefix:
88 description:
89 - Address-prefix for this VirtualHub.
90 type: str
91 route_table:
92 description:
93 - The routeTable associated with this virtual hub.
94 type: dict
95 suboptions:
96 routes:
97 description:
98 - List of all routes.
99 elements: dict
100 type: list
101 suboptions:
102 address_prefixes:
103 description:
104 - List of all addressPrefixes.
105 type: list
106 elements: str
107 next_hop_ip_address:
108 description:
109 - NextHop ip address.
110 type: str
111 security_provider_name:
112 description:
113 - The Security Provider name.
114 type: str
115 virtual_hub_route_table_v2_s:
116 description:
117 - List of all virtual hub route table v2s associated with this VirtualHub.
118 type: list
119 elements: dict
120 suboptions:
121 name:
122 description:
123 - The name of the resource that is unique within a resource group.
124 - This name can be used to access the resource.
125 type: str
126 routes:
127 description:
128 - List of all routes.
129 type: list
130 elements: dict
131 suboptions:
132 destination_type:
133 description:
134 - The type of destinations.
135 type: str
136 destinations:
137 description:
138 - List of all destinations.
139 type: list
140 elements: str
141 next_hop_type:
142 description:
143 - The type of next hops.
144 type: str
145 next_hops:
146 description:
147 - NextHops ip address.
148 type: list
149 elements: str
150 attached_connections:
151 description:
152 - List of all connections attached to this route table v2.
153 elements: str
154 type: list
155 sku:
156 description:
157 - The sku of this VirtualHub.
158 type: str
159 bgp_connections:
160 description:
161 - List of references to Bgp Connections.
162 type: list
163 elements: dict
164 suboptions:
165 id:
166 description:
167 - Resource ID.
168 type: str
169 ip_configurations:
170 description:
171 - List of references to IpConfigurations.
172 type: list
173 elements: dict
174 suboptions:
175 id:
176 description:
177 - Resource ID.
178 type: str
179 virtual_router_asn:
180 description:
181 - VirtualRouter ASN.
182 type: int
183 virtual_router_ips:
184 description:
185 - VirtualRouter IPs.
186 type: list
187 elements: str
188 enable_virtual_router_route_propogation:
189 description:
190 - Flag to control route propogation for VirtualRouter hub.
191 type: bool
192 state:
193 description:
194 - Assert the state of the VirtualHub.
195 - Use C(present) to create or update an VirtualHub and C(absent) to delete it.
196 default: present
197 type: str
198 choices:
199 - absent
200 - present
201 extends_documentation_fragment:
202 - azure.azcollection.azure
203 - azure.azcollection.azure_tags
204 author:
205 - Fred-Sun (@Fred-Sun)
206 - Haiyuan Zhang (@haiyuazhang)
207
208 '''
209
210 EXAMPLES = '''
211 - name: Create a VirtualHub
212 azure_rm_virtualhub:
213 resource_group: myResourceGroup
214 name: my_virtual_hub_name
215 address_prefix: 10.2.0.0/24
216 sku: Standard
217 location: eastus
218 enable_virtual_router_route_propogation: false
219 virtual_wan:
220 id: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualWans/fredwan
221
222 - name: Delete VirtualHub
223 azure_rm_virtualhub:
224 resource_group: myResourceGroup
225 name: my_virtual_hub_name
226 location: eastus
227 state: absent
228 '''
229
230 RETURN = '''
231 state:
232 description:
233 - Current state of the virtual hub.
234 type: complex
235 returned: always
236 contains:
237 id:
238 description:
239 - Resource ID.
240 returned: always
241 type: str
242 sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualHubs/my_virtual_hub_name
243 name:
244 description:
245 - Resource name.
246 returned: always
247 type: str
248 sample: my_virtual_hub_name
249 type:
250 description:
251 - Resource type.
252 returned: always
253 type: str
254 sample: Microsoft.Network/virtualHubs
255 location:
256 description:
257 - Resource location.
258 returned: always
259 type: str
260 sample: eastus
261 tags:
262 description:
263 - Resource tags.
264 returned: always
265 type: dict
266 sample: { 'key1': 'value1' }
267 etag:
268 description:
269 - A unique read-only string that changes whenever the resource is updated.
270 returned: always
271 type: str
272 sample: cf8c0b06-d339-4155-95fd-2a363945cce4
273 virtual_wan:
274 description:
275 - The VirtualWAN to which the VirtualHub belongs.
276 returned: always
277 type: complex
278 contains:
279 id:
280 description:
281 - Resource ID.
282 returned: always
283 type: str
284 sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualWans/fredwan
285 vpn_gateway:
286 description:
287 - The VpnGateway associated with this VirtualHub.
288 returned: always
289 type: complex
290 contains:
291 id:
292 description:
293 - Resource ID.
294 returned: always
295 type: str
296 sample: null
297 p2_s_vpn_gateway:
298 description:
299 - The P2SVpnGateway associated with this VirtualHub.
300 returned: always
301 type: complex
302 contains:
303 id:
304 description:
305 - Resource ID.
306 returned: always
307 type: str
308 sample: null
309 express_route_gateway:
310 description:
311 - The expressRouteGateway associated with this VirtualHub.
312 returned: always
313 type: dict
314 sample: null
315 contains:
316 id:
317 description:
318 - Resource ID.
319 returned: always
320 type: str
321 sample: null
322 azure_firewall:
323 description:
324 - The azureFirewall associated with this VirtualHub.
325 returned: always
326 type: complex
327 contains:
328 id:
329 description:
330 - Resource ID.
331 returned: always
332 type: str
333 sample: null
334 security_partner_provider:
335 description:
336 - The securityPartnerProvider associated with this VirtualHub.
337 returned: always
338 type: complex
339 contains:
340 id:
341 description:
342 - Resource ID.
343 returned: always
344 type: str
345 sample: null
346 address_prefix:
347 description:
348 - Address-prefix for this VirtualHub.
349 returned: always
350 type: str
351 sample: 10.2.0.0/24
352 route_table:
353 description:
354 - The routeTable associated with this virtual hub.
355 returned: always
356 type: complex
357 contains:
358 routes:
359 description:
360 - List of all routes.
361 returned: always
362 type: list
363 contains:
364 address_prefixes:
365 description:
366 - List of all addressPrefixes.
367 returned: always
368 type: list
369 sample: null
370 next_hop_ip_address:
371 description:
372 - NextHop ip address.
373 returned: always
374 type: str
375 sample: null
376 provisioning_state:
377 description:
378 - The provisioning state of the virtual hub resource.
379 returned: always
380 type: str
381 sample: Succeeded
382 security_provider_name:
383 description:
384 - The Security Provider name.
385 returned: always
386 type: str
387 sample: null
388 virtual_hub_route_table_v2_s:
389 description:
390 - List of all virtual hub route table v2s associated with this VirtualHub.
391 returned: always
392 type: complex
393 contains:
394 name:
395 description:
396 - The name of the resource that is unique within a resource group.
397 - This name can be used to access the resource.
398 returned: always
399 type: str
400 sample: null
401 routes:
402 description:
403 - List of all routes.
404 returned: always
405 type: list
406 contains:
407 destination_type:
408 description:
409 - The type of destinations.
410 returned: always
411 type: str
412 sample: null
413 destinations:
414 description:
415 - List of all destinations.
416 returned: always
417 type: list
418 sample: null
419 next_hop_type:
420 description:
421 - The type of next hops.
422 returned: always
423 type: str
424 sample: null
425 next_hops:
426 description:
427 - NextHops ip address.
428 returned: always
429 type: list
430 sample: null
431 attached_connections:
432 description:
433 - List of all connections attached to this route table v2.
434 returned: always
435 type: list
436 sample: null
437 sku:
438 description:
439 - The sku of this VirtualHub.
440 returned: always
441 type: str
442 sample: null
443 routing_state:
444 description:
445 - The routing state.
446 returned: always
447 type: str
448 sample: Standard
449 bgp_connections:
450 description:
451 - List of references to Bgp Connections.
452 returned: always
453 type: list
454 contains:
455 id:
456 description:
457 - Resource ID.
458 returned: always
459 type: str
460 sample: null
461 ip_configurations:
462 description:
463 - List of references to IpConfigurations.
464 returned: always
465 type: list
466 contains:
467 id:
468 description:
469 - Resource ID.
470 returned: always
471 type: str
472 sample: null
473 virtual_router_asn:
474 description:
475 - VirtualRouter ASN.
476 returned: always
477 type: int
478 sample: null
479 virtual_router_ips:
480 description:
481 - VirtualRouter IPs.
482 returned: always
483 type: list
484 sample: null
485 enable_virtual_router_route_propogation:
486 description:
487 - Flag to control route propogation for VirtualRouter hub.
488 returned: always
489 type: bool
490 sample: null
491
492 '''
493
494 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
495 try:
496 from azure.core.exceptions import ResourceNotFoundError
497 from azure.core.polling import LROPoller
498 except ImportError:
499 # This is handled in azure_rm_common
500 pass
501
502
503 class Actions:
504 NoAction, Create, Update, Delete = range(4)
505
506
507 class AzureRMVirtualHub(AzureRMModuleBaseExt):
508 def __init__(self):
509 self.module_arg_spec = dict(
510 resource_group=dict(
511 type='str',
512 required=True
513 ),
514 location=dict(
515 type='str',
516 ),
517 name=dict(
518 type='str',
519 required=True
520 ),
521 virtual_wan=dict(
522 type='dict',
523 options=dict(
524 id=dict(
525 type='str',
526 )
527 )
528 ),
529 vpn_gateway=dict(
530 type='dict',
531 options=dict(
532 id=dict(
533 type='str',
534 )
535 )
536 ),
537 p2_s_vpn_gateway=dict(
538 type='dict',
539 options=dict(
540 id=dict(
541 type='str',
542 )
543 )
544 ),
545 express_route_gateway=dict(
546 type='dict',
547 options=dict(
548 id=dict(
549 type='str',
550 )
551 )
552 ),
553 azure_firewall=dict(
554 type='dict',
555 options=dict(
556 id=dict(
557 type='str',
558 )
559 )
560 ),
561 security_partner_provider=dict(
562 type='dict',
563 options=dict(
564 id=dict(
565 type='str',
566 )
567 )
568 ),
569 address_prefix=dict(
570 type='str',
571 ),
572 route_table=dict(
573 type='dict',
574 options=dict(
575 routes=dict(
576 type='list',
577 elements='dict',
578 options=dict(
579 address_prefixes=dict(
580 type='list',
581 elements='str'
582 ),
583 next_hop_ip_address=dict(
584 type='str',
585 )
586 )
587 )
588 )
589 ),
590 security_provider_name=dict(
591 type='str',
592 ),
593 virtual_hub_route_table_v2_s=dict(
594 type='list',
595 elements='dict',
596 options=dict(
597 name=dict(
598 type='str',
599 ),
600 routes=dict(
601 type='list',
602 elements='dict',
603 options=dict(
604 destination_type=dict(
605 type='str',
606 ),
607 destinations=dict(
608 type='list',
609 elements='str'
610 ),
611 next_hop_type=dict(
612 type='str',
613 ),
614 next_hops=dict(
615 type='list',
616 elements='str'
617 )
618 )
619 ),
620 attached_connections=dict(
621 type='list',
622 elements='str'
623 )
624 )
625 ),
626 sku=dict(
627 type='str',
628 ),
629 bgp_connections=dict(
630 type='list',
631 elements='dict',
632 options=dict(
633 id=dict(
634 type='str',
635 )
636 )
637 ),
638 ip_configurations=dict(
639 type='list',
640 elements='dict',
641 options=dict(
642 id=dict(
643 type='str',
644 )
645 )
646 ),
647 virtual_router_asn=dict(
648 type='int',
649 ),
650 virtual_router_ips=dict(
651 type='list',
652 elements='str'
653 ),
654 enable_virtual_router_route_propogation=dict(
655 type='bool',
656 ),
657 state=dict(
658 type='str',
659 default='present',
660 choices=['present', 'absent']
661 )
662 )
663
664 self.resource_group = None
665 self.name = None
666 self.location = None
667 self.body = {}
668
669 self.results = dict(changed=False)
670 self.state = None
671 self.to_do = Actions.NoAction
672
673 super(AzureRMVirtualHub, self).__init__(derived_arg_spec=self.module_arg_spec,
674 supports_check_mode=True,
675 supports_tags=True)
676
677 def exec_module(self, **kwargs):
678 for key in list(self.module_arg_spec.keys()):
679 if hasattr(self, key):
680 setattr(self, key, kwargs[key])
681 elif kwargs[key] is not None:
682 self.body[key] = kwargs[key]
683
684 self.inflate_parameters(self.module_arg_spec, self.body, 0)
685
686 resource_group = self.get_resource_group(self.resource_group)
687 if self.location is None:
688 # Set default location
689 self.location = resource_group.location
690 self.body['location'] = self.location
691
692 old_response = None
693 response = None
694
695 old_response = self.get_resource()
696
697 if not old_response:
698 if self.state == 'present':
699 self.to_do = Actions.Create
700 else:
701 if self.state == 'absent':
702 self.to_do = Actions.Delete
703 else:
704 modifiers = {}
705 self.create_compare_modifiers(self.module_arg_spec, '', modifiers)
706 self.results['modifiers'] = modifiers
707 self.results['compare'] = []
708 if not self.default_compare(modifiers, self.body, old_response, '', self.results):
709 self.to_do = Actions.Update
710
711 if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
712 self.results['changed'] = True
713 if self.check_mode:
714 return self.results
715 response = self.create_update_resource()
716 elif self.to_do == Actions.Delete:
717 self.results['changed'] = True
718 if self.check_mode:
719 return self.results
720 self.delete_resource()
721 else:
722 self.results['changed'] = False
723 response = old_response
724
725 if response is not None:
726 self.results['state'] = response
727
728 return self.results
729
730 def create_update_resource(self):
731 try:
732 response = self.network_client.virtual_hubs.begin_create_or_update(resource_group_name=self.resource_group,
733 virtual_hub_name=self.name,
734 virtual_hub_parameters=self.body)
735 if isinstance(response, LROPoller):
736 response = self.get_poller_result(response)
737 except Exception as exc:
738 self.log('Error attempting to create the VirtualHub instance.')
739 self.fail('Error creating the VirtualHub instance: {0}'.format(str(exc)))
740 return response.as_dict()
741
742 def delete_resource(self):
743 try:
744 response = self.network_client.virtual_hubs.begin_delete(resource_group_name=self.resource_group,
745 virtual_hub_name=self.name)
746 if isinstance(response, LROPoller):
747 self.get_poller_result(response)
748 except Exception as e:
749 self.log('Error attempting to delete the VirtualHub instance.')
750 self.fail('Error deleting the VirtualHub instance: {0}'.format(str(e)))
751
752 return True
753
754 def get_resource(self):
755 try:
756 response = self.network_client.virtual_hubs.get(resource_group_name=self.resource_group,
757 virtual_hub_name=self.name)
758 except ResourceNotFoundError as e:
759 return False
760 return response.as_dict()
761
762
763 def main():
764 AzureRMVirtualHub()
765
766
767 if __name__ == '__main__':
768 main()