Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2022 xuzhang3 (@xuzhang3)
4 # XiuxiSun, (@Fred-sun)
5 #
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = '''
13 ---
14 module: azure_rm_virtualhubconnection
15 version_added: '1.14.0'
16 short_description: Manage Azure VirtualHub instance
17 description:
18 - Create, update and delete instance of Azure VirtualHub.
19 options:
20 resource_group:
21 description:
22 - The resource group name of the VirtualHub.
23 required: true
24 type: str
25 name:
26 description:
27 - The name of the VirtualHub connection.
28 required: true
29 type: str
30 vhub_name:
31 description:
32 - The VirtualHub name.
33 type: str
34 required: True
35 enable_internet_security:
36 description:
37 - Enable internet security.
38 type: bool
39 allow_remote_vnet_to_use_hub_vnet_gateways:
40 description:
41 - (Deprecated) Allow RemoteVnet to use Virtual Hub's gateways.
42 - This parameter has been deprecated and is ignored by the service.
43 - It will be removed in next major version release.
44 type: bool
45 allow_hub_to_remote_vnet_transit:
46 description:
47 - (Deprecated) VirtualHub to RemoteVnet transit to enabled or not.
48 - This parameter has been deprecated and is ignored by the service.
49 - It will be removed in next major version release.
50 type: bool
51 remote_virtual_network:
52 description:
53 - ID of the remote VNet to connect to.
54 type: dict
55 suboptions:
56 id:
57 description:
58 - The remote virtual network ID.
59 type: str
60 routing_configuration:
61 description:
62 - The Routing Configuration indicating the associated and propagated route tables on this connection.
63 type: dict
64 suboptions:
65 propagated_route_tables:
66 description:
67 - The list of RouteTables to advertise the routes to.
68 type: dict
69 suboptions:
70 labels:
71 description:
72 - The list of labels.
73 type: list
74 elements: str
75 ids:
76 description:
77 -The list of resource ids of all the virtual hub RouteTables.
78 type: list
79 elements: dict
80 suboptions:
81 id:
82 description:
83 - The ID of the RouteTables.
84 type: str
85 vnet_routes:
86 description:
87 - List of routes that control routing from VirtualHub into a virtual network connection.
88 type: dict
89 suboptions:
90 static_routes:
91 description:
92 - List of all Static Routes.
93 type: list
94 elements: dict
95 suboptions:
96 name:
97 description:
98 - The name of the StaticRoute that is unique within a VnetRoute.
99 type: str
100 address_prefixes:
101 description:
102 - List of all address prefixes.
103 type: list
104 elements: str
105 next_hop_ip_address:
106 description:
107 - The ip address of the next hop.
108 type: str
109 state:
110 description:
111 - Assert the state of the VirtualHub connection.
112 - Use C(present) to create or update an VirtualHub connection and C(absent) to delete it.
113 default: present
114 type: str
115 choices:
116 - absent
117 - present
118 extends_documentation_fragment:
119 - azure.azcollection.azure
120 author:
121 - Fred-Sun (@Fred-Sun)
122 - Xu Zhang (@xuzhang3)
123
124 '''
125
126 EXAMPLES = '''
127 - name: Create virtual hub connection
128 azure_rm_virtualhubconnection:
129 resource_group: myRG
130 vhub_name: testhub
131 name: Myconnection
132 enable_internet_security: false
133 remote_virtual_network:
134 id: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet
135 routing_configuration:
136 propagated_route_tables:
137 labels:
138 - labels1
139 - labels3
140 ids:
141 - id: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub01/hubRouteTables/testtable
142 vnet_routes:
143 static_routes:
144 - name: route1
145 address_prefixes:
146 - 10.1.0.0/16
147 - 10.2.0.0/16
148 next_hop_ip_address: 10.0.0.68
149 - name: route2
150 address_prefixes:
151 - 10.4.0.0/16
152 next_hop_ip_address: 10.0.0.65
153
154 - name: Delete virtual hub connection
155 azure_rm_virtualhubconnection:
156 resource_group: myRG
157 vhub_name: testhub
158 name: Myconnection
159 state: absent
160 '''
161
162 RETURN = '''
163 state:
164 description:
165 - A list of dict results for the virtual hub connection info.
166 returned: always
167 type: complex
168 contains:
169 id:
170 description:
171 - Resource ID.
172 returned: always
173 type: str
174 sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/vhub/hubVirtualNetworkConnections/MyConnection
175 name:
176 description:
177 - Resource name.
178 returned: always
179 type: str
180 sample: MyConnection
181 etag:
182 description:
183 - A unique read-only string that changes whenever the resource is updated.
184 returned: always
185 type: str
186 sample: 31102041-49e7-4cac-8573-aac1e1a16793
187 remote_virtual_network:
188 description:
189 - Name of ID of the remote VNet to connect to.
190 returned: always
191 type: complex
192 contains:
193 id:
194 description:
195 - The ID of the remote VNet to connect to.
196 returned: always
197 type: str
198 sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/testvnet
199 routing_configuration:
200 description:
201 - The routing configuration information
202 returned: always
203 type: complex
204 contains:
205 associated_route_table:
206 description:
207 - The resource ID of route table associated with this routing configuration.
208 type: complex
209 returned: always
210 contains:
211 id:
212 description:
213 - The ID of the routetable.
214 type: str
215 returned: always
216 sample: /subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name
217 propagated_route_tables:
218 description:
219 - Space-separated list of resource id of propagated route tables.
220 type: complex
221 returned: always
222 contains:
223 ids:
224 description:
225 - The list resource ID of propagated route tables.
226 type: list
227 returned: always
228 sample: [{ id: '/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualHubs/testhub/hubRouteTables/rt_name'}]
229 labels:
230 description:
231 - Space-separated list of labels for propagated route tables.
232 type: list
233 returned: always
234 sample: ['labels1', 'labels2']
235 vnet_routes:
236 description:
237 - The name of the Static Route that is unique within a Vnet Route.
238 returned: always
239 type: complex
240 contains:
241 static_routes:
242 description:
243 - The name of the Static Route.
244 type: list
245 returned: always
246 contains:
247 address_prefixes:
248 description:
249 - Space-separated list of all address prefixes.
250 type: list
251 returned: always
252 sample: ["10.1.0.0/16", "10.2.0.0/16"]
253 name:
254 description:
255 - The name of static router.
256 type: str
257 returned: always
258 sample: route1
259 next_hop_ip_address:
260 description:
261 - The next hop ip address.
262 type: str
263 returned: always
264 sample: 10.0.0.65
265 provisioning_state:
266 description:
267 - The provisioning state of the virtual hub connection resource.
268 returned: always
269 type: str
270 sample: Succeeded
271 allow_hub_to_remote_vnet_transit:
272 description:
273 - (Deprecated) Enable hub to remote VNet transit.
274 - This field has been deprecated and will be removed in next major version release.
275 returned: always
276 type: bool
277 sample: true
278 allow_remote_vnet_to_use_hub_vnet_gateways:
279 description:
280 - (Deprecated) Allow remote VNet to use hub's VNet gateways.
281 - This field has been deprecated and will be removed in next major version release.
282 returned: always
283 type: bool
284 sample: true
285 enable_internet_security:
286 description:
287 - Enable internet security and default is enabled.
288 type: bool
289 returned: always
290 sample: true
291 '''
292
293 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
294 try:
295 from azure.core.exceptions import ResourceNotFoundError
296 from azure.core.polling import LROPoller
297 except ImportError:
298 # This is handled in azure_rm_common
299 pass
300
301
302 class Actions:
303 NoAction, Create, Update, Delete = range(4)
304
305
306 static_routes_spec = dict(
307 name=dict(type='str'),
308 address_prefixes=dict(type='list', elements='str'),
309 next_hop_ip_address=dict(type='str')
310 )
311
312
313 class AzureRMVirtualHubConnection(AzureRMModuleBaseExt):
314 def __init__(self):
315 self.module_arg_spec = dict(
316 resource_group=dict(
317 type='str',
318 required=True
319 ),
320 vhub_name=dict(
321 type='str',
322 required=True
323 ),
324 name=dict(
325 type='str',
326 required=True
327 ),
328 enable_internet_security=dict(
329 type='bool'
330 ),
331 allow_remote_vnet_to_use_hub_vnet_gateways=dict(
332 type='bool',
333 removed_in_version='4.0.0',
334 removed_from_collection='azure.azcollection'
335 ),
336 allow_hub_to_remote_vnet_transit=dict(
337 type='bool',
338 removed_in_version='4.0.0',
339 removed_from_collection='azure.azcollection'
340 ),
341 remote_virtual_network=dict(
342 type='dict',
343 options=dict(
344 id=dict(
345 type='str',
346 )
347 )
348 ),
349 routing_configuration=dict(
350 type='dict',
351 options=dict(
352 propagated_route_tables=dict(
353 type='dict',
354 options=dict(
355 labels=dict(
356 type='list',
357 elements='str'
358 ),
359 ids=dict(
360 type='list',
361 elements='dict',
362 options=dict(
363 id=dict(
364 type='str',
365 )
366 )
367 )
368 )
369 ),
370 vnet_routes=dict(
371 type='dict',
372 options=dict(
373 static_routes=dict(
374 type='list',
375 elements='dict',
376 options=static_routes_spec
377 )
378 )
379 )
380 )
381 ),
382 state=dict(
383 type='str',
384 default='present',
385 choices=['present', 'absent']
386 )
387 )
388
389 self.resource_group = None
390 self.vhub_name = None
391 self.name = None
392 self.body = {}
393
394 self.results = dict(changed=False)
395 self.state = None
396 self.to_do = Actions.NoAction
397
398 super(AzureRMVirtualHubConnection, self).__init__(derived_arg_spec=self.module_arg_spec,
399 supports_check_mode=True,
400 supports_tags=False)
401
402 def exec_module(self, **kwargs):
403 for key in list(self.module_arg_spec.keys()):
404 if hasattr(self, key):
405 setattr(self, key, kwargs[key])
406 elif kwargs[key] is not None:
407 self.body[key] = kwargs[key]
408
409 self.inflate_parameters(self.module_arg_spec, self.body, 0)
410
411 old_response = None
412 response = None
413
414 old_response = self.get_resource()
415
416 if not old_response:
417 if self.state == 'present':
418 self.to_do = Actions.Create
419 else:
420 if self.state == 'absent':
421 self.to_do = Actions.Delete
422 else:
423 if self.body.get('enable_internet_security') is not None:
424 if bool(self.body['enable_internet_security']) != bool(old_response['enable_internet_security']):
425 self.to_do = Actions.Update
426 else:
427 self.body['enable_internet_security'] = old_response['enable_internet_security']
428 if self.body.get('allow_remote_vnet_to_use_hub_vnet_gateways') is not None:
429 if bool(self.body['allow_remote_vnet_to_use_hub_vnet_gateways']) != bool(old_response['allow_remote_vnet_to_use_hub_vnet_gateways']):
430 self.to_do = Actions.Update
431 else:
432 self.body['allow_remote_vnet_to_use_hub_vnet_gateways'] = old_response['allow_remote_vnet_to_use_hub_vnet_gateways']
433 if self.body.get('allow_hub_to_remote_vnet_transit') is not None:
434 if bool(self.body['allow_hub_to_remote_vnet_transit']) != bool(old_response['allow_hub_to_remote_vnet_transit']):
435 self.to_do = Actions.Update
436 else:
437 self.body['allow_hub_to_remote_vnet_transit'] = old_response['allow_hub_to_remote_vnet_transit']
438
439 if self.body.get('routing_configuration') is not None:
440 modifiers = {}
441 self.create_compare_modifiers(self.module_arg_spec, '', modifiers)
442 self.results['modifiers'] = modifiers
443 self.results['compare'] = []
444 if not self.default_compare(modifiers, self.body['routing_configuration'], old_response['routing_configuration'], '', self.results):
445 self.to_do = Actions.Update
446 else:
447 self.body['routing_configuration'] = old_response['routing_configuration']
448 else:
449 self.body['routing_configuration'] = old_response['routing_configuration']
450
451 if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
452 self.results['changed'] = True
453 if self.check_mode:
454 return self.results
455 response = self.create_update_resource()
456 elif self.to_do == Actions.Delete:
457 self.results['changed'] = True
458 if self.check_mode:
459 return self.results
460 self.delete_resource()
461 else:
462 self.results['changed'] = False
463 response = old_response
464
465 if response is not None:
466 self.results['state'] = response
467
468 return self.results
469
470 def create_update_resource(self):
471 try:
472 response = self.network_client.hub_virtual_network_connections.begin_create_or_update(resource_group_name=self.resource_group,
473 virtual_hub_name=self.vhub_name,
474 connection_name=self.name,
475 hub_virtual_network_connection_parameters=self.body)
476 if isinstance(response, LROPoller):
477 response = self.get_poller_result(response)
478 except Exception as exc:
479 self.log('Error attempting to create the VirtualHub instance.')
480 self.fail('Error creating the VirtualHub instance: {0}'.format(str(exc)))
481 return response.as_dict()
482
483 def delete_resource(self):
484 try:
485 response = self.network_client.hub_virtual_network_connections.begin_delete(resource_group_name=self.resource_group,
486 virtual_hub_name=self.vhub_name,
487 connection_name=self.name)
488 if isinstance(response, LROPoller):
489 self.get_poller_result(response)
490 except Exception as e:
491 self.log('Error attempting to delete the VirtualHub connection instance.')
492 self.fail('Error deleting the VirtualHub connection instance: {0}'.format(str(e)))
493
494 return True
495
496 def get_resource(self):
497 try:
498 response = self.network_client.hub_virtual_network_connections.get(resource_group_name=self.resource_group,
499 virtual_hub_name=self.vhub_name,
500 connection_name=self.name)
501 except ResourceNotFoundError as e:
502 return False
503 return response.as_dict()
504
505
506 def main():
507 AzureRMVirtualHubConnection()
508
509
510 if __name__ == '__main__':
511 main()