/// <summary>
 /// Backs up an Api Management service.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IApiManagementOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='name'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Backup operation.
 /// </param>
 /// <returns>
 /// The response of the CreateOrUpdate Api Management service long
 /// running operation.
 /// </returns>
 public static ApiServiceLongRunningOperationResponse Backup(this IApiManagementOperations operations, string resourceGroupName, string name, ApiServiceBackupRestoreParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IApiManagementOperations)s).BackupAsync(resourceGroupName, name, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public void BackupRestore()
        {
            using (var context = UndoContext.Current)
            {
                context.Start("ResourceProviderFunctionalTests", "BackupRestore");

                TryCreateApiService();

                // create storage account with blob container for the API Management backup
                var storageManagementClient = GetServiceClient<StorageManagementClient>();

                var storageAccountName = TestUtilities.GenerateName("hydraapimstorage");
                Assert.True(
                    storageManagementClient.StorageAccounts.CheckNameAvailability(storageAccountName).IsAvailable,
                    "Could not generate unique storage account name");

                var storageCreateParams = new StorageAccountCreateParameters
                {
                    Name = storageAccountName,
                    Location = Location,
                    AffinityGroup = null,
                    Label = "ApimHydraBackupRestoreTest",
                    Description = "Api Management hydra client backup/restore test",
                    AccountType = StorageAccountTypes.StandardGRS
                };
                storageManagementClient.StorageAccounts.Create(storageCreateParams);
                var storageKeysResponse = storageManagementClient.StorageAccounts.GetKeys(storageAccountName);

                const string apimBackupContainerName = "apimbackupcontainer";

                // create API Management backup
                var apiManagementClient = GetServiceClient<ApiManagementClient>(new CSMTestEnvironmentFactory());
                apiManagementClient.RefreshAccessToken();

                const string apimBackupName = "apimbackup.zip";
                var backupRestoreParameters = new ApiServiceBackupRestoreParameters
                {
                    StorageAccount = storageAccountName,
                    AccessKey = storageKeysResponse.PrimaryKey,
                    ContainerName = apimBackupContainerName,
                    BackupName = apimBackupName
                };
                var response =
                    apiManagementClient.ResourceProvider.Backup(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        backupRestoreParameters);

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                // restore Api Management service from backup

                apiManagementClient.RefreshAccessToken();

                response =
                    apiManagementClient.ResourceProvider.Restore(
                        ResourceGroupName,
                        ApiManagementServiceName,
                        backupRestoreParameters);

                Assert.NotNull(response);
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var getResponse = apiManagementClient.ResourceProvider.Get(ResourceGroupName, ApiManagementServiceName);
                Assert.NotNull(getResponse);
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                Assert.NotNull(getResponse.Value);
                Assert.Equal("Succeeded", getResponse.Value.Properties.ProvisioningState);
            }
        }
 /// <summary>
 /// Backs up an Api Management service.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.ApiManagement.IApiManagementOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='name'>
 /// Required. The name of the Api Management service.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Backup operation.
 /// </param>
 /// <returns>
 /// The response of the CreateOrUpdate Api Management service long
 /// running operation.
 /// </returns>
 public static Task<ApiServiceLongRunningOperationResponse> BackupAsync(this IApiManagementOperations operations, string resourceGroupName, string name, ApiServiceBackupRestoreParameters parameters)
 {
     return operations.BackupAsync(resourceGroupName, name, parameters, CancellationToken.None);
 }
        public ApiManagementLongRunningOperation BeginRestoreApiManagement(
            string resourceGroupName,
            string serviceName,
            string storageAccountName,
            string storageAccountKey,
            string backupContainer,
            string backupBlob)
        {
            var parameters = new ApiServiceBackupRestoreParameters
            {
                StorageAccount = storageAccountName,
                AccessKey = storageAccountKey,
                ContainerName = backupContainer,
                BackupName = backupBlob
            };

            var longrunningResponse = Client.ResourceProvider.BeginRestoring(resourceGroupName, serviceName, parameters);
            AdjustRetryAfter(longrunningResponse, _client.LongRunningOperationInitialTimeout);
            return ApiManagementLongRunningOperation.CreateLongRunningOperation("Restore-AzureRmApiManagement", longrunningResponse);
        }
        public ApiManagementLongRunningOperation BeginBackupApiManagement(
            string resourceGroupName,
            string serviceName,
            string storageAccountName,
            string storageAccountKey,
            string backupContainer,
            string backupBlob)
        {
            if (string.IsNullOrWhiteSpace(backupBlob))
            {
                backupBlob = string.Format("{0}-{1:yyyy-MM-dd-HH-mm}.apimbackup", serviceName, DateTime.Now);
            }

            var parameters = new ApiServiceBackupRestoreParameters
            {
                StorageAccount = storageAccountName,
                AccessKey = storageAccountKey,
                ContainerName = backupContainer,
                BackupName = backupBlob
            };

            var longrunningResponse = Client.ResourceProvider.BeginBackup(resourceGroupName, serviceName, parameters);
            AdjustRetryAfter(longrunningResponse, _client.LongRunningOperationInitialTimeout);
            return ApiManagementLongRunningOperation.CreateLongRunningOperation("Backup-AzureRmApiManagement", longrunningResponse);
        }