Skip to content

Commit 888088e

Browse files
authored
[Az.RecoveryServices.Backup] hot fix for cross subscription restore (#18238)
* hot fix for cross subscription restore * Re-recorded the failed test
1 parent 3b81757 commit 888088e

File tree

4 files changed

+1320
-1066
lines changed

4 files changed

+1320
-1066
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,13 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
443443
string targetVNetResourceGroup = (string)ProviderData[RestoreVMBackupItemParams.TargetVNetResourceGroup];
444444
string targetSubnetName = (string)ProviderData[RestoreVMBackupItemParams.TargetSubnetName];
445445
string targetSubscriptionId = (string)ProviderData[RestoreVMBackupItemParams.TargetSubscriptionId];
446-
446+
447447
Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(rp.Id);
448448
string containerUri = HelperUtils.GetContainerUri(uriDict, rp.Id);
449449

450-
GenericResource storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(storageAccountName);
450+
if (targetSubscriptionId == null || targetSubscriptionId == "") targetSubscriptionId = ServiceClientAdapter.SubscriptionId;
451+
452+
GenericResource storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(storageAccountName, targetSubscriptionId);
451453

452454
var useOsa = ShouldUseOsa(rp, osaOption);
453455

@@ -496,7 +498,7 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
496498
{
497499
identityInfo = new IdentityInfo();
498500
if (useSystemAssignedIdentity)
499-
{
501+
{
500502
identityInfo.IsSystemAssignedIdentity = true;
501503
}
502504
else
@@ -511,12 +513,10 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
511513
}
512514
else
513515
{
514-
throw new NotSupportedException(Resources.MSIRestoreNotSupportedForUnmanagedVM);
516+
throw new NotSupportedException(Resources.MSIRestoreNotSupportedForUnmanagedVM);
515517
}
516518
}
517-
518-
string targetSubscription = (targetSubscriptionId != null && targetSubscriptionId != "") ? targetSubscriptionId : ServiceClientAdapter.SubscriptionId;
519-
519+
520520
IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest()
521521
{
522522
CreateNewCloudService = false,
@@ -525,7 +525,7 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
525525
Region = vaultLocation ?? ServiceClientAdapter.BmsAdapter.GetResourceLocation(),
526526
StorageAccountId = storageAccountResource.Id,
527527
SourceResourceId = rp.SourceResourceId,
528-
TargetResourceGroupId = targetResourceGroupName != null ? "/subscriptions/" + targetSubscription + "/resourceGroups/" + targetResourceGroupName : null,
528+
TargetResourceGroupId = targetResourceGroupName != null ? "/subscriptions/" + targetSubscriptionId + "/resourceGroups/" + targetResourceGroupName : null,
529529
OriginalStorageAccountOption = useOsa,
530530
RestoreDiskLunList = restoreDiskLUNS,
531531
DiskEncryptionSetId = DiskEncryptionSetId,
@@ -562,8 +562,8 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
562562
}
563563

564564
restoreRequest.RecoveryType = RecoveryType.AlternateLocation;
565-
restoreRequest.TargetVirtualMachineId = "/subscriptions/" + targetSubscription + "/resourceGroups/" + targetResourceGroupName + "/providers/Microsoft.Compute/virtualMachines/" + targetVMName;
566-
restoreRequest.VirtualNetworkId = "/subscriptions/" + targetSubscription + "/resourceGroups/" + targetVNetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + targetVNetName;
565+
restoreRequest.TargetVirtualMachineId = "/subscriptions/" + targetSubscriptionId + "/resourceGroups/" + targetResourceGroupName + "/providers/Microsoft.Compute/virtualMachines/" + targetVMName;
566+
restoreRequest.VirtualNetworkId = "/subscriptions/" + targetSubscriptionId + "/resourceGroups/" + targetVNetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + targetVNetName;
567567
restoreRequest.SubnetId = restoreRequest.VirtualNetworkId + "/subnets/" + targetSubnetName;
568568
}
569569

src/RecoveryServices/RecoveryServices.Backup.ServiceClientAdapter/RMAPIs/ResourceAPIs.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ public GenericResource GetAzureResource(string resourceId)
4343
/// </summary>
4444
/// <param name="storageAccountName">Name of the container to unregister</param>
4545
/// <returns>Generic resource returned from the service</returns>
46-
public GenericResource GetStorageAccountResource(string storageAccountName)
46+
public GenericResource GetStorageAccountResource(string storageAccountName, string subscriptionId = null)
4747
{
4848
List<GenericResource> storageAccounts = null;
4949
GenericResource storageAccount = null;
5050
storageAccountName = storageAccountName.ToLower();
5151
ODataQuery<GenericResourceFilter> getItemQueryParams =
5252
new ODataQuery<GenericResourceFilter>(q =>
5353
q.ResourceType == "Microsoft.ClassicStorage/storageAccounts");
54+
55+
// switch subscription context
56+
string subscriptionContext = RMAdapter.Client.SubscriptionId;
57+
RMAdapter.Client.SubscriptionId = (subscriptionId != null)? subscriptionId: RMAdapter.Client.SubscriptionId;
58+
5459
Func<RestAzureNS.IPage<GenericResource>> listAsync =
5560
() => RMAdapter.Client.Resources.ListWithHttpMessagesAsync(
5661
getItemQueryParams,
@@ -81,6 +86,9 @@ public GenericResource GetStorageAccountResource(string storageAccountName)
8186
storageAccount = storageAccounts.Find(account =>
8287
string.Compare(account.Name, storageAccountName) == 0);
8388
}
89+
90+
RMAdapter.Client.SubscriptionId = subscriptionContext;
91+
8492
return storageAccount;
8593
}
8694
}

src/RecoveryServices/RecoveryServices.Backup.Test/SessionRecords/Microsoft.Azure.Commands.RecoveryServices.Backup.Test.ScenarioTests.ItemTests/TestAzureFSFullRestore.json

Lines changed: 1300 additions & 1054 deletions
Large diffs are not rendered by default.

src/RecoveryServices/RecoveryServices/ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
-->
2020
## Upcoming Release
2121
* Added support for Multi-user authorization using Resource Guard for recovery sevrices vault.
22-
* Added support for cross subscription restore for recovery services vault.
22+
* Added support for cross subscription restore for recovery services vault, modified storage account to be fetched from target subscription.
2323

2424
## Version 5.3.1
2525
* Added support for multiple backups per day (hourly) Enhanced policy for workloadType AzureVM.

0 commit comments

Comments
 (0)