| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_recoveryservicesvaultconfig |
| 13 |
version_added: "3.4.0" |
| 14 |
short_description: Set Azure Backup Vault Config |
| 15 |
description: |
| 16 |
- Get Azure Backup Vault Config |
| 17 |
- Most of the information here is also part of azure_rm_recoveryservicesvault |
| 18 |
- it's seperated out because of https://github.com/Azure/azure-rest-api-specs/issues/34218 |
| 19 |
- A vault config has to exist, so you can not change the state, you can only change values, only if they are overwriteable |
| 20 |
- Supports diff mode |
| 21 |
- Check mode assumes all changes you make are legal and accepted by the API |
| 22 |
|
| 23 |
options: |
| 24 |
vault_name: |
| 25 |
description: |
| 26 |
- The name of the Recovery Services Vault the vault config belongs to. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
resource_group: |
| 30 |
description: |
| 31 |
- The name of the resource group the vault is in. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
properties: |
| 35 |
description: |
| 36 |
- The recovery service vault config properties. |
| 37 |
- https://learn.microsoft.com/en-us/python/api/azure-mgmt-recoveryservicesbackup/ |
| 38 |
azure.mgmt.recoveryservicesbackup.activestamp.models.backupresourcevaultconfig?view=azure-python |
| 39 |
type: dict |
| 40 |
suboptions: |
| 41 |
enhanced_security_state: |
| 42 |
description: |
| 43 |
- Soft delete and security settings for hybrid workloads |
| 44 |
- Enables soft delete, MFA and alert notifications for workloads running on premises. |
| 45 |
- Refer to https://learn.microsoft.com/en-us/azure/backup/backup-azure-security-feature#minimum-version-requirements |
| 46 |
- for minimum version requirements |
| 47 |
- Can be reverted, unless set to AlwaysON |
| 48 |
type: str |
| 49 |
choices: |
| 50 |
- Disabled |
| 51 |
- Enabled |
| 52 |
- AlwaysON |
| 53 |
soft_delete_feature_state: |
| 54 |
description: |
| 55 |
- Enables soft delete for cloud workloads |
| 56 |
- https://learn.microsoft.com/en-us/azure/backup/backup-azure-enhanced-soft-delete-about#whats-enhanced-soft-delete |
| 57 |
- Can be reverted, unless set to AlwaysON |
| 58 |
type: str |
| 59 |
choices: |
| 60 |
- Disabled |
| 61 |
- Enabled |
| 62 |
- AlwaysON |
| 63 |
soft_delete_retention_period_in_days: |
| 64 |
description: |
| 65 |
- Duration of soft delete state |
| 66 |
- minimum 14 days |
| 67 |
- maximum 180 days |
| 68 |
type: int |
| 69 |
extends_documentation_fragment: |
| 70 |
- azure.azcollection.azure |
| 71 |
|
| 72 |
author: |
| 73 |
- Klaas Demter (@Klaas-) |
| 74 |
''' |
| 75 |
|
| 76 |
EXAMPLES = ''' |
| 77 |
- name: Set backup vault config soft delete retention time |
| 78 |
azure.azcollection.azure_rm_recoveryservicesvaultconfig: |
| 79 |
vault_name: Vault_Name |
| 80 |
resource_group: Resource_Group_Name |
| 81 |
properties: |
| 82 |
soft_delete_retention_period_in_days: 17 |
| 83 |
|
| 84 |
- name: Set backup vault config soft delete to always on for cloud and hybrid workloads (not reversible except by deleting vault) |
| 85 |
azure.azcollection.azure_rm_recoveryservicesvaultconfig: |
| 86 |
vault_name: Vault_Name |
| 87 |
resource_group: Resource_Group_Name |
| 88 |
properties: |
| 89 |
soft_delete_feature_state: AlwaysON |
| 90 |
enhanced_security_state: AlwaysON |
| 91 |
''' |
| 92 |
|
| 93 |
RETURN = ''' |
| 94 |
id: |
| 95 |
description: |
| 96 |
- Id of specified backup vault config. |
| 97 |
returned: always |
| 98 |
type: str |
| 99 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.RecoveryServices/vaults/Vault_Name/backupconfig/vaultconfig" |
| 100 |
name: |
| 101 |
description: |
| 102 |
- always returns "vaultconfig" |
| 103 |
type: str |
| 104 |
returned: always |
| 105 |
sample: "vaultconfig" |
| 106 |
type: |
| 107 |
description: |
| 108 |
- Type of backup vault config. |
| 109 |
type: str |
| 110 |
returned: always |
| 111 |
sample: "Microsoft.RecoveryServices/vaults/backupconfig" |
| 112 |
properties: |
| 113 |
description: |
| 114 |
- Attributes of the backup vault config. |
| 115 |
type: dict |
| 116 |
returned: always |
| 117 |
sample: { |
| 118 |
"enhanced_security_state": "Enabled", |
| 119 |
"is_soft_delete_feature_state_editable": true, |
| 120 |
"soft_delete_feature_state": "Enabled", |
| 121 |
"soft_delete_retention_period_in_days": 14 |
| 122 |
} |
| 123 |
''' |
| 124 |
|
| 125 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 126 |
|
| 127 |
try: |
| 128 |
from azure.core.exceptions import ResourceNotFoundError |
| 129 |
|
| 130 |
except ImportError: |
| 131 |
|
| 132 |
pass |
| 133 |
|
| 134 |
|
| 135 |
class AzureRMBackupVaultConfig(AzureRMModuleBaseExt): |
| 136 |
"""Information class for an Azure RM Backup Vault Configs""" |
| 137 |
|
| 138 |
def __init__(self): |
| 139 |
self.module_arg_spec = dict( |
| 140 |
vault_name=dict(type='str', required=True), |
| 141 |
resource_group=dict(type='str', required=True), |
| 142 |
properties=dict( |
| 143 |
type='dict', |
| 144 |
options=dict( |
| 145 |
enhanced_security_state=dict(type='str', choices=['Disabled', 'Enabled', 'AlwaysON']), |
| 146 |
soft_delete_feature_state=dict(type='str', choices=['Disabled', 'Enabled', 'AlwaysON']), |
| 147 |
soft_delete_retention_period_in_days=dict(type='int') |
| 148 |
) |
| 149 |
) |
| 150 |
) |
| 151 |
|
| 152 |
self.vault_name = None |
| 153 |
self.resource_group = None |
| 154 |
self.properties = None |
| 155 |
self.log_path = None |
| 156 |
self.log_mode = None |
| 157 |
|
| 158 |
self.results = dict( |
| 159 |
id=None, |
| 160 |
changed=False, |
| 161 |
diff=dict( |
| 162 |
before=None, |
| 163 |
after=None |
| 164 |
) |
| 165 |
) |
| 166 |
|
| 167 |
super(AzureRMBackupVaultConfig, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 168 |
supports_check_mode=True, |
| 169 |
supports_tags=False) |
| 170 |
|
| 171 |
def exec_module(self, **kwargs): |
| 172 |
"""Main module execution method""" |
| 173 |
|
| 174 |
for key in self.module_arg_spec: |
| 175 |
setattr(self, key, kwargs[key]) |
| 176 |
|
| 177 |
existing_backup_vault_config_object = None |
| 178 |
existing_backup_vault_config = None |
| 179 |
|
| 180 |
existing_backup_vault_config_object = self.get_vault_config() |
| 181 |
|
| 182 |
if existing_backup_vault_config_object: |
| 183 |
existing_backup_vault_config = self.serialize_obj(existing_backup_vault_config_object, 'BackupResourceVaultConfigResource') |
| 184 |
|
| 185 |
if existing_backup_vault_config: |
| 186 |
self.log("Vault exists, update config") |
| 187 |
result_compare = dict(compare=[]) |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
update_backup_vault_config = dict( |
| 192 |
properties=self.properties |
| 193 |
) |
| 194 |
merged_backup_vault_config = existing_backup_vault_config.copy() |
| 195 |
|
| 196 |
merged_backup_vault_config['properties'] = {**existing_backup_vault_config['properties'], **update_backup_vault_config['properties']} |
| 197 |
|
| 198 |
if not self.default_compare({}, merged_backup_vault_config, existing_backup_vault_config, '', result_compare): |
| 199 |
self.log("Properties update existing vault config") |
| 200 |
if self.check_mode: |
| 201 |
self.log("Check mode, not actually updating") |
| 202 |
self.set_results(merged_backup_vault_config, True, existing_backup_vault_config) |
| 203 |
else: |
| 204 |
self.log("Updating Vault config") |
| 205 |
updated_backup_vault_config = self.serialize_obj(self.set_vault_config(merged_backup_vault_config), 'BackupResourceVaultConfigResource') |
| 206 |
self.set_results(updated_backup_vault_config, True, existing_backup_vault_config) |
| 207 |
else: |
| 208 |
self.log("Properties do not update existing vault config") |
| 209 |
self.set_results(existing_backup_vault_config, False, existing_backup_vault_config) |
| 210 |
else: |
| 211 |
self.log("Vault does not exist") |
| 212 |
self.fail("Vault {0} does not exist in resource group {1}, failing".format(self.vault_name, self.resource_group)) |
| 213 |
|
| 214 |
return self.results |
| 215 |
|
| 216 |
def get_vault_config(self): |
| 217 |
''' |
| 218 |
Gets the properties of the specified backup vault config. |
| 219 |
https://learn.microsoft.com/en-us/python/api/azure-mgmt-recoveryservicesbackup/azure.mgmt.recoveryservicesbackup.activestamp.aio.operations.backupresourcevaultconfigsoperations?view=azure-python |
| 220 |
|
| 221 |
:return: BackupResourceVaultConfigResource |
| 222 |
''' |
| 223 |
self.log("Checking for vault config of vault {0} in resource group {1}".format(self.vault_name, self.resource_group)) |
| 224 |
|
| 225 |
vault_config = None |
| 226 |
|
| 227 |
try: |
| 228 |
vault_config = self.recovery_services_backup_client.backup_resource_vault_configs.get(vault_name=self.vault_name, |
| 229 |
resource_group_name=self.resource_group) |
| 230 |
except ResourceNotFoundError as ex: |
| 231 |
self.log("Could not find vault {0} in resource group {1}".format(self.vault_name, self.resource_group)) |
| 232 |
|
| 233 |
return vault_config |
| 234 |
|
| 235 |
def set_vault_config(self, merged_backup_vault_config): |
| 236 |
''' |
| 237 |
Sets the properties of the specified backup vault config. |
| 238 |
https://learn.microsoft.com/en-us/python/api/azure-mgmt-recoveryservicesbackup/azure.mgmt.recoveryservicesbackup.activestamp.aio.operations.backupresourcevaultconfigsoperations?view=azure-python |
| 239 |
|
| 240 |
:return: BackupResourceVaultConfigResource |
| 241 |
''' |
| 242 |
self.log("Updating vault config of vault {0} in resource group {1}".format(self.vault_name, self.resource_group)) |
| 243 |
|
| 244 |
vault_config = None |
| 245 |
|
| 246 |
try: |
| 247 |
vault_config = self.recovery_services_backup_client.backup_resource_vault_configs.update(vault_name=self.vault_name, |
| 248 |
resource_group_name=self.resource_group, |
| 249 |
parameters=merged_backup_vault_config) |
| 250 |
except ResourceNotFoundError as ex: |
| 251 |
self.log("Could not find vault {0} in resource group {1}".format(self.vault_name, self.resource_group)) |
| 252 |
|
| 253 |
return vault_config |
| 254 |
|
| 255 |
def set_results(self, vault_config, changed=False, before=None): |
| 256 |
if vault_config: |
| 257 |
self.results['id'] = vault_config['id'] |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
self.results['name'] = vault_config['name'] |
| 262 |
self.results['type'] = vault_config['type'] |
| 263 |
self.results['properties'] = vault_config['properties'] |
| 264 |
self.results['changed'] = changed |
| 265 |
self.results['diff']['before'] = before |
| 266 |
self.results['diff']['after'] = vault_config |
| 267 |
|
| 268 |
else: |
| 269 |
self.results['id'] = None |
| 270 |
self.results['name'] = None |
| 271 |
self.results['type'] = None |
| 272 |
|
| 273 |
|
| 274 |
def main(): |
| 275 |
"""Main execution""" |
| 276 |
AzureRMBackupVaultConfig() |
| 277 |
|
| 278 |
|
| 279 |
if __name__ == '__main__': |
| 280 |
main() |