| 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_managementgroup |
| 14 |
version_added: '0.1.1' |
| 15 |
short_description: Manage Azure ManagementGroup instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure ManagementGroup. |
| 18 |
options: |
| 19 |
group_id: |
| 20 |
description: |
| 21 |
- Management Group ID. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the management group. |
| 27 |
- For example 00000000-0000-0000-0000-000000000000. |
| 28 |
type: str |
| 29 |
id: |
| 30 |
description: |
| 31 |
- The fully qualified ID for the management group. |
| 32 |
- For example /providers/Microsoft.Management/managementGroups/0000000-0000-0000-0000-000000000000. |
| 33 |
type: str |
| 34 |
type: |
| 35 |
description: |
| 36 |
- The type of the resource. |
| 37 |
- For example /providers/Microsoft.Management/managementGroups. |
| 38 |
type: str |
| 39 |
properties: |
| 40 |
description: |
| 41 |
- The properties of the management group. |
| 42 |
type: dict |
| 43 |
suboptions: |
| 44 |
tenant_id: |
| 45 |
description: |
| 46 |
- The AAD Tenant ID associated with the management group. |
| 47 |
- For example 00000000-0000-0000-0000-000000000000. |
| 48 |
type: str |
| 49 |
display_name: |
| 50 |
description: |
| 51 |
- The friendly name of the management group. |
| 52 |
type: str |
| 53 |
parent_id: |
| 54 |
description: |
| 55 |
- The parent of the management group. |
| 56 |
type: str |
| 57 |
state: |
| 58 |
description: |
| 59 |
- Assert the state of the ManagementGroup. |
| 60 |
- Use C(present) to create or update an ManagementGroup and C(absent) to delete it. |
| 61 |
default: present |
| 62 |
choices: |
| 63 |
- absent |
| 64 |
- present |
| 65 |
type: str |
| 66 |
extends_documentation_fragment: |
| 67 |
- azure.azcollection.azure |
| 68 |
- azure.azcollection.azure_tags |
| 69 |
author: |
| 70 |
- Zim Kalinowski (@zikalino) |
| 71 |
|
| 72 |
''' |
| 73 |
|
| 74 |
EXAMPLES = ''' |
| 75 |
- name: Create new management group |
| 76 |
azure_rm_managementgroup: |
| 77 |
group_id: test |
| 78 |
type: /providers/Microsoft.Management/managementGroups/ |
| 79 |
name: test |
| 80 |
|
| 81 |
- name: create management group |
| 82 |
azure_rm_managementgroup: |
| 83 |
group_id: ChildGroup |
| 84 |
type: /providers/Microsoft.Management/managementGroups/ |
| 85 |
name: ChildGroup |
| 86 |
properties: |
| 87 |
tenant_id: 20000000-0000-0000-0000-000000000000 |
| 88 |
display_name: ChildGroup |
| 89 |
parent_id: /providers/Microsoft.Management/managementGroups/RootGroup |
| 90 |
|
| 91 |
- name: delete management group |
| 92 |
azure_rm_managementgroup: |
| 93 |
group_id: ChildGroup |
| 94 |
state: absent |
| 95 |
''' |
| 96 |
|
| 97 |
RETURN = ''' |
| 98 |
id: |
| 99 |
description: |
| 100 |
- The fully qualified ID for the management group. |
| 101 |
- For example /providers/Microsoft.Management/managementGroups/0000000-0000-0000-0000-000000000000. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
type: |
| 105 |
description: |
| 106 |
- The type of the resource. |
| 107 |
- For example /providers/Microsoft.Management/managementGroups. |
| 108 |
returned: always |
| 109 |
type: str |
| 110 |
name: |
| 111 |
description: |
| 112 |
- The name of the management group. |
| 113 |
- For example 00000000-0000-0000-0000-000000000000. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
properties: |
| 117 |
description: |
| 118 |
- The properties of the management group. |
| 119 |
returned: always |
| 120 |
type: complex |
| 121 |
contains: |
| 122 |
tenant_id: |
| 123 |
description: |
| 124 |
- The AAD Tenant ID associated with the management group. |
| 125 |
- For example 00000000-0000-0000-0000-000000000000. |
| 126 |
returned: always |
| 127 |
type: str |
| 128 |
display_name: |
| 129 |
description: |
| 130 |
- The friendly name of the management group. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
roles: |
| 134 |
description: |
| 135 |
- The role definitions associated with the management group. |
| 136 |
returned: always |
| 137 |
type: str |
| 138 |
details: |
| 139 |
description: |
| 140 |
- The details of the management group. |
| 141 |
returned: always |
| 142 |
type: complex |
| 143 |
contains: |
| 144 |
version: |
| 145 |
description: |
| 146 |
- The version number of the object. |
| 147 |
returned: always |
| 148 |
type: str |
| 149 |
updated_time: |
| 150 |
description: |
| 151 |
- The date and time when this object was last updated. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
updated_by: |
| 155 |
description: |
| 156 |
- The identity of the principal or process that updated the object. |
| 157 |
returned: always |
| 158 |
type: str |
| 159 |
parent: |
| 160 |
description: |
| 161 |
- The parent of the management group. |
| 162 |
returned: always |
| 163 |
type: complex |
| 164 |
contains: |
| 165 |
id: |
| 166 |
description: |
| 167 |
- The fully qualified ID for the parent management group. |
| 168 |
- For example /providers/Microsoft.Management/managementGroups/0000000-0000-0000-0000-000000000000. |
| 169 |
returned: always |
| 170 |
type: str |
| 171 |
name: |
| 172 |
description: |
| 173 |
- The name of the parent management group |
| 174 |
returned: always |
| 175 |
type: str |
| 176 |
display_name: |
| 177 |
description: |
| 178 |
- The friendly name of the parent management group. |
| 179 |
returned: always |
| 180 |
type: str |
| 181 |
children: |
| 182 |
description: |
| 183 |
- The list of children. |
| 184 |
returned: always |
| 185 |
type: complex |
| 186 |
contains: |
| 187 |
type: |
| 188 |
description: |
| 189 |
- The fully qualified resource type which includes provider namespace. |
| 190 |
- For example /providers/Microsoft.Management/managementGroups. |
| 191 |
returned: always |
| 192 |
type: str |
| 193 |
id: |
| 194 |
description: |
| 195 |
- The fully qualified ID for the child resource (management group or subscription). |
| 196 |
- For example /providers/Microsoft.Management/managementGroups/0000000-0000-0000-0000-000000000000. |
| 197 |
returned: always |
| 198 |
type: str |
| 199 |
name: |
| 200 |
description: |
| 201 |
- The name of the child entity. |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
display_name: |
| 205 |
description: |
| 206 |
- The friendly name of the child resource. |
| 207 |
returned: always |
| 208 |
type: str |
| 209 |
|
| 210 |
''' |
| 211 |
|
| 212 |
import time |
| 213 |
import json |
| 214 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 215 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 216 |
|
| 217 |
|
| 218 |
class Actions: |
| 219 |
NoAction, Create, Update, Delete = range(4) |
| 220 |
|
| 221 |
|
| 222 |
class AzureRMManagementGroups(AzureRMModuleBaseExt): |
| 223 |
def __init__(self): |
| 224 |
self.module_arg_spec = dict( |
| 225 |
group_id=dict(type='str', required=True), |
| 226 |
name=dict(type='str'), |
| 227 |
id=dict(type='str'), |
| 228 |
type=dict(type='str'), |
| 229 |
properties=dict( |
| 230 |
type='dict', |
| 231 |
options=dict( |
| 232 |
tenant_id=dict(type='str'), |
| 233 |
display_name=dict(type='str'), |
| 234 |
parent_id=dict(type='str') |
| 235 |
) |
| 236 |
), |
| 237 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 238 |
) |
| 239 |
|
| 240 |
self.group_id = None |
| 241 |
self.state = None |
| 242 |
|
| 243 |
self.results = dict(changed=False) |
| 244 |
self.mgmt_client = None |
| 245 |
self.url = None |
| 246 |
self.status_code = [200, 201, 202] |
| 247 |
self.to_do = Actions.NoAction |
| 248 |
self.body = {} |
| 249 |
self.query_parameters = {} |
| 250 |
self.query_parameters['api-version'] = '2018-03-01-preview' |
| 251 |
self.header_parameters = {} |
| 252 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 253 |
|
| 254 |
super(AzureRMManagementGroups, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 255 |
supports_check_mode=True, |
| 256 |
supports_tags=True) |
| 257 |
|
| 258 |
def exec_module(self, **kwargs): |
| 259 |
for key in list(self.module_arg_spec.keys()): |
| 260 |
if hasattr(self, key): |
| 261 |
setattr(self, key, kwargs[key]) |
| 262 |
elif key == 'properties' and kwargs[key] is not None: |
| 263 |
self.body['properties'] = {} |
| 264 |
for item in kwargs['properties'].keys(): |
| 265 |
if item == 'tenant_id': |
| 266 |
self.body['properties']['tenantId'] = kwargs['properties'][item] |
| 267 |
elif item == 'display_name': |
| 268 |
self.body['properties']['displayName'] = kwargs['properties'][item] |
| 269 |
elif item == 'parent_id': |
| 270 |
self.body['properties']['details'] = {} |
| 271 |
self.body['properties']['details']['parent'] = {} |
| 272 |
self.body['properties']['details']['parent']['id'] = kwargs['properties'][item] |
| 273 |
|
| 274 |
elif kwargs[key] is not None: |
| 275 |
self.body[key] = kwargs[key] |
| 276 |
|
| 277 |
old_response = None |
| 278 |
response = None |
| 279 |
|
| 280 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 281 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 282 |
|
| 283 |
self.url = ('/providers' + |
| 284 |
'/Microsoft.Management' + |
| 285 |
'/managementGroups' + |
| 286 |
'/{{ management_group_name }}') |
| 287 |
self.url = self.url.replace('{{ management_group_name }}', self.group_id) |
| 288 |
|
| 289 |
old_response = self.get_resource() |
| 290 |
|
| 291 |
if not old_response: |
| 292 |
self.log("ManagementGroup instance doesn't exist") |
| 293 |
|
| 294 |
if self.state == 'absent': |
| 295 |
self.log("Old instance didn't exist") |
| 296 |
else: |
| 297 |
self.to_do = Actions.Create |
| 298 |
else: |
| 299 |
self.log('ManagementGroup instance already exists') |
| 300 |
|
| 301 |
if self.state == 'absent': |
| 302 |
self.to_do = Actions.Delete |
| 303 |
else: |
| 304 |
for key in self.body.keys(): |
| 305 |
if key == 'properties': |
| 306 |
if old_response.get('properties') is None or \ |
| 307 |
not all(self.body['properties'][item] == old_response['properties'].get(item) |
| 308 |
for item in self.body['properties'].keys()): |
| 309 |
self.to_do = Actions.Update |
| 310 |
elif self.body[key] != old_response.get(key): |
| 311 |
self.to_do = Actions.Update |
| 312 |
|
| 313 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 314 |
self.log('Need to Create / Update the ManagementGroup instance') |
| 315 |
|
| 316 |
if self.check_mode: |
| 317 |
self.results['changed'] = True |
| 318 |
return self.results |
| 319 |
|
| 320 |
response = self.create_update_resource() |
| 321 |
|
| 322 |
self.results['body'] = self.body |
| 323 |
|
| 324 |
self.results['changed'] = True |
| 325 |
|
| 326 |
|
| 327 |
self.log('Creation / Update done') |
| 328 |
elif self.to_do == Actions.Delete: |
| 329 |
self.log('ManagementGroup instance deleted') |
| 330 |
self.results['changed'] = True |
| 331 |
|
| 332 |
if self.check_mode: |
| 333 |
return self.results |
| 334 |
|
| 335 |
self.delete_resource() |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
while self.get_resource(): |
| 340 |
time.sleep(20) |
| 341 |
else: |
| 342 |
self.log('ManagementGroup instance unchanged') |
| 343 |
self.results['changed'] = False |
| 344 |
response = old_response |
| 345 |
|
| 346 |
if response: |
| 347 |
self.results["id"] = response["id"] |
| 348 |
self.results["type"] = response["type"] |
| 349 |
self.results["name"] = response["name"] |
| 350 |
self.results["properties"] = response["properties"] |
| 351 |
|
| 352 |
return self.results |
| 353 |
|
| 354 |
def create_update_resource(self): |
| 355 |
|
| 356 |
|
| 357 |
try: |
| 358 |
if self.to_do == Actions.Create: |
| 359 |
response = self.mgmt_client.query(self.url, |
| 360 |
'PUT', |
| 361 |
self.query_parameters, |
| 362 |
self.header_parameters, |
| 363 |
self.body, |
| 364 |
self.status_code, |
| 365 |
600, |
| 366 |
30) |
| 367 |
else: |
| 368 |
response = self.mgmt_client.query(self.url, |
| 369 |
'PATCH', |
| 370 |
self.query_parameters, |
| 371 |
self.header_parameters, |
| 372 |
self.body, |
| 373 |
self.status_code, |
| 374 |
600, |
| 375 |
30) |
| 376 |
except Exception as exc: |
| 377 |
self.log('Error attempting to create the ManagementGroup instance.') |
| 378 |
self.fail('Error creating the ManagementGroup instance: {0}'.format(str(exc))) |
| 379 |
|
| 380 |
if hasattr(response, 'body'): |
| 381 |
response = json.loads(response.body()) |
| 382 |
elif hasattr(response, 'context'): |
| 383 |
response = response.context['deserialized_data'] |
| 384 |
else: |
| 385 |
self.fail("Create or Updating fail, no match message return, return info as {0}".format(response)) |
| 386 |
|
| 387 |
return response |
| 388 |
|
| 389 |
def delete_resource(self): |
| 390 |
|
| 391 |
try: |
| 392 |
response = self.mgmt_client.query(self.url, |
| 393 |
'DELETE', |
| 394 |
self.query_parameters, |
| 395 |
self.header_parameters, |
| 396 |
None, |
| 397 |
self.status_code, |
| 398 |
600, |
| 399 |
30) |
| 400 |
except Exception as e: |
| 401 |
self.log('Error attempting to delete the ManagementGroup instance.') |
| 402 |
self.fail('Error deleting the ManagementGroup instance: {0}'.format(str(e))) |
| 403 |
|
| 404 |
return True |
| 405 |
|
| 406 |
def get_resource(self): |
| 407 |
|
| 408 |
found = False |
| 409 |
try: |
| 410 |
response = self.mgmt_client.query(self.url, |
| 411 |
'GET', |
| 412 |
self.query_parameters, |
| 413 |
self.header_parameters, |
| 414 |
None, |
| 415 |
self.status_code, |
| 416 |
600, |
| 417 |
30) |
| 418 |
found = True |
| 419 |
response = json.loads(response.body()) |
| 420 |
self.log("Response : {0}".format(response)) |
| 421 |
|
| 422 |
except Exception as e: |
| 423 |
self.log('Did not find the ManagementGroup instance. msg: {0}'.format(e)) |
| 424 |
if found is True: |
| 425 |
return response |
| 426 |
|
| 427 |
return False |
| 428 |
|
| 429 |
|
| 430 |
def main(): |
| 431 |
AzureRMManagementGroups() |
| 432 |
|
| 433 |
|
| 434 |
if __name__ == '__main__': |
| 435 |
main() |