Skip to content

Commit cbb9634

Browse files
authored
[Az.RecoveryServices.Backup] Added support for customRG with create/modify policy (#22751)
* Added support for custom RG with suffix while creating or modifying policy for workload type AzureVM. Added test case. Added TLaD warning. * Updated new parameter names Regenerated help * Updated parameter name as per PR review suggestion * suppressing EmamplesIssues
1 parent 2b62f4e commit cbb9634

File tree

14 files changed

+493
-292
lines changed

14 files changed

+493
-292
lines changed

src/RecoveryServices/RecoveryServices.Backup.Models/AzureVmModels/AzureVmRecoveryPoint.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public class AzureVmRecoveryPoint : AzureRecoveryPoint
8686
/// </summary>
8787
public IDictionary<string, RecoveryPointMoveReadinessInfo> RecoveryPointMoveReadinessInfo;
8888

89+
// TODO: public extendedLocation for edge zone support
90+
8991
public AzureVmRecoveryPoint()
9092
{
9193

src/RecoveryServices/RecoveryServices.Backup.Models/CmdletParamEnums.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ public enum PolicyParams
133133
PolicySubType,
134134
ExistingPolicy,
135135
TieringPolicy,
136-
IsSmartTieringEnabled
136+
IsSmartTieringEnabled,
137+
BackupSnapshotResourceGroup,
138+
BackupSnapshotResourceGroupSuffix
137139
}
138140

139141
public enum ItemParams

src/RecoveryServices/RecoveryServices.Backup.Providers/Providers/IaasVmPsBackupProvider.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
3535
using CrrModel = Microsoft.Azure.Management.RecoveryServices.Backup.CrossRegionRestore.Models;
3636
using SystemNet = System.Net;
37+
using Microsoft.Azure.Commands.Common.Exceptions;
3738

3839
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel
3940
{
@@ -87,6 +88,8 @@ public RestAzureNS.AzureOperationResponse<ProtectedItemResource> EnableProtectio
8788
string auxiliaryAccessToken = ProviderData.ContainsKey(ResourceGuardParams.Token) ? (string)ProviderData[ResourceGuardParams.Token] : null;
8889
bool isMUAOperation = ProviderData.ContainsKey(ResourceGuardParams.IsMUAOperation) ? (bool)ProviderData[ResourceGuardParams.IsMUAOperation] : false;
8990

91+
Logger.Instance.WriteWarning("Ignite (November) 2023 onwards Virtual Machine deployments using PS and CLI will default to Trusted Launch configuration. You need to ensure Policy Name used with this command is of type Enhanced Policy for Trusted Launch VMs. Non-Trusted Launch Virtual Machines will not be impacted by this change. To know more about default change and Trusted Launch, please visit https://aka.ms/TLaD.");
92+
9093
ProtectionPolicyResource oldPolicy = null;
9194
ProtectionPolicyResource newPolicy = null;
9295
if (parameterSetName.Contains("Modify") && item.PolicyId != null && item.PolicyId != "")
@@ -843,10 +846,12 @@ public ProtectionPolicyResource CreatePolicy()
843846
ProviderData.ContainsKey(PolicyParams.SchedulePolicy) ?
844847
(SchedulePolicyBase)ProviderData[PolicyParams.SchedulePolicy] :
845848
null;
846-
847849
CmdletModel.TieringPolicy tieringDetails = ProviderData.ContainsKey(PolicyParams.TieringPolicy) ? (CmdletModel.TieringPolicy)ProviderData[PolicyParams.TieringPolicy] : null;
848850
bool isSmartTieringEnabled = ProviderData.ContainsKey(PolicyParams.IsSmartTieringEnabled) ? (bool)ProviderData[PolicyParams.IsSmartTieringEnabled] : false;
849851

852+
string snapshotRGName = (string)ProviderData[PolicyParams.BackupSnapshotResourceGroup];
853+
string snapshotRGNameSuffix = (string)ProviderData[PolicyParams.BackupSnapshotResourceGroupSuffix];
854+
850855
// do validations
851856
ValidateAzureVMWorkloadType(workloadType);
852857

@@ -913,7 +918,21 @@ public ProtectionPolicyResource CreatePolicy()
913918
timeZone = timeZoneInput;
914919
}
915920
}
916-
921+
922+
InstantRPAdditionalDetails instantRPAdditionalDetails = null;
923+
if (snapshotRGName != null)
924+
{
925+
instantRPAdditionalDetails = new InstantRPAdditionalDetails();
926+
927+
instantRPAdditionalDetails.AzureBackupRGNamePrefix = snapshotRGName;
928+
if (snapshotRGNameSuffix != null) instantRPAdditionalDetails.AzureBackupRGNameSuffix = snapshotRGNameSuffix;
929+
}
930+
else if(snapshotRGNameSuffix != null)
931+
{
932+
// resx
933+
throw new ArgumentException("The parameter BackupSnapshotResourceGroupSuffix cannot be used without the BackupSnapshotResourceGroup parameter. Please provide the BackupSnapshotResourceGroup parameter or remove the BackupSnapshotResourceGroupSuffix parameter.");
934+
}
935+
917936
// construct Service Client policy request
918937
ProtectionPolicyResource serviceClientRequest = new ProtectionPolicyResource()
919938
{
@@ -925,7 +944,8 @@ public ProtectionPolicyResource CreatePolicy()
925944
TieringPolicy = PolicyHelpers.GetServiceClientTieringPolicy(tieringDetails, isSmartTieringEnabled),
926945
PolicyType = (schedulePolicy.GetType() == typeof(CmdletModel.SimpleSchedulePolicyV2)) ? "V2" : null,
927946
TimeZone = timeZone,
928-
InstantRpRetentionRangeInDays = snapshotRetentionInDays
947+
InstantRpRetentionRangeInDays = snapshotRetentionInDays,
948+
InstantRPDetails = instantRPAdditionalDetails
929949
}
930950
};
931951

@@ -956,6 +976,9 @@ public RestAzureNS.AzureOperationResponse<ProtectionPolicyResource> ModifyPolicy
956976
CmdletModel.TieringPolicy tieringDetails = ProviderData.ContainsKey(PolicyParams.TieringPolicy) ? (CmdletModel.TieringPolicy)ProviderData[PolicyParams.TieringPolicy] : null;
957977
bool isSmartTieringEnabled = ProviderData.ContainsKey(PolicyParams.IsSmartTieringEnabled) ? (bool)ProviderData[PolicyParams.IsSmartTieringEnabled] : false;
958978

979+
string snapshotRGName = (string)ProviderData[PolicyParams.BackupSnapshotResourceGroup];
980+
string snapshotRGNameSuffix = (string)ProviderData[PolicyParams.BackupSnapshotResourceGroupSuffix];
981+
959982
// do validations
960983
ValidateAzureVMProtectionPolicy(policy);
961984
Logger.Instance.WriteDebug("Validation of Protection Policy is successful");
@@ -1032,7 +1055,21 @@ public RestAzureNS.AzureOperationResponse<ProtectionPolicyResource> ModifyPolicy
10321055
timeZone = timeZoneInput;
10331056
}
10341057
}
1035-
1058+
1059+
InstantRPAdditionalDetails instantRPAdditionalDetails = null;
1060+
if (snapshotRGName != null)
1061+
{
1062+
instantRPAdditionalDetails = new InstantRPAdditionalDetails();
1063+
1064+
instantRPAdditionalDetails.AzureBackupRGNamePrefix = snapshotRGName;
1065+
if (snapshotRGNameSuffix != null) instantRPAdditionalDetails.AzureBackupRGNameSuffix = snapshotRGNameSuffix;
1066+
}
1067+
else if (snapshotRGNameSuffix != null)
1068+
{
1069+
// resx
1070+
throw new ArgumentException("The parameter BackupSnapshotResourceGroupSuffix cannot be used without the BackupSnapshotResourceGroup parameter. Please provide the BackupSnapshotResourceGroup parameter or remove the BackupSnapshotResourceGroupSuffix parameter.");
1071+
}
1072+
10361073
// construct Service Client policy request
10371074
ProtectionPolicyResource serviceClientRequest = new ProtectionPolicyResource()
10381075
{
@@ -1045,7 +1082,7 @@ public RestAzureNS.AzureOperationResponse<ProtectionPolicyResource> ModifyPolicy
10451082
TimeZone = timeZone,
10461083
PolicyType = (((AzureVmPolicy)policy).SchedulePolicy.GetType() == typeof(CmdletModel.SimpleSchedulePolicyV2)) ? "V2" : null,
10471084
InstantRpRetentionRangeInDays = ((AzureVmPolicy)policy).SnapshotRetentionInDays,
1048-
InstantRPDetails = new InstantRPAdditionalDetails(
1085+
InstantRPDetails = (instantRPAdditionalDetails != null)? instantRPAdditionalDetails : new InstantRPAdditionalDetails(
10491086
((AzureVmPolicy)policy).AzureBackupRGName,
10501087
((AzureVmPolicy)policy).AzureBackupRGNameSuffix)
10511088
}

src/RecoveryServices/RecoveryServices.Backup.Test/ScenarioTests/IaasVm/PolicyTests.ps1

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,38 @@ function Test-AzureVMSmartTieringPolicy
9999
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType AzureVM -BackupManagementType AzureVM -ScheduleRunFrequency Weekly
100100

101101
# create tier recommended policy
102-
$pol = New-AzRecoveryServicesBackupProtectionPolicy -Name $tierRecommendedPolicy -WorkloadType AzureVM -BackupManagementType AzureVM -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $vault.ID -MoveToArchiveTier $true -TieringMode TierRecommended
102+
$pol = New-AzRecoveryServicesBackupProtectionPolicy -Name $tierRecommendedPolicy -WorkloadType AzureVM -BackupManagementType AzureVM -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $vault.ID -MoveToArchiveTier $true -TieringMode TierRecommended -BackupSnapshotResourceGroup "rgpref1" -BackupSnapshotResourceGroupSuffix "suffix1"
103+
104+
Assert-True { $pol.Name -eq $tierRecommendedPolicy }
105+
Assert-True { $pol.AzureBackupRGName -match "rgpref1" }
106+
Assert-True { $pol.AzureBackupRGNameSuffix -match "suffix1" }
103107

104-
Assert-True { $pol.Name -eq $tierRecommendedPolicy }
105-
106108
# error scenario for tier after policy
107-
Assert-ThrowsContains { $pol = New-AzRecoveryServicesBackupProtectionPolicy -Name $tierAfterPolicy -WorkloadType AzureVM -BackupManagementType AzureVM -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $vault.ID -MoveToArchiveTier $true -TieringMode TierAllEligible -TierAfterDuration 2 -TierAfterDurationType Months } `
109+
Assert-ThrowsContains { $pol = New-AzRecoveryServicesBackupProtectionPolicy -Name $tierAfterPolicy -WorkloadType AzureVM -BackupManagementType AzureVM -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $vault.ID -MoveToArchiveTier $true -TieringMode TierAllEligible -TierAfterDuration 2 -TierAfterDurationType Months -BackupSnapshotResourceGroup "rgpref1" -BackupSnapshotResourceGroupSuffix "suffix1"} `
108110
"TierAfterDuration needs to be >= 3 months, at least one of monthly or yearly retention should be >= (TierAfterDuration + 6) months";
109111

110112
# create tier after policy
111-
$pol = New-AzRecoveryServicesBackupProtectionPolicy -Name $tierAfterPolicy -WorkloadType AzureVM -BackupManagementType AzureVM -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $vault.ID -MoveToArchiveTier $true -TieringMode TierAllEligible -TierAfterDuration 3 -TierAfterDurationType Months
113+
$pol = New-AzRecoveryServicesBackupProtectionPolicy -Name $tierAfterPolicy -WorkloadType AzureVM -BackupManagementType AzureVM -RetentionPolicy $retPol -SchedulePolicy $schPol -VaultId $vault.ID -MoveToArchiveTier $true -TieringMode TierAllEligible -TierAfterDuration 3 -TierAfterDurationType Months -BackupSnapshotResourceGroup "rgpref1" -BackupSnapshotResourceGroupSuffix "suffix1"
112114

113115
Assert-True { $pol.Name -eq $tierAfterPolicy }
116+
Assert-True { $pol.AzureBackupRGName -match "rgpref1" }
117+
Assert-True { $pol.AzureBackupRGNameSuffix -match "suffix1" }
114118

115119
# modify policy
116120
$pol = Get-AzRecoveryServicesBackupProtectionPolicy -VaultId $vault.ID | Where { $_.Name -match $tierRecommendedPolicy }
117121
Set-AzRecoveryServicesBackupProtectionPolicy -VaultId $vault.ID -Policy $pol[0] -MoveToArchiveTier $false
118-
Set-AzRecoveryServicesBackupProtectionPolicy -VaultId $vault.ID -Policy $pol[0] -MoveToArchiveTier $true -TieringMode TierRecommended
122+
123+
$pol = Get-AzRecoveryServicesBackupProtectionPolicy -VaultId $vault.ID | Where { $_.Name -match $tierRecommendedPolicy }
124+
Assert-True { $pol.AzureBackupRGName -match "rgpref1" }
125+
Assert-True { $pol.AzureBackupRGNameSuffix -match "suffix1" }
126+
127+
Set-AzRecoveryServicesBackupProtectionPolicy -VaultId $vault.ID -Policy $pol[0] -MoveToArchiveTier $true -TieringMode TierRecommended -BackupSnapshotResourceGroup "rgpref2"
119128

120129
# error scenario for retention policy
121130
$pol = Get-AzRecoveryServicesBackupProtectionPolicy -VaultId $vault.ID | Where { $_.Name -match $tierRecommendedPolicy }
131+
Assert-True { $pol.AzureBackupRGName -match "rgpref2" }
132+
Assert-True { $pol.AzureBackupRGNameSuffix -eq $null }
133+
122134
$pol.RetentionPolicy.IsYearlyScheduleEnabled = $false
123135
$pol.RetentionPolicy.MonthlySchedule.DurationCountInMonths = 8
124136

0 commit comments

Comments
 (0)