| 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_registrationassignment |
| 14 |
version_added: '1.3.0' |
| 15 |
short_description: Manage Azure RegistrationAssignment instance |
| 16 |
description: |
| 17 |
- Create and delete instance of Azure RegistrationAssignment. |
| 18 |
options: |
| 19 |
scope: |
| 20 |
description: |
| 21 |
- Scope of the registration assignment. Can be in subscription or group level. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
registration_assignment_id: |
| 25 |
description: |
| 26 |
- ID of the registration assignment. |
| 27 |
- If is not specified, an UUID will be generated for it. |
| 28 |
type: str |
| 29 |
properties: |
| 30 |
description: |
| 31 |
- Properties of a registration assignment. |
| 32 |
type: dict |
| 33 |
suboptions: |
| 34 |
registration_definition_id: |
| 35 |
description: |
| 36 |
- Fully qualified path of the registration definition. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
state: |
| 40 |
description: |
| 41 |
- Assert the state of the RegistrationAssignment. |
| 42 |
- Use C(present) to create or update an RegistrationAssignment and C(absent) to delete it. |
| 43 |
default: present |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- absent |
| 47 |
- present |
| 48 |
extends_documentation_fragment: |
| 49 |
- azure.azcollection.azure |
| 50 |
- azure.azcollection.azure_tags |
| 51 |
author: |
| 52 |
- Fred-Sun (@Fred-Sun) |
| 53 |
|
| 54 |
''' |
| 55 |
|
| 56 |
EXAMPLES = ''' |
| 57 |
- name: Delete Registration Assignment |
| 58 |
azure_rm_registrationassignment: |
| 59 |
scope: subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 60 |
registration_assignment_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 61 |
state: absent |
| 62 |
|
| 63 |
|
| 64 |
- name: Create Registration Assignment in subscription level |
| 65 |
azure_rm_registrationassignment: |
| 66 |
scope: subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 67 |
registration_assignment_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 68 |
properties: |
| 69 |
registration_definition_id: /subscriptions/xxx-xxx/providers/Microsoft.ManagedServices/registrationDefinitions/xxx-xxx |
| 70 |
|
| 71 |
|
| 72 |
- name: Create Registration Assignment in resourcegroup level with randomly generating registration_assignment_id |
| 73 |
azure_rm_registrationassignment: |
| 74 |
scope: subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup |
| 75 |
properties: |
| 76 |
registration_definition_id: /subscriptions/xxx-xxx/providers/Microsoft.ManagedServices/registrationDefinitions/xxx-xxx |
| 77 |
''' |
| 78 |
|
| 79 |
RETURN = ''' |
| 80 |
state: |
| 81 |
description: |
| 82 |
- The state info of the registration assignment. |
| 83 |
type: complex |
| 84 |
returned: always |
| 85 |
contains: |
| 86 |
properties: |
| 87 |
description: |
| 88 |
- Properties of a registration assignment. |
| 89 |
returned: always |
| 90 |
type: complex |
| 91 |
contains: |
| 92 |
registration_definition_id: |
| 93 |
description: |
| 94 |
- Fully qualified path of the registration definition. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: null |
| 98 |
id: |
| 99 |
description: |
| 100 |
- The fully qualified path of the registration assignment. |
| 101 |
returned: always |
| 102 |
type: str |
| 103 |
sample: /subscriptions/xxx-xxx/providers/Microsoft.ManagedServices/registrationAssignments/xxx-xxx |
| 104 |
type: |
| 105 |
description: |
| 106 |
- Type of the resource. |
| 107 |
returned: always |
| 108 |
type: str |
| 109 |
sample: Microsoft.ManagedServices/registrationAssignments |
| 110 |
name: |
| 111 |
description: |
| 112 |
- Name of the registration assignment. |
| 113 |
returned: always |
| 114 |
type: str |
| 115 |
sample: 9b2895ec-fb1e-4a1e-a978-abd9933d6b20 |
| 116 |
|
| 117 |
''' |
| 118 |
import uuid |
| 119 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 120 |
try: |
| 121 |
from azure.mgmt.managedservices import ManagedServicesClient |
| 122 |
from azure.core.polling import LROPoller |
| 123 |
except ImportError: |
| 124 |
|
| 125 |
pass |
| 126 |
|
| 127 |
|
| 128 |
class Actions: |
| 129 |
NoAction, Create, Update, Delete = range(4) |
| 130 |
|
| 131 |
|
| 132 |
class AzureRMRegistrationAssignment(AzureRMModuleBaseExt): |
| 133 |
def __init__(self): |
| 134 |
self.module_arg_spec = dict( |
| 135 |
scope=dict( |
| 136 |
type='str', |
| 137 |
required=True |
| 138 |
), |
| 139 |
registration_assignment_id=dict( |
| 140 |
type='str', |
| 141 |
), |
| 142 |
properties=dict( |
| 143 |
type='dict', |
| 144 |
options=dict( |
| 145 |
registration_definition_id=dict( |
| 146 |
type='str', |
| 147 |
required=True |
| 148 |
) |
| 149 |
) |
| 150 |
), |
| 151 |
state=dict( |
| 152 |
type='str', |
| 153 |
default='present', |
| 154 |
choices=['present', 'absent'] |
| 155 |
) |
| 156 |
) |
| 157 |
|
| 158 |
self.scope = None |
| 159 |
self.registration_assignment_id = None |
| 160 |
self.expand_registration_definition = False |
| 161 |
self.body = {} |
| 162 |
|
| 163 |
self.results = dict(changed=False) |
| 164 |
self.mgmt_client = None |
| 165 |
self.state = None |
| 166 |
self.to_do = Actions.NoAction |
| 167 |
|
| 168 |
super(AzureRMRegistrationAssignment, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 169 |
supports_check_mode=True, |
| 170 |
supports_tags=True) |
| 171 |
|
| 172 |
def exec_module(self, **kwargs): |
| 173 |
for key in list(self.module_arg_spec.keys()): |
| 174 |
if hasattr(self, key): |
| 175 |
setattr(self, key, kwargs[key]) |
| 176 |
elif kwargs[key] is not None: |
| 177 |
self.body[key] = kwargs[key] |
| 178 |
|
| 179 |
self.inflate_parameters(self.module_arg_spec, self.body, 0) |
| 180 |
|
| 181 |
old_response = None |
| 182 |
response = None |
| 183 |
if self.registration_assignment_id is None: |
| 184 |
self.registration_assignment_id = str(uuid.uuid4()) |
| 185 |
|
| 186 |
self.mgmt_client = self.get_mgmt_svc_client(ManagedServicesClient, |
| 187 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 188 |
api_version='2022-10-01', |
| 189 |
suppress_subscription_id=True) |
| 190 |
|
| 191 |
old_response = self.get_resource() |
| 192 |
|
| 193 |
if not old_response: |
| 194 |
if self.state == 'present': |
| 195 |
self.to_do = Actions.Create |
| 196 |
else: |
| 197 |
if self.state == 'absent': |
| 198 |
self.to_do = Actions.Delete |
| 199 |
else: |
| 200 |
if self.body.get('properties') is not None and \ |
| 201 |
self.body['properties']['registration_definition_id'] != \ |
| 202 |
old_response['properties']['registration_definition_id']: |
| 203 |
self.to_do = Actions.Update |
| 204 |
|
| 205 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 206 |
self.results['changed'] = True |
| 207 |
if self.check_mode: |
| 208 |
return self.results |
| 209 |
response = self.create_update_resource() |
| 210 |
self.results['state'] = response |
| 211 |
elif self.to_do == Actions.Delete: |
| 212 |
self.results['changed'] = True |
| 213 |
if self.check_mode: |
| 214 |
return self.results |
| 215 |
self.delete_resource() |
| 216 |
else: |
| 217 |
self.results['changed'] = False |
| 218 |
response = old_response |
| 219 |
self.results['state'] = response |
| 220 |
|
| 221 |
if self.state == 'present': |
| 222 |
if self.results['state'].get('properties', None) is not None: |
| 223 |
registration_definition_id = self.results['state']['properties']['registration_definition_id'] |
| 224 |
self.results['state']['properties'].clear() |
| 225 |
self.results['state']['properties']['registration_definition_id'] = registration_definition_id |
| 226 |
|
| 227 |
return self.results |
| 228 |
|
| 229 |
def create_update_resource(self): |
| 230 |
try: |
| 231 |
response = self.mgmt_client.registration_assignments.begin_create_or_update(scope=self.scope, |
| 232 |
registration_assignment_id=self.registration_assignment_id, |
| 233 |
request_body=self.body) |
| 234 |
if isinstance(response, LROPoller): |
| 235 |
response = self.get_poller_result(response) |
| 236 |
except Exception as exc: |
| 237 |
self.log('Error attempting to create the RegistrationAssignment instance.') |
| 238 |
self.fail('Error creating the RegistrationAssignment instance: {0}'.format(str(exc))) |
| 239 |
return response.as_dict() |
| 240 |
|
| 241 |
def delete_resource(self): |
| 242 |
try: |
| 243 |
response = self.mgmt_client.registration_assignments.begin_delete(scope=self.scope, |
| 244 |
registration_assignment_id=self.registration_assignment_id) |
| 245 |
except Exception as e: |
| 246 |
self.log('Error attempting to delete the RegistrationAssignment instance.') |
| 247 |
self.fail('Error deleting the RegistrationAssignment instance: {0}'.format(str(e))) |
| 248 |
|
| 249 |
return True |
| 250 |
|
| 251 |
def get_resource(self): |
| 252 |
try: |
| 253 |
response = self.mgmt_client.registration_assignments.get(scope=self.scope, |
| 254 |
registration_assignment_id=self.registration_assignment_id, |
| 255 |
expand_registration_definition=self.expand_registration_definition) |
| 256 |
except Exception as e: |
| 257 |
return False |
| 258 |
return response.as_dict() |
| 259 |
|
| 260 |
|
| 261 |
def main(): |
| 262 |
AzureRMRegistrationAssignment() |
| 263 |
|
| 264 |
|
| 265 |
if __name__ == '__main__': |
| 266 |
main() |