Skip to content

[Az.RecoveryServices.Backup] hot fix for cross subscription restore #18238

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 2 commits into from
May 19, 2022
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 @@ -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