| 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_registrationdefinition |
| 14 |
version_added: '1.3.0' |
| 15 |
short_description: Manage Azure RegistrationDefinition instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure RegistrationDefinition. |
| 18 |
options: |
| 19 |
registration_definition_id: |
| 20 |
description: |
| 21 |
- ID of the registration definition. |
| 22 |
- If is not specified, an UUID will be generated for it. |
| 23 |
type: str |
| 24 |
scope: |
| 25 |
description: |
| 26 |
- The subscription ID defines the subscription in which the registration definition will be created. |
| 27 |
- If not specified, will use the subscription derived from AzureRMAuth. |
| 28 |
type: str |
| 29 |
properties: |
| 30 |
description: |
| 31 |
- Properties of a registration definition. |
| 32 |
type: dict |
| 33 |
suboptions: |
| 34 |
description: |
| 35 |
description: |
| 36 |
- Description of the registration definition. |
| 37 |
type: str |
| 38 |
authorizations: |
| 39 |
description: |
| 40 |
- Authorization tuple containing principal ID of the user/security group or service principal and ID of the build-in role. |
| 41 |
required: true |
| 42 |
type: list |
| 43 |
elements: dict |
| 44 |
suboptions: |
| 45 |
principal_id: |
| 46 |
description: |
| 47 |
- Principal ID of the security group/service principal/user that would be assigned permissions to the projected subscription. |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
role_definition_id: |
| 51 |
description: |
| 52 |
- The role definition identifier. |
| 53 |
- This role will define all the permissions that the security group/service principal/user must have on the projected subscription. |
| 54 |
- This role cannot be an owner role. |
| 55 |
required: true |
| 56 |
type: str |
| 57 |
registration_definition_name: |
| 58 |
description: |
| 59 |
- Name of the registration definition. |
| 60 |
type: str |
| 61 |
managed_by_tenant_id: |
| 62 |
description: |
| 63 |
- ID of the managedBy tenant. |
| 64 |
required: true |
| 65 |
type: str |
| 66 |
plan: |
| 67 |
description: |
| 68 |
- Plan details for the managed services. |
| 69 |
type: dict |
| 70 |
suboptions: |
| 71 |
name: |
| 72 |
description: |
| 73 |
- The plan name. |
| 74 |
required: true |
| 75 |
type: str |
| 76 |
publisher: |
| 77 |
description: |
| 78 |
- The publisher ID. |
| 79 |
required: true |
| 80 |
type: str |
| 81 |
product: |
| 82 |
description: |
| 83 |
- The product code. |
| 84 |
required: true |
| 85 |
type: str |
| 86 |
version: |
| 87 |
description: |
| 88 |
- The plan's version. |
| 89 |
required: true |
| 90 |
type: str |
| 91 |
state: |
| 92 |
description: |
| 93 |
- Assert the state of the RegistrationDefinition. |
| 94 |
- Use C(present) to create or update an RegistrationDefinition and C(absent) to delete it. |
| 95 |
default: present |
| 96 |
type: str |
| 97 |
choices: |
| 98 |
- absent |
| 99 |
- present |
| 100 |
extends_documentation_fragment: |
| 101 |
- azure.azcollection.azure |
| 102 |
- azure.azcollection.azure_tags |
| 103 |
author: |
| 104 |
- Fred-Sun (@Fred-Sun) |
| 105 |
|
| 106 |
''' |
| 107 |
|
| 108 |
EXAMPLES = ''' |
| 109 |
- name: Create Registration Definition without scope |
| 110 |
azure_rm_registrationdefinition: |
| 111 |
registration_definition_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 112 |
properties: |
| 113 |
description: test |
| 114 |
authorizations: |
| 115 |
- principal_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 116 |
role_definition_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 117 |
managed_by_tenant_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 118 |
registration_definition_name: def4 |
| 119 |
|
| 120 |
- name: Create Registration Definition with scope |
| 121 |
azure_rm_registrationdefinition: |
| 122 |
scope: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 123 |
registration_definition_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 124 |
properties: |
| 125 |
description: test |
| 126 |
authorizations: |
| 127 |
- principal_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 128 |
role_definition_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 129 |
managed_by_tenant_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 130 |
registration_definition_name: def5 |
| 131 |
|
| 132 |
- name: Delete Registration Definition |
| 133 |
azure_rm_registrationdefinition: |
| 134 |
registration_definition_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 135 |
state: absent |
| 136 |
''' |
| 137 |
|
| 138 |
RETURN = ''' |
| 139 |
state: |
| 140 |
description: |
| 141 |
- The state info of the registration assignment. |
| 142 |
type: complex |
| 143 |
returned: always |
| 144 |
contains: |
| 145 |
properties: |
| 146 |
description: |
| 147 |
- Properties of a registration definition. |
| 148 |
returned: always |
| 149 |
type: complex |
| 150 |
contains: |
| 151 |
description: |
| 152 |
description: |
| 153 |
- Description of the registration definition. |
| 154 |
returned: always |
| 155 |
type: str |
| 156 |
sample: test |
| 157 |
authorizations: |
| 158 |
description: |
| 159 |
- Authorization tuple containing principal ID of the user/security group or service principal and ID of the build-in role. |
| 160 |
returned: always |
| 161 |
type: complex |
| 162 |
contains: |
| 163 |
principal_id: |
| 164 |
description: |
| 165 |
- Principal ID of the security group/service principal/user that would be assigned permissions to the projected subscription |
| 166 |
returned: always |
| 167 |
type: str |
| 168 |
sample: 99e3227f-8701-4099-869f-bc3efc7f1e64 |
| 169 |
role_definition_id: |
| 170 |
description: |
| 171 |
- The role definition identifier. |
| 172 |
- This role will define all the permissions that the security group/service principal/user must have on the subscription. |
| 173 |
- This role cannot be an owner role. |
| 174 |
returned: always |
| 175 |
type: str |
| 176 |
sample: b24988ac-6180-42a0-ab88-20f7382dd24c |
| 177 |
registration_definition_name: |
| 178 |
description: |
| 179 |
- Name of the registration definition. |
| 180 |
returned: always |
| 181 |
type: str |
| 182 |
sample: null |
| 183 |
managed_by_tenant_id: |
| 184 |
description: |
| 185 |
- ID of the managedBy tenant. |
| 186 |
returned: always |
| 187 |
type: str |
| 188 |
sample: null |
| 189 |
plan: |
| 190 |
description: |
| 191 |
- Plan details for the managed services. |
| 192 |
returned: always |
| 193 |
type: complex |
| 194 |
contains: |
| 195 |
name: |
| 196 |
description: |
| 197 |
- The plan name. |
| 198 |
returned: always |
| 199 |
type: str |
| 200 |
sample: null |
| 201 |
publisher: |
| 202 |
description: |
| 203 |
- The publisher ID. |
| 204 |
returned: always |
| 205 |
type: str |
| 206 |
sample: null |
| 207 |
product: |
| 208 |
description: |
| 209 |
- The product code. |
| 210 |
returned: always |
| 211 |
type: str |
| 212 |
sample: null |
| 213 |
version: |
| 214 |
description: |
| 215 |
- The plan's version. |
| 216 |
returned: always |
| 217 |
type: str |
| 218 |
sample: null |
| 219 |
id: |
| 220 |
description: |
| 221 |
- Fully qualified path of the registration definition. |
| 222 |
returned: always |
| 223 |
type: str |
| 224 |
sample: null |
| 225 |
type: |
| 226 |
description: |
| 227 |
- Type of the resource. |
| 228 |
returned: always |
| 229 |
type: str |
| 230 |
sample: Microsoft.ManagedServices/registrationDefinitions |
| 231 |
name: |
| 232 |
description: |
| 233 |
- Name of the registration definition. |
| 234 |
returned: always |
| 235 |
type: str |
| 236 |
sample: /subscriptions/xxx-xxx/providers/Microsoft.ManagedServices/registrationDefinitions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 237 |
|
| 238 |
''' |
| 239 |
import uuid |
| 240 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 241 |
try: |
| 242 |
from azure.mgmt.managedservices import ManagedServicesClient |
| 243 |
from azure.core.polling import LROPoller |
| 244 |
except ImportError: |
| 245 |
|
| 246 |
pass |
| 247 |
|
| 248 |
|
| 249 |
class Actions: |
| 250 |
NoAction, Create, Update, Delete = range(4) |
| 251 |
|
| 252 |
|
| 253 |
class AzureRMRegistrationDefinition(AzureRMModuleBaseExt): |
| 254 |
def __init__(self): |
| 255 |
self.module_arg_spec = dict( |
| 256 |
scope=dict( |
| 257 |
type='str' |
| 258 |
), |
| 259 |
registration_definition_id=dict( |
| 260 |
type='str', |
| 261 |
), |
| 262 |
properties=dict( |
| 263 |
type='dict', |
| 264 |
options=dict( |
| 265 |
description=dict( |
| 266 |
type='str', |
| 267 |
), |
| 268 |
authorizations=dict( |
| 269 |
type='list', |
| 270 |
required=True, |
| 271 |
elements='dict', |
| 272 |
options=dict( |
| 273 |
principal_id=dict( |
| 274 |
type='str', |
| 275 |
required=True |
| 276 |
), |
| 277 |
role_definition_id=dict( |
| 278 |
type='str', |
| 279 |
required=True |
| 280 |
) |
| 281 |
) |
| 282 |
), |
| 283 |
registration_definition_name=dict( |
| 284 |
type='str', |
| 285 |
), |
| 286 |
managed_by_tenant_id=dict( |
| 287 |
type='str', |
| 288 |
required=True |
| 289 |
) |
| 290 |
) |
| 291 |
), |
| 292 |
plan=dict( |
| 293 |
type='dict', |
| 294 |
options=dict( |
| 295 |
name=dict( |
| 296 |
type='str', |
| 297 |
required=True |
| 298 |
), |
| 299 |
publisher=dict( |
| 300 |
type='str', |
| 301 |
required=True |
| 302 |
), |
| 303 |
product=dict( |
| 304 |
type='str', |
| 305 |
required=True |
| 306 |
), |
| 307 |
version=dict( |
| 308 |
type='str', |
| 309 |
required=True |
| 310 |
) |
| 311 |
) |
| 312 |
), |
| 313 |
state=dict( |
| 314 |
type='str', |
| 315 |
default='present', |
| 316 |
choices=['present', 'absent'] |
| 317 |
) |
| 318 |
) |
| 319 |
|
| 320 |
self.scope = None |
| 321 |
self.registration_definition_id = None |
| 322 |
self.body = {} |
| 323 |
|
| 324 |
self.results = dict(changed=False) |
| 325 |
self.mgmt_client = None |
| 326 |
self.state = None |
| 327 |
self.to_do = Actions.NoAction |
| 328 |
|
| 329 |
super(AzureRMRegistrationDefinition, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 330 |
supports_check_mode=True, |
| 331 |
supports_tags=True) |
| 332 |
|
| 333 |
def exec_module(self, **kwargs): |
| 334 |
for key in list(self.module_arg_spec.keys()): |
| 335 |
if hasattr(self, key): |
| 336 |
setattr(self, key, kwargs[key]) |
| 337 |
elif kwargs[key] is not None: |
| 338 |
self.body[key] = kwargs[key] |
| 339 |
|
| 340 |
if self.registration_definition_id is None: |
| 341 |
self.registration_definition_id = str(uuid.uuid4()) |
| 342 |
|
| 343 |
if not self.scope: |
| 344 |
self.scope = "/subscriptions/" + self.subscription_id |
| 345 |
else: |
| 346 |
self.scope = "/subscriptions/" + self.scope |
| 347 |
|
| 348 |
old_response = None |
| 349 |
response = None |
| 350 |
|
| 351 |
self.mgmt_client = self.get_mgmt_svc_client(ManagedServicesClient, |
| 352 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 353 |
api_version='2022-10-01', |
| 354 |
suppress_subscription_id=True) |
| 355 |
|
| 356 |
old_response = self.get_resource() |
| 357 |
|
| 358 |
if not old_response: |
| 359 |
if self.state == 'present': |
| 360 |
self.to_do = Actions.Create |
| 361 |
else: |
| 362 |
if self.state == 'absent': |
| 363 |
self.to_do = Actions.Delete |
| 364 |
else: |
| 365 |
if self.body.get('plan') is not None: |
| 366 |
if old_response.get('plan') is not None and \ |
| 367 |
not all(self.body['plan'][item] == old_response['plan'].get(item) |
| 368 |
for item in self.body['plan'].keys()): |
| 369 |
self.to_do = Actions.Update |
| 370 |
else: |
| 371 |
self.to_do = Actions.Update |
| 372 |
elif (self.body.get('properties') is not None and |
| 373 |
not all(self.body['properties'][item] == old_response['properties'].get(item) |
| 374 |
for item in self.body['properties'].keys())): |
| 375 |
self.to_do = Actions.Update |
| 376 |
|
| 377 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 378 |
self.results['changed'] = True |
| 379 |
if self.check_mode: |
| 380 |
return self.results |
| 381 |
response = self.create_update_resource() |
| 382 |
self.results['state'] = response |
| 383 |
elif self.to_do == Actions.Delete: |
| 384 |
self.results['changed'] = True |
| 385 |
if self.check_mode: |
| 386 |
return self.results |
| 387 |
self.delete_resource() |
| 388 |
else: |
| 389 |
self.results['changed'] = False |
| 390 |
response = old_response |
| 391 |
|
| 392 |
return self.results |
| 393 |
|
| 394 |
def create_update_resource(self): |
| 395 |
|
| 396 |
try: |
| 397 |
response = self.mgmt_client.registration_definitions.begin_create_or_update( |
| 398 |
registration_definition_id=self.registration_definition_id, |
| 399 |
scope=self.scope, |
| 400 |
request_body=self.body) |
| 401 |
|
| 402 |
if isinstance(response, LROPoller): |
| 403 |
response = self.get_poller_result(response) |
| 404 |
except Exception as exc: |
| 405 |
self.log('Error attempting to create the RegistrationDefinition instance.') |
| 406 |
self.fail('Error creating the RegistrationDefinition instance: {0}'.format(str(exc))) |
| 407 |
return response.as_dict() |
| 408 |
|
| 409 |
def delete_resource(self): |
| 410 |
try: |
| 411 |
response = self.mgmt_client.registration_definitions.delete(registration_definition_id=self.registration_definition_id, |
| 412 |
scope=self.scope) |
| 413 |
except Exception as e: |
| 414 |
self.log('Error attempting to delete the RegistrationDefinition instance.') |
| 415 |
self.fail('Error deleting the RegistrationDefinition instance: {0}'.format(str(e))) |
| 416 |
|
| 417 |
return True |
| 418 |
|
| 419 |
def get_resource(self): |
| 420 |
try: |
| 421 |
response = self.mgmt_client.registration_definitions.get(scope=self.scope, |
| 422 |
registration_definition_id=self.registration_definition_id) |
| 423 |
except Exception as e: |
| 424 |
return False |
| 425 |
return response.as_dict() |
| 426 |
|
| 427 |
|
| 428 |
def main(): |
| 429 |
AzureRMRegistrationDefinition() |
| 430 |
|
| 431 |
|
| 432 |
if __name__ == '__main__': |
| 433 |
main() |