/// <summary>
 /// Updates the account type or tags for a storage account. It can also
 /// be used to add a custom domain (note that custom domains cannot be
 /// added via the Create operation). Only one custom domain is
 /// supported per storage account. In order to replace a custom
 /// domain, the old value must be cleared before a new value may be
 /// set. To clear a custom domain, simply update the custom domain
 /// with empty string. Then call update again with the new cutsom
 /// domain name. The update API can only be used to update one of
 /// tags, accountType, or customDomain per call. To update multiple of
 /// these properties, call the API multiple times with one change per
 /// call. This call does not change the storage keys for the account.
 /// If you want to change storage account keys, use the RegenerateKey
 /// operation. The location and name of the storage account cannot be
 /// changed after creation.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Storage.IStorageAccountOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group within the user's
 /// subscription.
 /// </param>
 /// <param name='accountName'>
 /// Required. The name of the storage account within the specified
 /// resource group. Storage account names must be between 3 and 24
 /// characters in length and use numbers and lower-case letters only.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters to update on the account. Note that only
 /// one property can be changed at a time using this API.
 /// </param>
 /// <returns>
 /// The Update storage account operation response.
 /// </returns>
 public static StorageAccountUpdateResponse Update(this IStorageAccountOperations operations, string resourceGroupName, string accountName, StorageAccountUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IStorageAccountOperations)s).UpdateAsync(resourceGroupName, accountName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Updates the account type or tags for a storage account. It can also
 /// be used to add a custom domain (note that custom domains cannot be
 /// added via the Create operation). Only one custom domain is
 /// supported per storage account. In order to replace a custom
 /// domain, the old value must be cleared before a new value may be
 /// set. To clear a custom domain, simply update the custom domain
 /// with empty string. Then call update again with the new cutsom
 /// domain name. The update API can only be used to update one of
 /// tags, accountType, or customDomain per call. To update multiple of
 /// these properties, call the API multiple times with one change per
 /// call. This call does not change the storage keys for the account.
 /// If you want to change storage account keys, use the RegenerateKey
 /// operation. The location and name of the storage account cannot be
 /// changed after creation.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Storage.IStorageAccountOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group within the user's
 /// subscription.
 /// </param>
 /// <param name='accountName'>
 /// Required. The name of the storage account within the specified
 /// resource group. Storage account names must be between 3 and 24
 /// characters in length and use numbers and lower-case letters only.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters to update on the account. Note that only
 /// one property can be changed at a time using this API.
 /// </param>
 /// <returns>
 /// The Update storage account operation response.
 /// </returns>
 public static Task<StorageAccountUpdateResponse> UpdateAsync(this IStorageAccountOperations operations, string resourceGroupName, string accountName, StorageAccountUpdateParameters parameters)
 {
     return operations.UpdateAsync(resourceGroupName, accountName, parameters, CancellationToken.None);
 }
        public void StorageAccountUpdateTest()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (var context = UndoContext.Current)
            {
                context.Start();

                var resourcesClient = StorageManagementTestUtilities.GetResourceManagementClient(handler);
                var storageMgmtClient = StorageManagementTestUtilities.GetStorageManagementClient(handler);

                // Create resource group
                var rgname = StorageManagementTestUtilities.CreateResourceGroup(resourcesClient);

                // Create storage account
                string accountName = StorageManagementTestUtilities.CreateStorageAccount(storageMgmtClient, rgname);

                // TODO: Remove wait when CSM is fixed
                TestUtilities.Wait(30000);

                // Update storage account type
                var parameters = new StorageAccountUpdateParameters
                {
                    AccountType = AccountType.StandardLRS
                };
                var updateAccountTypeRequest = storageMgmtClient.StorageAccounts.Update(rgname, accountName, parameters);
                Assert.Equal(updateAccountTypeRequest.StorageAccount.AccountType, AccountType.StandardLRS);

                var getRequest = storageMgmtClient.StorageAccounts.GetProperties(rgname, accountName);
                Assert.Equal(getRequest.StorageAccount.AccountType, AccountType.StandardLRS);

                // Update storage tags
                parameters = new StorageAccountUpdateParameters
                {
                    Tags = new Dictionary<string, string> 
                    {
                        {"key3","value3"},
                        {"key4","value4"}, 
                        {"key5","value6"}
                    }
                };

                var updateTagsRequest = storageMgmtClient.StorageAccounts.Update(rgname, accountName, parameters);
                Assert.Equal(updateTagsRequest.StorageAccount.Tags.Count, parameters.Tags.Count);
                Assert.Equal(updateTagsRequest.StorageAccount.Tags.ElementAt(0), parameters.Tags.ElementAt(0));

                getRequest = storageMgmtClient.StorageAccounts.GetProperties(rgname, accountName);
                Assert.Equal(getRequest.StorageAccount.Tags.Count, parameters.Tags.Count);

                // Update storage custom domains
                parameters = new StorageAccountUpdateParameters
                {
                    CustomDomain = new CustomDomain
                    {
                        Name = "foo.example.com",
                        UseSubDomain = true
                    }
                };

                StorageAccountUpdateResponse updateCustomDomainsRequest;
                try
                {
                    updateCustomDomainsRequest = storageMgmtClient.StorageAccounts.Update(rgname, accountName, parameters);
                    Assert.True(false, "This request should fail with the below code."); 
                } catch (Hyak.Common.CloudException ex)
                {
                    Assert.Equal(HttpStatusCode.BadRequest, ex.Response.StatusCode);
                    Assert.Equal("StorageDomainNameCouldNotVerify", ex.Error.Code);
                    Assert.True(ex.Error.Message != null && ex.Error.Message.StartsWith("The custom domain " + 
                        "name could not be verified. CNAME mapping from foo.example.com to "));
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            Dictionary<string, string> tagDictionary = null;
            StorageAccountUpdateParameters updateParameters = null;

            if (ParameterSetName == UpdateAccountTypeParamSet)
            {
                updateParameters = new StorageAccountUpdateParameters
                    {
                        AccountType = ParseAccountType(this.Type)
                    };
            }
            else if (ParameterSetName == UpdateCustomDomainParamSet)
            {
                updateParameters = new StorageAccountUpdateParameters
                    {
                        CustomDomain = new CustomDomain
                        {
                            Name = CustomDomainName,
                            UseSubDomain = UseSubDomain
                        }
                    };
            }
            else
            {
                tagDictionary = TagsConversionHelper.CreateTagDictionary(Tags, validate: true);

                updateParameters = new StorageAccountUpdateParameters
                    {
                        Tags = tagDictionary ?? new Dictionary<string, string>()
                    };
            }

            var updatedAccountResponse = this.StorageClient.StorageAccounts.Update(
                this.ResourceGroupName,
                this.Name,
                updateParameters);

            var storageAccount = this.StorageClient.StorageAccounts.GetProperties(this.ResourceGroupName, this.Name).StorageAccount;

            WriteStorageAccount(storageAccount);
        }
 /// <summary>
 /// Updates the storage account
 /// </summary>
 /// <param name="rgname">Resource Group Name</param>
 /// <param name="acctName">Account Name</param>
 /// <param name="storageMgmtClient"></param>
 private static void UpdateStorageAccount(string rgname, string acctName, StorageManagementClient storageMgmtClient)
 {
     Console.WriteLine("Updating storage account...");
     // Update storage account type
     var parameters = new StorageAccountUpdateParameters
     {
         AccountType = AccountType.StandardLRS
     };
     var response = storageMgmtClient.StorageAccounts.Update(rgname, acctName, parameters);
     Console.WriteLine("Account type on storage account updated to " + response.StorageAccount.AccountType);
 }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (ShouldProcess(this.Name, "Set Storage Account"))
            {
                if (this.force || this.AccessTier == null || ShouldContinue("Changing the access tier may result in additional charges. See (http://go.microsoft.com/fwlink/?LinkId=786482) to learn more.", ""))
                {
                    StorageAccountUpdateParameters updateParameters = new StorageAccountUpdateParameters();
                    if (this.SkuName != null)
                    {
                        updateParameters.Sku = new Sku(ParseSkuName(this.SkuName));
                    }

                    if (this.Tag != null)
                    {
                        Dictionary<string, string> tagDictionary = TagsConversionHelper.CreateTagDictionary(Tag, validate: true);
                        updateParameters.Tags = tagDictionary ?? new Dictionary<string, string>();
                    }

                    if (this.CustomDomainName != null)
                    {
                        updateParameters.CustomDomain = new CustomDomain()
                        {
                            Name = CustomDomainName,
                            UseSubDomain = UseSubDomain
                        };
                    }
                    else if (UseSubDomain != null)
                    {
                        throw new System.ArgumentException(string.Format("UseSubDomain must be set together with CustomDomainName."));
                    }

                    if (this.EnableEncryptionService != null || this.DisableEncryptionService != null)
                    {
                        updateParameters.Encryption = ParseEncryption(EnableEncryptionService, DisableEncryptionService);
                    }

                    if (this.AccessTier != null)
                    {
                        updateParameters.AccessTier = ParseAccessTier(AccessTier);
                    }

                    var updatedAccountResponse = this.StorageClient.StorageAccounts.Update(
                        this.ResourceGroupName,
                        this.Name,
                        updateParameters);

                    var storageAccount = this.StorageClient.StorageAccounts.GetProperties(this.ResourceGroupName, this.Name);

                    WriteStorageAccount(storageAccount);
                }
            }
        }
 /// <summary>
 /// Updates the storage account
 /// </summary>
 /// <param name="rgname">Resource Group Name</param>
 /// <param name="acctName">Account Name</param>
 /// <param name="storageMgmtClient"></param>
 private static void UpdateStorageAccountSku(string rgname, string acctName, SkuName skuName, StorageManagementClient storageMgmtClient)
 {
     Console.WriteLine("Updating storage account...");
     // Update storage account sku
     var parameters = new StorageAccountUpdateParameters
     {
         Sku = new Sku(skuName)
     };
     var storageAccount = storageMgmtClient.StorageAccounts.Update(rgname, acctName, parameters);
     Console.WriteLine("Sku on storage account updated to " + storageAccount.Sku.Name);
 }