| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_resource |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Create any Azure resource |
| 16 |
description: |
| 17 |
- Create, update or delete any Azure resource using Azure REST API. |
| 18 |
- This module gives access to resources that are not supported via Ansible modules. |
| 19 |
- Refer to U(https://docs.microsoft.com/en-us/rest/api/) regarding details related to specific resource REST API. |
| 20 |
|
| 21 |
options: |
| 22 |
url: |
| 23 |
description: |
| 24 |
- Azure RM Resource URL. |
| 25 |
type: str |
| 26 |
api_version: |
| 27 |
description: |
| 28 |
- Specific API version to be used. |
| 29 |
type: str |
| 30 |
provider: |
| 31 |
description: |
| 32 |
- Provider type. |
| 33 |
- Required if URL is not specified. |
| 34 |
type: str |
| 35 |
resource_group: |
| 36 |
description: |
| 37 |
- Resource group to be used. |
| 38 |
- Required if URL is not specified. |
| 39 |
type: str |
| 40 |
resource_type: |
| 41 |
description: |
| 42 |
- Resource type. |
| 43 |
- Required if URL is not specified. |
| 44 |
type: str |
| 45 |
resource_name: |
| 46 |
description: |
| 47 |
- Resource name. |
| 48 |
- Required if URL Is not specified. |
| 49 |
type: str |
| 50 |
subresource: |
| 51 |
description: |
| 52 |
- List of subresources. |
| 53 |
default: [] |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
suboptions: |
| 57 |
namespace: |
| 58 |
description: |
| 59 |
- Subresource namespace. |
| 60 |
type: str |
| 61 |
type: |
| 62 |
description: |
| 63 |
- Subresource type. |
| 64 |
type: str |
| 65 |
name: |
| 66 |
description: |
| 67 |
- Subresource name. |
| 68 |
type: str |
| 69 |
body: |
| 70 |
description: |
| 71 |
- The body of the HTTP request/response to the web service. |
| 72 |
type: raw |
| 73 |
method: |
| 74 |
description: |
| 75 |
- The HTTP method of the request or response. It must be uppercase. |
| 76 |
type: str |
| 77 |
choices: |
| 78 |
- GET |
| 79 |
- PUT |
| 80 |
- POST |
| 81 |
- HEAD |
| 82 |
- PATCH |
| 83 |
- DELETE |
| 84 |
- MERGE |
| 85 |
default: "PUT" |
| 86 |
status_code: |
| 87 |
description: |
| 88 |
- A valid, numeric, HTTP status code that signifies success of the request. Can also be comma separated list of status codes. |
| 89 |
type: list |
| 90 |
elements: int |
| 91 |
default: [ 200, 201, 202 ] |
| 92 |
idempotency: |
| 93 |
description: |
| 94 |
- If enabled, idempotency check will be done by using I(method=GET) first and then comparing with I(body). |
| 95 |
default: false |
| 96 |
type: bool |
| 97 |
polling_timeout: |
| 98 |
description: |
| 99 |
- If enabled, idempotency check will be done by using I(method=GET) first and then comparing with I(body). |
| 100 |
default: 0 |
| 101 |
type: int |
| 102 |
polling_interval: |
| 103 |
description: |
| 104 |
- If enabled, idempotency check will be done by using I(method=GET) first and then comparing with I(body). |
| 105 |
default: 60 |
| 106 |
type: int |
| 107 |
state: |
| 108 |
description: |
| 109 |
- Assert the state of the resource. Use C(present) to create or update resource or C(absent) to delete resource. |
| 110 |
default: present |
| 111 |
type: str |
| 112 |
choices: |
| 113 |
- absent |
| 114 |
- present |
| 115 |
|
| 116 |
extends_documentation_fragment: |
| 117 |
- azure.azcollection.azure |
| 118 |
|
| 119 |
author: |
| 120 |
- Zim Kalinowski (@zikalino) |
| 121 |
|
| 122 |
''' |
| 123 |
|
| 124 |
EXAMPLES = ''' |
| 125 |
- name: Update scaleset info using azure_rm_resource |
| 126 |
azure_rm_resource: |
| 127 |
resource_group: myResourceGroup |
| 128 |
provider: compute |
| 129 |
resource_type: virtualmachinescalesets |
| 130 |
resource_name: myVmss |
| 131 |
api_version: "2017-12-01" |
| 132 |
body: { body } |
| 133 |
''' |
| 134 |
|
| 135 |
RETURN = ''' |
| 136 |
response: |
| 137 |
description: |
| 138 |
- Response specific to resource type. |
| 139 |
returned: always |
| 140 |
type: complex |
| 141 |
contains: |
| 142 |
id: |
| 143 |
description: |
| 144 |
- Resource ID. |
| 145 |
type: str |
| 146 |
returned: always |
| 147 |
sample: "/subscriptions/xxxx...xxxx/resourceGroups/v-xisuRG/providers/Microsoft.Storage/storageAccounts/staccb57dc95183" |
| 148 |
kind: |
| 149 |
description: |
| 150 |
- The kind of storage. |
| 151 |
type: str |
| 152 |
returned: always |
| 153 |
sample: Storage |
| 154 |
location: |
| 155 |
description: |
| 156 |
- The resource location, defaults to location of the resource group. |
| 157 |
type: str |
| 158 |
returned: always |
| 159 |
sample: eastus |
| 160 |
name: |
| 161 |
description: |
| 162 |
The storage account name. |
| 163 |
type: str |
| 164 |
returned: always |
| 165 |
sample: staccb57dc95183 |
| 166 |
properties: |
| 167 |
description: |
| 168 |
- The storage account's related properties. |
| 169 |
type: dict |
| 170 |
returned: always |
| 171 |
sample: { |
| 172 |
"creationTime": "2019-06-13T06:34:33.0996676Z", |
| 173 |
"encryption": { |
| 174 |
"keySource": "Microsoft.Storage", |
| 175 |
"services": { |
| 176 |
"blob": { |
| 177 |
"enabled": true, |
| 178 |
"lastEnabledTime": "2019-06-13T06:34:33.1934074Z" |
| 179 |
}, |
| 180 |
"file": { |
| 181 |
"enabled": true, |
| 182 |
"lastEnabledTime": "2019-06-13T06:34:33.1934074Z" |
| 183 |
} |
| 184 |
} |
| 185 |
}, |
| 186 |
"networkAcls": { |
| 187 |
"bypass": "AzureServices", |
| 188 |
"defaultAction": "Allow", |
| 189 |
"ipRules": [], |
| 190 |
"virtualNetworkRules": [] |
| 191 |
}, |
| 192 |
"primaryEndpoints": { |
| 193 |
"blob": "https://staccb57dc95183.blob.core.windows.net/", |
| 194 |
"file": "https://staccb57dc95183.file.core.windows.net/", |
| 195 |
"queue": "https://staccb57dc95183.queue.core.windows.net/", |
| 196 |
"table": "https://staccb57dc95183.table.core.windows.net/" |
| 197 |
}, |
| 198 |
"primaryLocation": "eastus", |
| 199 |
"provisioningState": "Succeeded", |
| 200 |
"secondaryLocation": "westus", |
| 201 |
"statusOfPrimary": "available", |
| 202 |
"statusOfSecondary": "available", |
| 203 |
"supportsHttpsTrafficOnly": false |
| 204 |
} |
| 205 |
sku: |
| 206 |
description: |
| 207 |
- The storage account SKU. |
| 208 |
type: dict |
| 209 |
returned: always |
| 210 |
sample: { |
| 211 |
"name": "Standard_GRS", |
| 212 |
"tier": "Standard" |
| 213 |
} |
| 214 |
tags: |
| 215 |
description: |
| 216 |
- Resource tags. |
| 217 |
type: dict |
| 218 |
returned: always |
| 219 |
sample: { 'key1': 'value1' } |
| 220 |
type: |
| 221 |
description: |
| 222 |
- The resource type. |
| 223 |
type: str |
| 224 |
returned: always |
| 225 |
sample: "Microsoft.Storage/storageAccounts" |
| 226 |
|
| 227 |
''' |
| 228 |
|
| 229 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 230 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 231 |
from ansible.module_utils.common.dict_transformations import dict_merge |
| 232 |
|
| 233 |
try: |
| 234 |
from azure.mgmt.core.tools import resource_id |
| 235 |
import json |
| 236 |
|
| 237 |
except ImportError: |
| 238 |
|
| 239 |
pass |
| 240 |
|
| 241 |
|
| 242 |
class AzureRMResource(AzureRMModuleBase): |
| 243 |
def __init__(self): |
| 244 |
|
| 245 |
self.module_arg_spec = dict( |
| 246 |
url=dict( |
| 247 |
type='str' |
| 248 |
), |
| 249 |
provider=dict( |
| 250 |
type='str', |
| 251 |
), |
| 252 |
resource_group=dict( |
| 253 |
type='str', |
| 254 |
), |
| 255 |
resource_type=dict( |
| 256 |
type='str', |
| 257 |
), |
| 258 |
resource_name=dict( |
| 259 |
type='str', |
| 260 |
), |
| 261 |
subresource=dict( |
| 262 |
type='list', |
| 263 |
elements='dict', |
| 264 |
default=[] |
| 265 |
), |
| 266 |
api_version=dict( |
| 267 |
type='str' |
| 268 |
), |
| 269 |
method=dict( |
| 270 |
type='str', |
| 271 |
default='PUT', |
| 272 |
choices=["GET", "PUT", "POST", "HEAD", "PATCH", "DELETE", "MERGE"] |
| 273 |
), |
| 274 |
body=dict( |
| 275 |
type='raw' |
| 276 |
), |
| 277 |
status_code=dict( |
| 278 |
type='list', |
| 279 |
elements='int', |
| 280 |
default=[200, 201, 202] |
| 281 |
), |
| 282 |
idempotency=dict( |
| 283 |
type='bool', |
| 284 |
default=False |
| 285 |
), |
| 286 |
polling_timeout=dict( |
| 287 |
type='int', |
| 288 |
default=0 |
| 289 |
), |
| 290 |
polling_interval=dict( |
| 291 |
type='int', |
| 292 |
default=60 |
| 293 |
), |
| 294 |
state=dict( |
| 295 |
type='str', |
| 296 |
default='present', |
| 297 |
choices=['present', 'absent'] |
| 298 |
) |
| 299 |
) |
| 300 |
|
| 301 |
self.results = dict( |
| 302 |
changed=False, |
| 303 |
response=None |
| 304 |
) |
| 305 |
self.mgmt_client = None |
| 306 |
self.url = None |
| 307 |
self.api_version = None |
| 308 |
self.provider = None |
| 309 |
self.resource_group = None |
| 310 |
self.resource_type = None |
| 311 |
self.resource_name = None |
| 312 |
self.subresource_type = None |
| 313 |
self.subresource_name = None |
| 314 |
self.subresource = [] |
| 315 |
self.method = None |
| 316 |
self.status_code = [] |
| 317 |
self.idempotency = False |
| 318 |
self.polling_timeout = None |
| 319 |
self.polling_interval = None |
| 320 |
self.state = None |
| 321 |
self.body = None |
| 322 |
super(AzureRMResource, self).__init__(self.module_arg_spec, supports_tags=False) |
| 323 |
|
| 324 |
def exec_module(self, **kwargs): |
| 325 |
for key in self.module_arg_spec: |
| 326 |
setattr(self, key, kwargs[key]) |
| 327 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 328 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 329 |
|
| 330 |
if self.state == 'absent': |
| 331 |
self.method = 'DELETE' |
| 332 |
self.status_code.append(204) |
| 333 |
|
| 334 |
if self.url is None: |
| 335 |
orphan = None |
| 336 |
rargs = dict() |
| 337 |
rargs['subscription'] = self.subscription_id |
| 338 |
rargs['resource_group'] = self.resource_group |
| 339 |
if not (self.provider is None or self.provider.lower().startswith('.microsoft')): |
| 340 |
rargs['namespace'] = "Microsoft." + self.provider |
| 341 |
else: |
| 342 |
rargs['namespace'] = self.provider |
| 343 |
|
| 344 |
if self.resource_type is not None and self.resource_name is not None: |
| 345 |
rargs['type'] = self.resource_type |
| 346 |
rargs['name'] = self.resource_name |
| 347 |
for i in range(len(self.subresource)): |
| 348 |
resource_ns = self.subresource[i].get('namespace', None) |
| 349 |
resource_type = self.subresource[i].get('type', None) |
| 350 |
resource_name = self.subresource[i].get('name', None) |
| 351 |
if resource_type is not None and resource_name is not None: |
| 352 |
rargs['child_namespace_' + str(i + 1)] = resource_ns |
| 353 |
rargs['child_type_' + str(i + 1)] = resource_type |
| 354 |
rargs['child_name_' + str(i + 1)] = resource_name |
| 355 |
else: |
| 356 |
orphan = resource_type |
| 357 |
else: |
| 358 |
orphan = self.resource_type |
| 359 |
|
| 360 |
self.url = resource_id(**rargs) |
| 361 |
|
| 362 |
if orphan is not None: |
| 363 |
self.url += '/' + orphan |
| 364 |
|
| 365 |
|
| 366 |
if not self.api_version: |
| 367 |
try: |
| 368 |
|
| 369 |
if "/providers/" in self.url: |
| 370 |
provider = self.url.split("/providers/")[1].split("/")[0] |
| 371 |
resourceType = self.url.split(provider + "/")[1].split("/")[0] |
| 372 |
url = "/subscriptions/" + self.subscription_id + "/providers/" + provider |
| 373 |
api_versions = json.loads(self.mgmt_client.query(url, "GET", {'api-version': '2015-01-01'}, None, None, [200], 0, 0).body()) |
| 374 |
for rt in api_versions['resourceTypes']: |
| 375 |
if rt['resourceType'].lower() == resourceType.lower(): |
| 376 |
self.api_version = rt['apiVersions'][0] |
| 377 |
break |
| 378 |
else: |
| 379 |
|
| 380 |
self.api_version = '2018-05-01' |
| 381 |
if not self.api_version: |
| 382 |
self.fail("Couldn't find api version for {0}/{1}".format(provider, resourceType)) |
| 383 |
except Exception as exc: |
| 384 |
self.fail("Failed to obtain API version: {0}".format(str(exc))) |
| 385 |
|
| 386 |
query_parameters = {} |
| 387 |
query_parameters['api-version'] = self.api_version |
| 388 |
|
| 389 |
header_parameters = {} |
| 390 |
header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 391 |
|
| 392 |
needs_update = True |
| 393 |
response = None |
| 394 |
|
| 395 |
if self.idempotency: |
| 396 |
original = self.mgmt_client.query(self.url, "GET", query_parameters, None, None, [200, 404], 0, 0) |
| 397 |
|
| 398 |
if original.status_code == 404: |
| 399 |
if self.state == 'absent': |
| 400 |
needs_update = False |
| 401 |
else: |
| 402 |
try: |
| 403 |
response = json.loads(original.body()) |
| 404 |
needs_update = (dict_merge(response, self.body) != response) |
| 405 |
except Exception: |
| 406 |
pass |
| 407 |
|
| 408 |
if needs_update: |
| 409 |
response = self.mgmt_client.query(self.url, |
| 410 |
self.method, |
| 411 |
query_parameters, |
| 412 |
header_parameters, |
| 413 |
self.body, |
| 414 |
self.status_code, |
| 415 |
self.polling_timeout, |
| 416 |
self.polling_interval) |
| 417 |
if self.state == 'present' and self.method != 'DELETE': |
| 418 |
if hasattr(response, 'body'): |
| 419 |
try: |
| 420 |
response = json.loads(response.body()) |
| 421 |
except Exception: |
| 422 |
response = response.body() |
| 423 |
elif hasattr(response, 'context'): |
| 424 |
response = response.context['deserialized_data'] |
| 425 |
else: |
| 426 |
self.fail("Create or Updating fail, no match message return, return info as {0}".format(response)) |
| 427 |
else: |
| 428 |
response = None |
| 429 |
|
| 430 |
self.results['response'] = response |
| 431 |
self.results['changed'] = needs_update |
| 432 |
|
| 433 |
return self.results |
| 434 |
|
| 435 |
|
| 436 |
def main(): |
| 437 |
AzureRMResource() |
| 438 |
|
| 439 |
|
| 440 |
if __name__ == '__main__': |
| 441 |
main() |