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
|
#!/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
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: azure_rm_resource
version_added: "2.6"
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 https://docs.microsoft.com/en-us/rest/api/ regarding details related to specific resource REST API.
options:
url:
description:
- Azure RM Resource URL.
api_version:
description:
- Specific API version to be used.
provider:
description:
- Provider type.
- Required if URL is not specified.
resource_group:
description:
- Resource group to be used.
- Required if URL is not specified.
resource_type:
description:
- Resource type.
- Required if URL is not specified.
resource_name:
description:
- Resource name.
- Required if URL Is not specified.
subresource:
description:
- List of subresources
suboptions:
namespace:
description:
- Subresource namespace
type:
description:
- Subresource type
name:
description:
- Subresource name
body:
description:
- The body of the http request/response to the web service.
method:
description:
- The HTTP method of the request or response. It MUST be uppercase.
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.
default: [ 200, 201, 202 ]
idempotency:
description:
- If enabled, idempotency check will be done by using GET method first and then comparing with I(body)
default: no
type: bool
polling_timeout:
description:
- If enabled, idempotency check will be done by using GET method first and then comparing with I(body)
default: 0
type: int
version_added: "2.8"
polling_interval:
description:
- If enabled, idempotency check will be done by using GET method first and then comparing with I(body)
default: 60
type: int
version_added: "2.8"
state:
description:
- Assert the state of the resource. Use C(present) to create or update resource or C(absent) to delete resource.
default: present
choices:
- absent
- present
extends_documentation_fragment:
- 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: dict
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.azure_rm_common_rest import GenericRestClient
from ansible.module_utils.common.dict_transformations import dict_merge
try:
from msrestazure.azure_exceptions import CloudError
from msrest.service_client import ServiceClient
from msrestazure.tools import resource_id, is_valid_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',
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',
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).text)
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.text)
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':
try:
response = json.loads(response.text)
except Exception:
response = response.text
else:
response = None
self.results['response'] = response
self.results['changed'] = needs_update
return self.results
def main():
AzureRMResource()
if __name__ == '__main__':
main()
|