| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = \ |
| 11 |
''' |
| 12 |
--- |
| 13 |
module: azure_rm_recoveryservicesvault |
| 14 |
version_added: '1.1.0' |
| 15 |
short_description: Create and Delete Azure Recovery Services vault |
| 16 |
description: |
| 17 |
- Create or Delete Azure Recovery Services vault. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the Azure Recovery Service Vault. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
location: |
| 30 |
description: |
| 31 |
- Azure Resource location. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
state: |
| 35 |
description: |
| 36 |
- Assert the state of the protection item. |
| 37 |
- Use C(present) for Creating Azure Recovery Service Vault. |
| 38 |
- Use C(absent) for Deleting Azure Recovery Service Vault. |
| 39 |
default: present |
| 40 |
type: str |
| 41 |
choices: |
| 42 |
- present |
| 43 |
- absent |
| 44 |
extends_documentation_fragment: |
| 45 |
- azure.azcollection.azure |
| 46 |
- azure.azcollection.azure_tags |
| 47 |
- azure.azcollection.azure_identity_multiple |
| 48 |
author: |
| 49 |
- Suyeb Ansari (@suyeb786) |
| 50 |
''' |
| 51 |
|
| 52 |
EXAMPLES = ''' |
| 53 |
- name: Create/Update Azure Recovery Service vault |
| 54 |
azure_rm_recoveryservicesvault: |
| 55 |
resource_group: 'myResourceGroup' |
| 56 |
name: 'testVault' |
| 57 |
location: 'westeurope' |
| 58 |
state: 'present' |
| 59 |
- name: Delete Recovery Service Vault |
| 60 |
azure_rm_recoveryservicesvault: |
| 61 |
resource_group: 'myResourceGroup' |
| 62 |
name: 'testVault' |
| 63 |
location: 'westeurope' |
| 64 |
state: 'absent' |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = ''' |
| 68 |
response: |
| 69 |
description: |
| 70 |
- The response about the current state of the recovery services vault. |
| 71 |
returned: always |
| 72 |
type: complex |
| 73 |
contains: |
| 74 |
etag: |
| 75 |
description: |
| 76 |
- A unique read-only string that changes whenever the resource create. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: "datetime'2020-09-16T02%3A44%3A27.834293Z'" |
| 80 |
id: |
| 81 |
description: |
| 82 |
- Resource ID. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: "/subscriptions/xxxxxxx/resourceGroups/resourcegroup_name/ \ |
| 86 |
providers/Microsoft.RecoveryServices/vaults/rev_name" |
| 87 |
location: |
| 88 |
description: |
| 89 |
- The location of the resource. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: "eastus" |
| 93 |
name: |
| 94 |
description: |
| 95 |
- Name of the recovery services vault name. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: revault_name |
| 99 |
properties: |
| 100 |
description: |
| 101 |
- The recovery service vault properties. |
| 102 |
returned: always |
| 103 |
type: dict |
| 104 |
sample: { |
| 105 |
"privateEndpointStateForBackup": "None", |
| 106 |
"privateEndpointStateForSiteRecovery": "None", |
| 107 |
"provisioningState": "Succeeded" |
| 108 |
} |
| 109 |
sku: |
| 110 |
description: |
| 111 |
- The sku type of the recovery service vault. |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
sample: Standard |
| 115 |
type: |
| 116 |
description: |
| 117 |
- The type of the recovery service vault. |
| 118 |
returned: always |
| 119 |
type: str |
| 120 |
sample: "Microsoft.RecoveryServices/vaults" |
| 121 |
tags: |
| 122 |
description: |
| 123 |
- The resource tags. |
| 124 |
returned: when-used |
| 125 |
type: dict |
| 126 |
sample: {'key1': 'value'} |
| 127 |
''' |
| 128 |
|
| 129 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 130 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 131 |
import json |
| 132 |
import time |
| 133 |
|
| 134 |
try: |
| 135 |
from azure.mgmt.recoveryservices.models import (IdentityData, UserIdentity) |
| 136 |
except ImportError: |
| 137 |
|
| 138 |
pass |
| 139 |
|
| 140 |
|
| 141 |
class AzureRMRecoveryServicesVault(AzureRMModuleBaseExt): |
| 142 |
def __init__(self): |
| 143 |
self.module_arg_spec = dict( |
| 144 |
resource_group=dict( |
| 145 |
type='str', |
| 146 |
required=True |
| 147 |
), |
| 148 |
name=dict( |
| 149 |
type='str', |
| 150 |
required=True |
| 151 |
), |
| 152 |
location=dict( |
| 153 |
type='str', |
| 154 |
required=True |
| 155 |
), |
| 156 |
identity=dict( |
| 157 |
type='dict', |
| 158 |
options=self.managed_identity_multiple_spec |
| 159 |
), |
| 160 |
state=dict( |
| 161 |
type='str', |
| 162 |
default='present', |
| 163 |
choices=['present', 'absent'] |
| 164 |
) |
| 165 |
) |
| 166 |
|
| 167 |
self.resource_group = None |
| 168 |
self.name = None |
| 169 |
self.location = None |
| 170 |
self.state = None |
| 171 |
self.identity = None |
| 172 |
self.tags = None |
| 173 |
|
| 174 |
self.results = dict(changed=False) |
| 175 |
self.mgmt_client = None |
| 176 |
self.url = None |
| 177 |
self.status_code = [200, 201, 202, 204] |
| 178 |
|
| 179 |
self.body = {} |
| 180 |
self.query_parameters = {} |
| 181 |
self.query_parameters['api-version'] = None |
| 182 |
self.header_parameters = {} |
| 183 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 184 |
|
| 185 |
self._managed_identity = None |
| 186 |
|
| 187 |
super(AzureRMRecoveryServicesVault, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 188 |
supports_check_mode=True, |
| 189 |
supports_tags=True |
| 190 |
) |
| 191 |
|
| 192 |
@property |
| 193 |
def managed_identity(self): |
| 194 |
if not self._managed_identity: |
| 195 |
self._managed_identity = {"identity": IdentityData, |
| 196 |
"user_assigned": UserIdentity |
| 197 |
} |
| 198 |
return self._managed_identity |
| 199 |
|
| 200 |
def get_api_version(self): |
| 201 |
return '2020-02-02' |
| 202 |
|
| 203 |
def get_url(self): |
| 204 |
if self.state == 'present' or self.state == 'absent': |
| 205 |
return '/subscriptions/' \ |
| 206 |
+ self.subscription_id \ |
| 207 |
+ '/resourceGroups/' \ |
| 208 |
+ self.resource_group \ |
| 209 |
+ '/providers/Microsoft.RecoveryServices' \ |
| 210 |
+ '/vaults' + '/' \ |
| 211 |
+ self.name |
| 212 |
|
| 213 |
def get_body(self): |
| 214 |
if self.state == 'present': |
| 215 |
return { |
| 216 |
"properties": {}, |
| 217 |
"sku": { |
| 218 |
"name": "Standard" |
| 219 |
}, |
| 220 |
"identity": self.identity, |
| 221 |
"location": self.location, |
| 222 |
'tags': self.tags |
| 223 |
} |
| 224 |
else: |
| 225 |
return {} |
| 226 |
|
| 227 |
def format_for_body(self, identity): |
| 228 |
if identity: |
| 229 |
identity = identity.as_dict() |
| 230 |
if identity.get("user_assigned_identities", None): |
| 231 |
identity["userAssignedIdentities"] = identity.pop("user_assigned_identities") |
| 232 |
return identity |
| 233 |
|
| 234 |
def format_for_helper(self, identity): |
| 235 |
if identity and identity.get("userAssignedIdentities"): |
| 236 |
identity["user_assigned_identities"] = identity.pop("userAssignedIdentities") |
| 237 |
return identity |
| 238 |
|
| 239 |
def exec_module(self, **kwargs): |
| 240 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 241 |
if hasattr(self, key): |
| 242 |
setattr(self, key, kwargs[key]) |
| 243 |
elif kwargs[key] is not None: |
| 244 |
self.body[key] = kwargs[key] |
| 245 |
|
| 246 |
self.inflate_parameters(self.module_arg_spec, self.body, 0) |
| 247 |
|
| 248 |
self.query_parameters['api-version'] = self.get_api_version() |
| 249 |
self.url = self.get_url() |
| 250 |
old_response = None |
| 251 |
response = None |
| 252 |
|
| 253 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 254 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 255 |
|
| 256 |
old_response = self.get_resource() |
| 257 |
|
| 258 |
update_identity = False |
| 259 |
if self.identity: |
| 260 |
old_identity = old_response and old_response.get('identity', None) |
| 261 |
old_identity = self.format_for_helper(old_identity) |
| 262 |
update_identity, identity = self.update_managed_identity(curr_identity=old_identity, |
| 263 |
new_identity=self.identity) |
| 264 |
self.identity = self.format_for_body(identity) |
| 265 |
else: |
| 266 |
if old_response is not False: |
| 267 |
self.identity = old_response.get('identity') |
| 268 |
|
| 269 |
update_tags = False |
| 270 |
if old_response is not False: |
| 271 |
update_tags, self.tags = self.update_tags(old_response.get('tags')) |
| 272 |
self.body = self.get_body() |
| 273 |
|
| 274 |
changed = False |
| 275 |
if self.state == 'present': |
| 276 |
if old_response is False: |
| 277 |
changed = True |
| 278 |
self.create_recovery_service_vault() |
| 279 |
elif update_identity is True or update_tags is True: |
| 280 |
changed = True |
| 281 |
self.create_recovery_service_vault() |
| 282 |
else: |
| 283 |
changed = False |
| 284 |
response = self.get_resource() |
| 285 |
if self.state == 'absent': |
| 286 |
changed = True |
| 287 |
response = self.delete_recovery_service_vault() |
| 288 |
|
| 289 |
self.results['response'] = response |
| 290 |
self.results['changed'] = changed |
| 291 |
|
| 292 |
return self.results |
| 293 |
|
| 294 |
def create_recovery_service_vault(self): |
| 295 |
|
| 296 |
try: |
| 297 |
response = self.mgmt_client.query( |
| 298 |
self.url, |
| 299 |
'PUT', |
| 300 |
self.query_parameters, |
| 301 |
self.header_parameters, |
| 302 |
self.body, |
| 303 |
self.status_code, |
| 304 |
600, |
| 305 |
30, |
| 306 |
) |
| 307 |
except Exception as e: |
| 308 |
self.log('Error in creating Azure Recovery Service Vault.') |
| 309 |
self.fail('Error in creating Azure Recovery Service Vault {0}'.format(str(e))) |
| 310 |
|
| 311 |
if hasattr(response, 'body'): |
| 312 |
response = json.loads(response.body()) |
| 313 |
elif hasattr(response, 'context'): |
| 314 |
response = response.context['deserialized_data'] |
| 315 |
else: |
| 316 |
self.fail("Create or Updating fail, no match message return, return info as {0}".format(response)) |
| 317 |
|
| 318 |
return response |
| 319 |
|
| 320 |
def delete_recovery_service_vault(self): |
| 321 |
|
| 322 |
try: |
| 323 |
response = self.mgmt_client.query( |
| 324 |
self.url, |
| 325 |
'DELETE', |
| 326 |
self.query_parameters, |
| 327 |
self.header_parameters, |
| 328 |
None, |
| 329 |
self.status_code, |
| 330 |
600, |
| 331 |
30, |
| 332 |
) |
| 333 |
except Exception as e: |
| 334 |
self.log('Error attempting to delete Azure Recovery Service Vault.') |
| 335 |
self.fail('Error while deleting Azure Recovery Service Vault: {0}'.format(str(e))) |
| 336 |
|
| 337 |
def get_resource(self): |
| 338 |
|
| 339 |
found = False |
| 340 |
retries = 0 |
| 341 |
retry_limit = 60 |
| 342 |
sleep_time = 2 |
| 343 |
try: |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
provisioning = True |
| 348 |
while provisioning is True and retries < retry_limit: |
| 349 |
response = self.mgmt_client.query( |
| 350 |
self.url, |
| 351 |
'GET', |
| 352 |
self.query_parameters, |
| 353 |
self.header_parameters, |
| 354 |
None, |
| 355 |
self.status_code, |
| 356 |
600, |
| 357 |
30, |
| 358 |
) |
| 359 |
response = json.loads(response.body()) |
| 360 |
provisioning_status = response['properties']['provisioningState'] |
| 361 |
provisioning = provisioning_status != 'Succeeded' and True or False |
| 362 |
retries += 1 |
| 363 |
time.sleep(sleep_time) |
| 364 |
found = True |
| 365 |
if retries >= retry_limit: |
| 366 |
self.module.warn( |
| 367 |
'Retry limit {0} exceeded while waiting for resource to provision'.format(retry_limit) |
| 368 |
) |
| 369 |
except Exception as e: |
| 370 |
self.log('Recovery Service Vault Does not exist.') |
| 371 |
if found is True: |
| 372 |
return response |
| 373 |
else: |
| 374 |
return False |
| 375 |
|
| 376 |
|
| 377 |
def main(): |
| 378 |
AzureRMRecoveryServicesVault() |
| 379 |
|
| 380 |
|
| 381 |
if __name__ == '__main__': |
| 382 |
main() |