Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2022 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
9 __metaclass__ = type
10
11
12 DOCUMENTATION = '''
13 ---
14 module: azure_rm_bastionhost
15
16 version_added: "1.13.0"
17
18 short_description: Managed bastion host resource
19
20 description:
21 - Create, Update or Delete bastion host resource.
22
23 options:
24 name:
25 description:
26 - The name of the bastion host.
27 type: str
28 required: True
29 resource_group:
30 description:
31 - The name of the resource group.
32 type: str
33 required: True
34 location:
35 description:
36 - The resource location.
37 type: str
38 sku:
39 description:
40 - The sku of this Bastion Host.
41 type: dict
42 suboptions:
43 name:
44 description:
45 - The name of the SKU.
46 type: str
47 choices:
48 - Standard
49 - Basic
50 enable_tunneling:
51 description:
52 - Enable or Disable Tunneling feature of the Bastion Host resource.
53 type: bool
54 enable_shareable_link:
55 description:
56 - Enable or Disable Shareable Link of the Bastion Host resource.
57 type: bool
58 enable_ip_connect:
59 description:
60 - Enable or Disable IP Connect feature of the Bastion Host resource.
61 type: bool
62 enable_file_copy:
63 description:
64 - Enable or Disable File Copy feature of the Bastion Host resource.
65 type: bool
66 scale_units:
67 description:
68 - The scale units for the Bastion Host resource.
69 type: int
70 disable_copy_paste:
71 description:
72 - Enable or Disable Copy or Paste feature of the Bastion Host resource.
73 type: bool
74 ip_configurations:
75 description:
76 - An array of bastion host IP configurations.
77 type: list
78 elements: dict
79 suboptions:
80 name:
81 description:
82 - The name of bastion host ip configuration.
83 type: str
84 subnet:
85 description:
86 - Reference of the subnet resource.
87 type: dict
88 suboptions:
89 id:
90 description:
91 - The ID of the Subnet.
92 type: str
93 public_ip_address:
94 description:
95 - Reference of the PublicIP resource.
96 type: dict
97 suboptions:
98 id:
99 description:
100 - The ID of the public IP.
101 type: str
102 private_ip_allocation_method:
103 description:
104 - Private IP allocation method.
105 type: str
106 choices:
107 - Static
108 - Dynamic
109 state:
110 description:
111 - Assert the state of the pirvate link service.
112 - Use I(state=present) to create or update the link service and I(state=absent) to delete it.
113 type: str
114 default: present
115 choices:
116 - present
117 - absent
118
119 extends_documentation_fragment:
120 - azure.azcollection.azure
121 - azure.azcollection.azure_tags
122
123 author:
124 - xuzhang3 (@xuzhang3)
125 - Fred-sun (@Fred-sun)
126 '''
127
128 EXAMPLES = '''
129 - name: Create bastion host info
130 azure_rm_bastionhost:
131 name: bastion-name
132 resource_group: myResourceGroup
133 ip_configurations:
134 - name: testip_configuration
135 subnet:
136 id: "{{ subnet_output.state.id }}"
137 public_ip_address:
138 id: "{{ publicIP_output.publicipaddresses[0].id }}"
139 private_ip_allocation_method: Dynamic
140 sku:
141 name: Standard
142 enable_tunneling: false
143 enable_shareable_link: false
144 enable_ip_connect: false
145 enable_file_copy: false
146 scale_units: 6
147 disable_copy_paste: false
148 tags:
149 key1: value1
150
151 - name: Create bastion host info
152 azure_rm_bastionhost:
153 name: bastion-name
154 resource_group: myResourceGroup
155 state: absent
156 '''
157
158 RETURN = '''
159 bastion_host:
160 description:
161 - List of Azure bastion host info.
162 returned: always
163 type: complex
164 contains:
165 id:
166 description:
167 - Resource ID of the Azure bastion host.
168 sample: "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/bastionHosts/testbastion"
169 returned: always
170 type: str
171 name:
172 description:
173 - Name of the Azure bastion host.
174 returned: always
175 type: str
176 sample: linkservice
177 location:
178 description:
179 - Resource location.
180 returned: always
181 type: str
182 sample: eastus
183 etag:
184 description:
185 - A unique read-only string that changes whenever the resource is updated.
186 type: str
187 returned: always
188 sample: "fb0e3a90-6afa-4a01-9171-9c84d144a0f3"
189 type:
190 description:
191 - The resource type.
192 type: str
193 returned: always
194 sample: Microsoft.Network/bastionHosts
195 tags:
196 description:
197 - The resource tags.
198 type: list
199 returned: always
200 sample: { 'key1': 'value1' }
201 provisioning_state:
202 description:
203 - The provisioning state of the bastion host resource.
204 type: str
205 returned: always
206 sample: Succeeded
207 scale_units:
208 description:
209 - The scale units for the Bastion Host resource.
210 type: int
211 returned: always
212 sample: 2
213 enable_tunneling:
214 description:
215 - Enable/Disable Tunneling feature of the Bastion Host resource.
216 type: bool
217 returned: always
218 sample: False
219 enable_shareable_link:
220 description:
221 - Enable/Disable Shareable Link of the Bastion Host resource.
222 type: bool
223 returned: always
224 sample: False
225 enable_ip_connect:
226 description:
227 - Enable/Disable IP Connect feature of the Bastion Host resource.
228 type: bool
229 returned: always
230 sample: False
231 enable_file_copy:
232 description:
233 - Enable/Disable File Copy feature of the Bastion Host resource.
234 type: bool
235 returned: always
236 sample: False
237 dns_name:
238 description:
239 - FQDN for the endpoint on which bastion host is accessible.
240 type: str
241 returned: always
242 sample: bst-0ca1e1b6-9969-4167-be54-5972e1395c25.bastion.azure.com
243 disable_copy_paste:
244 description:
245 - Enable/Disable Copy/Paste feature of the Bastion Host resource.
246 type: bool
247 returned: always
248 sample: False
249 sku:
250 description:
251 - The sku of this Bastion Host.
252 type: complex
253 returned: always
254 contains:
255 name:
256 description:
257 - The name of this Bastion Host.
258 type: str
259 returned: always
260 sample: Standard
261 ip_configurations:
262 description:
263 - An array of bastion host IP configurations.
264 type: complex
265 returned: always
266 contains:
267 name:
268 description:
269 - Name of the resource that is unique within a resource group.
270 - This name can be used to access the resource.
271 type: str
272 returned: always
273 sample: IpConf
274 private_ip_allocation_method:
275 description:
276 - Private IP allocation method.
277 type: str
278 returned: always
279 sample: Static
280 public_ip_address:
281 description:
282 - Reference of the PublicIP resource.
283 type: complex
284 returned: always
285 contains:
286 id:
287 description:
288 - The ID of the public IP address.
289 returned: always
290 type: str
291 sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/publicIPAddresses/Myip"
292 subnet:
293 description:
294 - The reference to the subnet resource.
295 returned: always
296 type: str
297 contains:
298 id:
299 description:
300 - The ID of the subnet..
301 returned: always
302 type: str
303 sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/vnet/subnets/AzureBastionSubnet"
304 '''
305
306 try:
307 from azure.core.exceptions import ResourceNotFoundError
308 except Exception:
309 # This is handled in azure_rm_common
310 pass
311
312
313 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
314
315
316 sku_spec = dict(
317 name=dict(type='str', choices=['Standard', 'Basic'])
318 )
319
320 subnet_spec = dict(
321 id=dict(type='str')
322 )
323
324 public_ip_address_spec = dict(
325 id=dict(type='str')
326 )
327
328
329 class AzureRMBastionHost(AzureRMModuleBase):
330
331 def __init__(self):
332
333 self.module_arg_spec = dict(
334 name=dict(type="str", required=True),
335 resource_group=dict(type="str", required=True),
336 state=dict(type='str', default='present', choices=['present', 'absent']),
337 location=dict(type='str'),
338 ip_configurations=dict(
339 type='list',
340 elements='dict',
341 options=dict(
342 name=dict(type='str'),
343 subnet=dict(type='dict', options=subnet_spec),
344 public_ip_address=dict(type='dict', options=public_ip_address_spec),
345 private_ip_allocation_method=dict(type='str', choices=['Static', 'Dynamic'])
346 )
347 ),
348 sku=dict(type='dict', options=sku_spec),
349 enable_tunneling=dict(type='bool'),
350 enable_shareable_link=dict(type='bool'),
351 enable_ip_connect=dict(type='bool'),
352 enable_file_copy=dict(type='bool'),
353 scale_units=dict(type='int'),
354 disable_copy_paste=dict(type='bool')
355 )
356
357 self.name = None
358 self.resource_group = None
359 self.location = None
360 self.tags = None
361 self.state = None
362 self.results = dict(
363 changed=False,
364 )
365 self.body = {}
366
367 super(AzureRMBastionHost, self).__init__(self.module_arg_spec,
368 supports_check_mode=True,
369 supports_tags=True,
370 facts_module=False)
371
372 def exec_module(self, **kwargs):
373 for key in list(self.module_arg_spec.keys()) + ['tags']:
374 if hasattr(self, key):
375 setattr(self, key, kwargs[key])
376 elif kwargs[key] is not None:
377 self.body[key] = kwargs[key]
378
379 old_response = self.get_item()
380 result = None
381 changed = False
382
383 if not self.location:
384 resource_group = self.get_resource_group(self.resource_group)
385 self.location = resource_group.location
386
387 self.body['location'] = self.location
388 self.body['tags'] = self.tags
389
390 if self.state == 'present':
391 if old_response:
392 update_tags, tags = self.update_tags(old_response['tags'])
393 if update_tags:
394 changed = True
395 self.body['tags'] = tags
396
397 if self.body.get('disable_copy_paste') is not None:
398 if bool(self.body.get('disable_copy_paste')) != bool(old_response['disable_copy_paste']):
399 changed = True
400 else:
401 self.body['disable_copy_paste'] = old_response['disable_copy_paste']
402
403 if self.body.get('enable_file_copy') is not None:
404 if bool(self.body.get('enable_file_copy')) != bool(old_response['enable_file_copy']):
405 changed = True
406 else:
407 self.body['enable_file_copy'] = old_response['enable_file_copy']
408
409 if self.body.get('enable_ip_connect') is not None:
410 if bool(self.body.get('enable_ip_connect')) != bool(old_response['enable_ip_connect']):
411 changed = True
412 else:
413 self.body['enable_ip_connect'] = old_response['enable_ip_connect']
414
415 if self.body.get('enable_shareable_link') is not None:
416 if bool(self.body.get('enable_shareable_link')) != bool(old_response['enable_shareable_link']):
417 changed = True
418 else:
419 self.body['enable_shareable_link'] = old_response['enable_shareable_link']
420
421 if self.body.get('enable_tunneling') is not None:
422 if bool(self.body.get('enable_tunneling')) != bool(old_response['enable_tunneling']):
423 changed = True
424 else:
425 self.body['enable_tunneling'] = old_response['enable_tunneling']
426
427 if self.body.get('scale_units') is not None:
428 if self.body.get('scale_units') != old_response['scale_units']:
429 changed = True
430 else:
431 self.body['scale_units'] = old_response['scale_units']
432
433 if self.body.get('sku') is not None:
434 if self.body.get('sku') != old_response['sku']:
435 changed = True
436 else:
437 self.body['sku'] = old_response['sku']
438
439 if self.body.get('ip_configurations') is not None:
440 if self.body['ip_configurations'] != old_response['ip_configurations']:
441 self.fail("Bastion Host IP configuration not support to update!")
442 else:
443 self.body['ip_configurations'] = old_response['ip_configurations']
444 else:
445 changed = True
446
447 if changed:
448 if self.check_mode:
449 self.log("Check mode test. The bastion host is exist, will be create or updated")
450 else:
451 result = self.create_or_update(self.body)
452 else:
453 if self.check_mode:
454 self.log("Check mode test. The Azure Bastion Host is exist, No operation in this task")
455 else:
456 self.log("The Azure Bastion Host is exist, No operation in this task")
457 result = old_response
458 else:
459 if old_response:
460 changed = True
461 if self.check_mode:
462 self.log("Check mode test. The bastion host is exist, will be deleted")
463 else:
464 result = self.delete_resource()
465 else:
466 if self.check_mode:
467 self.log("The bastion host isn't exist, no action")
468 else:
469 self.log("The bastion host isn't exist, don't need to delete")
470
471 self.results["bastion_host"] = result
472 self.results['changed'] = changed
473 return self.results
474
475 def get_item(self):
476 self.log("Get properties for {0} in {1}".format(self.name, self.resource_group))
477 try:
478 response = self.network_client.bastion_hosts.get(self.resource_group, self.name)
479 return self.bastion_to_dict(response)
480 except ResourceNotFoundError:
481 self.log("Could not get info for {0} in {1}".format(self.name, self.resource_group))
482
483 return []
484
485 def create_or_update(self, parameters):
486 self.log("Create or update the bastion host for {0} in {1}".format(self.name, self.resource_group))
487 try:
488 response = self.network_client.bastion_hosts.begin_create_or_update(self.resource_group, self.name, parameters)
489
490 result = self.network_client.bastion_hosts.get(self.resource_group, self.name)
491 return self.bastion_to_dict(result)
492 except Exception as ec:
493 self.fail("Create or Update {0} in {1} failed, mesage {2}".format(self.name, self.resource_group, ec))
494
495 return []
496
497 def delete_resource(self):
498 self.log("delete the bastion host for {0} in {1}".format(self.name, self.resource_group))
499 try:
500 response = self.network_client.bastion_hosts.begin_delete(self.resource_group, self.name)
501 except Exception as ec:
502 self.fail("Delete {0} in {1} failed, message {2}".format(self.name, self.resource_group, ec))
503
504 return []
505
506 def bastion_to_dict(self, bastion_info):
507 bastion = bastion_info.as_dict()
508 result = dict(
509 id=bastion.get("id"),
510 name=bastion.get('name'),
511 type=bastion.get('type'),
512 etag=bastion.get('etag'),
513 location=bastion.get('location'),
514 tags=bastion.get('tags'),
515 sku=dict(),
516 ip_configurations=list(),
517 dns_name=bastion.get('dns_name'),
518 provisioning_state=bastion.get('provisioning_state'),
519 scale_units=bastion.get('scale_units'),
520 disable_copy_paste=bastion.get('disable_copy_paste'),
521 enable_file_copy=bastion.get('enable_file_copy'),
522 enable_ip_connect=bastion.get('enable_ip_connect'),
523 enable_shareable_link=bastion.get('enable_tunneling'),
524 enable_tunneling=bastion.get('enable_tunneling')
525 )
526
527 if bastion.get('sku'):
528 result['sku']['name'] = bastion['sku']['name']
529
530 if bastion.get('ip_configurations'):
531 for items in bastion['ip_configurations']:
532 result['ip_configurations'].append(
533 {
534 "name": items['name'],
535 "subnet": dict(id=items['subnet']['id']),
536 "public_ip_address": dict(id=items['public_ip_address']['id']),
537 "private_ip_allocation_method": items['private_ip_allocation_method'],
538 }
539 )
540 return result
541
542
543 def main():
544 AzureRMBastionHost()
545
546
547 if __name__ == "__main__":
548 main()