Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_privateendpoint.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_privateendpoint
14 version_added: "1.8.0"
15 short_description: Manage Azure private endpoint
16 description:
17 - Create, update or delete a private endpoint.
18 options:
19 resource_group:
20 description:
21 - Name of resource group.
22 required: true
23 type: str
24 location:
25 description:
26 - Valid Azure location. Defaults to location of the resource group.
27 type: str
28 name:
29 description:
30 - Name of the private endpoint.
31 required: true
32 type: str
33 subnet:
34 description:
35 - The ID of the subnet from which the private IP will be allocated.
36 - This parameter is required for create or update.
37 type: dict
38 suboptions:
39 id:
40 description:
41 - The ID of the subnet from which the private IP will be allocated.
42 type: str
43 manual_private_link_service_connections:
44 description:
45 - A grouping of information about the connection to the remote resource.
46 - Used when the network admin does not have access to approve connections to the remote resource.
47 type: list
48 elements: dict
49 suboptions:
50 name:
51 description:
52 - The name of the resource that is unique within a resource group.
53 type: str
54 private_link_service_id:
55 description:
56 - The resource id of the private endpoint to connect to.
57 type: str
58 group_ids:
59 description:
60 - The ID(s) of the group(s) obtained from the remote resource that this private endpoint should connect to.
61 type: list
62 elements: str
63 private_link_service_connections:
64 description:
65 - A grouping of information about the connection to the remote resource.
66 - This parameter is required for create or update.
67 type: list
68 elements: dict
69 suboptions:
70 name:
71 description:
72 - The name of the resource that is unique within a resource group.
73 type: str
74 private_link_service_id:
75 description:
76 - The resource id of the private endpoint to connect to.
77 type: str
78 group_ids:
79 description:
80 - The ID(s) of the group(s) obtained from the remote resource that this private endpoint should connect to.
81 type: list
82 elements: str
83 application_security_groups:
84 description:
85 - The application security group in a resource group.
86 type: list
87 elements: dict
88 suboptions:
89 id:
90 description:
91 - The application security group's ID.
92 type: str
93 custom_dns_configs:
94 description:
95 - An array of custom dns configurations.
96 type: list
97 elements: dict
98 suboptions:
99 fqdn:
100 description:
101 - Fqdn that resolves to private endpoint ip address.
102 type: str
103 ip_addresses:
104 description:
105 - A list of private ip addresses of the private endpoint.
106 type: list
107 elements: str
108 custom_network_interface_name:
109 description:
110 - The custom name of the network interface attached to the private endpoint.
111 type: str
112 ip_configurations:
113 description:
114 - A list of IP configurations of the private endpoint.
115 - This will be used to map to the First Party Service's endpoints.
116 type: list
117 elements: dict
118 suboptions:
119 name:
120 description:
121 - The name of the resource that is unique within a resource group.
122 type: str
123 group_id:
124 description:
125 - The ID of a group obtained from the remote resource that this private endpoint should connect to.
126 type: str
127 member_name:
128 description:
129 - The member name of a group obtained from the remote resource that this private endpoint should connect to.
130 type: str
131 private_ip_address:
132 description:
133 - A private ip address obtained from the private endpoint's subnet.
134 type: str
135 state:
136 description:
137 - State of the virtual network. Use C(present) to create or update and C(absent) to delete.
138 default: present
139 type: str
140 choices:
141 - absent
142 - present
143
144 extends_documentation_fragment:
145 - azure.azcollection.azure
146 - azure.azcollection.azure_tags
147
148 author:
149 - Fred-sun (@Fred-sun)
150
151 '''
152
153 EXAMPLES = '''
154 - name: Create private endpoint with private_link_service_connections
155 azure_rm_privateendpoint:
156 name: testprivateendpoint
157 resource_group: v-xisuRG
158 private_link_service_connections:
159 - name: Test_private_link_service
160 private_link_service_id: /subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privateLinkServices/testervice
161 subnet:
162 id: /subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/fredvnet/subnets/default
163 tags:
164 key1: value1
165 key2: value2
166
167 - name: Create private endpoint with ip_configuration
168 azure_rm_privateendpoint:
169 name: "privateendpoint02"
170 resource_group: "{{ resource_group }}"
171 private_link_service_connections:
172 - name: Test_private_link_service
173 private_link_service_id: /subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privateLinkServices/testervice
174 group_ids:
175 - postgresqlServer
176 subnet:
177 id: /subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/fredvnet/subnets/default
178 application_security_groups:
179 - id: "/subscriptions/xxx/resourceGroups/myResoruceGroup/providers/Microsoft.Network/applicationSecurityGroups/app01"
180 - id: "/subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/applicationSecurityGroups/app02"
181 custom_network_interface_name: nic01
182 ip_configurations:
183 - name: ipc01
184 group_id: postgresqlServer
185 member_name: postgresqlServer
186 private_ip_address: 10.1.0.9
187 custom_dns_configs:
188 - fqdn: testfred001
189 ip_addresses:
190 - 10.1.0.9
191
192 - name: Delete private endpoint
193 azure_rm_privateendpoint:
194 name: testprivateendpoint
195 resource_group: myResourceGroup
196 state: absent
197 '''
198
199
200 RETURN = '''
201 state:
202 description:
203 - List of private endpoint dict with same format as M(azure.azcollection.azure_rm_privateendpoint) module paramter.
204 returned: always
205 type: complex
206 contains:
207 id:
208 description:
209 - Resource ID of the private endpoint.
210 sample: /subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privateEndpoints/testprivateendpoint
211 returned: always
212 type: str
213 etag:
214 description:
215 - A unique read-only string that changes whenever the resource is updated.
216 sample: 'W/\"20803842-7d51-46b2-a790-ded8971b4d8a'
217 returned: always
218 type: str
219 network_interfaces:
220 description:
221 - List ID of the network interfaces.
222 returned: always
223 type: list
224 sample: ["/subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/fredprivateendpoint002.nic"]
225 location:
226 description:
227 - Valid Azure location.
228 returned: always
229 type: str
230 sample: eastus
231 tags:
232 description:
233 - Tags assigned to the resource. Dictionary of string:string pairs.
234 returned: always
235 type: dict
236 sample: { "tag1": "abc" }
237 provisioning_state:
238 description:
239 - Provisioning state of the resource.
240 returned: always
241 sample: Succeeded
242 type: str
243 name:
244 description:
245 - Name of the private endpoint.
246 returned: always
247 type: str
248 sample: estprivateendpoint
249 subnets_id:
250 description:
251 - Subnets associated with the virtual network.
252 returned: always
253 type: str
254 sample: "/subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/fredtestRG-vnet/subnets/default"
255 manual_private_link_service_connections:
256 description:
257 - The resource id of the private endpoint to connect.
258 returned: always
259 type: list
260 sample: ["/subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Network/privateEndpoints/point/privateLinkServiceConnections/point02",]
261 private_link_service_connections:
262 description:
263 - The resource id of the private endpoint to connect.
264 returned: always
265 type: list
266 sample: ["/subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Network/privateEndpoints/point/privateLinkServiceConnections/point",]
267 type:
268 description:
269 - Resource type.
270 returned: always
271 type: str
272 sample: Microsoft.Network/privateEndpoints
273 application_security_groups:
274 description:
275 - The application security group in a resource group.
276 type: complex
277 returned: always
278 contains:
279 id:
280 description:
281 - The application security group's ID.
282 type: str
283 returned: when-used
284 sample: "/subscriptions/xxx/resourceGroups/testRG/providers/Microsoft.Network/applicationSecurityGroups/app01"
285 custom_dns_configs:
286 description:
287 - An array of custom dns configurations.
288 type: complex
289 returned: always
290 contains:
291 fqdn:
292 description:
293 - Fqdn that resolves to private endpoint ip address.
294 type: str
295 returned: when-used
296 sample: "postgresqlsrvprivate02.postgres.database.azure.com"
297 ip_addresses:
298 description:
299 - A list of private ip addresses of the private endpoint.
300 type: complex
301 returned: when-used
302 sample: ["10.1.0.9"]
303 custom_network_interface_name:
304 description:
305 - The custom name of the network interface attached to the private endpoint.
306 type: str
307 returned: always
308 sample: nic01
309 ip_configurations:
310 description:
311 - A list of IP configurations of the private endpoint.
312 - This will be used to map to the First Party Service's endpoints.
313 type: complex
314 returned: always
315 contains:
316 name:
317 description:
318 - The name of the resource that is unique within a resource group.
319 type: str
320 returned: when-used
321 sample: ipc01
322 group_id:
323 description:
324 - The ID of a group obtained from the remote resource that this private endpoint should connect to.
325 type: str
326 returned: when-used
327 sample: postgresqlServer
328 member_name:
329 description:
330 - The member name of a group obtained from the remote resource that this private endpoint should connect to.
331 type: str
332 returned: when-used
333 sample: postgresqlServer
334 private_ip_address:
335 description:
336 - A private ip address obtained from the private endpoint's subnet.
337 type: str
338 returned: when-used
339 sample: 10.1.0.9
340 '''
341
342 try:
343 from azure.core.exceptions import ResourceNotFoundError
344 except ImportError:
345 # This is handled in azure_rm_common
346 pass
347
348 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
349
350
351 network_interfaces_spec = dict(
352 id=dict(type='str')
353 )
354
355
356 manual_private_service_connection_spec = dict(
357 name=dict(type='str'),
358 private_link_service_id=dict(type='str'),
359 group_ids=dict(type='list', elements='str')
360 )
361
362
363 private_service_connection_spec = dict(
364 name=dict(type='str'),
365 private_link_service_id=dict(type='str'),
366 group_ids=dict(type='list', elements='str')
367 )
368
369
370 subnet_spec = dict(
371 id=dict(type='str')
372 )
373
374
375 custom_dns_config_spec = dict(
376 fqdn=dict(type='str'),
377 ip_addresses=dict(type='list', elements='str')
378 )
379
380
381 application_security_group_spec = dict(
382 id=dict(type='str')
383 )
384
385
386 ip_configuration_spec = dict(
387 name=dict(type='str'),
388 group_id=dict(type='str'),
389 member_name=dict(type='str'),
390 private_ip_address=dict(type='str')
391 )
392
393
394 class Actions:
395 NoAction, Create, Update, Delete = range(4)
396
397
398 class AzureRMPrivateEndpoint(AzureRMModuleBaseExt):
399
400 def __init__(self):
401
402 self.module_arg_spec = dict(
403 resource_group=dict(type='str', required=True),
404 name=dict(type='str', required=True),
405 state=dict(type='str', default='present', choices=['present', 'absent']),
406 location=dict(type='str'),
407 subnet=dict(type='dict', options=subnet_spec),
408 private_link_service_connections=dict(type='list', elements='dict', options=private_service_connection_spec),
409 manual_private_link_service_connections=dict(type='list', elements='dict', options=manual_private_service_connection_spec),
410 custom_network_interface_name=dict(type='str'),
411 ip_configurations=dict(type='list', elements='dict', options=ip_configuration_spec),
412 application_security_groups=dict(type='list', elements='dict', options=application_security_group_spec),
413 custom_dns_configs=dict(type='list', elements='dict', options=custom_dns_config_spec)
414 )
415
416 self.resource_group = None
417 self.name = None
418 self.state = None
419 self.location = None
420 self.body = {}
421 self.tags = None
422
423 self.results = dict(
424 changed=False,
425 state=dict()
426 )
427 self.to_do = Actions.NoAction
428 mutually_exclusive = [['private_link_service_connections', 'manual_private_link_service_connections']]
429
430 super(AzureRMPrivateEndpoint, self).__init__(self.module_arg_spec,
431 supports_tags=True,
432 supports_check_mode=True,
433 mutually_exclusive=mutually_exclusive)
434
435 def exec_module(self, **kwargs):
436
437 for key in list(self.module_arg_spec.keys()) + ['tags']:
438 if hasattr(self, key):
439 setattr(self, key, kwargs[key])
440 elif kwargs[key] is not None:
441 self.body[key] = kwargs[key]
442
443 self.inflate_parameters(self.module_arg_spec, self.body, 0)
444
445 resource_group = self.get_resource_group(self.resource_group)
446 if not self.location:
447 # Set default location
448 self.location = resource_group.location
449 self.body['location'] = self.location
450 self.body['tags'] = self.tags
451
452 self.log('Fetching private endpoint {0}'.format(self.name))
453 old_response = self.get_resource()
454
455 if old_response is None:
456 if self.state == "present":
457 self.to_do = Actions.Create
458 else:
459 if self.state == 'absent':
460 self.to_do = Actions.Delete
461 else:
462 update_tags, newtags = self.update_tags(old_response.get('tags', {}))
463 if update_tags:
464 self.body['tags'] = newtags
465 self.to_do = Actions.Update
466
467 if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
468 self.results['changed'] = True
469 if self.check_mode:
470 return self.results
471 response = self.create_update_resource_private_endpoint(self.body)
472 elif self.to_do == Actions.Delete:
473 self.results['changed'] = True
474 if self.check_mode:
475 return self.results
476 response = self.delete_private_endpoint()
477 else:
478 self.results['changed'] = False
479 response = old_response
480 if response is not None:
481 self.results['state'] = response
482 return self.results
483
484 def create_update_resource_private_endpoint(self, privateendpoint):
485 try:
486 poller = self.network_client.private_endpoints.begin_create_or_update(resource_group_name=self.resource_group,
487 private_endpoint_name=self.name, parameters=privateendpoint)
488 new_privateendpoint = self.get_poller_result(poller)
489 except Exception as exc:
490 self.fail("Error creating or updating private endpoint {0} - {1}".format(self.name, str(exc)))
491
492 return self.private_endpoints_to_dict(new_privateendpoint)
493
494 def delete_private_endpoint(self):
495 try:
496 poller = self.network_client.private_endpoints.begin_delete(self.resource_group, self.name)
497 result = self.get_poller_result(poller)
498 except Exception as exc:
499 self.fail("Error deleting private endpoint {0} - {1}".format(self.name, str(exc)))
500 return result
501
502 def get_resource(self):
503 found = False
504 try:
505 private_endpoint = self.network_client.private_endpoints.get(self.resource_group, self.name)
506 results = self.private_endpoints_to_dict(private_endpoint)
507 found = True
508 self.log("Response : {0}".format(results))
509 except ResourceNotFoundError:
510 self.log("Did not find the private endpoint resource")
511 if found is True:
512 return results
513 else:
514 return None
515
516 def private_endpoints_to_dict(self, privateendpoint):
517 results = dict(
518 id=privateendpoint.id,
519 name=privateendpoint.name,
520 location=privateendpoint.location,
521 tags=privateendpoint.tags,
522 provisioning_state=privateendpoint.provisioning_state,
523 type=privateendpoint.type,
524 etag=privateendpoint.etag,
525 subnet=dict(id=privateendpoint.subnet.id),
526 custom_network_interface_name=privateendpoint.custom_network_interface_name,
527 custom_dns_configs=[],
528 application_security_groups=[],
529 ip_configurations=[],
530 )
531 if privateendpoint.network_interfaces and len(privateendpoint.network_interfaces) > 0:
532 results['network_interfaces'] = []
533 for interface in privateendpoint.network_interfaces:
534 results['network_interfaces'].append(interface.id)
535 if privateendpoint.private_link_service_connections and len(privateendpoint.private_link_service_connections) > 0:
536 results['private_link_service_connections'] = []
537 for connections in privateendpoint.private_link_service_connections:
538 results['private_link_service_connections'].append(dict(private_link_service_id=connections.private_link_service_id, name=connections.name))
539 if privateendpoint.manual_private_link_service_connections and len(privateendpoint.manual_private_link_service_connections) > 0:
540 results['manual_private_link_service_connections'] = []
541 for connections in privateendpoint.manual_private_link_service_connections:
542 results['manual_private_link_service_connections'].append(dict(
543 private_link_service_id=connections.private_link_service_id, name=connections.name))
544 if privateendpoint.ip_configurations:
545 for item in privateendpoint.ip_configurations:
546 ip_config = dict(
547 name=item.name,
548 group_id=item.group_id,
549 member_name=item.member_name,
550 private_ip_address=item.private_ip_address
551 )
552 results['ip_configurations'].append(ip_config)
553 if privateendpoint.application_security_groups:
554 for item in privateendpoint.application_security_groups:
555 app_security_group = dict(
556 id=item.id
557 )
558 results['application_security_groups'].append(app_security_group)
559 if privateendpoint.custom_dns_configs:
560 for item in privateendpoint.custom_dns_configs:
561 dns_config = dict(
562 fqdn=item.fqdn,
563 ip_addresses=item.ip_addresses
564 )
565 results['custom_dns_configs'].append(dns_config)
566
567 return results
568
569
570 def main():
571 AzureRMPrivateEndpoint()
572
573
574 if __name__ == '__main__':
575 main()