/// <summary> /// Restores the disk based on the recovery point and other input parameters /// </summary> /// <param name="rp">Recovery point to restore the disk to</param> /// <param name="storageAccountId">ID of the storage account where to restore the disk</param> /// <param name="storageAccountLocation">Location of the storage account where to restore the disk</param> /// <param name="storageAccountType">Type of the storage account where to restore the disk</param> /// <returns>Job created by this operation</returns> public BaseRecoveryServicesJobResponse RestoreDisk(AzureVmRecoveryPoint rp, string storageAccountId, string storageAccountLocation, string storageAccountType) { string resourceGroupName = BmsAdapter.GetResourceGroupName(); string resourceName = BmsAdapter.GetResourceName(); string vaultLocation = BmsAdapter.GetResourceLocation(); Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(rp.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, rp.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, rp.Id); string recoveryPointId = rp.RecoveryPointId; //validtion block if(storageAccountLocation != vaultLocation) { throw new Exception(Resources.RestoreDiskIncorrectRegion); } string vmType = containerUri.Split(';')[1].Equals("iaasvmcontainer", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; string strType = storageAccountType.Equals("Microsoft.ClassicStorage/StorageAccounts", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; if(vmType != strType) { throw new Exception(String.Format(Resources.RestoreDiskStorageTypeError, vmType)); } IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest() { CreateNewCloudService = false, RecoveryPointId = recoveryPointId, RecoveryType = RecoveryType.RestoreDisks, Region = vaultLocation, StorageAccountId = storageAccountId, SourceResourceId = rp.SourceResourceId, }; TriggerRestoreRequest triggerRestoreRequest = new TriggerRestoreRequest(); triggerRestoreRequest.Item = new RestoreRequestResource(); triggerRestoreRequest.Item.Properties = new RestoreRequest(); triggerRestoreRequest.Item.Properties = restoreRequest; var response = BmsAdapter.Client.Restores.TriggerRestoreAsync( resourceGroupName, resourceName, BmsAdapter.GetCustomRequestHeaders(), AzureFabricName, containerUri, protectedItemUri, recoveryPointId, triggerRestoreRequest, BmsAdapter.CmdletCancellationToken).Result; return response; }
public string RestoreProtectedItem(string rsVaultRgName, string rsVaultName, string containerUri, string protectedItemUri, string sourceResourceId, string storageAccountId, RecoveryPointResource recoveryPointResource) { string fabricName = CommonTestHelper.GetSetting(TestConstants.ProviderTypeAzureIaasVM); RecoveryPoint recoveryPoint = (RecoveryPoint)recoveryPointResource.Properties; TriggerRestoreRequest restoreRequest = new TriggerRestoreRequest(); restoreRequest.Item = new RestoreRequestResource(); IaasVMRestoreRequest iaasVmRestoreRequest = new IaasVMRestoreRequest(); if (recoveryPoint.KeyAndSecret != null && recoveryPoint.KeyAndSecret.BekDetails != null && recoveryPoint.KeyAndSecret.KekDetails != null) { iaasVmRestoreRequest.EncryptionDetails = new EncryptionDetails(); iaasVmRestoreRequest.EncryptionDetails.EncryptionEnabled = true; iaasVmRestoreRequest.EncryptionDetails.KekUrl = recoveryPoint.KeyAndSecret.KekDetails.KeyUrl; iaasVmRestoreRequest.EncryptionDetails.KekVaultId = recoveryPoint.KeyAndSecret.KekDetails.KeyVaultId; iaasVmRestoreRequest.EncryptionDetails.SecretKeyUrl = recoveryPoint.KeyAndSecret.BekDetails.SecretUrl; iaasVmRestoreRequest.EncryptionDetails.SecretKeyVaultId = recoveryPoint.KeyAndSecret.BekDetails.SecretVaultId; } iaasVmRestoreRequest.RecoveryPointId = recoveryPointResource.Name; iaasVmRestoreRequest.RecoveryType = RecoveryType.RestoreDisks.ToString(); iaasVmRestoreRequest.SourceResourceId = sourceResourceId; iaasVmRestoreRequest.StorageAccountId = storageAccountId; restoreRequest.Item.Properties = iaasVmRestoreRequest; var response = Client.Restores.TriggerRestore(rsVaultRgName, rsVaultName, CommonTestHelper.GetCustomRequestHeaders(), fabricName, containerUri, protectedItemUri, recoveryPointResource.Name, restoreRequest); Assert.NotNull(response); Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); Assert.NotNull(response.Location); Assert.NotNull(response.AzureAsyncOperation); Assert.NotNull(response.RetryAfter); var operationResponse = Client.ProtectedItems.GetProtectedItemOperationResultByURLAsync(response.Location, CommonTestHelper.GetCustomRequestHeaders()); while (operationResponse.Result.StatusCode == HttpStatusCode.Accepted) { if (HttpMockServer.Mode == HttpRecorderMode.Record) { System.Threading.Thread.Sleep(5 * 1000); } operationResponse = Client.ProtectedItems.GetProtectedItemOperationResultByURLAsync(response.Location, CommonTestHelper.GetCustomRequestHeaders()); } var operationStatusResponse = Client.GetOperationStatusByURLAsync(response.AzureAsyncOperation, CommonTestHelper.GetCustomRequestHeaders()); var operationJobResponse = (OperationStatusJobExtendedInfo)operationStatusResponse.Result.OperationStatus.Properties; Assert.NotNull(operationJobResponse.JobId); return operationJobResponse.JobId; }
public void RestoreDiskTest() { using (UndoContext context = UndoContext.Current) { context.Start(); string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"]; string vaultLocation = ConfigurationManager.AppSettings["vaultLocation"]; string fabricName = ConfigurationManager.AppSettings["AzureBackupFabricName"]; string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRestore"]; string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"]; string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRestore"]; string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"]; string recoveryPointId = ConfigurationManager.AppSettings["RecoveryPointName"]; string storageAccountId = ConfigurationManager.AppSettings["StorageAccountId"]; var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace); string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRestore"]; string resourceName = ConfigurationManager.AppSettings["RsVaultNameRestore"]; string containerUri = containeType + ";" + containerUniqueName; string itemUri = itemType + ";" + itemUniqueName; IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest() { AffinityGroup = String.Empty, CloudServiceOrResourceGroup = String.Empty, CreateNewCloudService = false, RecoveryPointId = recoveryPointId, RecoveryType = RecoveryType.RestoreDisks, Region = vaultLocation, StorageAccountId = storageAccountId, SubnetId = string.Empty, VirtualMachineName = string.Empty, VirtualNetworkId = string.Empty, }; TriggerRestoreRequest triggerRestoreRequest = new TriggerRestoreRequest(); triggerRestoreRequest.Item = new RestoreRequestResource(); triggerRestoreRequest.Item.Properties = new RestoreRequest(); triggerRestoreRequest.Item.Properties = restoreRequest; var response = client.Restores.TriggerRestore(resourceGroupName, resourceName, CommonTestHelper.GetCustomRequestHeaders(), fabricName, containerUri, itemUri, recoveryPointId, triggerRestoreRequest); Assert.Equal(HttpStatusCode.Accepted, response.StatusCode); Assert.True(!string.IsNullOrEmpty(response.Location), "Location cant be null"); } }
/// <summary> /// Restores the disk based on the recovery point and other input parameters /// </summary> /// <param name="rp">Recovery point to restore the disk to</param> /// <param name="storageAccountId">ID of the storage account where to restore the disk</param> /// <param name="storageAccountLocation">Location of the storage account where to restore the disk</param> /// <param name="storageAccountType">Type of the storage account where to restore the disk</param> /// <returns>Job created by this operation</returns> public BaseRecoveryServicesJobResponse RestoreDisk(AzureVmRecoveryPoint rp, string storageAccountId, string storageAccountLocation, string storageAccountType) { string resourceGroupName = BmsAdapter.GetResourceGroupName(); string resourceName = BmsAdapter.GetResourceName(); string vaultLocation = BmsAdapter.GetResourceLocation(); string containerName = rp.ContainerName; string protectedItemName = rp.ItemName; string recoveryPointId = rp.RecoveryPointId; //validtion block if(storageAccountLocation != vaultLocation) { throw new Exception(Resources.RestoreDiskIncorrectRegion); } string vmType = containerName.Split(';')[1].Equals("iaasvmcontainer", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; string strType = storageAccountType.Equals("Microsoft.ClassicStorage/StorageAccounts", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; if(vmType != strType) { throw new Exception(String.Format(Resources.RestoreDiskStorageTypeError, vmType)); } IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest() { CreateNewCloudService = false, RecoveryPointId = recoveryPointId, RecoveryType = RecoveryType.RestoreDisks, Region = vaultLocation, StorageAccountId = storageAccountId, SourceResourceId = rp.SourceResourceId, }; if (rp.EncryptionEnabled) { restoreRequest.EncryptionDetails = new EncryptionDetails() { EncryptionEnabled = rp.EncryptionEnabled, KekUrl = rp.KeyAndSecretDetails.KeyUrl, KekVaultId = rp.KeyAndSecretDetails.KeyVaultId, SecretKeyUrl = rp.KeyAndSecretDetails.SecretUrl, SecretKeyVaultId = rp.KeyAndSecretDetails.SecretVaultId, }; } TriggerRestoreRequest triggerRestoreRequest = new TriggerRestoreRequest(); triggerRestoreRequest.Item = new RestoreRequestResource(); triggerRestoreRequest.Item.Properties = new RestoreRequest(); triggerRestoreRequest.Item.Properties = restoreRequest; var response = BmsAdapter.Client.Restores.TriggerRestoreAsync( resourceGroupName, resourceName, BmsAdapter.GetCustomRequestHeaders(), AzureFabricName, containerName, protectedItemName, recoveryPointId, triggerRestoreRequest, BmsAdapter.CmdletCancellationToken).Result; return response; }