Skip to content

Commit 8441f8c

Browse files
sql registration fix (#27772)
1 parent d762e83 commit 8441f8c

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

src/RecoveryServices/RecoveryServices.Backup.Providers/Providers/AzureWorkloadPsBackupProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ public void RegisterContainer()
970970
string containerName = (string)ProviderData[ContainerParams.Name];
971971
string backupManagementType = (string)ProviderData[ContainerParams.BackupManagementType];
972972
string workloadType = (string)ProviderData[ContainerParams.ContainerType];
973+
string vmResourceGroupName = (string)ProviderData[ContainerParams.ResourceGroupName];
973974
ContainerBase containerBase =
974975
(ContainerBase)ProviderData[ContainerParams.Container];
975976
AzureVmWorkloadContainer container = (AzureVmWorkloadContainer)ProviderData[ContainerParams.Container];
@@ -985,8 +986,14 @@ public void RegisterContainer()
985986
List<ProtectableContainerResource> unregisteredVmContainers =
986987
GetUnRegisteredVmContainers(vaultName, vaultResourceGroupName);
987988
ProtectableContainerResource unregisteredVmContainer = unregisteredVmContainers.Find(
988-
vmContainer => string.Compare(vmContainer.Name.Split(';').Last(),
989-
containerName, true) == 0);
989+
vmContainer => {
990+
string[] containerNameSplit = vmContainer.Name.Split(';');
991+
int containerNameSplitLen = containerNameSplit.Length;
992+
bool vmNameMatch = string.Compare(containerNameSplit[containerNameSplitLen - 1], containerName, true) == 0;
993+
bool rgNameMatch = string.Compare(containerNameSplit[containerNameSplitLen - 2], vmResourceGroupName, true) == 0;
994+
995+
return vmNameMatch && rgNameMatch;
996+
});
990997

991998
if (unregisteredVmContainer != null || container != null)
992999
{

src/RecoveryServices/RecoveryServices.Backup.Test/ScenarioTests/AzureWorkload/ContainerTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void UnregisterAzureWorkloadContainer()
4747
"Unregister-AzureWorkloadContainer"
4848
);
4949
}
50-
50+
5151
[Fact]
5252
[Trait(Category.AcceptanceType, Category.CheckIn)]
5353
[Trait(TestConstants.Workload, TestConstants.AzureVmWorkload)]
@@ -59,5 +59,17 @@ public void TestAzureVmWorkloadUnDeleteContainer()
5959
"Test-AzureVmWorkloadUnDeleteContainer"
6060
);
6161
}
62+
63+
[Fact]
64+
[Trait(Category.AcceptanceType, Category.CheckIn)]
65+
[Trait(TestConstants.Workload, TestConstants.AzureVmWorkload)]
66+
public void TestSQLContainerRegError()
67+
{
68+
TestRunner.RunTestScript(
69+
$"Import-Module {_AzureWorkloadcommonModule.AsAbsoluteLocation()}",
70+
$"Import-Module {_AzureWorkloadtestModule.AsAbsoluteLocation()}",
71+
"Test-SQLContainerRegError"
72+
);
73+
}
6274
}
6375
}

src/RecoveryServices/RecoveryServices.Backup.Test/ScenarioTests/AzureWorkload/ContainerTests.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ $containerName = "psbvtsqlvm"
1616
$resourceGroupName = "pstestwlRG1bca8"
1717
$vaultName = "pstestwlRSV1bca8"
1818
$resourceId = "/subscriptions/38304e13-357e-405e-9e9a-220351dcce8c/resourceGroups/pscloudtestrg/providers/Microsoft.Compute/virtualMachines/psbvtsqlvm"
19+
$sqlRegTestVlt = "utkvlt"
20+
$sqlRegTestVm = "utkvm"
21+
$sqlRegTestRg1 = "rg1"
22+
$sqlRegTestRg2 = "rg2"
23+
$sqlRegTestVM2Id = "/subscriptions/af95aa3c-30fd-41c6-a938-4b3676fc36fb/resourceGroups/rg2/providers/Microsoft.Compute/virtualMachines/utkvm"
24+
25+
function Test-SQLContainerRegError
26+
{
27+
$vault1 = Get-AzRecoveryServicesVault -ResourceGroupName $sqlRegTestRg1 -Name $sqlRegTestVlt
28+
$vault2 = Get-AzRecoveryServicesVault -ResourceGroupName $sqlRegTestRg2 -Name $sqlRegTestVlt
29+
30+
#$Unregister containers if already registered
31+
Get-AzRecoveryServicesBackupContainer `
32+
-VaultId $vault1.ID `
33+
-ContainerType AzureVMAppContainer `
34+
-FriendlyName $sqlRegTestVm | Unregister-AzRecoveryServicesBackupContainer -VaultId $vault1.ID -Force
35+
36+
Get-AzRecoveryServicesBackupContainer `
37+
-VaultId $vault2.ID `
38+
-ContainerType AzureVMAppContainer `
39+
-FriendlyName $sqlRegTestVm | Unregister-AzRecoveryServicesBackupContainer -VaultId $vault2.ID -Force
40+
41+
$v = get-azrecoveryservicesvault -ResourceGroupName $sqlRegTestRg2 -Name $sqlRegTestVlt
42+
Set-AzRecoveryServicesVaultContext -Vault $v
43+
44+
Register-AzRecoveryServicesBackupContainer -ResourceId $sqlRegTestVM2Id -VaultId $v.Id -WorkloadType "MSSQL" -BackupManagementType "AzureWorkload" -Force
45+
46+
$container = Get-AzRecoveryServicesBackupContainer `
47+
-VaultId $vault2.ID `
48+
-ContainerType AzureVMAppContainer `
49+
-FriendlyName $sqlRegTestVm
50+
51+
Assert-True { $container.FriendlyName -eq $sqlRegTestVm }
52+
}
1953

2054
function Test-AzureVmWorkloadUnDeleteContainer
2155
{

src/RecoveryServices/RecoveryServices.Backup/Cmdlets/Container/RegisterAzureRmRecoveryServicesBackupContainer.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public override void ExecuteCmdlet()
8888
{
8989
ExecutionBlock(() =>
9090
{
91-
string containerName = Container != null ? Container.Name : ResourceId.Split('/')[8];
91+
string containerName = Container != null ? Container.Name : ResourceId.Split('/')[8];
9292

9393
ConfirmAction(
9494
Force.IsPresent,
@@ -98,10 +98,30 @@ public override void ExecuteCmdlet()
9898
{
9999
base.ExecuteCmdlet();
100100

101+
string vmResourceGroupParsed = null;
101102
ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
102103
string vaultName = resourceIdentifier.ResourceName;
103104
string vaultResourceGroupName = resourceIdentifier.ResourceGroupName;
104105

106+
if (Container != null)
107+
{
108+
if (Container is AzureVmWorkloadContainer)
109+
{
110+
AzureVmWorkloadContainer azureVmWorkloadContainer = (AzureVmWorkloadContainer)Container;
111+
Dictionary<UriEnums, string> keyValueDict = HelperUtils.ParseUri(azureVmWorkloadContainer.SourceResourceId);
112+
vmResourceGroupParsed = HelperUtils.GetResourceGroupNameFromId(keyValueDict, ResourceId);
113+
}
114+
else
115+
{
116+
vmResourceGroupParsed = vaultResourceGroupName;
117+
}
118+
}
119+
else
120+
{
121+
Dictionary<UriEnums, string> keyValueDict = HelperUtils.ParseUri(ResourceId);
122+
vmResourceGroupParsed = HelperUtils.GetResourceGroupNameFromId(keyValueDict, ResourceId);
123+
}
124+
105125
PsBackupProviderManager providerManager =
106126
new PsBackupProviderManager(new Dictionary<Enum, object>()
107127
{
@@ -110,7 +130,8 @@ public override void ExecuteCmdlet()
110130
{ ContainerParams.Name, containerName },
111131
{ ContainerParams.ContainerType, ServiceClientHelpers.GetServiceClientWorkloadType(WorkloadType).ToString() },
112132
{ ContainerParams.BackupManagementType, BackupManagementType.ToString() },
113-
{ ContainerParams.Container, Container}
133+
{ ContainerParams.Container, Container},
134+
{ ContainerParams.ResourceGroupName, vmResourceGroupParsed },
114135
}, ServiceClientAdapter);
115136

116137
IPsBackupProvider psBackupProvider =

src/RecoveryServices/RecoveryServices/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* Updated `GetAzureRmRecoveryServicesBackupSchedulePolicyObject` to default the `PolicySubType` to `Enhanced` for AzureVM.
2626
* Updated Azure Backup SDK version to support protecting TVM with standard policy.
2727

28+
## Version 7.8.0
29+
* Added check to compare the VM resource group in the list of VMs returned for registration
30+
2831
## Version 7.7.0
2932
* Fix for reprotect cmdlet in Azure Site Recovery for Azure to Azure provider.
3033
* Deprecated the `Token` parameter for cross-tenant authentication in MUA scenarios for handling breaking change in Get-AzAccessToken cmdlet, use parameter `SecureToken` going forward.

0 commit comments

Comments
 (0)