Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_privateendpointconnection.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_privateendpointconnection
15
16 version_added: "1.12.0"
17
18 short_description: Managed private endpoint connection
19
20 description:
21 - Update or delete the private endpoint connection.
22
23 options:
24 name:
25 description:
26 - The name of the private end point connection.
27 type: str
28 required: True
29 service_name:
30 description:
31 - The name of the private link service.
32 type: str
33 required: true
34 resource_group:
35 description:
36 - The name of the resource group.
37 type: str
38 required: true
39 connection_state:
40 description:
41 - A collection of information about the state of the connection between service consumer and provider.
42 type: dict
43 suboptions:
44 status:
45 description:
46 - Indicates whether the connection has been Approved/Rejected/Removed by the owner of the service.
47 type: str
48 choices:
49 - Approved
50 - Rejected
51 - Removed
52 description:
53 description:
54 - The reason for approval/rejection of the connection.
55 type: str
56 actions_required:
57 description:
58 - A message indicating if changes on the service provider require any updates on the consumer.
59 type: str
60 state:
61 description:
62 - Assert the state of the connection. Use C(present) to update an connection and C(absent) to delete it.
63 default: present
64 type: str
65 choices:
66 - absent
67 - present
68
69 extends_documentation_fragment:
70 - azure.azcollection.azure
71
72 author:
73 - xuzhang3 (@xuzhang3)
74 - Fred-sun (@Fred-sun)
75 '''
76
77 EXAMPLES = '''
78 - name: Update private endpoint connection
79 azure_rm_privateendpointconnection_info:
80 name: pe-connection-name
81 service_name: testserviceName
82 resource_group: myRG
83 connection_state:
84 description: "new_description string"
85 actions_required: "Message string"
86 status: "Rejected"
87
88 - name: Delee private endpoint connection
89 azure_rm_privateendpointconnection_info:
90 name: pe-connection-name
91 service_name: testserviceName
92 resource_group: myRG
93 state: absent
94 '''
95
96 RETURN = '''
97 state:
98 description:
99 - List of private endpoint connection info.
100 returned: always
101 type: complex
102 contains:
103 id:
104 description:
105 - Resource ID of the private endpoint connection.
106 returned: always
107 type: str
108 sample: "/subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Network/privateLinkServices/linkservice/privateEndpointConnections/link.09"
109 name:
110 description:
111 - Name of the private endpoint connection.
112 returned: always
113 type: str
114 sample: testlink.09
115 link_identifier:
116 description:
117 - The consumer link id.
118 returned: always
119 type: str
120 sample: 536890208
121 PrivateEndpoint:
122 description:
123 - The resource of private end point.
124 type: complex
125 returned: always
126 contains:
127 id:
128 description:
129 - The private endpoint resource ID.
130 type: str
131 returned: always
132 sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/privateEndpoints/testlink02"
133 private_link_service_connection_state:
134 description:
135 - A collection of information about the state of the connection between service consumer and provider.
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 provisioning_state:
158 description:
159 - Provisioning state of the resource.
160 returned: always
161 type: str
162 sample: Succeeded
163 etag:
164 description:
165 - A unique read-only string that changes whenever the resource is updated.
166 type: str
167 returned: always
168 sample: "f7d60f37-ea2b-4091-8546-1327f35468c4"
169 type:
170 description:
171 - The resource type.
172 type: str
173 returned: always
174 sample: Microsoft.Network/privateLinkServices/privateEndpointConnections
175 '''
176
177 try:
178 from azure.core.exceptions import ResourceNotFoundError
179 from azure.core.polling import LROPoller
180 except Exception:
181 # This is handled in azure_rm_common
182 pass
183
184 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
185
186
187 connection_state_spec = dict(
188 status=dict(type='str', choices=['Approved', 'Rejected', 'Removed']),
189 description=dict(type='str'),
190 actions_required=dict(type='str')
191 )
192
193
194 class AzureRMPrivateEndpointConnection(AzureRMModuleBase):
195
196 def __init__(self):
197
198 self.module_arg_spec = dict(
199 name=dict(type="str", required=True),
200 service_name=dict(type="str", required=True),
201 resource_group=dict(type="str", required=True),
202 connection_state=dict(type='dict', options=connection_state_spec),
203 state=dict(type='str', choices=['present', 'absent'], default='present'),
204 )
205
206 self.name = None
207 self.service_name = None
208 self.resource_group = None
209 self.connection_state = None
210 self.results = dict(
211 changed=False,
212 )
213
214 super(AzureRMPrivateEndpointConnection, self).__init__(self.module_arg_spec,
215 supports_check_mode=True,
216 supports_tags=False,
217 facts_module=False)
218
219 def exec_module(self, **kwargs):
220 for key in list(self.module_arg_spec.keys()):
221 setattr(self, key, kwargs[key])
222
223 old_response = self.get_resource()
224 response = None
225 changed = False
226
227 if self.state == 'present':
228 if old_response:
229 if self.connection_state is not None:
230
231 if self.connection_state.get('status') is not None:
232 if self.connection_state.get('status') != old_response['private_link_service_connection_state']['status']:
233 changed = True
234 else:
235 self.connection_state['status'] = old_response['private_link_service_connection_state']['status']
236
237 if self.connection_state.get('description'):
238 if self.connection_state.get('description') != old_response['private_link_service_connection_state']['description']:
239 changed = True
240 else:
241 self.connection_state['description'] = old_response['private_link_service_connection_state']['description']
242 if self.connection_state.get('actions_required'):
243 if self.connection_state.get('actions_required') != old_response['private_link_service_connection_state']['actions_required']:
244 changed = True
245 else:
246 self.connection_state['actions_required'] = old_response['private_link_service_connection_state']['actions_required']
247
248 if changed:
249 if self.check_mode:
250 self.log("The private endpoint connection is exist, will be updated")
251 else:
252 parameters = {'private_link_service_connection_state': self.connection_state}
253 response = self.update_resource(parameters)
254 if response:
255 response = self.connect_to_dict(response)
256 else:
257 if self.check_mode:
258 self.log("Check mode test. The private endpoint connection is exist, No operation in this task")
259 else:
260 response = old_response
261 self.log("The private endpoint connection is exist, No operation in this task")
262 else:
263 if self.check_mode:
264 changed = True
265 self.log("The private endpoint conneection is not exist, will be created, but this module not support create funciont")
266 else:
267 self.fail("The private endpoint connection {0} isn't exist, This Module not support create".format(self.name))
268 else:
269 if old_response:
270 changed = True
271 if self.check_mode:
272 self.log("The private endpoint conneection is exist, will be deleted")
273 else:
274 self.delete_resource()
275 else:
276 if self.check_mode:
277 self.log("The private endpoint connection isn't exist, no action")
278 else:
279 self.log("The private endpoint connection isn't exist, don't need to delete")
280
281 self.results['changed'] = changed
282 self.results['state'] = response
283 return self.results
284
285 def get_resource(self):
286 self.log("Get properties for {0} in {1}".format(self.name, self.service_name))
287 try:
288 response = self.network_client.private_link_services.get_private_endpoint_connection(self.resource_group, self.service_name, self.name)
289 return self.connect_to_dict(response)
290 except ResourceNotFoundError:
291 self.log("Could not get info for {0} in {1}".format(self.name, self.service_name))
292
293 return []
294
295 def update_resource(self, parameters):
296 self.log("Update the private endpoint connection for {0} in {1}".format(self.name, self.service_name))
297 try:
298 response = self.network_client.private_link_services.update_private_endpoint_connection(self.resource_group,
299 self.service_name,
300 self.name, parameters)
301 if isinstance(response, LROPoller):
302 response = self.get_poller_result(response)
303 return response
304 except Exception:
305 self.log("Update {0} in {1} failed".format(self.name, self.service_name))
306
307 return []
308
309 def delete_resource(self):
310 self.log("delete the private endpoint connection for {0} in {1}".format(self.name, self.service_name))
311 try:
312 response = self.network_client.private_link_services.begin_delete_private_endpoint_connection(self.resource_group, self.service_name, self.name)
313 if isinstance(response, LROPoller):
314 response = self.get_poller_result(response)
315 return response
316 except Exception:
317 self.log("Delete {0} in {1} failed".format(self.name, self.service_name))
318
319 return []
320
321 def connect_to_dict(self, connect_info):
322 connect = connect_info.as_dict()
323 result = dict(
324 id=connect.get("id"),
325 name=connect.get('name'),
326 type=connect.get('type'),
327 etag=connect.get('etag'),
328 private_endpoint=dict(),
329 private_link_service_connection_state=dict(),
330 provisioning_state=connect.get('provisioning_state'),
331 link_identifier=connect.get('link_identifier')
332 )
333 if connect.get('private_endpoint') is not None:
334 result['private_endpoint']['id'] = connect.get('private_endpoint')['id']
335
336 if connect.get('private_link_service_connection_state') is not None:
337 result['private_link_service_connection_state']['status'] = connect.get('private_link_service_connection_state')['status']
338 result['private_link_service_connection_state']['description'] = connect.get('private_link_service_connection_state')['description']
339 result['private_link_service_connection_state']['actions_required'] = connect.get('private_link_service_connection_state')['actions_required']
340 return result
341
342
343 def main():
344 AzureRMPrivateEndpointConnection()
345
346
347 if __name__ == "__main__":
348 main()