Skip to content

Commit 836da7b

Browse files
committed
Merge pull request Azure#1086 from markcowl/storagedsc
Fixes for dsc and storage cmdlets
2 parents fc33f7f + 0ce925d commit 836da7b

File tree

9 files changed

+79
-47
lines changed

9 files changed

+79
-47
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
using Microsoft.Azure.Common.Authentication.Models;
21+
22+
namespace Microsoft.WindowsAzure.Commands.Common
23+
{
24+
public static class AzureSubscriptionExtensions
25+
{
26+
27+
public static string GetStorageAccountName(this AzureSubscription subscription)
28+
{
29+
if (subscription == null || !subscription.IsPropertySet(AzureSubscription.Property.StorageAccount))
30+
{
31+
return null;
32+
}
33+
34+
var result = subscription.GetProperty(AzureSubscription.Property.StorageAccount);
35+
if (!string.IsNullOrWhiteSpace(result))
36+
{
37+
try
38+
{
39+
var pairs = result.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries);
40+
foreach (var pair in pairs)
41+
{
42+
var sides = pair.Split(new char[] {'='}, 2, StringSplitOptions.RemoveEmptyEntries);
43+
if (string.Equals("AccountName", sides[0].Trim(), StringComparison.OrdinalIgnoreCase))
44+
{
45+
result = sides[1].Trim();
46+
break;
47+
}
48+
}
49+
}
50+
catch
51+
{
52+
// if there are any errors, return the unchanged account name
53+
}
54+
}
55+
56+
return result;
57+
}
58+
59+
}
60+
}

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<Compile Include="AzurePowerShell.cs" />
149149
<Compile Include="AzureRmProfileProvider.cs" />
150150
<Compile Include="AzureSMProfileProvder.cs" />
151+
<Compile Include="AzureSubscriptionExtensions.cs" />
151152
<Compile Include="Constants.cs" />
152153
<Compile Include="ContextExtensions.cs" />
153154
<Compile Include="IProfileProvider.cs" />

src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/NewAzureDeployment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Management.Automation;
1818
using System.Net;
1919
using Microsoft.Azure.Common.Authentication.Models;
20+
using Microsoft.WindowsAzure.Commands.Common;
2021
using Microsoft.WindowsAzure.Commands.ServiceManagement.Extensions;
2122
using Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers;
2223
using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
@@ -113,7 +114,7 @@ public virtual void NewPaaSDeploymentProcess()
113114
AssertNoPersistenVmRoleExistsInDeployment(PVM.DeploymentSlotType.Production);
114115
AssertNoPersistenVmRoleExistsInDeployment(PVM.DeploymentSlotType.Staging);
115116

116-
var storageName = Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount);
117+
var storageName = Profile.Context.Subscription.GetStorageAccountName();
117118

118119
Uri packageUrl;
119120
if (this.Package.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) ||

src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/SetAzureDeployment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Microsoft.WindowsAzure.Management.Compute.Models;
2424
using Microsoft.WindowsAzure.Management.Compute;
2525
using Hyak.Common;
26+
using Microsoft.WindowsAzure.Commands.Common;
2627

2728
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices
2829
{
@@ -198,7 +199,7 @@ public void ExecuteCommand()
198199
if (string.Compare(ParameterSetName, "Upgrade", StringComparison.OrdinalIgnoreCase) == 0)
199200
{
200201
bool removePackage = false;
201-
var storageName = Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount);
202+
var storageName = Profile.Context.Subscription.GetStorageAccountName();
202203

203204
Uri packageUrl = null;
204205
if (Package.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) ||

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/CustomScript/SetAzureVMCustomScriptExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected override void ValidateParameters()
210210

211211
protected string GetStorageName()
212212
{
213-
return Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount);
213+
return Profile.Context.Subscription.GetStorageAccountName();
214214
}
215215

216216
protected string GetStorageKey(string storageName)

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/DSC/DscExtensionCmdletCommonBase.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,10 @@ internal static StorageCredentials GetStorageCredentials(this AzureSMCmdlet cmdl
4444
}
4545
else
4646
{
47-
var storageAccountName = cmdlet.Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount);
48-
49-
var storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(
50-
cmdlet.Profile, cmdlet.Profile.Context.Subscription, AzureEnvironment.Endpoint.ServiceManagement);
51-
52-
if (!string.IsNullOrEmpty(storageAccountName) && storageClient != null)
47+
var storageAccount = cmdlet.Profile.Context.GetCurrentStorageAccount();
48+
if (storageAccount != null)
5349
{
54-
var keys = storageClient.StorageAccounts.GetKeys(storageAccountName);
55-
56-
if (keys != null)
57-
{
58-
var storageAccountKey = string.IsNullOrEmpty(keys.PrimaryKey) ? keys.SecondaryKey : keys.PrimaryKey;
59-
60-
credentials = new StorageCredentials(storageAccountName, storageAccountKey);
61-
}
50+
credentials = storageAccount.Credentials;
6251
}
6352
}
6453

src/ServiceManagement/Profile/Commands.Profile/Models/PsAzureSubscription.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,5 @@ public PSAzureSubscription(AzureSubscription subscription, AzureSMProfile profil
5757
public string CurrentStorageAccountName { get; set; }
5858

5959
public string TenantId { get; set; }
60-
61-
public string GetAccountName()
62-
{
63-
var result = CurrentStorageAccountName;
64-
if (!string.IsNullOrWhiteSpace(result))
65-
{
66-
try
67-
{
68-
var pairs = result.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries);
69-
foreach (var pair in pairs)
70-
{
71-
var sides = pair.Split(new char[] {'='}, 2, StringSplitOptions.RemoveEmptyEntries);
72-
if (string.Equals("AccountName", sides[0].Trim(), StringComparison.OrdinalIgnoreCase))
73-
{
74-
result = sides[1].Trim();
75-
break;
76-
}
77-
}
78-
}
79-
catch
80-
{
81-
// if there are any errors, return the unchanged account name
82-
}
83-
}
84-
85-
return result;
86-
}
8760
}
8861
}

src/ServiceManagement/Services/Commands.Utilities/CloudService/CloudServiceClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ private PublishContext CreatePublishContext(
502502
// If there's no storage service provided, try using the default one
503503
if (string.IsNullOrEmpty(storageServiceName))
504504
{
505-
storageServiceName = Subscription.GetProperty(AzureSubscription.Property.StorageAccount);
505+
storageServiceName = Subscription.GetStorageAccountName();
506506
}
507507

508508
ServiceSettings serviceSettings = ServiceSettings.LoadDefault(

src/ServiceManagement/Services/Commands/Websites/EnableAzureWebsiteDiagnostic.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.Generic;
1616
using System.Management.Automation;
1717
using Microsoft.Azure.Common.Authentication.Models;
18+
using Microsoft.WindowsAzure.Commands.Common;
1819
using Microsoft.WindowsAzure.Commands.Utilities.Websites;
1920
using Microsoft.WindowsAzure.Commands.Utilities.Websites.Common;
2021
using Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.DeploymentEntities;
@@ -68,8 +69,14 @@ public override void ExecuteCmdlet()
6869
}
6970
else if (TableStorage.IsPresent || BlobStorage.IsPresent)
7071
{
71-
properties[DiagnosticProperties.StorageAccountName] = string.IsNullOrEmpty(StorageAccountName) ?
72-
Profile.Context.Subscription.GetProperty(AzureSubscription.Property.StorageAccount) : StorageAccountName;
72+
if (string.IsNullOrWhiteSpace(StorageAccountName))
73+
{
74+
properties[DiagnosticProperties.StorageAccountName] = Profile.Context.Subscription.GetStorageAccountName();
75+
}
76+
else
77+
{
78+
properties[DiagnosticProperties.StorageAccountName] = StorageAccountName;
79+
}
7380

7481
if (TableStorage.IsPresent)
7582
{

0 commit comments

Comments
 (0)