1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
#!/usr/bin/python
#
# Copyright (c) 2020 Fred-Sun, (@Fred-Sun)
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: azure_rm_privateendpoint
version_added: "1.8.0"
short_description: Manage Azure private endpoint
description:
- Create, update or delete a private endpoint.
options:
resource_group:
description:
- Name of resource group.
required: true
type: str
location:
description:
- Valid Azure location. Defaults to location of the resource group.
type: str
name:
description:
- Name of the private endpoint.
required: true
type: str
subnet:
description:
- The ID of the subnet from which the private IP will be allocated.
- This parameter is required for create or update.
type: dict
suboptions:
id:
description:
- The ID of the subnet from which the private IP will be allocated.
type: str
private_link_service_connections:
description:
- A grouping of information about the connection to the remote resource.
- This parameter is required for create or update.
type: list
elements: dict
suboptions:
name:
description:
- The name of the resource that is unique within a resource group.
type: str
private_link_service_id:
description:
- The resource id of the private endpoint to connect to.
type: str
group_ids:
description:
- The ID(s) of the group(s) obtained from the remote resource that this private endpoint should connect to.
type: list
elements: str
state:
description:
- State of the virtual network. Use C(present) to create or update and C(absent) to delete.
default: present
type: str
choices:
- absent
- present
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
author:
- Fred-sun (@Fred-sun)
'''
EXAMPLES = '''
- name: Create private endpoint
azure_rm_privateendpoint:
name: testprivateendpoint
resource_group: v-xisuRG
private_link_service_connections:
- name: Test_private_link_service
private_link_service_id: /subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privateLinkServices/testervice
subnet:
id: /subscriptions/xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/fredvnet/subnets/default
tags:
key1: value1
key2: value2
- name: Delete private endpoint
azure_rm_privateendpoint:
name: testprivateendpoint
resource_group: myResourceGroup
state: absent
'''
RETURN = '''
state:
description:
- List of private endpoint dict with same format as M(azure.azcollection.azure_rm_privateendpoint) module paramter.
returned: always
type: complex
contains:
id:
description:
- Resource ID of the private endpoint.
sample: /subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/privateEndpoints/testprivateendpoint
returned: always
type: str
etag:
description:
- A unique read-only string that changes whenever the resource is updated.
sample: 'W/\"20803842-7d51-46b2-a790-ded8971b4d8a'
returned: always
type: str
network_interfaces:
description:
- List ID of the network interfaces.
returned: always
type: list
sample: ["/subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/fredprivateendpoint002.nic"]
location:
description:
- Valid Azure location.
returned: always
type: str
sample: eastus
tags:
description:
- Tags assigned to the resource. Dictionary of string:string pairs.
returned: always
type: dict
sample: { "tag1": "abc" }
provisioning_state:
description:
- Provisioning state of the resource.
returned: always
sample: Succeeded
type: str
name:
description:
- Name of the private endpoint.
returned: always
type: str
sample: estprivateendpoint
subnets_id:
description:
- Subnets associated with the virtual network.
returned: always
type: str
sample: "/subscriptions/xxx-xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/fredtestRG-vnet/subnets/default"
private_link_service_connections:
description:
- The resource id of the private endpoint to connect.
returned: always
type: list
sample: ["/subscriptions/xxx/resourceGroups/myRG/providers/Microsoft.Network/privateEndpoints/point/privateLinkServiceConnections/point",]
type:
description:
- Resource type.
returned: always
type: str
sample: Microsoft.Network/privateEndpoints
'''
try:
from azure.core.exceptions import ResourceNotFoundError
except ImportError:
# This is handled in azure_rm_common
pass
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
network_interfaces_spec = dict(
id=dict(type='str')
)
private_service_connection_spec = dict(
name=dict(type='str'),
private_link_service_id=dict(type='str'),
group_ids=dict(type='list', elements='str')
)
subnet_spec = dict(
id=dict(type='str')
)
class Actions:
NoAction, Create, Update, Delete = range(4)
class AzureRMPrivateEndpoint(AzureRMModuleBaseExt):
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(type='str', required=True),
name=dict(type='str', required=True),
state=dict(type='str', default='present', choices=['present', 'absent']),
location=dict(type='str'),
subnet=dict(type='dict', options=subnet_spec),
private_link_service_connections=dict(type='list', elements='dict', options=private_service_connection_spec),
)
self.resource_group = None
self.name = None
self.state = None
self.location = None
self.body = {}
self.tags = None
self.results = dict(
changed=False,
state=dict()
)
self.to_do = Actions.NoAction
super(AzureRMPrivateEndpoint, self).__init__(self.module_arg_spec,
supports_tags=True,
supports_check_mode=True)
def exec_module(self, **kwargs):
for key in list(self.module_arg_spec.keys()) + ['tags']:
if hasattr(self, key):
setattr(self, key, kwargs[key])
elif kwargs[key] is not None:
self.body[key] = kwargs[key]
self.inflate_parameters(self.module_arg_spec, self.body, 0)
resource_group = self.get_resource_group(self.resource_group)
if not self.location:
# Set default location
self.location = resource_group.location
self.body['location'] = self.location
self.body['tags'] = self.tags
self.log('Fetching private endpoint {0}'.format(self.name))
old_response = self.get_resource()
if old_response is None:
if self.state == "present":
self.to_do = Actions.Create
else:
if self.state == 'absent':
self.to_do = Actions.Delete
else:
update_tags, newtags = self.update_tags(old_response.get('tags', {}))
if update_tags:
self.body['tags'] = newtags
self.to_do = Actions.Update
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
self.results['changed'] = True
if self.check_mode:
return self.results
response = self.create_update_resource_private_endpoint(self.body)
elif self.to_do == Actions.Delete:
self.results['changed'] = True
if self.check_mode:
return self.results
response = self.delete_private_endpoint()
else:
self.results['changed'] = False
response = old_response
if response is not None:
self.results['state'] = response
return self.results
def create_update_resource_private_endpoint(self, privateendpoint):
try:
poller = self.network_client.private_endpoints.begin_create_or_update(resource_group_name=self.resource_group,
private_endpoint_name=self.name, parameters=privateendpoint)
new_privateendpoint = self.get_poller_result(poller)
except Exception as exc:
self.fail("Error creating or updating private endpoint {0} - {1}".format(self.name, str(exc)))
return self.private_endpoints_to_dict(new_privateendpoint)
def delete_private_endpoint(self):
try:
poller = self.network_client.private_endpoints.begin_delete(self.resource_group, self.name)
result = self.get_poller_result(poller)
except Exception as exc:
self.fail("Error deleting private endpoint {0} - {1}".format(self.name, str(exc)))
return result
def get_resource(self):
found = False
try:
private_endpoint = self.network_client.private_endpoints.get(self.resource_group, self.name)
results = self.private_endpoints_to_dict(private_endpoint)
found = True
self.log("Response : {0}".format(results))
except ResourceNotFoundError:
self.log("Did not find the private endpoint resource")
if found is True:
return results
else:
return None
def private_endpoints_to_dict(self, privateendpoint):
results = dict(
id=privateendpoint.id,
name=privateendpoint.name,
location=privateendpoint.location,
tags=privateendpoint.tags,
provisioning_state=privateendpoint.provisioning_state,
type=privateendpoint.type,
etag=privateendpoint.etag,
subnet=dict(id=privateendpoint.subnet.id)
)
if privateendpoint.network_interfaces and len(privateendpoint.network_interfaces) > 0:
results['network_interfaces'] = []
for interface in privateendpoint.network_interfaces:
results['network_interfaces'].append(interface.id)
if privateendpoint.private_link_service_connections and len(privateendpoint.private_link_service_connections) > 0:
results['private_link_service_connections'] = []
for connections in privateendpoint.private_link_service_connections:
results['private_link_service_connections'].append(dict(private_link_service_id=connections.private_link_service_id, name=connections.name))
return results
def main():
AzureRMPrivateEndpoint()
if __name__ == '__main__':
main()
|