示例#1
0
        internal static StorageCredentials GetStorageCredentials(this AzurePSCmdlet cmdlet, String resourceGroupName, String storageAccountName)
        {
            StorageCredentials credentials = null;
            var storageClient = GetStorageClient(cmdlet);

            if (storageClient != null && storageClient.StorageAccounts != null)
            {
                var keys = storageClient.StorageAccounts.ListKeys(resourceGroupName, storageAccountName);

                if (keys != null && keys.StorageAccountKeys != null)
                {
                    var storageAccountKey = string.IsNullOrEmpty(keys.StorageAccountKeys.Key1) ? keys.StorageAccountKeys.Key2 : keys.StorageAccountKeys.Key1;

                    credentials = new StorageCredentials(storageAccountName, storageAccountKey);
                }
            }

            if (credentials == null)
            {
                cmdlet.ThrowTerminatingError(
                    new ErrorRecord(
                        new UnauthorizedAccessException(Properties.Resources.AzureVMDscDefaultStorageCredentialsNotFound),
                        "CredentialsNotFound",
                        ErrorCategory.PermissionDenied,
                        null));
            }

            if (string.IsNullOrEmpty(credentials.AccountName))
            {
                ThrowInvalidArgumentError(cmdlet, Properties.Resources.AzureVMDscStorageContextMustIncludeAccountName);
            }

            return(credentials);
        }
 internal static void ThrowInvalidArgumentError(this AzurePSCmdlet cmdlet, string format, params object[] args)
 {
     cmdlet.ThrowTerminatingError(
         new ErrorRecord(
             new ArgumentException(string.Format(CultureInfo.CurrentUICulture, format, args)),
             "InvalidArgument",
             ErrorCategory.InvalidArgument,
             null));
 }
示例#3
0
        private static IStorageManagementClient GetStorageClient(this AzurePSCmdlet cmdlet)
        {
            if (_storageClientWrapper == null)
            {
                _storageClientWrapper = new StorageManagementClientWrapper(cmdlet.Profile.Context);
            }

            return(_storageClientWrapper.StorageManagementClient);
        }
示例#4
0
        /// <summary>
        /// Flushes the debug messages to debug stream
        /// </summary>
        /// <param name="numDebugMessagesToFlush">number of debug messages to flush</param>
        /// <param name="cmdlet">Instance of AzurePSCCmdlet</param>
        private void FlushDebugMessages(int numDebugMessagesToFlush, AzurePSCmdlet cmdlet)
        {
            string message;
            int    count = 0;

            while ((count++ < numDebugMessagesToFlush) && cmdlet.DebugMessages.TryDequeue(out message))
            {
                cmdlet.WriteDebug(message);
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ParameterSetName == DefaultProfilePrameterSet)
            {
                Profile = AzurePSCmdlet.InitializeDefaultProfile();
            }

            if (Profile == null)
            {
                throw new ArgumentException(Resources.AzureProfileMustNotBeNull);
            }

            AzurePSCmdlet.CurrentProfile = Profile;
            WriteObject(Profile);
        }
        /// <summary>
        /// Attempts to get the user's credentials from the given Storage Context or the current subscription, if the former is null.
        /// Throws a terminating error if the credentials cannot be determined.
        /// </summary>
        internal static StorageCredentials GetStorageCredentials(this AzurePSCmdlet cmdlet, AzureStorageContext storageContext)
        {
            StorageCredentials credentials = null;

            if (storageContext != null)
            {
                credentials = storageContext.StorageAccount.Credentials;
            }
            else
            {
                var storageAccountName = cmdlet.Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount);

                var storageClient = AzureSession.ClientFactory.CreateClient <StorageManagementClient>(
                    cmdlet.Profile, cmdlet.Profile.Context.Subscription, AzureEnvironment.Endpoint.ServiceManagement);

                if (!string.IsNullOrEmpty(storageAccountName) && storageClient != null)
                {
                    var keys = storageClient.StorageAccounts.GetKeys(storageAccountName);

                    if (keys != null)
                    {
                        var storageAccountKey = string.IsNullOrEmpty(keys.PrimaryKey) ? keys.SecondaryKey : keys.PrimaryKey;

                        credentials = new StorageCredentials(storageAccountName, storageAccountKey);
                    }
                }
            }

            if (credentials == null)
            {
                cmdlet.ThrowTerminatingError(
                    new ErrorRecord(
                        new UnauthorizedAccessException(Resources.AzureVMDscDefaultStorageCredentialsNotFound),
                        "CredentialsNotFound",
                        ErrorCategory.PermissionDenied,
                        null));
            }

            if (string.IsNullOrEmpty(credentials.AccountName))
            {
                ThrowInvalidArgumentError(cmdlet, Resources.AzureVMDscStorageContextMustIncludeAccountName);
            }

            return(credentials);
        }
示例#7
0
 public bool IsMetricTermAccepted()
 {
     return(AzurePSCmdlet.IsDataCollectionAllowed());
 }
示例#8
0
 public PSCmdletAdapter(AzurePSCmdlet cmdlet, SessionState state)
 {
     this.CommandRuntime = cmdlet.CommandRuntime;
     _sessionState       = state;
 }