Skip to content

Commit ef8a5e8

Browse files
authored
[Backup] az backup item set-policy: Add warning message for migration from Standard to Enhanced Policy (#28317)
1 parent 2ba94b2 commit ef8a5e8

File tree

5 files changed

+5193
-5982
lines changed

5 files changed

+5193
-5982
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def load_arguments(self, _):
207207
c.argument('backup_management_type', backup_management_type)
208208
c.argument('workload_type', workload_type)
209209
c.argument('tenant_id', help='ID of the tenant if the Resource Guard protecting the vault exists in a different tenant.')
210+
c.argument('yes', options_list=['--yes', '-y'], help='Skip confirmation when updating Standard to Enhanced Policies.', action='store_true')
210211

211212
with self.argument_context('backup item list') as c:
212213
c.argument('vault_name', vault_name_type, id_part=None)

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

+30-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from datetime import datetime, timedelta, timezone
99
# pylint: disable=too-many-lines
1010
from knack.log import get_logger
11+
from knack.prompting import prompt_y_n
1112
from azure.mgmt.core.tools import is_valid_resource_id
1213

1314
from azure.mgmt.recoveryservicesbackup.activestamp import RecoveryServicesBackupClient
@@ -126,6 +127,12 @@
126127
os_linux = 'Linux'
127128
password_offset = 33
128129
password_length = 15
130+
vm_policy_type_map = {
131+
'v2': 'enhanced',
132+
'v1': 'standard'
133+
}
134+
enhanced_policy_type = "v2"
135+
standard_policy_type = "v1"
129136
# pylint: disable=too-many-function-args
130137

131138

@@ -173,9 +180,9 @@ def create_vault(client, vault_name, resource_group_name, location, tags=None,
173180
"to their default values. It is recommended to use az backup vault update instead.")
174181

175182
# If the vault exists, we move to the update flow instead
176-
update_vault(client, vault_name, resource_group_name, tags, public_network_access,
177-
immutability_state, cross_subscription_restore_state, classic_alerts,
178-
azure_monitor_alerts_for_job_failures)
183+
return update_vault(client, vault_name, resource_group_name, tags, public_network_access,
184+
immutability_state, cross_subscription_restore_state, classic_alerts,
185+
azure_monitor_alerts_for_job_failures)
179186
except CoreResourceNotFoundError:
180187
vault_properties = VaultProperties()
181188

@@ -983,7 +990,7 @@ def list_items(cmd, client, resource_group_name, vault_name, container_name=None
983990

984991

985992
def update_policy_for_item(cmd, client, resource_group_name, vault_name, item, policy, tenant_id=None,
986-
is_critical_operation=False):
993+
is_critical_operation=False, yes=False):
987994
if item.properties.backup_management_type != policy.properties.backup_management_type:
988995
raise CLIError(
989996
"""
@@ -1004,16 +1011,33 @@ def update_policy_for_item(cmd, client, resource_group_name, vault_name, item, p
10041011
vm_item_properties.policy_id = policy.id
10051012
vm_item_properties.source_resource_id = item.properties.source_resource_id
10061013
vm_item = ProtectedItemResource(properties=vm_item_properties)
1014+
existing_policy = common.show_policy(protection_policies_cf(cmd.cli_ctx), resource_group_name, vault_name,
1015+
item.properties.policy_name)
1016+
10071017
if is_critical_operation:
1008-
existing_policy = common.show_policy(protection_policies_cf(cmd.cli_ctx), resource_group_name, vault_name,
1009-
item.properties.policy_name)
10101018
if cust_help.is_retention_duration_decreased(existing_policy, policy, "AzureIaasVM"):
10111019
# update the payload with critical operation and add auxiliary header for cross tenant case
10121020
if tenant_id is not None:
10131021
client = get_mgmt_service_client(cmd.cli_ctx, RecoveryServicesBackupClient,
10141022
aux_tenants=[tenant_id]).protected_items
10151023
vm_item.properties.resource_guard_operation_requests = [cust_help.get_resource_guard_operation_request(
10161024
cmd.cli_ctx, resource_group_name, vault_name, "updateProtection")]
1025+
1026+
# Raise warning for standard->enhanced policy
1027+
try:
1028+
existing_policy_type = existing_policy.properties.policy_type.lower()
1029+
new_policy_type = policy.properties.policy_type.lower()
1030+
if (not yes and
1031+
new_policy_type in vm_policy_type_map and vm_policy_type_map[new_policy_type] == 'enhanced' and
1032+
existing_policy_type in vm_policy_type_map and vm_policy_type_map[existing_policy_type] == 'standard'):
1033+
warning_prompt = ('Upgrading to enhanced policy can incur additional charges. Once upgraded to the enhanced '
1034+
'policy, it is not possible to revert back to the standard policy. Do you want to continue?')
1035+
if not prompt_y_n(warning_prompt):
1036+
logger.warning('Cancelling policy update operation')
1037+
return None
1038+
except (AttributeError):
1039+
logger.warning("Unable to fetch policy type for either existing or new policy. Proceeding with update.")
1040+
10171041
# Update policy
10181042
result = client.create_or_update(vault_name, resource_group_name, fabric_name,
10191043
container_uri, item_uri, vm_item, cls=cust_help.get_pipeline_response)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def disable_protection(cmd, client, resource_group_name, vault_name, item_name,
208208

209209

210210
def update_policy_for_item(cmd, client, resource_group_name, vault_name, container_name, item_name, policy_name,
211-
workload_type=None, backup_management_type=None, tenant_id=None):
211+
workload_type=None, backup_management_type=None, tenant_id=None, yes=None):
212212

213213
items_client = backup_protected_items_cf(cmd.cli_ctx)
214214
item = show_item(cmd, items_client, resource_group_name, vault_name, container_name, item_name,
@@ -226,7 +226,7 @@ def update_policy_for_item(cmd, client, resource_group_name, vault_name, contain
226226

227227
if item.properties.backup_management_type.lower() == "azureiaasvm":
228228
return custom.update_policy_for_item(cmd, client, resource_group_name, vault_name, item, policy, tenant_id,
229-
is_critical_operation)
229+
is_critical_operation, yes)
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,

0 commit comments

Comments
 (0)