Skip to content

Commit 390d35b

Browse files
author
Hovsep
committed
Merge pull request Azure#922 from hovsepm/dev
Changed SelectAzureRMContext to SetAzureRMContext.
2 parents 670693b + df405c1 commit 390d35b

File tree

6 files changed

+76
-150
lines changed

6 files changed

+76
-150
lines changed

src/Common/Commands.ResourceManager.Common/RMProfileClient.cs

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -78,55 +78,43 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
7878
return _profile;
7979
}
8080

81-
public AzureContext UpdateCurrentContext(string subscriptionId, string tenantId)
81+
public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
8282
{
83-
AzureSubscription newSubscription = null;
84-
AzureTenant newTenant = null;
85-
AzureAccount account = _profile.Context.Account;
86-
AzureEnvironment envrionment = _profile.Context.Environment;
87-
ShowDialog promptBehavior = ShowDialog.Auto;
88-
89-
if (!string.IsNullOrWhiteSpace(tenantId) &&
90-
!string.IsNullOrWhiteSpace(subscriptionId))
91-
{
92-
if(TryGetTenantSubscription(account, envrionment, tenantId, subscriptionId, null, promptBehavior, out newSubscription, out newTenant))
83+
if (!string.IsNullOrWhiteSpace(tenantId))
84+
{
85+
_profile.Context = new AzureContext(
86+
_profile.Context.Subscription,
87+
_profile.Context.Account,
88+
_profile.Context.Environment,
89+
new AzureTenant() { Id = new Guid(tenantId) });
90+
91+
if (_profile.Context.Account != null)
9392
{
94-
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
93+
_profile.Context.Account.Properties[AzureAccount.Property.Tenants] = tenantId;
9594
}
96-
}
97-
else if (!string.IsNullOrWhiteSpace(tenantId))
98-
{
99-
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
100-
account,
101-
envrionment,
102-
tenantId,
103-
null,
104-
promptBehavior,
105-
TokenCache.DefaultShared);
106-
107-
account.Properties[AzureAccount.Property.Tenants] = accessToken.TenantId;
108-
newTenant = new AzureTenant();
109-
newTenant.Id = new Guid(accessToken.TenantId);
110-
newTenant.Domain = accessToken.GetDomain();
111-
_profile.Context = new AzureContext(account, envrionment, newTenant);
112-
}
113-
else if(!string.IsNullOrWhiteSpace(subscriptionId))
114-
{
115-
foreach (var tenant in ListAccountTenants(account, envrionment, null, promptBehavior))
95+
if (_profile.Context.Subscription != null)
11696
{
117-
if (TryGetTenantSubscription(account, envrionment, tenant.Id.ToString(), subscriptionId, null, promptBehavior, out newSubscription, out newTenant))
118-
{
119-
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
120-
break;
121-
}
97+
_profile.Context.Subscription.Properties[AzureSubscription.Property.Tenants] = tenantId;
12298
}
12399
}
124-
else
100+
101+
if(!string.IsNullOrWhiteSpace(subscriptionId))
125102
{
126-
throw new PSNotSupportedException();
127-
}
103+
var newSubscription = new AzureSubscription { Id = new Guid(subscriptionId) };
104+
if(_profile.Context.Subscription != null)
105+
{
106+
newSubscription.Account = _profile.Context.Subscription.Account;
107+
newSubscription.Environment = _profile.Context.Subscription.Environment;
108+
newSubscription.Properties = _profile.Context.Subscription.Properties;
109+
newSubscription.Name = null;
110+
}
128111

129-
_profile.Context.TokenCache = TokenCache.DefaultShared.Serialize();
112+
_profile.Context = new AzureContext(
113+
newSubscription,
114+
_profile.Context.Account,
115+
_profile.Context.Environment,
116+
_profile.Context.Tenant);
117+
}
130118

131119
return _profile.Context;
132120
}

src/Common/Commands.ResourceManager.Profile.Test/Commands.ResourceManager.Profile.Test.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,12 @@
183183
</ItemGroup>
184184
<ItemGroup>
185185
<Compile Include="AzureRMProfileTests.cs" />
186-
<Compile Include="ContextCmdletTests.Live.cs" />
187186
<Compile Include="MockSubscriptionClientFactory.cs" />
188187
<Compile Include="ProfileController.cs" />
189188
<Compile Include="SubscriptionCmdletTests.cs" />
190189
<Compile Include="TenantCmdletTests.cs" />
191190
<Compile Include="LoginCmdletTests.cs" />
192-
<Compile Include="ContextCmdletTests.Mocked.cs" />
191+
<Compile Include="ContextCmdletTests.cs" />
193192
<Compile Include="ProfileCmdletTests.cs" />
194193
<Compile Include="Properties\AssemblyInfo.cs" />
195194
</ItemGroup>

src/Common/Commands.ResourceManager.Profile.Test/ContextCmdletTests.Mocked.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/Common/Commands.ResourceManager.Profile.Test/ContextCmdletTests.Live.cs renamed to src/Common/Commands.ResourceManager.Profile.Test/ContextCmdletTests.cs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,120 +23,120 @@
2323
using Xunit;
2424
using System;
2525
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
26-
using System.Management.Automation;
2726

2827
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
2928
{
30-
public class ContextCmdletTestsLive
29+
public class ContextCmdletTests : RMTestBase
3130
{
3231
private MemoryDataStore dataStore;
3332
private MockCommandRuntime commandRuntimeMock;
3433

35-
public ContextCmdletTestsLive()
34+
public ContextCmdletTests()
3635
{
3736
dataStore = new MemoryDataStore();
3837
AzureSession.DataStore = dataStore;
3938
commandRuntimeMock = new MockCommandRuntime();
40-
AzureRMCmdlet.DefaultProfile = new AzureRMProfile();
39+
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
4140
}
42-
41+
4342
[Fact]
44-
[Trait(Category.AcceptanceType, Category.LiveOnly)]
45-
public void SelectAzureContextWithSubscriptionAndTenant()
43+
[Trait(Category.AcceptanceType, Category.CheckIn)]
44+
public void GetAzureContext()
4645
{
47-
var cmdlt = new SelectAzureRMContextCommand();
46+
var cmdlt = new GetAzureRMContextCommand();
4847
// Setup
4948
cmdlt.CommandRuntime = commandRuntimeMock;
50-
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
51-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
5249

5350
// Act
54-
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
5551
cmdlt.InvokeBeginProcessing();
5652
cmdlt.ExecuteCmdlet();
5753
cmdlt.InvokeEndProcessing();
5854

5955
// Verify
60-
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
61-
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
62-
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
56+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
57+
var context = (AzureContext) commandRuntimeMock.OutputPipeline[0];
58+
Assert.Equal("test", context.Subscription.Name);
6359
}
64-
60+
6561
[Fact]
66-
[Trait(Category.AcceptanceType, Category.LiveOnly)]
67-
public void SelectAzureContextWithSubscriptionAndNoTenant()
62+
[Trait(Category.AcceptanceType, Category.CheckIn)]
63+
public void SelectAzureContextWithSubscriptionAndTenant()
6864
{
69-
var cmdlt = new SelectAzureRMContextCommand();
65+
var cmdlt = new SetAzureRMContextCommand();
7066
// Setup
7167
cmdlt.CommandRuntime = commandRuntimeMock;
7268
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
69+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
7370

7471
// Act
75-
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
7672
cmdlt.InvokeBeginProcessing();
7773
cmdlt.ExecuteCmdlet();
7874
cmdlt.InvokeEndProcessing();
7975

8076
// Verify
81-
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
82-
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
77+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
78+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
8379
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
8480
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
8581
}
8682

8783
[Fact]
88-
[Trait(Category.AcceptanceType, Category.LiveOnly)]
89-
public void SelectAzureContextWithNoSubscriptionAndTenant()
84+
[Trait(Category.AcceptanceType, Category.CheckIn)]
85+
public void SelectAzureContextWithSubscriptionAndNoTenant()
9086
{
91-
var cmdlt = new SelectAzureRMContextCommand();
87+
var cmdlt = new SetAzureRMContextCommand();
9288
// Setup
9389
cmdlt.CommandRuntime = commandRuntimeMock;
94-
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
90+
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
9591

9692
// Act
97-
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
9893
cmdlt.InvokeBeginProcessing();
9994
cmdlt.ExecuteCmdlet();
10095
cmdlt.InvokeEndProcessing();
10196

10297
// Verify
103-
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
104-
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
105-
Assert.Null(context.Subscription);
106-
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
98+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
99+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
100+
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
107101
}
108102

109103
[Fact]
110-
[Trait(Category.AcceptanceType, Category.LiveOnly)]
111-
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
104+
[Trait(Category.AcceptanceType, Category.CheckIn)]
105+
public void SelectAzureContextWithNoSubscriptionAndTenant()
112106
{
113-
var cmdlt = new SelectAzureRMContextCommand();
107+
var cmdlt = new SetAzureRMContextCommand();
114108
// Setup
115109
cmdlt.CommandRuntime = commandRuntimeMock;
110+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
116111

117112
// Act
118-
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
119113
cmdlt.InvokeBeginProcessing();
120-
121-
// Verify
122-
Assert.Throws<PSNotSupportedException>(() => cmdlt.ExecuteCmdlet());
114+
cmdlt.ExecuteCmdlet();
123115
cmdlt.InvokeEndProcessing();
116+
117+
// Verify
118+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
119+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
120+
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
124121
}
125122

126-
private void Login(string subscriptionId, string tenantId)
123+
[Fact]
124+
[Trait(Category.AcceptanceType, Category.CheckIn)]
125+
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
127126
{
128-
var cmdlt = new LoginAzureRMAccountCommand();
127+
var cmdlt = new SetAzureRMContextCommand();
129128
// Setup
130129
cmdlt.CommandRuntime = commandRuntimeMock;
131-
cmdlt.SubscriptionId = subscriptionId;
132-
cmdlt.Tenant = tenantId;
133130

134131
// Act
135132
cmdlt.InvokeBeginProcessing();
136133
cmdlt.ExecuteCmdlet();
137134
cmdlt.InvokeEndProcessing();
138135

139-
Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
136+
// Verify
137+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
138+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
139+
Assert.NotNull(context);
140140
}
141141
}
142142
}

src/Common/Commands.ResourceManager.Profile/Commands.ResourceManager.Profile.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
<Compile Include="GetAzureRMSubscription.cs" />
136136
<Compile Include="Account\LoginAzureRMAccount.cs" />
137137
<Compile Include="Context\GetAzureRMContext.cs" />
138-
<Compile Include="Context\SelectAzureRMContext.cs" />
138+
<Compile Include="Context\SetAzureRMContext.cs" />
139139
<Compile Include="Profile\SelectAzureRMProfile.cs" />
140140
<Compile Include="Profile\SaveAzureRMProfile.cs" />
141141
<Compile Include="Properties\AssemblyInfo.cs" />

src/Common/Commands.ResourceManager.Profile/Context/SelectAzureRMContext.cs renamed to src/Common/Commands.ResourceManager.Profile/Context/SetAzureRMContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace Microsoft.Azure.Commands.Profile
2121
/// <summary>
2222
/// Cmdlet to change current Azure context.
2323
/// </summary>
24-
[Cmdlet(VerbsCommon.Select, "AzureRMContext")]
24+
[Cmdlet(VerbsCommon.Set, "AzureRMContext")]
2525
[OutputType(typeof(AzureContext))]
26-
public class SelectAzureRMContextCommand : AzureRMCmdlet
26+
public class SetAzureRMContextCommand : AzureRMCmdlet
2727
{
2828
private const string TenantParameterSet = "Tenant";
2929
private const string SubscriptionParameterSet = "Subscription";
@@ -43,7 +43,7 @@ protected override void ProcessRecord()
4343
{
4444
var profileClient = new RMProfileClient(AzureRMCmdlet.DefaultProfile);
4545

46-
AzureRMCmdlet.DefaultProfile.Context = profileClient.UpdateCurrentContext(SubscriptionId, Tenant);
46+
AzureRMCmdlet.DefaultProfile.Context = profileClient.SetCurrentContext(SubscriptionId, Tenant);
4747

4848
WriteObject(AzureRMCmdlet.DefaultProfile.Context);
4949
}

0 commit comments

Comments
 (0)