Skip to content

Commit

Permalink
[Az.RecoveryServices.Backup] hot fix for cross subscription restore (A…
Browse files Browse the repository at this point in the history
…zure#18238)

* hot fix for cross subscription restore

* Re-recorded the failed test
  • Loading branch information
hiaga authored May 19, 2022
1 parent 3b81757 commit 888088e
Show file tree
Hide file tree
Showing 4 changed files with 1,320 additions and 1,066 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,13 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
string targetVNetResourceGroup = (string)ProviderData[RestoreVMBackupItemParams.TargetVNetResourceGroup];
string targetSubnetName = (string)ProviderData[RestoreVMBackupItemParams.TargetSubnetName];
string targetSubscriptionId = (string)ProviderData[RestoreVMBackupItemParams.TargetSubscriptionId];

Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(rp.Id);
string containerUri = HelperUtils.GetContainerUri(uriDict, rp.Id);

GenericResource storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(storageAccountName);
if (targetSubscriptionId == null || targetSubscriptionId == "") targetSubscriptionId = ServiceClientAdapter.SubscriptionId;

GenericResource storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(storageAccountName, targetSubscriptionId);

var useOsa = ShouldUseOsa(rp, osaOption);

Expand Down Expand Up @@ -496,7 +498,7 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
{
identityInfo = new IdentityInfo();
if (useSystemAssignedIdentity)
{
{
identityInfo.IsSystemAssignedIdentity = true;
}
else
Expand All @@ -511,12 +513,10 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
}
else
{
throw new NotSupportedException(Resources.MSIRestoreNotSupportedForUnmanagedVM);
throw new NotSupportedException(Resources.MSIRestoreNotSupportedForUnmanagedVM);
}
}

string targetSubscription = (targetSubscriptionId != null && targetSubscriptionId != "") ? targetSubscriptionId : ServiceClientAdapter.SubscriptionId;


IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest()
{
CreateNewCloudService = false,
Expand All @@ -525,7 +525,7 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
Region = vaultLocation ?? ServiceClientAdapter.BmsAdapter.GetResourceLocation(),
StorageAccountId = storageAccountResource.Id,
SourceResourceId = rp.SourceResourceId,
TargetResourceGroupId = targetResourceGroupName != null ? "/subscriptions/" + targetSubscription + "/resourceGroups/" + targetResourceGroupName : null,
TargetResourceGroupId = targetResourceGroupName != null ? "/subscriptions/" + targetSubscriptionId + "/resourceGroups/" + targetResourceGroupName : null,
OriginalStorageAccountOption = useOsa,
RestoreDiskLunList = restoreDiskLUNS,
DiskEncryptionSetId = DiskEncryptionSetId,
Expand Down Expand Up @@ -562,8 +562,8 @@ public RestAzureNS.AzureOperationResponse TriggerRestore()
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ public GenericResource GetAzureResource(string resourceId)
/// </summary>
/// <param name="storageAccountName">Name of the container to unregister</param>
/// <returns>Generic resource returned from the service</returns>
public GenericResource GetStorageAccountResource(string storageAccountName)
public GenericResource GetStorageAccountResource(string storageAccountName, string subscriptionId = null)
{
List<GenericResource> storageAccounts = null;
GenericResource storageAccount = null;
storageAccountName = storageAccountName.ToLower();
ODataQuery<GenericResourceFilter> getItemQueryParams =
new ODataQuery<GenericResourceFilter>(q =>
q.ResourceType == "Microsoft.ClassicStorage/storageAccounts");

// switch subscription context
string subscriptionContext = RMAdapter.Client.SubscriptionId;
RMAdapter.Client.SubscriptionId = (subscriptionId != null)? subscriptionId: RMAdapter.Client.SubscriptionId;

Func<RestAzureNS.IPage<GenericResource>> listAsync =
() => RMAdapter.Client.Resources.ListWithHttpMessagesAsync(
getItemQueryParams,
Expand Down Expand Up @@ -81,6 +86,9 @@ public GenericResource GetStorageAccountResource(string storageAccountName)
storageAccount = storageAccounts.Find(account =>
string.Compare(account.Name, storageAccountName) == 0);
}

RMAdapter.Client.SubscriptionId = subscriptionContext;

return storageAccount;
}
}
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/RecoveryServices/RecoveryServices/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-->
## Upcoming Release
* Added support for Multi-user authorization using Resource Guard for recovery sevrices vault.
* Added support for cross subscription restore for recovery services vault.
* Added support for cross subscription restore for recovery services vault, modified storage account to be fetched from target subscription.

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

0 comments on commit 888088e

Please sign in to comment.