diff --git a/src/Common/Commands.Common/AzureSubscriptionExtensions.cs b/src/Common/Commands.Common/AzureSubscriptionExtensions.cs new file mode 100644 index 000000000000..7be35662bc98 --- /dev/null +++ b/src/Common/Commands.Common/AzureSubscriptionExtensions.cs @@ -0,0 +1,60 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Azure.Common.Authentication.Models; + +namespace Microsoft.WindowsAzure.Commands.Common +{ + public static class AzureSubscriptionExtensions + { + + public static string GetStorageAccountName(this AzureSubscription subscription) + { + if (subscription == null || !subscription.IsPropertySet(AzureSubscription.Property.StorageAccount)) + { + return null; + } + + var result = subscription.GetProperty(AzureSubscription.Property.StorageAccount); + if (!string.IsNullOrWhiteSpace(result)) + { + try + { + var pairs = result.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries); + foreach (var pair in pairs) + { + var sides = pair.Split(new char[] {'='}, 2, StringSplitOptions.RemoveEmptyEntries); + if (string.Equals("AccountName", sides[0].Trim(), StringComparison.OrdinalIgnoreCase)) + { + result = sides[1].Trim(); + break; + } + } + } + catch + { + // if there are any errors, return the unchanged account name + } + } + + return result; + } + + } +} diff --git a/src/Common/Commands.Common/Commands.Common.csproj b/src/Common/Commands.Common/Commands.Common.csproj index 9546b1199367..6a2a6519ba57 100644 --- a/src/Common/Commands.Common/Commands.Common.csproj +++ b/src/Common/Commands.Common/Commands.Common.csproj @@ -148,6 +148,7 @@ + diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/NewAzureDeployment.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/NewAzureDeployment.cs index 3bf023b5faf3..6046108d14ec 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/NewAzureDeployment.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/NewAzureDeployment.cs @@ -17,6 +17,7 @@ using System.Management.Automation; using System.Net; using Microsoft.Azure.Common.Authentication.Models; +using Microsoft.WindowsAzure.Commands.Common; using Microsoft.WindowsAzure.Commands.ServiceManagement.Extensions; using Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers; using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties; @@ -113,7 +114,7 @@ public virtual void NewPaaSDeploymentProcess() AssertNoPersistenVmRoleExistsInDeployment(PVM.DeploymentSlotType.Production); AssertNoPersistenVmRoleExistsInDeployment(PVM.DeploymentSlotType.Staging); - var storageName = Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount); + var storageName = Profile.Context.Subscription.GetStorageAccountName(); Uri packageUrl; if (this.Package.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) || diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/SetAzureDeployment.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/SetAzureDeployment.cs index 114aa15cba3c..33b93a0d6de5 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/SetAzureDeployment.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/SetAzureDeployment.cs @@ -23,6 +23,7 @@ using Microsoft.WindowsAzure.Management.Compute.Models; using Microsoft.WindowsAzure.Management.Compute; using Hyak.Common; +using Microsoft.WindowsAzure.Commands.Common; namespace Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices { @@ -198,7 +199,7 @@ public void ExecuteCommand() if (string.Compare(ParameterSetName, "Upgrade", StringComparison.OrdinalIgnoreCase) == 0) { bool removePackage = false; - var storageName = Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount); + var storageName = Profile.Context.Subscription.GetStorageAccountName(); Uri packageUrl = null; if (Package.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) || diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/CustomScript/SetAzureVMCustomScriptExtension.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/CustomScript/SetAzureVMCustomScriptExtension.cs index ed7caee7b15c..9333d2f6d08f 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/CustomScript/SetAzureVMCustomScriptExtension.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/CustomScript/SetAzureVMCustomScriptExtension.cs @@ -210,7 +210,7 @@ protected override void ValidateParameters() protected string GetStorageName() { - return Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount); + return Profile.Context.Subscription.GetStorageAccountName(); } protected string GetStorageKey(string storageName) diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/DSC/DscExtensionCmdletCommonBase.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/DSC/DscExtensionCmdletCommonBase.cs index 74f205f77d39..90c2a1271933 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/DSC/DscExtensionCmdletCommonBase.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/DSC/DscExtensionCmdletCommonBase.cs @@ -44,21 +44,10 @@ internal static StorageCredentials GetStorageCredentials(this AzureSMCmdlet cmdl } else { - var storageAccountName = cmdlet.Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount); - - var storageClient = AzureSession.ClientFactory.CreateClient( - cmdlet.Profile, cmdlet.Profile.Context.Subscription, AzureEnvironment.Endpoint.ServiceManagement); - - if (!string.IsNullOrEmpty(storageAccountName) && storageClient != null) + var storageAccount = cmdlet.Profile.Context.GetCurrentStorageAccount(); + if (storageAccount != 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); - } + credentials = storageAccount.Credentials; } } diff --git a/src/ServiceManagement/Profile/Commands.Profile/Models/PsAzureSubscription.cs b/src/ServiceManagement/Profile/Commands.Profile/Models/PsAzureSubscription.cs index d3eb9a0d2f88..1478f48af722 100644 --- a/src/ServiceManagement/Profile/Commands.Profile/Models/PsAzureSubscription.cs +++ b/src/ServiceManagement/Profile/Commands.Profile/Models/PsAzureSubscription.cs @@ -57,32 +57,5 @@ public PSAzureSubscription(AzureSubscription subscription, AzureSMProfile profil public string CurrentStorageAccountName { get; set; } public string TenantId { get; set; } - - public string GetAccountName() - { - var result = CurrentStorageAccountName; - if (!string.IsNullOrWhiteSpace(result)) - { - try - { - var pairs = result.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries); - foreach (var pair in pairs) - { - var sides = pair.Split(new char[] {'='}, 2, StringSplitOptions.RemoveEmptyEntries); - if (string.Equals("AccountName", sides[0].Trim(), StringComparison.OrdinalIgnoreCase)) - { - result = sides[1].Trim(); - break; - } - } - } - catch - { - // if there are any errors, return the unchanged account name - } - } - - return result; - } } } diff --git a/src/ServiceManagement/Services/Commands.Utilities/CloudService/CloudServiceClient.cs b/src/ServiceManagement/Services/Commands.Utilities/CloudService/CloudServiceClient.cs index e9b7fad50cc3..1f734ed34575 100644 --- a/src/ServiceManagement/Services/Commands.Utilities/CloudService/CloudServiceClient.cs +++ b/src/ServiceManagement/Services/Commands.Utilities/CloudService/CloudServiceClient.cs @@ -502,7 +502,7 @@ private PublishContext CreatePublishContext( // If there's no storage service provided, try using the default one if (string.IsNullOrEmpty(storageServiceName)) { - storageServiceName = Subscription.GetProperty(AzureSubscription.Property.StorageAccount); + storageServiceName = Subscription.GetStorageAccountName(); } ServiceSettings serviceSettings = ServiceSettings.LoadDefault( diff --git a/src/ServiceManagement/Services/Commands/Websites/EnableAzureWebsiteDiagnostic.cs b/src/ServiceManagement/Services/Commands/Websites/EnableAzureWebsiteDiagnostic.cs index 79d491c40c1a..e09993e1d257 100644 --- a/src/ServiceManagement/Services/Commands/Websites/EnableAzureWebsiteDiagnostic.cs +++ b/src/ServiceManagement/Services/Commands/Websites/EnableAzureWebsiteDiagnostic.cs @@ -15,6 +15,7 @@ using System.Collections.Generic; using System.Management.Automation; using Microsoft.Azure.Common.Authentication.Models; +using Microsoft.WindowsAzure.Commands.Common; using Microsoft.WindowsAzure.Commands.Utilities.Websites; using Microsoft.WindowsAzure.Commands.Utilities.Websites.Common; using Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.DeploymentEntities; @@ -68,8 +69,14 @@ public override void ExecuteCmdlet() } else if (TableStorage.IsPresent || BlobStorage.IsPresent) { - properties[DiagnosticProperties.StorageAccountName] = string.IsNullOrEmpty(StorageAccountName) ? - Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount) : StorageAccountName; + if (string.IsNullOrWhiteSpace(StorageAccountName)) + { + properties[DiagnosticProperties.StorageAccountName] = Profile.Context.Subscription.GetStorageAccountName(); + } + else + { + properties[DiagnosticProperties.StorageAccountName] = StorageAccountName; + } if (TableStorage.IsPresent) {