예제 #1
0
 /// <summary>
 /// The Get Operation Status operation returns the status of the
 /// specified operation. After calling an asynchronous operation, you
 /// can call Get Operation Status to determine whether the operation
 /// has succeeded, failed, or is still in progress.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460783.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Storage.IStorageManagementClient.
 /// </param>
 /// <param name='requestId'>
 /// Required. The request ID for the request you wish to track. The
 /// request ID is returned in the x-ms-request-id response header for
 /// every request.
 /// </param>
 /// <returns>
 /// The response body contains the status of the specified asynchronous
 /// operation, indicating whether it has succeeded, is inprogress, or
 /// has failed. Note that this status is distinct from the HTTP status
 /// code returned for the Get Operation Status operation itself. If
 /// the asynchronous operation succeeded, the response body includes
 /// the HTTP status code for the successful request. If the
 /// asynchronous operation failed, the response body includes the HTTP
 /// status code for the failed request and error information regarding
 /// the failure.
 /// </returns>
 public static OperationStatusResponse GetOperationStatus(this IStorageManagementClient operations, string requestId)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IStorageManagementClient)s).GetOperationStatusAsync(requestId);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
 public static PSStorageAccount Create(StorageModels.StorageAccount storageAccount, IStorageManagementClient client)
 {
     var result = new PSStorageAccount(storageAccount);
     var credentials = StorageUtilities.GenerateStorageCredentials(client, result.ResourceGroupName, result.StorageAccountName);
     CloudStorageAccount account = new CloudStorageAccount(credentials,
         storageAccount.PrimaryEndpoints.Blob, storageAccount.PrimaryEndpoints.Queue, storageAccount.PrimaryEndpoints.Table, null);
     result.Context = new AzureStorageContext(account);
     return result;
 }
예제 #3
0
        public static PSStorageAccount Create(StorageAccount storageAccount, IStorageManagementClient client)
        {
            var result                  = new PSStorageAccount(storageAccount);
            var credentials             = StorageUtilities.GenerateStorageCredentials(client, result.ResourceGroupName, result.StorageAccountName);
            CloudStorageAccount account = new CloudStorageAccount(credentials,
                                                                  storageAccount.PrimaryEndpoints.Blob, storageAccount.PrimaryEndpoints.Queue, storageAccount.PrimaryEndpoints.Table, null);

            result.Context = new AzureStorageContext(account);
            return(result);
        }
예제 #4
0
        private async Task <IStorageManagementClient> GetStorageManagementAsync()
        {
            if (_storage != null)
            {
                return(_storage);
            }
            var token = await _servicePrincipalAuthentication.AquireTokenAsync();

            _storage = new StorageManagementClient(new TokenCloudCredentials(Configuration.SubscriptionId, token));
            return(_storage);
        }
        /// <summary>
        /// Create a cloud storage account using a service management storage client
        /// </summary>
        /// <param name="storageClient">The client to use to get storage account details.</param>
        /// <param name="accountName">The name of the storage account.</param>
        /// <returns>A CloudStorageAccount that can be used by windows azure storage library to manipulate objects in the storage account.</returns>
        public static CloudStorageAccount GenerateCloudStorageAccount(IStorageManagementClient storageClient, string accountName)
        {
            if (!TestMockSupport.RunningMocked)
            {
                var storageServiceResponse = storageClient.StorageAccounts.Get(accountName);

                Uri fileEndpoint = null;
                Uri blobEndpoint = null;
                Uri queueEndpoint = null;
                Uri tableEndpoint = null;

                if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 4)
                {
                    fileEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[3].ToString());
                }

                if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 3)
                {
                    tableEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[2].ToString());
                    queueEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[1].ToString());
                }

                if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 1)
                {
                    blobEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[0].ToString());
                }

                return new CloudStorageAccount(
                    GenerateStorageCredentials(storageClient, storageServiceResponse.StorageAccount.Name),
                    blobEndpoint,
                    queueEndpoint,
                    tableEndpoint,
                    fileEndpoint);
            }
            else
            {
                return new CloudStorageAccount(
                    new StorageCredentials(accountName,
                        Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))),
                    new Uri(string.Format("https://{0}.blob.core.windows.net", accountName)),
                    new Uri(string.Format("https://{0}.queue.core.windows.net", accountName)),
                    new Uri(string.Format("https://{0}.table.core.windows.net", accountName)),
                    new Uri(string.Format("https://{0}.file.core.windows.net", accountName)));
            }
        }
        /// <summary>
        /// Create a cloud storage account using a service management storage client
        /// </summary>
        /// <param name="storageClient">The client to use to get storage account details.</param>
        /// <param name="accountName">The name of the storage account.</param>
        /// <returns>A CloudStorageAccount that can be used by windows azure storage library to manipulate objects in the storage account.</returns>
        public static CloudStorageAccount GenerateCloudStorageAccount(IStorageManagementClient storageClient, string accountName)
        {
            if (!TestMockSupport.RunningMocked)
            {
                var storageServiceResponse = storageClient.StorageAccounts.Get(accountName);

                Uri fileEndpoint  = null;
                Uri blobEndpoint  = null;
                Uri queueEndpoint = null;
                Uri tableEndpoint = null;

                if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 4)
                {
                    fileEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[3].ToString());
                }

                if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 3)
                {
                    tableEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[2].ToString());
                    queueEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[1].ToString());
                }

                if (storageServiceResponse.StorageAccount.Properties.Endpoints.Count >= 1)
                {
                    blobEndpoint =
                        StorageUtilities.CreateHttpsEndpoint(
                            storageServiceResponse.StorageAccount.Properties.Endpoints[0].ToString());
                }

                return(new CloudStorageAccount(
                           GenerateStorageCredentials(storageClient, storageServiceResponse.StorageAccount.Name),
                           blobEndpoint,
                           queueEndpoint,
                           tableEndpoint,
                           fileEndpoint));
            }
            else
            {
                return(new CloudStorageAccount(
                           new StorageCredentials(accountName,
                                                  Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))),
                           new Uri(string.Format("https://{0}.blob.core.windows.net", accountName)),
                           new Uri(string.Format("https://{0}.queue.core.windows.net", accountName)),
                           new Uri(string.Format("https://{0}.table.core.windows.net", accountName)),
                           new Uri(string.Format("https://{0}.file.core.windows.net", accountName))));
            }
        }
예제 #7
0
        private static bool TryGetStorageAccount(IStorageManagementClient storageClient, string storageAccountName, out StorageAccount storageAccount)
        {
            try
            {
                storageAccount = storageClient.StorageAccounts.Get(storageAccountName).StorageAccount;
            }
            catch
            {
                storageAccount = null;
            }

            return(storageAccount != null);
        }
        private static bool TryGetStorageAccount(IStorageManagementClient storageClient, string storageAccountName, out StorageAccount storageAccount)
        {
            try
            {
                var storageAccounts = storageClient.StorageAccounts.List();
                storageAccount = storageAccounts == null ? null : storageAccounts.FirstOrDefault(account => account.Name.Equals(storageAccountName));
            }
            catch
            {
                storageAccount = null;
            }

            return(storageAccount != null);
        }
예제 #9
0
        public static PSStorageAccount Create(StorageModels.StorageAccount storageAccount, IStorageManagementClient client)
        {
            var result = new PSStorageAccount(storageAccount);
             result.Context = new LazyAzureStorageContext((s) => 
             { 
                var credentials = StorageUtilities.GenerateStorageCredentials(new ARMStorageProvider(client), result.ResourceGroupName, s); 
                 return new CloudStorageAccount(credentials, 
                     ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Blob), 
                     ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Queue), 
                     ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Table), 
                     ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.File)); 
             }, result.StorageAccountName) as AzureStorageContext; 

            return result;
        }
 /// <summary>
 /// Create storage credentials for the given account
 /// </summary>
 /// <param name="storageClient">The RDFE storage management client.</param>
 /// <param name="accountName">The storage account name.</param>
 /// <returns>Storage credentials for the given account.</returns>
 public static StorageCredentials GenerateStorageCredentials(IStorageManagementClient storageClient,
                                                             string accountName)
 {
     if (!TestMockSupport.RunningMocked)
     {
         var storageKeysResponse = storageClient.StorageAccounts.GetKeys(accountName);
         return(new StorageCredentials(accountName,
                                       storageKeysResponse.PrimaryKey));
     }
     else
     {
         return(new StorageCredentials(accountName,
                                       Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()))));
     }
 }
예제 #11
0
 /// <summary>
 /// The Get Operation Status operation returns the status of
 /// thespecified operation. After calling an asynchronous operation,
 /// you can call Get Operation Status to determine whether the
 /// operation has succeeded, failed, or is still in progress.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460783.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Storage.IStorageManagementClient.
 /// </param>
 /// <param name='requestId'>
 /// The request ID for the request you wish to track. The request ID is
 /// returned in the x-ms-request-id response header for every request.
 /// </param>
 /// <returns>
 /// The response body contains the status of the specified asynchronous
 /// operation, indicating whether it has succeeded, is inprogress, or
 /// has failed. Note that this status is distinct from the HTTP status
 /// code returned for the Get Operation Status operation itself.  If
 /// the asynchronous operation succeeded, the response body includes
 /// the HTTP status code for the successful request.  If the
 /// asynchronous operation failed, the response body includes the HTTP
 /// status code for the failed request, and also includes error
 /// information regarding the failure.
 /// </returns>
 public static StorageOperationStatusResponse GetOperationStatus(this IStorageManagementClient operations, string requestId)
 {
     try
     {
         return(operations.GetOperationStatusAsync(requestId).Result);
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
        /// <summary>
        /// Initialize the storage account key if it's not specified.
        /// It can be defined in multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one we try to resolve within current subscription
        /// 2. The one defined in PrivateConfig in the configuration file
        /// </summary>
        public static string InitializeStorageAccountKey(IStorageManagementClient storageClient, string storageAccountName = null, string configurationPath = null)
        {
            string         storageAccountKey = null;
            StorageAccount storageAccount    = null;

            if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                var account = new ARMStorageProvider(storageClient).GetCloudStorageAccount(storageAccount.Name, ARMStorageService.ParseResourceGroupFromId(storageAccount.Id));
                // Help user retrieve the storage account key
                var credentials = account.Credentials;
                storageAccountKey = credentials.ExportBase64EncodedKey();
            }
            else
            {
                // Use the one defined in PrivateConfig
                storageAccountKey = GetConfigValueFromPrivateConfig(configurationPath, StorageAccountElemStr, PrivConfKeyAttr);
            }

            return(storageAccountKey);
        }
예제 #13
0
        /// <summary>
        /// Initialize the storage account key if it's not specified.
        /// It can be defined in multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one we try to resolve within current subscription
        /// 2. The one defined in PrivateConfig in the configuration file
        /// </summary>
        public static string InitializeStorageAccountKey(IStorageManagementClient storageClient, string storageAccountName = null, string configurationPath = null)
        {
            string         storageAccountKey = null;
            StorageAccount storageAccount    = null;

            if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Help user retrieve the storage account key
                var psStorageAccount = new PSStorageAccount(storageAccount);
                var credentials      = StorageUtilities.GenerateStorageCredentials(storageClient, psStorageAccount.ResourceGroupName, psStorageAccount.StorageAccountName);
                storageAccountKey = credentials.ExportBase64EncodedKey();
            }
            else
            {
                // Use the one defined in PrivateConfig
                storageAccountKey = GetStorageAccountInfoFromPrivateConfig(configurationPath, PrivConfKeyAttr);
            }

            return(storageAccountKey);
        }
예제 #14
0
        /// <summary>
        /// Initialize the storage account key if it's not specified.
        /// It can be defined in multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one we try to resolve within current subscription
        /// 2. The one defined in PrivateConfig in the configuration file
        /// </summary>
        public static string InitializeStorageAccountKey(IStorageManagementClient storageClient, string storageAccountName = null, string configurationPath = null)
        {
            string         storageAccountKey = null;
            StorageAccount storageAccount    = null;

            if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Help user retrieve the storage account key
                var keys = storageClient.StorageAccounts.GetKeys(storageAccount.Name);
                if (keys != null)
                {
                    storageAccountKey = !string.IsNullOrEmpty(keys.PrimaryKey) ? keys.PrimaryKey : keys.SecondaryKey;
                }
            }
            else
            {
                // Use the one defined in PrivateConfig
                storageAccountKey = GetConfigValueFromPrivateConfig(configurationPath, StorageAccountElemStr, PrivConfKeyAttr);
            }

            return(storageAccountKey);
        }
예제 #15
0
        /// <summary>
        /// Initialize the storage account key if it's not specified.
        /// It can be defined in multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one we try to resolve within current subscription
        /// 2. The one defined in PrivateConfig in the configuration file
        /// </summary>
        public static string InitializeStorageAccountKey(IStorageManagementClient storageClient, string storageAccountName = null, string configurationPath = null)
        {
            string storageAccountKey = null;
            StorageAccount storageAccount = null;

            if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Help user retrieve the storage account key
                var psStorageAccount = new PSStorageAccount(storageAccount);
                var credentials = StorageUtilities.GenerateStorageCredentials(storageClient, psStorageAccount.ResourceGroupName, psStorageAccount.StorageAccountName);
                storageAccountKey = credentials.ExportBase64EncodedKey();
            }
            else
            {
                // Use the one defined in PrivateConfig
                storageAccountKey = GetStorageAccountInfoFromPrivateConfig(configurationPath, PrivConfKeyAttr);
            }

            return storageAccountKey;
        }
예제 #16
0
        /// <summary>
        /// Initialize the storage account endpoint if it's not specified.
        /// We can get the value from multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one get from StorageContext parameter
        /// 2. The one get from the storage account
        /// 3. The one get from PrivateConfig element in config file
        /// 4. The one get from current Azure Environment
        /// </summary>
        public static string InitializeStorageAccountEndpoint(string storageAccountName, string storageAccountKey, IStorageManagementClient storageClient,
            AzureStorageContext storageContext = null, string configurationPath = null, AzureContext defaultContext = null)
        {
            string storageAccountEndpoint = null;
            StorageAccount storageAccount = null;

            if (storageContext != null)
            {
                // Get value from StorageContext
                storageAccountEndpoint = GetEndpointFromStorageContext(storageContext);
            }
            else if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Get value from StorageAccount
                var endpoints = storageAccount.PrimaryEndpoints;
                var context = CreateStorageContext(endpoints.Blob, endpoints.Queue, endpoints.Table, endpoints.File, storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }
            else if (!string.IsNullOrEmpty(
                storageAccountEndpoint = GetStorageAccountInfoFromPrivateConfig(configurationPath, PrivConfEndpointAttr)))
            {
                // We can get the value from PrivateConfig
            }
            else if (defaultContext != null && defaultContext.Environment != null)
            {
                // Get value from default azure environment. Default to use https
                Uri blobEndpoint = defaultContext.Environment.GetStorageBlobEndpoint(storageAccountName);
                Uri queueEndpoint = defaultContext.Environment.GetStorageQueueEndpoint(storageAccountName);
                Uri tableEndpoint = defaultContext.Environment.GetStorageTableEndpoint(storageAccountName);
                Uri fileEndpoint = defaultContext.Environment.GetStorageFileEndpoint(storageAccountName);
                var context = CreateStorageContext(blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint, storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }

            return storageAccountEndpoint;
        }
        private static bool TryGetStorageAccount(IStorageManagementClient storageClient, string storageAccountName, out StorageAccount storageAccount)
        {
            try
            {
                storageAccount = storageClient.StorageAccounts.Get(storageAccountName).StorageAccount;
            }
            catch
            {
                storageAccount = null;
            }

            return storageAccount != null;
        }
        /// <summary>
        /// Initialize the storage account key if it's not specified.
        /// It can be defined in multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one we try to resolve within current subscription
        /// 2. The one defined in PrivateConfig in the configuration file
        /// </summary>
        public static string InitializeStorageAccountKey(IStorageManagementClient storageClient, string storageAccountName = null, string configurationPath = null)
        {
            string storageAccountKey = null;
            StorageAccount storageAccount = null;

            if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Help user retrieve the storage account key
                var keys = storageClient.StorageAccounts.GetKeys(storageAccount.Name);
                if (keys != null)
                {
                    storageAccountKey = !string.IsNullOrEmpty(keys.PrimaryKey) ? keys.PrimaryKey : keys.SecondaryKey;
                }
            }
            else
            {
                // Use the one defined in PrivateConfig
                storageAccountKey = GetConfigValueFromPrivateConfig(configurationPath, StorageAccountElemStr, PrivConfKeyAttr);
            }

            return storageAccountKey;
        }
예제 #19
0
        /// <summary>
        /// Initialize the storage account endpoint if it's not specified.
        /// We can get the value from multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one get from StorageContext parameter
        /// 2. The one get from the storage account
        /// 3. The one get from PrivateConfig element in config file
        /// 4. The one get from current Azure Environment
        /// </summary>
        public static string InitializeStorageAccountEndpoint(string storageAccountName, string storageAccountKey, IStorageManagementClient storageClient,
                                                              IStorageContext storageContext = null, string configurationPath = null, IAzureContext defaultContext = null)
        {
            string         storageAccountEndpoint = null;
            StorageAccount storageAccount         = null;

            if (storageContext != null)
            {
                // Get value from StorageContext
                storageAccountEndpoint = GetEndpointFromStorageContext(storageContext);
            }
            else if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount) &&
                     storageAccount.Properties.Endpoints.Count >= 4)
            {
                // Get value from StorageAccount
                var endpoints = storageAccount.Properties.Endpoints;
                var context   = CreateStorageContext(endpoints[0], endpoints[1], endpoints[2], endpoints[3], storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }
            else if (!string.IsNullOrEmpty(
                         storageAccountEndpoint = GetConfigValueFromPrivateConfig(configurationPath, StorageAccountElemStr, PrivConfEndpointAttr)))
            {
                // We can get value from PrivateConfig
            }
            else if (defaultContext != null && defaultContext.Environment != null)
            {
                // Get value from default azure environment. Default to use https
                Uri blobEndpoint  = defaultContext.Environment.GetStorageBlobEndpoint(storageAccountName);
                Uri queueEndpoint = defaultContext.Environment.GetStorageQueueEndpoint(storageAccountName);
                Uri tableEndpoint = defaultContext.Environment.GetStorageTableEndpoint(storageAccountName);
                Uri fileEndpoint  = defaultContext.Environment.GetStorageFileEndpoint(storageAccountName);
                var context       = CreateStorageContext(blobEndpoint, queueEndpoint, tableEndpoint, fileEndpoint, storageAccountName, storageAccountKey);
                storageAccountEndpoint = GetEndpointFromStorageContext(context);
            }

            return(storageAccountEndpoint);
        }
        public static PSStorageAccount Create(StorageModels.StorageAccount storageAccount, IStorageManagementClient client)
        {
            var result = new PSStorageAccount(storageAccount);

            result.Context = new LazyAzureStorageContext((s) =>
            {
                return((new ARMStorageProvider(client)).GetCloudStorageAccount(s, result.ResourceGroupName));
            }, result.StorageAccountName) as AzureStorageContext;

            return(result);
        }
 private async Task<IStorageManagementClient> GetStorageManagementAsync()
 {
     if (_storage != null)
     {
         return _storage;
     }
     var token = await _servicePrincipalAuthentication.AquireTokenAsync();
     _storage = new StorageManagementClient(new TokenCloudCredentials(Configuration.SubscriptionId, token));
     return _storage;
 }
예제 #22
0
 public ARMStorageProvider(IStorageManagementClient client)
 {
     _client = client;
 }
예제 #23
0
        private static bool TryGetStorageAccount(IStorageManagementClient storageClient, string storageAccountName, out StorageAccount storageAccount)
        {
            try
            {
                var storageAccounts = storageClient.StorageAccounts.List().StorageAccounts;
                storageAccount = storageAccounts == null ? null : storageAccounts.FirstOrDefault(account => account.Name.Equals(storageAccountName));
            }
            catch
            {
                storageAccount = null;
            }

            return storageAccount != null;
        }
        /// <summary>
        /// Parse public and private configurations, and automatically resolve storage key for private configuration.
        /// </summary>
        /// <param name="publicConfigPath">Public configuration file path</param>
        /// <param name="privateConfigPath">Private configuration file path, can be empty</param>
        /// <param name="storageClient">Storage client</param>
        /// <returns>A tuple with public configuration as first element and private configuration as second element</returns>
        public static Tuple <Hashtable, Hashtable> GetConfigurationsFromFiles(string publicConfigPath, string privateConfigPath, string resourceId, Cmdlet cmdlet, IStorageManagementClient storageClient)
        {
            var publicConfig  = GetPublicConfigFromJsonFile(publicConfigPath, null, resourceId, cmdlet);
            var privateConfig = string.IsNullOrEmpty(privateConfigPath) ? new Hashtable() :
                                JsonConvert.DeserializeObject <Hashtable>(File.ReadAllText(privateConfigPath));

            // Resolve storage account name
            // Storage account name must be provided in public config
            var storageAccountNameInPublicConfig = publicConfig[StorageAccount] as string;

            if (string.IsNullOrEmpty(storageAccountNameInPublicConfig))
            {
                throw new ArgumentException(Properties.Resources.DiagnosticsExtensionNullStorageAccountName);
            }

            string storageAccountNameInPrivateConfig = privateConfig[StorageAccountNameTag] as string;

            if (!string.IsNullOrEmpty(storageAccountNameInPrivateConfig) &&
                !string.Equals(storageAccountNameInPrivateConfig, storageAccountNameInPublicConfig, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(Properties.Resources.DiagnosticsExtensionMismatchStorageAccountName, StorageAccountNameTag);
            }

            privateConfig[StorageAccountNameTag] = storageAccountNameInPublicConfig;

            string storageAccountKey = null;

            // If sas token is provided, just use it.
            // Otherwise, try to resolve storage key.
            if (string.IsNullOrEmpty(privateConfig[StorageAccountSASTokenTag] as string))
            {
                // Resolve storage account key
                storageAccountKey = InitializeStorageAccountKey(storageClient, storageAccountNameInPublicConfig, privateConfigPath);
                if (string.IsNullOrEmpty(storageAccountKey))
                {
                    storageAccountKey = privateConfig[StorageAccountKeyTag] as string;

                    if (string.IsNullOrEmpty(storageAccountKey))
                    {
                        // Throw exception if no storage key provided in private config and cannot retrieve it from server
                        throw new ArgumentException(Properties.Resources.DiagnosticsExtensionNullStorageAccountKey);
                    }
                }
                else
                {
                    // If storage key can be retrieved, use that one.
                    privateConfig[StorageAccountKeyTag] = storageAccountKey;
                }
            }


            // Resolve storage account endpoint
            var storageAccountEndpoint = storageAccountKey == null? null: InitializeStorageAccountEndpoint(storageAccountNameInPublicConfig, storageAccountKey, storageClient);

            if (string.IsNullOrEmpty(storageAccountEndpoint))
            {
                storageAccountEndpoint = privateConfig[StorageAccountEndPointTag] as string;

                if (string.IsNullOrEmpty(storageAccountEndpoint))
                {
                    // Throw exception if no storage endpoint provided in private config and cannot retrieve it from server
                    throw new ArgumentNullException(Properties.Resources.DiagnosticsExtensionNullStorageAccountEndpoint);
                }
            }
            else
            {
                // If storage account endpoint can be retrieved, use that one.
                privateConfig[StorageAccountEndPointTag] = storageAccountEndpoint;
            }

            return(new Tuple <Hashtable, Hashtable>(publicConfig, privateConfig));
        }
 /// <summary>
 /// The Get Create Operation Status operation returns the status of the
 /// specified create operation. After calling the asynchronous Begin
 /// Create operation, you can call Get Create Operation Status to
 /// determine whether the operation has succeeded, failed, or is still
 /// in progress.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Storage.IStorageManagementClient.
 /// </param>
 /// <param name='operationStatusLink'>
 /// Required. The URL where the status of the long-running create
 /// operation can be checked.
 /// </param>
 /// <returns>
 /// The Create storage account operation response.
 /// </returns>
 public static Task <StorageAccountCreateResponse> GetCreateOperationStatusAsync(this IStorageManagementClient operations, string operationStatusLink)
 {
     return(operations.GetCreateOperationStatusAsync(operationStatusLink, CancellationToken.None));
 }
 public StorageManagementClientWrapper(IStorageManagementClient resourceManagementClient)
 {
     StorageManagementClient = resourceManagementClient;
 }
예제 #27
0
        /// <summary>
        /// Parse public and private configurations, and automatically resolve storage key for private configuration.
        /// </summary>
        /// <param name="publicConfigPath">Public configuration file path</param>
        /// <param name="privateConfigPath">Private configuration file path, can be empty</param>
        /// <param name="storageClient">Storage client</param>
        /// <returns>A tuple with public configuration as first element and private configuration as second element</returns>
        public static Tuple <Hashtable, Hashtable> GetConfigurationsFromFiles(string publicConfigPath, string privateConfigPath, string resourceId, Cmdlet cmdlet, IStorageManagementClient storageClient)
        {
            var publicConfig  = GetPublicConfigFromJsonFile(publicConfigPath, null, resourceId, cmdlet);
            var privateConfig = string.IsNullOrEmpty(privateConfigPath) ? new Hashtable() :
                                JsonConvert.DeserializeObject <Hashtable>(File.ReadAllText(privateConfigPath));

            // Resolve storage account name
            // Storage account name must be provided in public config
            var storageAccountName = publicConfig[StorageAccount] as string;

            if (string.IsNullOrEmpty(storageAccountName))
            {
                throw new ArgumentException(Properties.Resources.DiagnosticsExtensionNullStorageAccountName);
            }

            privateConfig[StorageAccountNameTag] = storageAccountName;

            // Resolve storage account key
            var storageAccountKey = InitializeStorageAccountKey(storageClient, storageAccountName, privateConfigPath);

            if (string.IsNullOrEmpty(storageAccountKey))
            {
                storageAccountKey = privateConfig[StorageAccountKeyTag] as string;

                if (string.IsNullOrEmpty(storageAccountKey))
                {
                    // Throw exception if no storage key provided in private config and cannot retrieve it from server
                    throw new ArgumentException(Properties.Resources.DiagnosticsExtensionNullStorageAccountKey);
                }
            }
            else
            {
                // If storage key can be retrieved, use that one.
                privateConfig[StorageAccountKeyTag] = storageAccountKey;
            }


            // Resolve storage account endpoint
            var storageAccountEndpoint = InitializeStorageAccountEndpoint(storageAccountName, storageAccountKey, storageClient);

            if (string.IsNullOrEmpty(storageAccountEndpoint))
            {
                storageAccountEndpoint = privateConfig[StorageAccountEndPointTag] as string;

                if (string.IsNullOrEmpty(storageAccountEndpoint))
                {
                    // Throw exception if no storage endpoint provided in private config and cannot retrieve it from server
                    throw new ArgumentNullException(Properties.Resources.DiagnosticsExtensionNullStorageAccountEndpoint);
                }
            }
            else
            {
                // If storage account endpoint can be retrieved, use that one.
                privateConfig[StorageAccountEndPointTag] = storageAccountEndpoint;
            }

            return(new Tuple <Hashtable, Hashtable>(publicConfig, privateConfig));
        }
 public StorageClientWrapper(IStorageManagementClient storageManagementClient)
 {
     StorageManagementClient = storageManagementClient;
     CloudBlobClientFactory = (uri, cred) => new CloudBlobClient(uri, cred);
 }
예제 #29
0
        public static PSStorageAccount Create(StorageModels.StorageAccount storageAccount, IStorageManagementClient client)
        {
            var result                  = new PSStorageAccount(storageAccount);
            var credentials             = StorageUtilities.GenerateStorageCredentials(new ARMStorageProvider(client), result.ResourceGroupName, result.StorageAccountName);
            CloudStorageAccount account = new CloudStorageAccount(credentials,
                                                                  ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Blob),
                                                                  ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Queue),
                                                                  ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.Table),
                                                                  ARMStorageService.GetUri(storageAccount.PrimaryEndpoints.File));

            result.Context = new AzureStorageContext(account);
            return(result);
        }
 internal AzureStorageServiceManager()
 {            
     StorageManagementClient = new StorageManagementClient(Dependencies.Subscription.Credentials, 
         new Uri(Dependencies.Subscription.CoreEndpointUrl));
 }
예제 #31
0
 public RDFEStorageProvider(IStorageManagementClient client, IAzureEnvironment environment)
 {
     _client      = client;
     _environment = environment;
 }
예제 #32
0
        /// <summary>
        /// Parse public and private configurations, and automatically resolve storage key for private configuration.
        /// </summary>
        /// <param name="publicConfigPath">Public configuration file path</param>
        /// <param name="privateConfigPath">Private configuration file path, can be empty</param>
        /// <param name="storageClient">Storage client</param>
        /// <returns>A tuple with public configuration as first element and private configuration as second element</returns>
        public static Tuple<Hashtable, Hashtable> GetConfigurationsFromFiles(string publicConfigPath, string privateConfigPath, string resourceId, Cmdlet cmdlet, IStorageManagementClient storageClient)
        {
            var publicConfig = GetPublicConfigFromJsonFile(publicConfigPath, null, resourceId, cmdlet);
            var privateConfig = string.IsNullOrEmpty(privateConfigPath) ? new Hashtable() :
                JsonConvert.DeserializeObject<Hashtable>(File.ReadAllText(privateConfigPath));

            // Resolve storage account name
            // Storage account name must be provided in public config
            var storageAccountName = publicConfig[StorageAccount] as string;
            if (string.IsNullOrEmpty(storageAccountName))
            {
                throw new ArgumentException(Properties.Resources.DiagnosticsExtensionNullStorageAccountName);
            }

            privateConfig[StorageAccountNameTag] = storageAccountName;

            // Resolve storage account key
            var storageAccountKey = InitializeStorageAccountKey(storageClient, storageAccountName, privateConfigPath);
            if (string.IsNullOrEmpty(storageAccountKey))
            {
                storageAccountKey = privateConfig[StorageAccountKeyTag] as string;

                if (string.IsNullOrEmpty(storageAccountKey))
                {
                    // Throw exception if no storage key provided in private config and cannot retrieve it from server
                    throw new ArgumentException(Properties.Resources.DiagnosticsExtensionNullStorageAccountKey);
                }
            }
            else
            {
                // If storage key can be retrieved, use that one.
                privateConfig[StorageAccountKeyTag] = storageAccountKey;
            }


            // Resolve storage account endpoint
            var storageAccountEndpoint = InitializeStorageAccountEndpoint(storageAccountName, storageAccountKey, storageClient);
            if (string.IsNullOrEmpty(storageAccountEndpoint))
            {
                storageAccountEndpoint = privateConfig[StorageAccountEndPointTag] as string;

                if (string.IsNullOrEmpty(storageAccountEndpoint))
                {
                    // Throw exception if no storage endpoint provided in private config and cannot retrieve it from server
                    throw new ArgumentNullException(Properties.Resources.DiagnosticsExtensionNullStorageAccountEndpoint);
                }
            }
            else
            {
                // If storage account endpoint can be retrieved, use that one.
                privateConfig[StorageAccountEndPointTag] = storageAccountEndpoint;
            }

            return new Tuple<Hashtable, Hashtable>(publicConfig, privateConfig);
        }
예제 #33
0
 public ARMStorageProvider(IStorageManagementClient client)
 {
     _client = client;
 }
예제 #34
0
        public static PSStorageAccount Create(StorageModels.StorageAccount storageAccount, IStorageManagementClient client)
        {
            var result = new PSStorageAccount(storageAccount);

            result.Context = new LazyAzureStorageContext((s) =>
            {
                //var credentials = StorageUtilities.GenerateStorageCredentials(client, result.ResourceGroupName, result.StorageAccountName);
                //var credentials = StorageUtilities.GenerateStorageCredentials(new ARMStorageProvider(client), result.ResourceGroupName, result.StorageAccountName);

                //return new CloudStorageAccount(credentials,
                //    new Uri(storageAccount.PrimaryEndpoints.Blob), new Uri(storageAccount.PrimaryEndpoints.Queue), new Uri(storageAccount.PrimaryEndpoints.Table), null);


                return((new ARMStorageProvider(client)).GetCloudStorageAccount(s, result.ResourceGroupName));
            }, result.StorageAccountName) as AzureStorageContext;

            return(result);
        }
예제 #35
0
 public StorageClientWrapper(IStorageManagementClient storageManagementClient)
 {
     StorageManagementClient = storageManagementClient;
     CloudBlobClientFactory  = (uri, cred) => new CloudBlobClient(uri, cred);
 }
예제 #36
0
 /// <summary>
 /// The Get Operation Status operation returns the status of
 /// thespecified operation. After calling an asynchronous operation,
 /// you can call Get Operation Status to determine whether the
 /// operation has succeeded, failed, or is still in progress.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460783.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Storage.IStorageManagementClient.
 /// </param>
 /// <param name='requestId'>
 /// The request ID for the request you wish to track. The request ID is
 /// returned in the x-ms-request-id response header for every request.
 /// </param>
 /// <returns>
 /// The response body contains the status of the specified asynchronous
 /// operation, indicating whether it has succeeded, is inprogress, or
 /// has failed. Note that this status is distinct from the HTTP status
 /// code returned for the Get Operation Status operation itself.  If
 /// the asynchronous operation succeeded, the response body includes
 /// the HTTP status code for the successful request.  If the
 /// asynchronous operation failed, the response body includes the HTTP
 /// status code for the failed request, and also includes error
 /// information regarding the failure.
 /// </returns>
 public static Task <StorageOperationStatusResponse> GetOperationStatusAsync(this IStorageManagementClient operations, string requestId)
 {
     return(operations.GetOperationStatusAsync(requestId, CancellationToken.None));
 }
예제 #37
0
 /// <summary>
 ///     Creates new MediaServicesClient.
 /// </summary>
 /// <param name="logger">The logger action</param>
 /// <param name="mediaServicesManagementClient"></param>
 /// <param name="storageClient"></param>
 public MediaServicesClient(Action <string> logger, IMediaServicesManagementClient mediaServicesManagementClient, IStorageManagementClient storageClient)
 {
     Logger         = logger;
     _storageClient = storageClient;
     _mediaServicesManagementClient = mediaServicesManagementClient;
 }
예제 #38
0
 /// <summary>
 /// Create storage credentials for the given account
 /// </summary>
 /// <param name="storageClient">The RDFE storage management client.</param>
 /// <param name="accountName">The storage account name.</param>
 /// <returns>Storage credentials for the given account.</returns>
 public static StorageCredentials GenerateStorageCredentials(IStorageManagementClient storageClient,
    string accountName)
 {
     if (!TestMockSupport.RunningMocked)
     {
         var storageKeysResponse = storageClient.StorageAccounts.GetKeys(accountName);
         return new StorageCredentials(accountName,
             storageKeysResponse.PrimaryKey);
     }
     else
     {
         return new StorageCredentials(accountName,
             Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())));
     }
 }
        /// <summary>
        /// Initialize the storage account key if it's not specified.
        /// It can be defined in multiple places, we only take the one with higher precedence. And the precedence is:
        /// 1. The one we try to resolve within current subscription
        /// 2. The one defined in PrivateConfig in the configuration file
        /// </summary>
        public static string InitializeStorageAccountKey(IStorageManagementClient storageClient, string storageAccountName = null, string configurationPath = null)
        {
            string storageAccountKey = null;
            StorageAccount storageAccount = null;

            if (TryGetStorageAccount(storageClient, storageAccountName, out storageAccount))
            {
                // Help user retrieve the storage account key
                var credentials = StorageUtilities.GenerateStorageCredentials(new ARMStorageProvider(storageClient),
                    ARMStorageService.ParseResourceGroupFromId(storageAccount.Id), storageAccount.Name);
                storageAccountKey = credentials.ExportBase64EncodedKey();
            }
            else
            {
                // Use the one defined in PrivateConfig
                storageAccountKey = GetConfigValueFromPrivateConfig(configurationPath, StorageAccountElemStr, PrivConfKeyAttr);
            }

            return storageAccountKey;
        }
예제 #40
0
 public StorageManagementClientWrapper(IStorageManagementClient resourceManagementClient)
 {
     StorageManagementClient = resourceManagementClient;
 }
 internal AzureStorageServiceManager()
 {
     StorageManagementClient = new StorageManagementClient(Dependencies.Subscription.Credentials,
                                                           new Uri(Dependencies.Subscription.CoreEndpointUrl));
 }