Skip to content

Commit 20423fe

Browse files
author
ianna1-admin
committed
backup tier client side validations + tests
1 parent 1a67dde commit 20423fe

File tree

11 files changed

+19549
-1632
lines changed

11 files changed

+19549
-1632
lines changed

src/azure-cli/azure/cli/command_modules/backup/_params.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ def load_arguments(self, _):
237237
c.argument('fix_for_inconsistent_items', arg_type=get_three_state_flag(), options_list=['--fix-for-inconsistent-items'], help='Specify whether or not to retry Policy Update for failed items.')
238238
c.argument('backup_management_type', backup_management_type)
239239
c.argument('tenant_id', help='ID of the tenant if the Resource Guard protecting the vault exists in a different tenant.')
240-
240+
c.argument('yes', options_list=['--yes', '-y'], help='Skip confirmation when updating Standard to Enhanced Policies.', action='store_true')
241+
241242
with self.argument_context('backup policy create') as c:
242243
c.argument('policy', type=file_type, help='JSON encoded policy definition. Use the show command with JSON output to obtain a policy object. Modify the values using a file editor and pass the object.', completer=FilesCompleter())
243244
c.argument('name', options_list=['--name', '-n'], help='Name of the Policy.')

src/azure-cli/azure/cli/command_modules/backup/custom_afs.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def list_recovery_points(cmd, client, resource_group_name, vault_name, item, sta
313313

314314

315315
def update_policy_for_item(cmd, client, resource_group_name, vault_name, item, policy, tenant_id=None,
316-
is_critical_operation=False):
316+
is_critical_operation=False, yes=False):
317317
if item.properties.backup_management_type != policy.properties.backup_management_type:
318318
raise CLIError(
319319
"""
@@ -346,7 +346,7 @@ def update_policy_for_item(cmd, client, resource_group_name, vault_name, item, p
346346
existing_policy_name = item.properties.policy_id.split('/')[-1]
347347
existing_policy = common.show_policy(protection_policies_cf(cmd.cli_ctx), resource_group_name, vault_name,
348348
existing_policy_name)
349-
helper.validate_update_policy_request(existing_policy, policy)
349+
helper.validate_update_policy_request(existing_policy, policy, yes)
350350

351351
# Update policy
352352
result = client.create_or_update(vault_name, resource_group_name, fabric_name,
@@ -411,7 +411,7 @@ def _get_storage_account_id(cli_ctx, storage_account_name, storage_account_rg):
411411

412412

413413
def set_policy(cmd, client, resource_group_name, vault_name, policy, policy_name, tenant_id=None,
414-
is_critical_operation=False):
414+
is_critical_operation=False, yes=False):
415415
if policy_name is None:
416416
raise CLIError(
417417
"""
@@ -421,7 +421,8 @@ def set_policy(cmd, client, resource_group_name, vault_name, policy, policy_name
421421
policy_object = helper.get_policy_from_json(client, policy)
422422
policy_object.properties.work_load_type = workload_type
423423
existing_policy = common.show_policy(client, resource_group_name, vault_name, policy_name)
424-
helper.validate_update_policy_request(existing_policy, policy_object)
424+
425+
helper.validate_update_policy_request(existing_policy, policy_object, yes)
425426
if is_critical_operation:
426427
if helper.is_retention_duration_decreased(existing_policy, policy_object, "AzureStorage"):
427428
# update the payload with critical operation and add auxiliary header for cross tenant case

src/azure-cli/azure/cli/command_modules/backup/custom_base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def update_policy_for_item(cmd, client, resource_group_name, vault_name, contain
230230

231231
if item.properties.backup_management_type.lower() == "azurestorage":
232232
return custom_afs.update_policy_for_item(cmd, client, resource_group_name, vault_name, item, policy, tenant_id,
233-
is_critical_operation)
233+
is_critical_operation, yes)
234234

235235
if item.properties.backup_management_type.lower() == "azureworkload":
236236
return custom_wl.update_policy_for_item(cmd, client, resource_group_name, vault_name, item, policy, tenant_id,
@@ -239,19 +239,18 @@ def update_policy_for_item(cmd, client, resource_group_name, vault_name, contain
239239

240240

241241
def set_policy(cmd, client, resource_group_name, vault_name, policy=None, name=None,
242-
fix_for_inconsistent_items=None, backup_management_type=None, tenant_id=None):
242+
fix_for_inconsistent_items=None, backup_management_type=None, tenant_id=None, yes=False):
243243
if backup_management_type is None and policy is not None:
244244
policy_object = custom_help.get_policy_from_json(client, policy)
245245
backup_management_type = policy_object.properties.backup_management_type.lower()
246246
is_critical_operation = custom_help.has_resource_guard_mapping(cmd.cli_ctx, resource_group_name, vault_name,
247247
"updatePolicy")
248-
249248
if backup_management_type.lower() == "azureiaasvm":
250249
return custom.set_policy(cmd, client, resource_group_name, vault_name, policy, name, tenant_id,
251250
is_critical_operation)
252251
if backup_management_type.lower() == "azurestorage":
253252
return custom_afs.set_policy(cmd, client, resource_group_name, vault_name, policy, name, tenant_id,
254-
is_critical_operation)
253+
is_critical_operation, yes)
255254
if backup_management_type.lower() == "azureworkload":
256255
return custom_wl.set_policy(cmd, client, resource_group_name, vault_name, policy, name,
257256
fix_for_inconsistent_items, tenant_id, is_critical_operation)

src/azure-cli/azure/cli/command_modules/backup/custom_help.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def validate_and_extract_container_type(container_name, backup_management_type):
672672
return None
673673

674674

675-
def validate_update_policy_request(existing_policy, new_policy):
675+
def validate_update_policy_request(existing_policy, new_policy, yes=False):
676676
existing_backup_management_type = existing_policy.properties.backup_management_type
677677
new_backup_management_type = new_policy.properties.backup_management_type
678678
if existing_backup_management_type != new_backup_management_type:
@@ -685,7 +685,7 @@ def validate_update_policy_request(existing_policy, new_policy):
685685
Please create a new policy for snapshot-only backups.
686686
""")
687687
# snapshot -> vault
688-
if hasattr(existing_policy.properties, 'retention_policy') and existing_policy.properties.retention_policy is not None and hasattr(new_policy.properties, 'vault_retention_policy') and new_policy.properties.vault_retention_policy is not None:
688+
if not yes and hasattr(existing_policy.properties, 'retention_policy') and existing_policy.properties.retention_policy is not None and hasattr(new_policy.properties, 'vault_retention_policy') and new_policy.properties.vault_retention_policy is not None:
689689
warning_prompt = ('Changing the backup tier keeps current snapshots as-is under the existing policy. Future backups will be stored in the vault with new retention settings.'
690690
'This action is irreversible and incurs additional costs. Switching from vault to snapshot requires reconfiguration.'
691691
'Learn more at https://learn.microsoft.com/en-us/azure/backup/azure-file-share-backup-overview?tabs=snapshot.')

src/azure-cli/azure/cli/command_modules/backup/tests/latest/preparers.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def _get_key_url(self, **kwargs):
395395
class AFSPolicyPreparer(AbstractPreparer, SingleValueReplacer):
396396
def __init__(self, name_prefix='clitest-item', parameter_name='policy_name', vault_parameter_name='vault_name',
397397
resource_group_parameter_name='resource_group',
398-
instant_rp_days=None):
398+
backup_tier="Snapshot"):
399399
super(AFSPolicyPreparer, self).__init__(name_prefix, 24)
400400
from azure.cli.core.mock import DummyCli
401401
self.cli_ctx = DummyCli()
@@ -404,7 +404,7 @@ def __init__(self, name_prefix='clitest-item', parameter_name='policy_name', vau
404404
self.resource_group_parameter_name = resource_group_parameter_name
405405
self.vault = None
406406
self.vault_parameter_name = vault_parameter_name
407-
self.instant_rp_days = instant_rp_days
407+
self.backup_tier = backup_tier
408408

409409
def create_resource(self, name, **kwargs):
410410
if not os.environ.get('AZURE_CLI_TEST_DEV_BACKUP_POLICY_NAME', None):
@@ -413,10 +413,46 @@ def create_resource(self, name, **kwargs):
413413

414414
policy_json = execute(self.cli_ctx, 'az backup policy show -g {} -v {} -n {}'
415415
.format(self.resource_group, self.vault, 'DefaultPolicy')).get_output_in_json()
416+
417+
# Remove unwanted keys from default AzureVM policy
418+
keys_to_remove = [
419+
'instantRpDetails',
420+
'instantRpRetentionRangeInDays',
421+
'policyType',
422+
'snapshotConsistencyType',
423+
'tieringPolicy'
424+
]
425+
426+
for key in keys_to_remove:
427+
policy_json['properties'].pop(key, None)
428+
416429
policy_json['name'] = name
417-
if self.instant_rp_days:
418-
policy_json['properties']['instantRpRetentionRangeInDays'] = self.instant_rp_days
430+
419431
policy_json['properties']['backupManagementType'] = "AzureStorage"
432+
policy_json['properties']['workLoadType'] = "AzureFileShare"
433+
434+
# Modify the policy based on the backup tier
435+
if self.backup_tier.lower() == 'vaultstandard':
436+
# Set retentionPolicy to null
437+
policy_json['properties'].pop('retentionPolicy', None)
438+
439+
# Add vaultRetentionPolicy with the required properties
440+
policy_json['properties']['vaultRetentionPolicy'] = {
441+
"snapshotRetentionInDays": 5,
442+
"vaultRetention": {
443+
"dailySchedule": {
444+
"retentionDuration": {
445+
"count": 30,
446+
"durationType": "Days"
447+
},
448+
"retentionTimes": policy_json['properties']['schedulePolicy']['scheduleRunTimes']
449+
},
450+
"monthlySchedule": None,
451+
"retentionPolicyType": "LongTermRetentionPolicy",
452+
"weeklySchedule": None,
453+
"yearlySchedule": None
454+
}
455+
}
420456
policy_json = json.dumps(policy_json)
421457

422458
command_string = 'az backup policy create -g {} -v {} --policy \'{}\' -n {} --backup-management-type {}'

0 commit comments

Comments
 (0)