Skip to content

Commit fed6d76

Browse files
committed
Responding to review feedback
1 parent a368dce commit fed6d76

File tree

11 files changed

+88
-26
lines changed

11 files changed

+88
-26
lines changed

src/Common/Commands.Common.Storage/AzureContextExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
2727
{
2828
public static class AzureContextExtensions
2929
{
30-
private static Dictionary<Guid, CloudStorageAccount> storageAccountCache = new Dictionary<Guid,CloudStorageAccount>();
31-
3230
/// <summary>
3331
/// Set the current storage account using the given connection string
3432
/// </summary>
@@ -49,7 +47,8 @@ public static void SetCurrentStorageAccount(this AzureContext context, string co
4947
/// <param name="account">A storage account.</param>
5048
public static void SetCurrentStorageAccount(this AzureContext context, IStorageContextProvider account)
5149
{
52-
if (context.Subscription != null && account != null && account.Context != null && account.Context.StorageAccount != null)
50+
if (context.Subscription != null && account != null && account.Context != null
51+
&& account.Context.StorageAccount != null)
5352
{
5453
context.SetCurrentStorageAccount(account.Context.StorageAccount.ToString(true));
5554
}

src/Common/Commands.Common.Storage/IStorageContextProvider.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
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;
216
using System.Collections.Generic;
317
using System.Linq;
418
using System.Text;

src/Common/Commands.Common/AzureDataCmdlet.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected override Azure.Common.Authentication.Models.AzureContext DefaultContex
4242

4343
if (SMProfile == null || SMProfile.Context == null)
4444
{
45-
throw new InvalidOperationException("There is no current context, please log in using Login-AzureRmAccount for Azure Resource Manager or Add-AzureAccount for Azure Service Management.");
45+
throw new InvalidOperationException(Resources.NoCurrentContextForDataCmdlet);
4646
}
4747

4848
return SMProfile.Context;
@@ -59,22 +59,6 @@ public AzureRMProfile RMProfile
5959
get { return AzureRmProfileProvider.Instance.Profile; }
6060
}
6161

62-
public static void ClearCurrentStorageAccount()
63-
{
64-
var RMProfile = AzureRmProfileProvider.Instance.Profile;
65-
if (RMProfile != null && RMProfile.Context != null &&
66-
RMProfile.Context.Subscription != null && RMProfile.Context.Subscription.IsPropertySet(AzureSubscription.Property.StorageAccount))
67-
{
68-
RMProfile.Context.Subscription.SetProperty(AzureSubscription.Property.StorageAccount, null);
69-
}
70-
71-
var SMProfile = AzureSMProfileProvider.Instance.Profile;
72-
if (SMProfile != null && SMProfile.Context != null && SMProfile.Context.Subscription != null && SMProfile.Context.Subscription.IsPropertySet(AzureSubscription.Property.StorageAccount))
73-
{
74-
SMProfile.Context.Subscription.SetProperty(AzureSubscription.Property.StorageAccount, null);
75-
}
76-
}
77-
7862
protected override void SaveDataCollectionProfile()
7963
{
8064
if (_dataCollectionProfile == null)

src/Common/Commands.Common/GeneralUtilities.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System.Xml;
2828
using Hyak.Common;
2929
using Microsoft.Azure.Common.Authentication;
30+
using Microsoft.Azure.Common.Authentication.Models;
3031
using Microsoft.WindowsAzure.Commands.Common;
3132

3233
namespace Microsoft.WindowsAzure.Commands.Utilities.Common
@@ -416,5 +417,31 @@ public static void EnsureDefaultProfileDirectoryExists()
416417
AzureSession.DataStore.CreateDirectory(AzureSession.ProfileDirectory);
417418
}
418419
}
420+
421+
/// <summary>
422+
/// Clear the current storage account from the context - guarantees that only one storage account will be active
423+
/// at a time.
424+
/// </summary>
425+
/// <param name="clearSMContext">Whenter to clear the service management context.</param>
426+
public static void ClearCurrentStorageAccount(bool clearSMContext = false)
427+
{
428+
var RMProfile = AzureRmProfileProvider.Instance.Profile;
429+
if (RMProfile != null && RMProfile.Context != null &&
430+
RMProfile.Context.Subscription != null && RMProfile.Context.Subscription.IsPropertySet(AzureSubscription.Property.StorageAccount))
431+
{
432+
RMProfile.Context.Subscription.SetProperty(AzureSubscription.Property.StorageAccount, null);
433+
}
434+
435+
if (clearSMContext)
436+
{
437+
var SMProfile = AzureSMProfileProvider.Instance.Profile;
438+
if (SMProfile != null && SMProfile.Context != null && SMProfile.Context.Subscription != null &&
439+
SMProfile.Context.Subscription.IsPropertySet(AzureSubscription.Property.StorageAccount))
440+
{
441+
SMProfile.Context.Subscription.SetProperty(AzureSubscription.Property.StorageAccount, null);
442+
}
443+
}
444+
}
445+
419446
}
420447
}

src/Common/Commands.Common/ProfileClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Microsoft.Azure.Common.Authentication.Factories;
2424
using Microsoft.Azure.Common.Authentication.Models;
2525
using Microsoft.WindowsAzure.Commands.Common.Properties;
26+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2627
using Microsoft.WindowsAzure.Subscriptions;
2728

2829
namespace Microsoft.Azure.Common.Authentication
@@ -650,6 +651,11 @@ public AzureSubscription SetSubscriptionAsDefault(Guid id, string accountName)
650651

651652
if (subscription != null)
652653
{
654+
if (subscription.IsPropertySet(AzureSubscription.Property.StorageAccount))
655+
{
656+
GeneralUtilities.ClearCurrentStorageAccount();
657+
}
658+
653659
Profile.DefaultSubscription = subscription;
654660
Profile.DefaultSubscription.Account = accountName;
655661
}

src/Common/Commands.Common/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Common/Commands.Common/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,4 +1541,7 @@ Select Y to enable data collection [Y/N]:</value>
15411541
<data name="UserNameNeedsToBeSpecified" xml:space="preserve">
15421542
<value>User name needs to be specified.</value>
15431543
</data>
1544+
<data name="NoCurrentContextForDataCmdlet" xml:space="preserve">
1545+
<value>"There is no current context, please log in using Login-AzureRmAccount for Azure Resource Manager or Add-AzureAccount for Azure Service Management."</value>
1546+
</data>
15441547
</root>

src/ResourceManager/Profile/Commands.Profile.Test/CommonDataCmdletTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.Azure.Commands.Profile.Models;
2323
using Microsoft.WindowsAzure.Commands.Common;
2424
using Microsoft.WindowsAzure.Commands.ScenarioTest;
25+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2526
using Xunit.Extensions;
2627

2728
namespace Microsoft.Azure.Commands.Profile.Test
@@ -115,7 +116,7 @@ public void CanClearStorageAccountForSMProfile(string connectionString, string e
115116
() =>
116117
{
117118
Assert.Equal(expected, AzureSMProfileProvider.Instance.Profile.Context.GetCurrentStorageAccountName());
118-
AzureDataCmdlet.ClearCurrentStorageAccount();
119+
GeneralUtilities.ClearCurrentStorageAccount(true);
119120
Assert.True(string.IsNullOrEmpty(AzureSMProfileProvider.Instance.Profile.Context.GetCurrentStorageAccountName()));
120121
});
121122
}
@@ -133,7 +134,7 @@ public void CanClearStorageAccountForRMProfile(string connectionString, string e
133134
() =>
134135
{
135136
Assert.Equal(expected, AzureRmProfileProvider.Instance.Profile.Context.GetCurrentStorageAccountName());
136-
AzureDataCmdlet.ClearCurrentStorageAccount();
137+
GeneralUtilities.ClearCurrentStorageAccount();
137138
Assert.True(string.IsNullOrEmpty(AzureRmProfileProvider.Instance.Profile.Context.GetCurrentStorageAccountName()));
138139
});
139140
}
@@ -149,7 +150,7 @@ public void CanClearStorageAccountForEmptyProfile()
149150
new AzureSMProfile(),
150151
() =>
151152
{
152-
AzureDataCmdlet.ClearCurrentStorageAccount();
153+
GeneralUtilities.ClearCurrentStorageAccount(true);
153154
Assert.True(string.IsNullOrEmpty(AzureSMProfileProvider.Instance.Profile.Context.GetCurrentStorageAccountName()));
154155
});
155156
}

src/ResourceManager/Storage/Commands.Management.Storage/StorageAccount/SetAzureRmCurrentStorageAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected override void ProcessRecord()
5555
}
5656

5757
// Clear the current storage account for both SM and RM
58-
AzureDataCmdlet.ClearCurrentStorageAccount();
58+
GeneralUtilities.ClearCurrentStorageAccount(true);
5959
DefaultContext.SetCurrentStorageAccount(account.ToString(true));
6060
WriteObject(account.Credentials.AccountName);
6161
}

src/ServiceManagement/Compute/Commands.ServiceManagement/Model/PSStorageService.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
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;
216
using System.Collections.Generic;
317
using System.Linq;
418
using System.Management.Automation;

src/ServiceManagement/Profile/Commands.Profile/Subscription/SetAzureSubscription.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ public override void ExecuteCmdlet()
160160
if (!string.IsNullOrEmpty(CurrentStorageAccountName) || Context != null)
161161
{
162162
ProfileClient.GetAccount(subscription.Account);
163+
if (Profile.Context != null && Profile.Context.Subscription != null &&
164+
Profile.Context.Subscription.Id == subscription.Id)
165+
{
166+
GeneralUtilities.ClearCurrentStorageAccount();
167+
}
163168
var context = new AzureContext(subscription, ProfileClient.GetAccount(subscription.Account), ProfileClient.GetEnvironmentOrDefault(subscription.Environment));
164169
if (Context != null)
165170
{

0 commit comments

Comments
 (0)