/// <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 AzureSMCmdlet cmdlet, AzureStorageContext storageContext) { StorageCredentials credentials = null; if (storageContext != null) { credentials = storageContext.StorageAccount.Credentials; } else { var storageAccount = cmdlet.Profile.Context.GetCurrentStorageAccount(); if (storageAccount != null) { credentials = storageAccount.Credentials; } } 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); }
internal static void ThrowInvalidArgumentError(this AzureSMCmdlet cmdlet, string format, params object[] args) { cmdlet.ThrowTerminatingError( new ErrorRecord( new ArgumentException(string.Format(CultureInfo.CurrentUICulture, format, args)), "InvalidArgument", ErrorCategory.InvalidArgument, null)); }
/// <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 AzureSMCmdlet cmdlet, IStorageContext storageContext) { StorageCredentials credentials = null; if (storageContext != null) { credentials = storageContext.GetCloudStorageAccount().Credentials; } else { var storageAccount = cmdlet.Profile.Context.GetCurrentStorageAccount( new RDFEStorageProvider(AzureSession.Instance.ClientFactory.CreateClient <StorageManagementClient>( cmdlet.Profile.Context, AzureEnvironment.Endpoint.ServiceManagement), cmdlet.Profile.Context.Environment)); if (storageAccount != null) { credentials = storageAccount.Credentials; } } 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); }