Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 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_virtualwan
14 version_added: '1.5.0'
15 short_description: Manage Azure VirtualWan instance
16 description:
17 - Create, update and delete instance of Azure VirtualWan.
18 options:
19 resource_group:
20 description:
21 - The resource group name of the VirtualWan.
22 required: true
23 type: str
24 office365_local_breakout_category:
25 description:
26 - Specifies the Office365 local breakout category.
27 - Default value is C(None).
28 type: str
29 choices:
30 - Optimize
31 - OptimizeAndAllow
32 - All
33 - None
34 name:
35 description:
36 - The name of the VirtualWAN being retrieved.
37 required: true
38 type: str
39 location:
40 description:
41 - The virtual wan location.
42 type: str
43 disable_vpn_encryption:
44 description:
45 - Vpn encryption to be disabled or not.
46 type: bool
47 virtual_hubs:
48 description:
49 - List of VirtualHubs in the VirtualWAN.
50 type: list
51 elements: dict
52 suboptions:
53 id:
54 description:
55 - The virtual hub resource ID.
56 type: str
57 vpn_sites:
58 description:
59 - List of VpnSites in the VirtualWAN.
60 type: list
61 elements: dict
62 suboptions:
63 id:
64 description:
65 - The vpn site resource ID.
66 type: str
67 allow_branch_to_branch_traffic:
68 description:
69 - True if branch to branch traffic is allowed.
70 type: bool
71 allow_vnet_to_vnet_traffic:
72 description:
73 - C(True) if Vnet to Vnet traffic is allowed.
74 type: bool
75 virtual_wan_type:
76 description:
77 - The type of the VirtualWAN.
78 type: str
79 choices:
80 - Basic
81 - Standard
82 state:
83 description:
84 - Assert the state of the VirtualWan.
85 - Use C(present) to create or update an VirtualWan and C(absent) to delete it.
86 default: present
87 type: str
88 choices:
89 - absent
90 - present
91 extends_documentation_fragment:
92 - azure.azcollection.azure
93 - azure.azcollection.azure_tags
94 author:
95 - Fred-Sun (@Fred-Sun)
96
97 '''
98
99 EXAMPLES = '''
100 - name: Create a VirtualWan
101 azure_rm_virtualwan:
102 resource_group: myResouceGroup
103 name: testwan
104 disable_vpn_encryption: true
105 allow_branch_to_branch_traffic: true
106 allow_vnet_to_vnet_traffic: true
107 virtual_wan_type: Standard
108
109 - name: Delete the VirtualWan
110 azure_rm_virtualwan:
111 resource_group: myResouceGroup
112 name: testwan
113 state: absent
114 '''
115
116 RETURN = '''
117 state:
118 description:
119 - Current state of the virtual wan.
120 type: complex
121 returned: success
122 contains:
123 id:
124 description:
125 - Resource ID.
126 returned: always
127 type: str
128 sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualWans/virtual_wan_name
129 name:
130 description:
131 - Resource name.
132 returned: always
133 type: str
134 sample: virtualwanb57dc9555691
135 type:
136 description:
137 - Resource type.
138 returned: always
139 type: str
140 sample: Microsoft.Network/virtualWans
141 location:
142 description:
143 - The virtual wan resource location.
144 returned: always
145 type: str
146 sample: eastus
147 tags:
148 description:
149 - Resource tags.
150 returned: always
151 type: dict
152 sample: { 'key1': 'value1'}
153 etag:
154 description:
155 - A unique read-only string that changes whenever the resource is updated.
156 returned: always
157 type: str
158 sample: 52def36b-84b6-49aa-a825-16ba167fc559
159 disable_vpn_encryption:
160 description:
161 - Vpn encryption to be disabled or not.
162 returned: always
163 type: bool
164 sample: true
165 virtual_hubs:
166 description:
167 - List of VirtualHubs in the VirtualWAN.
168 type: complex
169 returned: always
170 contains:
171 id:
172 description:
173 - The virtual hubs ID.
174 type: str
175 returned: always
176 sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualHubs/test
177 vpn_sites:
178 description:
179 - List of VpnSites in the VirtualWAN.
180 returned: always
181 type: list
182 contains:
183 id:
184 description:
185 - The vpn sites resouce ID.
186 returned: always
187 type: str
188 sample: /subscriptions/xxx-xxx/resourceGroups/resource_group/providers/Microsoft.Network/vpnSites/test1
189 allow_branch_to_branch_traffic:
190 description:
191 - True if branch to branch traffic is allowed.
192 returned: always
193 type: bool
194 sample: true
195 allow_vnet_to_vnet_traffic:
196 description:
197 - True if Vnet to Vnet traffic is allowed.
198 returned: always
199 type: bool
200 sample: true
201 office365_local_breakout_category:
202 description:
203 - The office local breakout category.
204 returned: always
205 type: str
206 sample: None
207 provisioning_state:
208 description:
209 - The provisioning state of the virtual WAN resource.
210 returned: always
211 type: str
212 sample: Succeeded
213 virtual_wan_type:
214 description:
215 - The type of the VirtualWAN.
216 returned: always
217 type: str
218 sample: Standard
219
220 '''
221
222 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
223 try:
224 from azure.core.exceptions import ResourceNotFoundError
225 from azure.core.polling import LROPoller
226 except ImportError:
227 # This is handled in azure_rm_common
228 pass
229
230
231 class Actions:
232 NoAction, Create, Update, Update_tags, Delete = range(5)
233
234
235 class AzureRMVirtualWan(AzureRMModuleBaseExt):
236 def __init__(self):
237 self.module_arg_spec = dict(
238 location=dict(
239 type='str'
240 ),
241 resource_group=dict(
242 type='str',
243 required=True
244 ),
245 office365_local_breakout_category=dict(
246 type='str',
247 choices=['Optimize', 'OptimizeAndAllow', 'All', 'None']
248 ),
249 name=dict(
250 type='str',
251 required=True
252 ),
253 disable_vpn_encryption=dict(
254 type='bool'
255 ),
256 virtual_hubs=dict(
257 type='list',
258 elements='dict',
259 options=dict(
260 id=dict(
261 type='str'
262 )
263 )
264 ),
265 vpn_sites=dict(
266 type='list',
267 elements='dict',
268 options=dict(
269 id=dict(
270 type='str'
271 )
272 )
273 ),
274 allow_branch_to_branch_traffic=dict(
275 type='bool'
276 ),
277 allow_vnet_to_vnet_traffic=dict(
278 type='bool'
279 ),
280 virtual_wan_type=dict(
281 type='str',
282 choices=['Basic', 'Standard']
283 ),
284 state=dict(
285 type='str',
286 default='present',
287 choices=['present', 'absent']
288 )
289 )
290
291 self.resource_group = None
292 self.name = None
293 self.location = None
294 self.body = {}
295
296 self.results = dict(changed=False)
297 self.state = None
298 self.to_do = Actions.NoAction
299
300 super(AzureRMVirtualWan, self).__init__(derived_arg_spec=self.module_arg_spec,
301 supports_check_mode=True,
302 supports_tags=True)
303
304 def exec_module(self, **kwargs):
305 for key in list(self.module_arg_spec.keys()) + ['tags']:
306 if hasattr(self, key):
307 setattr(self, key, kwargs[key])
308 elif kwargs[key] is not None:
309 self.body[key] = kwargs[key]
310
311 resource_group = self.get_resource_group(self.resource_group)
312 if self.location is None:
313 # Set default location
314 self.location = resource_group.location
315 self.body['location'] = self.location
316
317 old_response = None
318 response = None
319
320 old_response = self.get_resource()
321
322 if not old_response:
323 if self.state == 'present':
324 self.to_do = Actions.Create
325 else:
326 if self.state == 'absent':
327 self.to_do = Actions.Delete
328 else:
329 compare_list = ['disable_vpn_encryption', 'allow_branch_to_branch_traffic']
330 for key in compare_list:
331 if self.body.get(key) is not None and self.body[key] != old_response[key]:
332 self.log('parameter {0} does not match the configuration'.format(key))
333 self.to_do = Actions.Update
334 else:
335 self.body[key] = old_response[key]
336
337 update_tags, self.tags = self.update_tags(old_response.get('tags'))
338 if update_tags:
339 self.to_do = Actions.Update_tags
340
341 if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
342 self.results['changed'] = True
343 if self.check_mode:
344 return self.results
345 response = self.create_update_resource()
346 elif self.to_do == Actions.Update_tags:
347 self.results['changed'] = True
348 if self.check_mode:
349 return self.results
350 response = self.update_resource_tags(dict(tags=self.tags))
351 elif self.to_do == Actions.Delete:
352 self.results['changed'] = True
353 if self.check_mode:
354 return self.results
355 self.delete_resource()
356 else:
357 self.results['changed'] = False
358 response = old_response
359 if response is not None:
360 self.results['state'] = response
361 return self.results
362
363 def update_resource_tags(self, tags_parameters):
364 try:
365 response = self.network_client.virtual_wans.update_tags(resource_group_name=self.resource_group,
366 virtual_wan_name=self.name,
367 wan_parameters=tags_parameters)
368 if isinstance(response, LROPoller):
369 response = self.get_poller_result(response)
370 except Exception as exc:
371 self.log('Error attempting to Update the VirtualWan instance.')
372 self.fail('Error Updating the VirtualWan instance tags: {0}'.format(str(exc)))
373 return response.as_dict()
374
375 def create_update_resource(self):
376 try:
377 response = self.network_client.virtual_wans.begin_create_or_update(resource_group_name=self.resource_group,
378 virtual_wan_name=self.name,
379 wan_parameters=self.body)
380 if isinstance(response, LROPoller):
381 response = self.get_poller_result(response)
382 except Exception as exc:
383 self.log('Error attempting to create the VirtualWan instance.')
384 self.fail('Error creating the VirtualWan instance: {0}'.format(str(exc)))
385 return response.as_dict()
386
387 def delete_resource(self):
388 try:
389 response = self.network_client.virtual_wans.begin_delete(resource_group_name=self.resource_group,
390 virtual_wan_name=self.name)
391 if isinstance(response, LROPoller):
392 self.get_poller_result(response)
393 except Exception as e:
394 self.log('Error attempting to delete the VirtualWan instance.')
395 self.fail('Error deleting the VirtualWan instance: {0}'.format(str(e)))
396
397 return True
398
399 def get_resource(self):
400 try:
401 response = self.network_client.virtual_wans.get(resource_group_name=self.resource_group,
402 virtual_wan_name=self.name)
403 except ResourceNotFoundError as e:
404 return False
405 return response.as_dict()
406
407
408 def main():
409 AzureRMVirtualWan()
410
411
412 if __name__ == '__main__':
413 main()