Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Common/Commands.Common/AzureSubscriptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
1 change: 1 addition & 0 deletions src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<Compile Include="AzurePowerShell.cs" />
<Compile Include="AzureRmProfileProvider.cs" />
<Compile Include="AzureSMProfileProvder.cs" />
<Compile Include="AzureSubscriptionExtensions.cs" />
<Compile Include="Constants.cs" />
<Compile Include="ContextExtensions.cs" />
<Compile Include="IProfileProvider.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageManagementClient>(
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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down