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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
#!/usr/bin/python
#
# Copyright (c) 2018 Zim Kalinowski, <zikalino@microsoft.com>
#
# 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_resource
version_added: "0.1.2"
short_description: Create any Azure resource
description:
- Create, update or delete any Azure resource using Azure REST API.
- This module gives access to resources that are not supported via Ansible modules.
- Refer to U(https://docs.microsoft.com/en-us/rest/api/) regarding details related to specific resource REST API.
options:
url:
description:
- Azure RM Resource URL.
type: str
api_version:
description:
- Specific API version to be used.
type: str
provider:
description:
- Provider type.
- Required if URL is not specified.
type: str
resource_group:
description:
- Resource group to be used.
- Required if URL is not specified.
type: str
resource_type:
description:
- Resource type.
- Required if URL is not specified.
type: str
resource_name:
description:
- Resource name.
- Required if URL Is not specified.
type: str
subresource:
description:
- List of subresources.
default: []
type: list
elements: dict
suboptions:
namespace:
description:
- Subresource namespace.
type: str
type:
description:
- Subresource type.
type: str
name:
description:
- Subresource name.
type: str
body:
description:
- The body of the HTTP request/response to the web service.
type: raw
method:
description:
- The HTTP method of the request or response. It must be uppercase.
type: str
choices:
- GET
- PUT
- POST
- HEAD
- PATCH
- DELETE
- MERGE
default: "PUT"
status_code:
description:
- A valid, numeric, HTTP status code that signifies success of the request. Can also be comma separated list of status codes.
type: list
elements: int
default: [ 200, 201, 202 ]
idempotency:
description:
- If enabled, idempotency check will be done by using I(method=GET) first and then comparing with I(body).
default: false
type: bool
polling_timeout:
description:
- If enabled, idempotency check will be done by using I(method=GET) first and then comparing with I(body).
default: 0
type: int
polling_interval:
description:
- If enabled, idempotency check will be done by using I(method=GET) first and then comparing with I(body).
default: 60
type: int
state:
description:
- Assert the state of the resource. Use C(present) to create or update resource or C(absent) to delete resource.
default: present
type: str
choices:
- absent
- present
extends_documentation_fragment:
- azure.azcollection.azure
author:
- Zim Kalinowski (@zikalino)
'''
EXAMPLES = '''
- name: Update scaleset info using azure_rm_resource
azure_rm_resource:
resource_group: myResourceGroup
provider: compute
resource_type: virtualmachinescalesets
resource_name: myVmss
api_version: "2017-12-01"
body: { body }
'''
RETURN = '''
response:
description:
- Response specific to resource type.
returned: always
type: complex
contains:
id:
description:
- Resource ID.
type: str
returned: always
sample: "/subscriptions/xxxx...xxxx/resourceGroups/v-xisuRG/providers/Microsoft.Storage/storageAccounts/staccb57dc95183"
kind:
description:
- The kind of storage.
type: str
returned: always
sample: Storage
location:
description:
- The resource location, defaults to location of the resource group.
type: str
returned: always
sample: eastus
name:
description:
The storage account name.
type: str
returned: always
sample: staccb57dc95183
properties:
description:
- The storage account's related properties.
type: dict
returned: always
sample: {
"creationTime": "2019-06-13T06:34:33.0996676Z",
"encryption": {
"keySource": "Microsoft.Storage",
"services": {
"blob": {
"enabled": true,
"lastEnabledTime": "2019-06-13T06:34:33.1934074Z"
},
"file": {
"enabled": true,
"lastEnabledTime": "2019-06-13T06:34:33.1934074Z"
}
}
},
"networkAcls": {
"bypass": "AzureServices",
"defaultAction": "Allow",
"ipRules": [],
"virtualNetworkRules": []
},
"primaryEndpoints": {
"blob": "https://staccb57dc95183.blob.core.windows.net/",
"file": "https://staccb57dc95183.file.core.windows.net/",
"queue": "https://staccb57dc95183.queue.core.windows.net/",
"table": "https://staccb57dc95183.table.core.windows.net/"
},
"primaryLocation": "eastus",
"provisioningState": "Succeeded",
"secondaryLocation": "westus",
"statusOfPrimary": "available",
"statusOfSecondary": "available",
"supportsHttpsTrafficOnly": false
}
sku:
description:
- The storage account SKU.
type: dict
returned: always
sample: {
"name": "Standard_GRS",
"tier": "Standard"
}
tags:
description:
- Resource tags.
type: dict
returned: always
sample: { 'key1': 'value1' }
type:
description:
- The resource type.
type: str
returned: always
sample: "Microsoft.Storage/storageAccounts"
'''
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient
from ansible.module_utils.common.dict_transformations import dict_merge
try:
from azure.mgmt.core.tools import resource_id
import json
except ImportError:
# This is handled in azure_rm_common
pass
class AzureRMResource(AzureRMModuleBase):
def __init__(self):
# define user inputs into argument
self.module_arg_spec = dict(
url=dict(
type='str'
),
provider=dict(
type='str',
),
resource_group=dict(
type='str',
),
resource_type=dict(
type='str',
),
resource_name=dict(
type='str',
),
subresource=dict(
type='list',
elements='dict',
default=[]
),
api_version=dict(
type='str'
),
method=dict(
type='str',
default='PUT',
choices=["GET", "PUT", "POST", "HEAD", "PATCH", "DELETE", "MERGE"]
),
body=dict(
type='raw'
),
status_code=dict(
type='list',
elements='int',
default=[200, 201, 202]
),
idempotency=dict(
type='bool',
default=False
),
polling_timeout=dict(
type='int',
default=0
),
polling_interval=dict(
type='int',
default=60
),
state=dict(
type='str',
default='present',
choices=['present', 'absent']
)
)
# store the results of the module operation
self.results = dict(
changed=False,
response=None
)
self.mgmt_client = None
self.url = None
self.api_version = None
self.provider = None
self.resource_group = None
self.resource_type = None
self.resource_name = None
self.subresource_type = None
self.subresource_name = None
self.subresource = []
self.method = None
self.status_code = []
self.idempotency = False
self.polling_timeout = None
self.polling_interval = None
self.state = None
self.body = None
super(AzureRMResource, self).__init__(self.module_arg_spec, supports_tags=False)
def exec_module(self, **kwargs):
for key in self.module_arg_spec:
setattr(self, key, kwargs[key])
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient,
base_url=self._cloud_environment.endpoints.resource_manager)
if self.state == 'absent':
self.method = 'DELETE'
self.status_code.append(204)
if self.url is None:
orphan = None
rargs = dict()
rargs['subscription'] = self.subscription_id
rargs['resource_group'] = self.resource_group
if not (self.provider is None or self.provider.lower().startswith('.microsoft')):
rargs['namespace'] = "Microsoft." + self.provider
else:
rargs['namespace'] = self.provider
if self.resource_type is not None and self.resource_name is not None:
rargs['type'] = self.resource_type
rargs['name'] = self.resource_name
for i in range(len(self.subresource)):
resource_ns = self.subresource[i].get('namespace', None)
resource_type = self.subresource[i].get('type', None)
resource_name = self.subresource[i].get('name', None)
if resource_type is not None and resource_name is not None:
rargs['child_namespace_' + str(i + 1)] = resource_ns
rargs['child_type_' + str(i + 1)] = resource_type
rargs['child_name_' + str(i + 1)] = resource_name
else:
orphan = resource_type
else:
orphan = self.resource_type
self.url = resource_id(**rargs)
if orphan is not None:
self.url += '/' + orphan
# if api_version was not specified, get latest one
if not self.api_version:
try:
# extract provider and resource type
if "/providers/" in self.url:
provider = self.url.split("/providers/")[1].split("/")[0]
resourceType = self.url.split(provider + "/")[1].split("/")[0]
url = "/subscriptions/" + self.subscription_id + "/providers/" + provider
api_versions = json.loads(self.mgmt_client.query(url, "GET", {'api-version': '2015-01-01'}, None, None, [200], 0, 0).body())
for rt in api_versions['resourceTypes']:
if rt['resourceType'].lower() == resourceType.lower():
self.api_version = rt['apiVersions'][0]
break
else:
# if there's no provider in API version, assume Microsoft.Resources
self.api_version = '2018-05-01'
if not self.api_version:
self.fail("Couldn't find api version for {0}/{1}".format(provider, resourceType))
except Exception as exc:
self.fail("Failed to obtain API version: {0}".format(str(exc)))
query_parameters = {}
query_parameters['api-version'] = self.api_version
header_parameters = {}
header_parameters['Content-Type'] = 'application/json; charset=utf-8'
needs_update = True
response = None
if self.idempotency:
original = self.mgmt_client.query(self.url, "GET", query_parameters, None, None, [200, 404], 0, 0)
if original.status_code == 404:
if self.state == 'absent':
needs_update = False
else:
try:
response = json.loads(original.body())
needs_update = (dict_merge(response, self.body) != response)
except Exception:
pass
if needs_update:
response = self.mgmt_client.query(self.url,
self.method,
query_parameters,
header_parameters,
self.body,
self.status_code,
self.polling_timeout,
self.polling_interval)
if self.state == 'present' and self.method != 'DELETE':
if hasattr(response, 'body'):
try:
response = json.loads(response.body())
except Exception:
response = response.body()
elif hasattr(response, 'context'):
response = response.context['deserialized_data']
else:
self.fail("Create or Updating fail, no match message return, return info as {0}".format(response))
else:
response = None
self.results['response'] = response
self.results['changed'] = needs_update
return self.results
def main():
AzureRMResource()
if __name__ == '__main__':
main()
|