Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_privateendpoint_info.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_info
14
15 version_added: "1.8.0"
16
17 short_description: Get private endpoints info
18
19 description:
20 - Get facts for private endpoints.
21
22 options:
23 name:
24 description:
25 - Name of the private endpoint.
26 type: str
27 resource_group:
28 description:
29 - Name of resource group, limit results by resource group.
30 type: str
31 tags:
32 description:
33 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
34 type: list
35 elements: str
36
37 extends_documentation_fragment:
38 - azure.azcollection.azure
39
40 author:
41 - Fred-sun (@Fred-sun)
42 '''
43
44 EXAMPLES = '''
45 - name: Get facts for one private endpoint
46 azure_rm_privateendpoint_info:
47 resource_group: myResourceGroup
48 name: testprivateendpoint
49
50 - name: Get all private endpoint under the resource group
51 azure_rm_privateendpoint_info:
52 resource_group: myResourceGroup
53
54 - name: Get all private endpoint under subscription
55 azure_rm_virtualnetwork_info:
56 tags:
57 - key1:value1
58 '''
59
60 RETURN = '''
61 state:
62 description:
63 - List of private endpoint dict with same format as M(azure.azcollection.azure_rm_privateendpoint) module paramter.
64 returned: always
65 type: complex
66 contains:
67 id:
68 description:
69 - Resource ID of the private endpoint.
70 sample: /subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privateEndpoints/ped01
71 returned: always
72 type: str
73 etag:
74 description:
75 - A unique read-only string that changes whenever the resource is updated.
76 sample: 'W/\"20803842-7d51-46b2-a790-ded8971b4d8a'
77 returned: always
78 type: str
79 network_interfaces:
80 description:
81 - List ID of the network interfaces.
82 returned: always
83 type: list
84 sample: ["/subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/fredprivateendpoint002.nic"]
85 location:
86 description:
87 - Valid Azure location.
88 returned: always
89 type: str
90 sample: eastus
91 tags:
92 description:
93 - Tags assigned to the resource. Dictionary of string:string pairs.
94 returned: always
95 type: dict
96 sample: { "tag1": "abc" }
97 provisioning_state:
98 description:
99 - Provisioning state of the resource.
100 returned: always
101 sample: Succeeded
102 type: str
103 name:
104 description:
105 - Name of the private endpoint.
106 returned: always
107 type: str
108 sample: ped01
109 subnets_id:
110 description:
111 - Subnets associated with the virtual network.
112 returned: always
113 type: str
114 sample: "/subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/fredtestRG-vnet/subnets/default"
115 manual_private_link_service_connections:
116 description:
117 - The resource id of the private endpoint to connect.
118 returned: when-used
119 type: complex
120 contains:
121 id:
122 description:
123 - The resource id of the private endpoint to connect.
124 returned: always
125 type: str
126 sample: "/subscriptions/xxx/resourceGroups/testRG/providers/Microsoft.Network/privateEndpoints/ped01/privateLinkServiceConnections/ped01"
127 name:
128 description:
129 - The name of the private endpoint connection.
130 returned: always
131 type: str
132 sample: ped_name01
133 connection_state:
134 description:
135 - State details of endpoint connection
136 type: complex
137 returned: always
138 contains:
139 description:
140 description:
141 - The reason for approval/rejection of the connection.
142 returned: always
143 type: str
144 sample: "Auto Approved"
145 status:
146 description:
147 - Indicates whether the connection has been Approved/Rejected/Removed by the owner of the service.
148 returned: always
149 type: str
150 sample: Approved
151 actions_required:
152 description:
153 - A message indicating if changes on the service provider require any updates on the consumer.
154 type: str
155 returned: always
156 sample: "This is action_required string"
157 private_link_service_connections:
158 description:
159 - The resource id of the private endpoint to connect.
160 returned: when-used
161 type: complex
162 contains:
163 id:
164 description:
165 - The resource id of the private endpoint to connect.
166 returned: always
167 type: str
168 sample: "/subscriptions/xxx/resourceGroups/testRG/providers/Microsoft.Network/privateEndpoints/ped01/privateLinkServiceConnections/ped02"
169 name:
170 description:
171 - The name of the private endpoint connection.
172 returned: always
173 type: str
174 sample: ped_name02
175 connection_state:
176 description:
177 - State details of endpoint connection
178 type: complex
179 returned: always
180 contains:
181 description:
182 description:
183 - The reason for approval/rejection of the connection.
184 returned: always
185 type: str
186 sample: "Auto Approved"
187 status:
188 description:
189 - Indicates whether the connection has been Approved/Rejected/Removed by the owner of the service.
190 returned: always
191 type: str
192 sample: Approved
193 actions_required:
194 description:
195 - A message indicating if changes on the service provider require any updates on the consumer.
196 type: str
197 returned: always
198 sample: "This is action_required string"
199 group_ids:
200 description:
201 - List of group_ids associated with private endpoint
202 returned: always
203 type: list
204 sample: ["postgresqlServer"]
205 type:
206 description:
207 - Resource type.
208 returned: always
209 type: str
210 sample: Microsoft.Network/privateEndpoints
211 application_security_groups:
212 description:
213 - The application security group in a resource group.
214 type: complex
215 returned: always
216 contains:
217 id:
218 description:
219 - The application security group's ID.
220 type: str
221 returned: when-used
222 sample: "/subscriptions/xxx/resourceGroups/testRG/providers/Microsoft.Network/applicationSecurityGroups/app01"
223 custom_dns_configs:
224 description:
225 - An array of custom dns configurations.
226 type: complex
227 returned: always
228 contains:
229 fqdn:
230 description:
231 - Fqdn that resolves to private endpoint ip address.
232 type: str
233 returned: when-used
234 sample: "postgresqlsrvprivate02.postgres.database.azure.com"
235 ip_addresses:
236 description:
237 - A list of private ip addresses of the private endpoint.
238 type: complex
239 returned: when-used
240 sample: ["10.1.0.9"]
241 custom_network_interface_name:
242 description:
243 - The custom name of the network interface attached to the private endpoint.
244 type: str
245 returned: always
246 sample: nic01
247 ip_configurations:
248 description:
249 - A list of IP configurations of the private endpoint.
250 - This will be used to map to the First Party Service's endpoints.
251 type: complex
252 returned: always
253 contains:
254 name:
255 description:
256 - The name of the resource that is unique within a resource group.
257 type: str
258 returned: when-used
259 sample: ipc01
260 group_id:
261 description:
262 - The ID of a group obtained from the remote resource that this private endpoint should connect to.
263 type: str
264 returned: when-used
265 sample: postgresqlServer
266 member_name:
267 description:
268 - The member name of a group obtained from the remote resource that this private endpoint should connect to.
269 type: str
270 returned: when-used
271 sample: postgresqlServer
272 private_ip_address:
273 description:
274 - A private ip address obtained from the private endpoint's subnet.
275 type: str
276 returned: when-used
277 sample: 10.1.0.9
278 '''
279
280 try:
281 from azure.core.exceptions import ResourceNotFoundError
282 except Exception:
283 # This is handled in azure_rm_common
284 pass
285
286 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
287
288
289 class AzureRMPrivateEndpointInfo(AzureRMModuleBase):
290
291 def __init__(self):
292
293 self.module_arg_spec = dict(
294 name=dict(type='str'),
295 resource_group=dict(type='str'),
296 tags=dict(type='list', elements='str')
297 )
298
299 self.results = dict(
300 changed=False,
301 privateendpoints=[]
302 )
303
304 self.name = None
305 self.resource_group = None
306 self.tags = None
307 self.results = dict(
308 changed=False
309 )
310
311 super(AzureRMPrivateEndpointInfo, self).__init__(self.module_arg_spec,
312 supports_check_mode=True,
313 supports_tags=False,
314 facts_module=True)
315
316 def exec_module(self, **kwargs):
317 for key in list(self.module_arg_spec.keys()) + ['tags']:
318 setattr(self, key, kwargs[key])
319
320 if self.name is not None:
321 self.results['privateendpoints'] = self.get_item()
322 elif self.resource_group is not None:
323 self.results['privateendpoints'] = self.list_resource_group()
324 else:
325 self.results['privateendpoints'] = self.list_items()
326
327 return self.results
328
329 def get_item(self):
330 self.log('Get properties for {0}'.format(self.name))
331 item = None
332 results = []
333
334 try:
335 item = self.network_client.private_endpoints.get(self.resource_group, self.name)
336 except ResourceNotFoundError:
337 self.log('Could not get info for @(Model.ModuleOperationNameUpper).')
338 format_item = self.privateendpoints_to_dict(item)
339
340 if format_item and self.has_tags(format_item['tags'], self.tags):
341 results = [format_item]
342 return results
343
344 def list_resource_group(self):
345 self.log('List items for resource group')
346 try:
347 response = self.network_client.private_endpoints.list(self.resource_group)
348 except ResourceNotFoundError as exc:
349 self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))
350
351 results = []
352 for item in response:
353 format_item = self.privateendpoints_to_dict(item)
354 if self.has_tags(format_item['tags'], self.tags):
355 results.append(format_item)
356 return results
357
358 def list_items(self):
359 self.log('List all for items')
360 try:
361 response = self.network_client.private_endpoints.list_by_subscription()
362 except ResourceNotFoundError as exc:
363 self.fail("Failed to list all items - {0}".format(str(exc)))
364
365 results = []
366 for item in response:
367 format_item = self.privateendpoints_to_dict(item)
368 if self.has_tags(format_item['tags'], self.tags):
369 results.append(format_item)
370 return results
371
372 def privateendpoints_to_dict(self, privateendpoint):
373 if privateendpoint is None:
374 return None
375 results = dict(
376 id=privateendpoint.id,
377 name=privateendpoint.name,
378 location=privateendpoint.location,
379 tags=privateendpoint.tags,
380 provisioning_state=privateendpoint.provisioning_state,
381 type=privateendpoint.type,
382 etag=privateendpoint.etag,
383 subnet_id=privateendpoint.subnet.id,
384 custom_network_interface_name=privateendpoint.custom_network_interface_name,
385 custom_dns_configs=[],
386 application_security_groups=[],
387 ip_configurations=[],
388 )
389 if privateendpoint.network_interfaces and len(privateendpoint.network_interfaces) > 0:
390 results['network_interfaces'] = []
391 for interface in privateendpoint.network_interfaces:
392 results['network_interfaces'].append(interface.id)
393 if privateendpoint.private_link_service_connections and len(privateendpoint.private_link_service_connections) > 0:
394 results['private_link_service_connections'] = []
395 for connections in privateendpoint.private_link_service_connections:
396 connection = {}
397 connection['connection_state'] = {}
398 connection['id'] = connections.id
399 connection['name'] = connections.name
400 connection['type'] = connections.type
401 connection['group_ids'] = connections.group_ids
402 connection['connection_state']['status'] = connections.private_link_service_connection_state.status
403 connection['connection_state']['description'] = connections.private_link_service_connection_state.description
404 connection['connection_state']['actions_required'] = connections.private_link_service_connection_state.actions_required
405 results['private_link_service_connections'].append(connection)
406 if privateendpoint.manual_private_link_service_connections and len(privateendpoint.manual_private_link_service_connections) > 0:
407 results['manual_private_link_service_connections'] = []
408 for connections in privateendpoint.manual_private_link_service_connections:
409 connection = {}
410 connection['connection_state'] = {}
411 connection['id'] = connections.id
412 connection['name'] = connections.name
413 connection['type'] = connections.type
414 connection['group_ids'] = connections.group_ids
415 connection['connection_state']['status'] = connections.manual_private_link_service_connection_state.status
416 connection['connection_state']['description'] = connections.manual_private_link_service_connection_state.description
417 connection['connection_state']['actions_required'] = connections.manual_private_link_service_connection_state.actions_required
418 results['manual_private_link_service_connections'].append(connection)
419 if privateendpoint.ip_configurations:
420 for item in privateendpoint.ip_configurations:
421 ip_config = dict(
422 name=item.name,
423 group_id=item.group_id,
424 member_name=item.member_name,
425 private_ip_address=item.private_ip_address
426 )
427 results['ip_configurations'].append(ip_config)
428 if privateendpoint.application_security_groups:
429 for item in privateendpoint.application_security_groups:
430 app_security_group = dict(
431 id=item.id
432 )
433 results['application_security_groups'].append(app_security_group)
434 if privateendpoint.custom_dns_configs:
435 for item in privateendpoint.custom_dns_configs:
436 dns_config = dict(
437 fqdn=item.fqdn,
438 ip_addresses=item.ip_addresses
439 )
440 results['custom_dns_configs'].append(dns_config)
441
442 return results
443
444
445 def main():
446 AzureRMPrivateEndpointInfo()
447
448
449 if __name__ == '__main__':
450 main()