Пример #1
0
        /// <summary>
        /// If the subscription already contains a storage account by the same name create will be skipped.
        /// </summary>
        /// <param name="storageClient">The StorageClient that mapps our subscription.</param>
        /// <param name="storageAccountName">The Storage Account we want to create.</param>
        /// <param name="location">The location where we want the storage account.</param>
        /// <exception cref="InvalidOperationException">If the <paramref name="storageAccountName"/> is not globally unique.</exception>
        public static void CreateNewStorageAccountIfNotExists(this IStorageClient storageClient, string storageAccountName, string location = LocationConstants.NorthEurope)
        {
            var storageAccountList = storageClient.GetStorageAccountList();

            if (storageAccountList.Any(sa => sa.Name == storageAccountName))
            {
                return;
            }

            try
            {
                storageClient.CreateNewStorageAccount(storageAccountName, location);
            }
            catch (WebException we)
            {
                if (we.Status != WebExceptionStatus.ProtocolError ||
                    we.Message != "The remote server returned an error: (409) Conflict.")
                {
                    throw new InvalidOperationException(string.Format("The storage account '{0}' already exists.", storageAccountName), we);
                }
                throw;
            }
        }