Skip to content

Commit 2731e8e

Browse files
authored
Merge pull request Azure#9910 from MabOneSdk/users/sarath/policy-fixes
RecoveryServices : Adding new attribute to policy object
2 parents d99847b + 3a5924a commit 2731e8e

File tree

14 files changed

+645995
-53057
lines changed

14 files changed

+645995
-53057
lines changed

src/RecoveryServices/RecoveryServices.Backup.Helpers/Conversions/ConversionHelpers.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public static PolicyBase GetPolicyModelForAzureIaaSVM(ServiceClientModel.Protect
162162
iaasPolicyModel.BackupManagementType = BackupManagementType.AzureVM;
163163
iaasPolicyModel.SnapshotRetentionInDays = ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.
164164
Properties).InstantRpRetentionRangeInDays;
165+
iaasPolicyModel.ProtectedItemsCount = ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.
166+
Properties).ProtectedItemsCount;
165167
iaasPolicyModel.RetentionPolicy = PolicyHelpers.GetPSLongTermRetentionPolicy((ServiceClientModel.LongTermRetentionPolicy)
166168
((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).RetentionPolicy,
167169
((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).TimeZone);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models
2323
public class AzureVmItem : AzureItem
2424
{
2525
public string VirtualMachineId { get; set; }
26-
26+
27+
public string HealthStatus { get; set; }
28+
2729
/// <summary>
2830
/// Constructor. Takes the service client object representing the protected item
2931
/// and converts it in to the PS protected item model
@@ -43,6 +45,7 @@ public AzureVmItem(ProtectedItemResource protectedItemResource,
4345
EnumUtils.GetEnum<ItemProtectionState>(protectedItem.ProtectionState.ToString());
4446
ProtectionStatus = EnumUtils.GetEnum<ItemProtectionStatus>(protectedItem.ProtectionStatus);
4547
VirtualMachineId = protectedItem.VirtualMachineId;
48+
HealthStatus = protectedItem.HealthStatus;
4649
}
4750
}
4851

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class AzureVmPolicy : AzurePolicy
2323
/// Object defining the retention days for a snapshot
2424
/// </summary>
2525
public int? SnapshotRetentionInDays { get; set; }
26+
27+
/// <summary>
28+
/// Object defining the number of associated items for the policy
29+
/// </summary>
30+
public int? ProtectedItemsCount { get; set; }
2631
}
2732

2833
}

src/RecoveryServices/RecoveryServices.Backup.Models/Properties/Resources.Designer.cs

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RecoveryServices/RecoveryServices.Backup.Models/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,7 @@ Please contact Microsoft for further assistance.</value>
571571
<data name="RetentionDurationCountInDaysInvalidException" xml:space="preserve">
572572
<value>RetentionDuration in Days should be from 7 - 9999</value>
573573
</data>
574+
<data name="ProtectedItemsCountExceededException" xml:space="preserve">
575+
<value>Cannot configure backup for more than 100 VMs per policy</value>
576+
</data>
574577
</root>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public RestAzureNS.AzureOperationResponse<ProtectedItemResource> EnableProtectio
8787
bool isComputeAzureVM = false;
8888
string sourceResourceId = null;
8989

90+
AzureVmPolicy azureVmPolicy = (AzureVmPolicy)ProviderData[ItemParams.Policy];
91+
ValidateProtectedItemCount(azureVmPolicy);
92+
9093
if (itemBase == null)
9194
{
9295
isComputeAzureVM = string.IsNullOrEmpty(azureVMCloudServiceName) ? true : false;
@@ -835,6 +838,15 @@ public void RegisterContainer()
835838

836839
#region private
837840

841+
842+
private void ValidateProtectedItemCount(AzureVmPolicy azureVmPolicy)
843+
{
844+
if (azureVmPolicy.ProtectedItemsCount > 100)
845+
{
846+
throw new ArgumentException(Resources.ProtectedItemsCountExceededException);
847+
}
848+
}
849+
838850
private void ValidateAzureVMWorkloadType(CmdletModel.WorkloadType type)
839851
{
840852
if (type != CmdletModel.WorkloadType.AzureVM)
@@ -888,7 +900,6 @@ private void ValidateAzureVMProtectionPolicy(PolicyBase policy)
888900
}
889901

890902
ValidateAzureVMWorkloadType(policy.WorkloadType);
891-
892903
// call validation
893904
policy.Validate();
894905
}

src/RecoveryServices/RecoveryServices.Backup.Test/ScenarioTests/Common.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
-Name $name `
9191
-Location $location `
9292
-Type "Standard_LRS"
93-
$job | Wait-Job
9493
$sa = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $name
9594
}
9695
return $name

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

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,80 @@ function Create-VM(
7070
return $vm
7171
}
7272

73+
74+
function Create-UnmanagedVM(
75+
[string] $resourceGroupName,
76+
[string] $location,
77+
[string] $saname,
78+
[int] $nick = 0)
79+
{
80+
$suffix = $(Get-RandomSuffix 5) + $nick
81+
$vmName = "PSTestVM" + $suffix
82+
83+
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -ErrorAction Ignore
84+
85+
if ($vm -eq $null)
86+
{
87+
$subnetConfigName = "PSTestSNC" + $suffix
88+
$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name $subnetConfigName -AddressPrefix 192.168.1.0/24
89+
90+
91+
$vnetName = "PSTestVNET" + $suffix
92+
$vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Location $location `
93+
-Name $vnetName -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig -Force
94+
95+
$pipName = "pstestpublicdns" + $suffix
96+
$pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Location $location `
97+
-AllocationMethod Static -IdleTimeoutInMinutes 4 -Name $pipName -Force
98+
99+
100+
$nsgRuleRDPName = "PSTestNSGRuleRDP" + $suffix
101+
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name $nsgRuleRDPName -Protocol Tcp `
102+
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
103+
-DestinationPortRange 3389 -Access Allow
104+
105+
$nsgRuleWebName = "PSTestNSGRuleWeb" + $suffix
106+
$nsgRuleWeb = New-AzNetworkSecurityRuleConfig -Name $nsgRuleWebName -Protocol Tcp `
107+
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
108+
-DestinationPortRange 80 -Access Allow
109+
110+
$nsgName = "PSTestNSG" + $suffix
111+
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroupName -Location $location `
112+
-Name $nsgName -SecurityRules $nsgRuleRDP,$nsgRuleWeb -Force
113+
114+
115+
$nicName = "PSTestNIC" + $suffix
116+
$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $resourceGroupName -Location $location `
117+
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id -Force
118+
119+
$UserName='demouser'
120+
$PasswordString = $(Get-RandomSuffix 12)
121+
$Password=$PasswordString| ConvertTo-SecureString -Force -AsPlainText
122+
$Credential=New-Object PSCredential($UserName,$Password)
123+
124+
125+
$vmsize = "Standard_D1"
126+
$vm = New-AzVMConfig -VMName $vmName -VMSize $vmSize
127+
$pubName = "MicrosoftWindowsServer"
128+
$offerName = "WindowsServer"
129+
$skuName = "2016-Datacenter"
130+
$vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $Credential
131+
$vm = Set-AzVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
132+
$vm = Add-AzVMNetworkInterface -VM $vm -Id $NIC.Id
133+
134+
135+
$sa = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $saname
136+
$diskName = "mydisk"
137+
$OSDiskUri = $sa.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName? + ".vhd"
138+
139+
$vm = Set-AzVMOSDisk -VM $vm -Name $diskName -VhdUri $OSDiskUri -CreateOption fromImage
140+
141+
New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vm | Out-Null
142+
}
143+
144+
return $vm
145+
}
146+
73147
function Create-GalleryVM(
74148
[string] $resourceGroupName,
75149
[string] $location,
@@ -164,7 +238,8 @@ function Start-TestSleep($milliseconds)
164238

165239
function Enable-Protection(
166240
$vault,
167-
$vm)
241+
$vm,
242+
[string] $resourceGroupName = "")
168243
{
169244
# Sleep to give the service time to add the default policy to the vault
170245
Start-TestSleep 5000
@@ -173,6 +248,11 @@ function Enable-Protection(
173248
-ContainerType AzureVM `
174249
-FriendlyName $vm.Name;
175250

251+
if($resourceGroupName -eq "")
252+
{
253+
$resourceGroupName = $vm.ResourceGroupName
254+
}
255+
176256
if ($container -eq $null)
177257
{
178258
$policy = Get-AzRecoveryServicesBackupProtectionPolicy `
@@ -183,7 +263,7 @@ function Enable-Protection(
183263
-VaultId $vault.ID `
184264
-Policy $policy `
185265
-Name $vm.Name `
186-
-ResourceGroupName $vm.ResourceGroupName | Out-Null
266+
-ResourceGroupName $resourceGroupName | Out-Null
187267

188268
$container = Get-AzRecoveryServicesBackupContainer `
189269
-VaultId $vault.ID `

src/RecoveryServices/RecoveryServices.Backup.Test/ScenarioTests/IaasVm/ItemTests.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ public void TestAzureVMGetRPs()
6262
}
6363

6464

65-
#if NETSTANDARD
66-
[Fact(Skip = "Needs investigation, TestManagementClientHelper class wasn't initialized with the ResourceManagementClient client.")]
67-
#else
65+
6866
[Fact]
69-
#endif
7067
[Trait(Category.AcceptanceType, Category.CheckIn)]
7168
[Trait(TestConstants.Workload, TestConstants.AzureVM)]
7269
public void TestAzureVMFullRestore()
@@ -75,6 +72,15 @@ public void TestAzureVMFullRestore()
7572
_logger, PsBackupProviderTypes.IaasVm, "Test-AzureVMFullRestore");
7673
}
7774

75+
[Fact]
76+
[Trait(Category.AcceptanceType, Category.CheckIn)]
77+
[Trait(TestConstants.Workload, TestConstants.AzureVM)]
78+
public void TestAzureUnmanagedVMFullRestore()
79+
{
80+
TestController.NewInstance.RunPsTest(
81+
_logger, PsBackupProviderTypes.IaasVm, "Test-AzureUnmanagedVMFullRestore");
82+
}
83+
7884
[Fact]
7985
[Trait(Category.AcceptanceType, Category.CheckIn)]
8086
[Trait(TestConstants.Workload, TestConstants.AzureVM)]

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ function Test-AzureVMProtection
145145
-Name $vm.Name `
146146
-ResourceGroupName $vm.ResourceGroupName;
147147

148+
$policy = Get-AzRecoveryServicesBackupProtectionPolicy `
149+
-VaultId $vault.ID `
150+
-Name "DefaultPolicy";
151+
152+
Assert-True {$policy.ProtectedItemsCount -eq 1};
153+
148154
$container = Get-AzRecoveryServicesBackupContainer `
149155
-VaultId $vault.ID `
150156
-ContainerType AzureVM `
@@ -161,6 +167,13 @@ function Test-AzureVMProtection
161167
-Item $item `
162168
-RemoveRecoveryPoints `
163169
-Force;
170+
171+
$policy = Get-AzRecoveryServicesBackupProtectionPolicy `
172+
-VaultId $vault.ID `
173+
-Name "DefaultPolicy";
174+
175+
Assert-True {$policy.ProtectedItemsCount -eq 0};
176+
164177
}
165178
finally
166179
{
@@ -288,6 +301,36 @@ function Test-AzureVMFullRestore
288301
}
289302
}
290303

304+
function Test-AzureUnmanagedVMFullRestore
305+
{
306+
$location = Get-ResourceGroupLocation
307+
$resourceGroupName = Create-ResourceGroup $location
308+
309+
try
310+
{
311+
$saName = Create-SA $resourceGroupName $location
312+
$vm = Create-UnmanagedVM $resourceGroupName $location $saName
313+
$vault = Create-RecoveryServicesVault $resourceGroupName $location
314+
$item = Enable-Protection $vault $vm $resourceGroupName
315+
$backupJob = Backup-Item $vault $item
316+
$rp = Get-RecoveryPoint $vault $item $backupJob
317+
318+
$restoreJob = Restore-AzRecoveryServicesBackupItem `
319+
-VaultId $vault.ID `
320+
-VaultLocation $vault.Location `
321+
-RecoveryPoint $rp `
322+
-StorageAccountName $saName `
323+
-StorageAccountResourceGroupName $resourceGroupName `
324+
-UseOriginalStorageAccount | Wait-AzRecoveryServicesBackupJob -VaultId $vault.ID
325+
326+
Assert-True { $restoreJob.Status -eq "Completed" }
327+
}
328+
finally
329+
{
330+
Cleanup-ResourceGroup $resourceGroupName
331+
}
332+
}
333+
291334
function Test-AzureVMRPMountScript
292335
{
293336
$location = Get-ResourceGroupLocation

0 commit comments

Comments
 (0)