Skip to content

sql registration fix #27772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ public void RegisterContainer()
string containerName = (string)ProviderData[ContainerParams.Name];
string backupManagementType = (string)ProviderData[ContainerParams.BackupManagementType];
string workloadType = (string)ProviderData[ContainerParams.ContainerType];
string vmResourceGroupName = (string)ProviderData[ContainerParams.ResourceGroupName];
ContainerBase containerBase =
(ContainerBase)ProviderData[ContainerParams.Container];
AzureVmWorkloadContainer container = (AzureVmWorkloadContainer)ProviderData[ContainerParams.Container];
Expand All @@ -985,8 +986,14 @@ public void RegisterContainer()
List<ProtectableContainerResource> unregisteredVmContainers =
GetUnRegisteredVmContainers(vaultName, vaultResourceGroupName);
ProtectableContainerResource unregisteredVmContainer = unregisteredVmContainers.Find(
vmContainer => string.Compare(vmContainer.Name.Split(';').Last(),
containerName, true) == 0);
vmContainer => {
string[] containerNameSplit = vmContainer.Name.Split(';');
int containerNameSplitLen = containerNameSplit.Length;
bool vmNameMatch = string.Compare(containerNameSplit[containerNameSplitLen - 1], containerName, true) == 0;
bool rgNameMatch = string.Compare(containerNameSplit[containerNameSplitLen - 2], vmResourceGroupName, true) == 0;

return vmNameMatch && rgNameMatch;
});

if (unregisteredVmContainer != null || container != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void UnregisterAzureWorkloadContainer()
"Unregister-AzureWorkloadContainer"
);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(TestConstants.Workload, TestConstants.AzureVmWorkload)]
Expand All @@ -59,5 +59,17 @@ public void TestAzureVmWorkloadUnDeleteContainer()
"Test-AzureVmWorkloadUnDeleteContainer"
);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(TestConstants.Workload, TestConstants.AzureVmWorkload)]
public void TestSQLContainerRegError()
{
TestRunner.RunTestScript(
$"Import-Module {_AzureWorkloadcommonModule.AsAbsoluteLocation()}",
$"Import-Module {_AzureWorkloadtestModule.AsAbsoluteLocation()}",
"Test-SQLContainerRegError"
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,40 @@ $containerName = "psbvtsqlvm"
$resourceGroupName = "pstestwlRG1bca8"
$vaultName = "pstestwlRSV1bca8"
$resourceId = "/subscriptions/38304e13-357e-405e-9e9a-220351dcce8c/resourceGroups/pscloudtestrg/providers/Microsoft.Compute/virtualMachines/psbvtsqlvm"
$sqlRegTestVlt = "utkvlt"
$sqlRegTestVm = "utkvm"
$sqlRegTestRg1 = "rg1"
$sqlRegTestRg2 = "rg2"
$sqlRegTestVM2Id = "/subscriptions/af95aa3c-30fd-41c6-a938-4b3676fc36fb/resourceGroups/rg2/providers/Microsoft.Compute/virtualMachines/utkvm"

function Test-SQLContainerRegError
{
$vault1 = Get-AzRecoveryServicesVault -ResourceGroupName $sqlRegTestRg1 -Name $sqlRegTestVlt
$vault2 = Get-AzRecoveryServicesVault -ResourceGroupName $sqlRegTestRg2 -Name $sqlRegTestVlt

#$Unregister containers if already registered
Get-AzRecoveryServicesBackupContainer `
-VaultId $vault1.ID `
-ContainerType AzureVMAppContainer `
-FriendlyName $sqlRegTestVm | Unregister-AzRecoveryServicesBackupContainer -VaultId $vault1.ID -Force

Get-AzRecoveryServicesBackupContainer `
-VaultId $vault2.ID `
-ContainerType AzureVMAppContainer `
-FriendlyName $sqlRegTestVm | Unregister-AzRecoveryServicesBackupContainer -VaultId $vault2.ID -Force

$v = get-azrecoveryservicesvault -ResourceGroupName $sqlRegTestRg2 -Name $sqlRegTestVlt
Set-AzRecoveryServicesVaultContext -Vault $v

Register-AzRecoveryServicesBackupContainer -ResourceId $sqlRegTestVM2Id -VaultId $v.Id -WorkloadType "MSSQL" -BackupManagementType "AzureWorkload" -Force

$container = Get-AzRecoveryServicesBackupContainer `
-VaultId $vault2.ID `
-ContainerType AzureVMAppContainer `
-FriendlyName $sqlRegTestVm

Assert-True { $container.FriendlyName -eq $sqlRegTestVm }
}

function Test-AzureVmWorkloadUnDeleteContainer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override void ExecuteCmdlet()
{
ExecutionBlock(() =>
{
string containerName = Container != null ? Container.Name : ResourceId.Split('/')[8];
string containerName = Container != null ? Container.Name : ResourceId.Split('/')[8];

ConfirmAction(
Force.IsPresent,
Expand All @@ -98,10 +98,30 @@ public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

string vmResourceGroupParsed = null;
ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
string vaultName = resourceIdentifier.ResourceName;
string vaultResourceGroupName = resourceIdentifier.ResourceGroupName;

if (Container != null)
{
if (Container is AzureVmWorkloadContainer)
{
AzureVmWorkloadContainer azureVmWorkloadContainer = (AzureVmWorkloadContainer)Container;
Dictionary<UriEnums, string> keyValueDict = HelperUtils.ParseUri(azureVmWorkloadContainer.SourceResourceId);
vmResourceGroupParsed = HelperUtils.GetResourceGroupNameFromId(keyValueDict, ResourceId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass SourceResourceId here instead of ResourceId

}
else
{
vmResourceGroupParsed = vaultResourceGroupName;
}
}
else
{
Dictionary<UriEnums, string> keyValueDict = HelperUtils.ParseUri(ResourceId);
vmResourceGroupParsed = HelperUtils.GetResourceGroupNameFromId(keyValueDict, ResourceId);
}

PsBackupProviderManager providerManager =
new PsBackupProviderManager(new Dictionary<Enum, object>()
{
Expand All @@ -110,7 +130,8 @@ public override void ExecuteCmdlet()
{ ContainerParams.Name, containerName },
{ ContainerParams.ContainerType, ServiceClientHelpers.GetServiceClientWorkloadType(WorkloadType).ToString() },
{ ContainerParams.BackupManagementType, BackupManagementType.ToString() },
{ ContainerParams.Container, Container}
{ ContainerParams.Container, Container},
{ ContainerParams.ResourceGroupName, vmResourceGroupParsed },
}, ServiceClientAdapter);

IPsBackupProvider psBackupProvider =
Expand Down
3 changes: 3 additions & 0 deletions src/RecoveryServices/RecoveryServices/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
-->
## Upcoming Release

## Version 7.8.0
* Added check to compare the VM resource group in the list of VMs returned for registration

## Version 7.7.0
* Fix for reprotect cmdlet in Azure Site Recovery for Azure to Azure provider.
* Deprecated the `Token` parameter for cross-tenant authentication in MUA scenarios for handling breaking change in Get-AzAccessToken cmdlet, use parameter `SecureToken` going forward.
Expand Down