Skip to content

Commit 1413bf3

Browse files
committed
Merge pull request #210 from markcowl/profile2
Hiding Profile property and CurrentProfile static property
2 parents ee7f125 + aaea7b3 commit 1413bf3

File tree

10 files changed

+92
-51
lines changed

10 files changed

+92
-51
lines changed

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,39 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
2727
public abstract class AzurePSCmdlet : PSCmdlet
2828
{
2929
private readonly RecordingTracingInterceptor _httpTracingInterceptor = new RecordingTracingInterceptor();
30+
protected static AzureProfile _currentProfile = null;
3031

3132
[Parameter(Mandatory = false, HelpMessage = "In-memory profile.")]
3233
public AzureProfile Profile { get; set; }
3334

34-
public static AzureProfile CurrentProfile { get; set; }
35+
/// <summary>
36+
/// Sets the current profile - the profile used when no Profile is explicitly passed in. Should be used only by
37+
/// Profile cmdlets and tests that need to set up a particular profile
38+
/// </summary>
39+
public static AzureProfile CurrentProfile
40+
{
41+
private get
42+
{
43+
if (_currentProfile == null)
44+
{
45+
_currentProfile = InitializeDefaultProfile();
46+
SetTokenCacheForProfile(_currentProfile);
47+
}
48+
49+
return _currentProfile;
50+
}
51+
52+
set
53+
{
54+
SetTokenCacheForProfile(value);
55+
_currentProfile = value;
56+
}
57+
}
3558

3659
protected static TokenCache DefaultDiskTokenCache { get; set; }
3760

3861
protected static TokenCache DefaultMemoryTokenCache { get; set; }
3962

40-
protected static AzureProfile DefaultProfile { get; set; }
41-
4263
static AzurePSCmdlet()
4364
{
4465
if (!TestMockSupport.RunningMocked)
@@ -50,9 +71,7 @@ static AzurePSCmdlet()
5071
if (!TestMockSupport.RunningMocked)
5172
{
5273
InitializeTokenCaches();
53-
DefaultProfile = InitializeDefaultProfile();
54-
CurrentProfile = DefaultProfile;
55-
UpdateSessionStateForProfile(CurrentProfile);
74+
SetTokenCacheForProfile(CurrentProfile);
5675
AzureSession.DataStore = new DiskDataStore();
5776
}
5877
}
@@ -96,7 +115,7 @@ protected static void InitializeTokenCaches()
96115
/// Update the token cache when setting the profile
97116
/// </summary>
98117
/// <param name="profile"></param>
99-
protected static void UpdateSessionStateForProfile(AzureProfile profile)
118+
protected static void SetTokenCacheForProfile(AzureProfile profile)
100119
{
101120
var defaultProfilePath = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
102121
if (string.Equals(profile.ProfilePath, defaultProfilePath, StringComparison.OrdinalIgnoreCase))
@@ -124,7 +143,7 @@ protected override void BeginProcessing()
124143
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
125144
}
126145

127-
if (Profile.Context != null && Profile.Context.Account != null && Profile.Context.Account.Id != null)
146+
if (Profile != null && Profile.Context != null && Profile.Context.Account != null && Profile.Context.Account.Id != null)
128147
{
129148
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", Profile.Context.Account.Id));
130149
}
@@ -137,14 +156,14 @@ protected override void BeginProcessing()
137156
/// <summary>
138157
/// Ensure that there is a profile for the command
139158
/// </summary>
140-
private void InitializeProfile()
159+
protected virtual void InitializeProfile()
141160
{
142161
if (Profile == null)
143162
{
144163
Profile = AzurePSCmdlet.CurrentProfile;
145164
}
146165

147-
UpdateSessionStateForProfile(Profile);
166+
SetTokenCacheForProfile(Profile);
148167
}
149168

150169
/// <summary>

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
@@ -1370,4 +1370,7 @@ use and privacy statement at &lt;url&gt; and (c) agree to sharing my contact inf
13701370
<data name="MissingSubscriptionInProfileProperties" xml:space="preserve">
13711371
<value>Property bag Hashtable must contain a 'SubscriptionId'.</value>
13721372
</data>
1373+
<data name="AzureProfileMustNotBeNull" xml:space="preserve">
1374+
<value>Selected profile must not be null.</value>
1375+
</data>
13731376
</root>

src/Common/Commands.Profile/Profile/NewAzureProfile.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class NewAzureProfileCommand : AzurePSCmdlet
5252
internal const string EnvironmentKey = "Environment";
5353
internal const string StorageAccountKey = "StorageAccount";
5454

55-
5655
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = CertificateParameterSet)]
5756
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = ServicePrincipalParameterSet)]
5857
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = AccessTokenParameterSet)]
@@ -97,6 +96,9 @@ public class NewAzureProfileCommand : AzurePSCmdlet
9796
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = PropertyBagParameterSet)]
9897
public Hashtable Properties { get; set; }
9998

99+
// do not use the Profile parameter for this cmdlet
100+
private new AzureProfile Profile { get; set; }
101+
100102
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
101103
public override void ExecuteCmdlet()
102104
{
@@ -127,6 +129,11 @@ public override void ExecuteCmdlet()
127129
WriteObject(azureProfile);
128130
}
129131

132+
protected override void InitializeProfile()
133+
{
134+
// do not initialize the current profile for this cmdlet
135+
}
136+
130137
private void InitializeAzureProfile(AzureProfile profile, string parameterSet, AzureProfileSettings settings)
131138
{
132139
var profileClient = new ProfileClient(profile);

src/Common/Commands.Profile/Profile/SelectAzureProfile.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace Microsoft.WindowsAzure.Commands.Profile
3333
/// Creates new Microsoft Azure profile.
3434
/// </summary>
3535
[Cmdlet(VerbsCommon.Select, "AzureProfile"), OutputType(typeof(AzureProfile))]
36-
public class SelectAzureProfileCommand : SubscriptionCmdletBase
36+
public class SelectAzureProfileCommand : AzurePSCmdlet
3737
{
3838
internal const string NewProfileParameterSet = "NewProfile";
3939
internal const string DefaultProfilePrameterSet = "DefaultProfile";
@@ -44,25 +44,25 @@ public class SelectAzureProfileCommand : SubscriptionCmdletBase
4444
[Parameter(ParameterSetName=DefaultProfilePrameterSet, Mandatory=true)]
4545
public SwitchParameter Default { get; set; }
4646

47-
public SelectAzureProfileCommand() : base(true)
47+
protected override void InitializeProfile()
4848
{
49+
// do not initialize the profile for this cmdlet
4950
}
5051

5152
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
5253
public override void ExecuteCmdlet()
5354
{
5455
if (ParameterSetName == DefaultProfilePrameterSet)
5556
{
56-
Profile = AzurePSCmdlet.DefaultProfile;
57+
Profile = AzurePSCmdlet.InitializeDefaultProfile();
5758
}
5859

5960
if (Profile == null)
6061
{
61-
throw new ArgumentException("Selected profile must not be null.");
62+
throw new ArgumentException(Resources.AzureProfileMustNotBeNull);
6263
}
6364

6465
AzurePSCmdlet.CurrentProfile = Profile;
65-
AzurePSCmdlet.UpdateSessionStateForProfile(AzurePSCmdlet.CurrentProfile);
6666
WriteObject(Profile);
6767
}
6868
}

src/Common/Commands.ScenarioTests.Common/EnvironmentSetupHelper.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public class EnvironmentSetupHelper
4242
public EnvironmentSetupHelper()
4343
{
4444
AzureSession.DataStore = new MockDataStore();
45-
AzurePSCmdlet.CurrentProfile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
46-
ProfileClient = new ProfileClient(AzurePSCmdlet.CurrentProfile);
45+
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
46+
AzurePSCmdlet.CurrentProfile = profile;
47+
ProfileClient = new ProfileClient(profile);
4748

4849
// Ignore SSL errors
4950
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => true;
@@ -104,12 +105,12 @@ private void SetupAzureEnvironmentFromEnvironmentVariables(AzureModule mode)
104105

105106
if (csmEnvironment != null)
106107
{
107-
environment.Endpoints[AzureEnvironment.Endpoint.ResourceManager] = csmEnvironment.BaseUri.AbsoluteUri;
108+
environment.Endpoints[AzureEnvironment.Endpoint.ResourceManager] = csmEnvironment.BaseUri.AbsoluteUri;
108109
}
109110

110111
if (rdfeEnvironment != null)
111112
{
112-
environment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = rdfeEnvironment.BaseUri.AbsoluteUri;
113+
environment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = rdfeEnvironment.BaseUri.AbsoluteUri;
113114
}
114115

115116
if (!ProfileClient.Profile.Environments.ContainsKey(testEnvironmentName))

src/ServiceManagement/Services/Commands.Test/Profile/ProfileCmdltsTests.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,9 @@ public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
206206
public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnAdd()
207207
{
208208
// Setup
209-
var profile = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
210-
AzurePSCmdlet.CurrentProfile =
211-
new AzureProfile(profile);
212-
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
209+
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
210+
AzurePSCmdlet.CurrentProfile = profile;
211+
ProfileClient client = new ProfileClient(profile);
213212
client.AddOrSetEnvironment(azureEnvironment);
214213
client.Profile.Save();
215214
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
@@ -260,10 +259,9 @@ public void SetAzureSubscriptionThrowsExceptionWithoutCertificateOnAdd()
260259
public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet()
261260
{
262261
// Setup
263-
var profile = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
264-
AzurePSCmdlet.CurrentProfile =
265-
new AzureProfile(profile);
266-
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
262+
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
263+
AzurePSCmdlet.CurrentProfile = profile;
264+
ProfileClient client = new ProfileClient(profile);
267265
client.AddOrSetAccount(azureAccount);
268266
client.AddOrSetEnvironment(azureEnvironment);
269267
client.AddOrSetSubscription(azureSubscription1);
@@ -291,9 +289,9 @@ public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet(
291289
public void SetAzureSubscriptionDerivesEnvironmentFromServiceEndpointParameterOnSet()
292290
{
293291
// Setup
294-
AzurePSCmdlet.CurrentProfile =
295-
new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
296-
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
292+
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
293+
AzurePSCmdlet.CurrentProfile = profile;
294+
ProfileClient client = new ProfileClient(profile);
297295
client.AddOrSetAccount(azureAccount);
298296
client.AddOrSetEnvironment(azureEnvironment);
299297
client.AddOrSetSubscription(azureSubscription1);
@@ -384,9 +382,9 @@ public void SetAzureSubscriptionDerivesEnvironmentFromBothEndpointParameters()
384382
public void SetAzureSubscriptionUpdatesSubscriptionWithCertificate()
385383
{
386384
// Setup
387-
AzurePSCmdlet.CurrentProfile =
388-
new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
389-
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
385+
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
386+
AzurePSCmdlet.CurrentProfile = profile;
387+
ProfileClient client = new ProfileClient(profile);
390388
client.AddOrSetAccount(azureAccount);
391389
client.AddOrSetEnvironment(azureEnvironment);
392390
client.AddOrSetSubscription(azureSubscription1);
@@ -517,6 +515,8 @@ public void ImportPublishSettingsWorksForCustomProfile()
517515
// Setup
518516
AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
519517
Commands.Common.Test.Properties.Resources.ValidProfileChina);
518+
var oldProfile = new AzureProfile();
519+
AzurePSCmdlet.CurrentProfile = oldProfile;
520520
var profile = new AzureProfile();
521521
ProfileClient client = new ProfileClient(profile);
522522
var oldDataStore = FileUtilities.DataStore;
@@ -543,8 +543,8 @@ public void ImportPublishSettingsWorksForCustomProfile()
543543
Assert.Equal(client.GetSubscription(subscription.Id).Environment, expectedEnv);
544544
}
545545
Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
546-
Assert.Equal(AzurePSCmdlet.CurrentProfile.Subscriptions.Count, 0);
547-
Assert.Equal(AzurePSCmdlet.CurrentProfile.Accounts.Count, 0);
546+
Assert.Equal(oldProfile.Subscriptions.Count, 0);
547+
Assert.Equal(oldProfile.Accounts.Count, 0);
548548
}
549549
finally
550550
{
@@ -603,7 +603,7 @@ public void SelectAzureSubscriptionByNameUpdatesProfile()
603603
[Fact]
604604
public void SelectAzureSubscriptionByNameUpdatesCustomProfile()
605605
{
606-
SetupDefaultProfile();
606+
var client = SetupDefaultProfile();
607607
var profile = SetupCustomProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
608608
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
609609

@@ -622,7 +622,7 @@ public void SelectAzureSubscriptionByNameUpdatesCustomProfile()
622622
Assert.NotNull(cmdlt.Profile.Context.Subscription);
623623
Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
624624
// current profile unchanged
625-
Assert.Equal(azureSubscription1.Id, AzurePSCmdlet.CurrentProfile.Context.Subscription.Id);
625+
Assert.Equal(azureSubscription1.Id, client.Profile.Context.Subscription.Id);
626626
}
627627

628628
[Fact]
@@ -943,12 +943,12 @@ public void CanCreateProfileFromHashWithTokenAuth()
943943
[Fact]
944944
public void CanCreateAzureProfileWithFile()
945945
{
946-
var savedProfile = AzurePSCmdlet.CurrentProfile;
947946
var dataStore = AzureSession.DataStore;
948947
try
949948
{
950949
AzureSession.DataStore = new MockDataStore();
951-
AzurePSCmdlet.CurrentProfile = new AzureProfile();
950+
var oldProfile = new AzureProfile();
951+
AzurePSCmdlet.CurrentProfile = oldProfile;
952952
string myFile = Path.GetTempFileName();
953953
var profile = SetupCustomProfile(myFile);
954954
var cmdlet = new NewAzureProfileCommand();
@@ -983,13 +983,12 @@ public void CanCreateAzureProfileWithFile()
983983
}
984984

985985
// current profile does not change
986-
Assert.Equal(AzurePSCmdlet.CurrentProfile.Accounts.Count, 0);
987-
Assert.Equal(AzurePSCmdlet.CurrentProfile.Subscriptions.Count, 0);
986+
Assert.Equal(oldProfile.Accounts.Count, 0);
987+
Assert.Equal(oldProfile.Subscriptions.Count, 0);
988988
}
989989
finally
990990
{
991991
AzureSession.DataStore = dataStore;
992-
AzurePSCmdlet.CurrentProfile = savedProfile;
993992
}
994993
}
995994

@@ -1100,9 +1099,9 @@ private void ValidateCredential(PSCredential credential, AzureProfile profile,
11001099

11011100
private ProfileClient SetupDefaultProfile()
11021101
{
1103-
AzurePSCmdlet.CurrentProfile =
1104-
new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
1105-
ProfileClient client = new ProfileClient(AzurePSCmdlet.CurrentProfile);
1102+
var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
1103+
AzurePSCmdlet.CurrentProfile = profile;
1104+
ProfileClient client = new ProfileClient(profile);
11061105
client.AddOrSetEnvironment(azureEnvironment);
11071106
client.AddOrSetAccount(azureAccount);
11081107
client.AddOrSetSubscription(azureSubscription1);

src/ServiceManagement/Services/Commands.Test/Profile/ProfileTestController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Azure.Common.Authentication.Models;
1818
using Microsoft.VisualStudio.TestTools.UnitTesting;
1919
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
20+
using Microsoft.WindowsAzure.Commands.Profile;
2021
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2122
using Microsoft.Azure.Test;
2223
using System;
@@ -99,7 +100,10 @@ public void RunPSTestWithToken(Func<AzureContext, string, string> testBuilder ,
99100
() =>
100101
{
101102
savedAuthFactory = AzureSession.AuthenticationFactory;
102-
var profile = AzurePSCmdlet.CurrentProfile;
103+
var command = new GetAzureSubscriptionCommand();
104+
command.CommandRuntime = new MockCommandRuntime();
105+
command.InvokeBeginProcessing();
106+
var profile = command.Profile;
103107
var context = profile.Context;
104108
var account = context.Account;
105109
var tenant = account.IsPropertySet(AzureAccount.Property.Tenants)

src/ServiceManagement/Services/Commands.Test/Profile/SelectAzureProfileTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void TestMakeArmCallWithCreatedProfile()
3737
[Trait(Category.AcceptanceType, Category.CheckIn)]
3838
public void TestMakeRdfeCallWithCreatedProfile()
3939
{
40-
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInARMMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
40+
ProfileTestController.NewRdfeInstance.RunPSTestWithToken((context, token) => string.Format("Test-NewAzureProfileInRDFEMode {0} {1} {2}", token, context.Account.Id, context.Subscription.Id));
4141
}
4242
}
4343
}

src/ServiceManagement/Services/Commands.Test/Profile/SelectAzureProfileTests.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ function Test-SelectDefaultProfile
2222
$profile = $(New-AzureProfile -SubscriptionId $sub -AccessToken $token -AccountId $user)
2323
Assert-AreEqual "AzureCloud" $profile.Context.Environment.Name
2424
Select-AzureProfile $profile
25-
Select-AzureProfile -Default
26-
$current = [Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet]::CurrentProfile
27-
Assert-NotNull $($current.ProfilePath)
25+
$profile2 = Select-AzureProfile -Default
26+
Assert-NotNull $($profile2.ProfilePath)
2827
}
2928

3029
<#

0 commit comments

Comments
 (0)