Skip to content

Commit 2f2a24e

Browse files
author
Hovsep Mkrtchyan
committed
Added SelectAzureRMContextCommand.
1 parent b4b5ecb commit 2f2a24e

File tree

7 files changed

+156
-23
lines changed

7 files changed

+156
-23
lines changed

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

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,38 @@ public class RMProfileClient
3434
public RMProfileClient(AzureRMProfile profile)
3535
{
3636
_profile = profile;
37+
38+
if (_profile != null && _profile.Context != null &&
39+
_profile.Context.TokenCache != null && _profile.Context.TokenCache.Length > 0)
40+
{
41+
TokenCache.DefaultShared.Deserialize(_profile.Context.TokenCache);
42+
}
3743
}
3844

3945
public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password)
4046
{
4147
AzureSubscription newSubscription = null;
4248
AzureTenant newTenant = new AzureTenant();
43-
44-
if (_profile != null && _profile.Context != null &&
45-
_profile.Context.TokenCache != null && _profile.Context.TokenCache.Length > 0)
46-
{
47-
TokenCache.DefaultShared.Deserialize(_profile.Context.TokenCache);
48-
}
49+
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
4950

5051
// (tenant and subscription are present) OR
5152
// (tenant is present and subscription is not provided)
5253
if (!string.IsNullOrEmpty(tenantId))
5354
{
54-
newTenant.Id = new Guid(tenantId);
55-
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
56-
TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription);
55+
if (TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription))
56+
{
57+
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
58+
}
5759
}
5860
// (tenant is not provided and subscription is present) OR
5961
// (tenant is not provided and subscription is not provided)
6062
else
6163
{
62-
foreach(var tenant in ListAccountTenants(account, environment, password))
64+
foreach(var tenant in ListAccountTenants(account, environment, password, promptBehavior))
6365
{
6466
if (TryGetTenantSubscription(account, environment, tenant, subscriptionId, password, ShowDialog.Auto, out newSubscription))
6567
{
66-
newTenant.Id = new Guid(tenant);
68+
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
6769
break;
6870
}
6971
}
@@ -80,6 +82,59 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
8082
return _profile;
8183
}
8284

85+
public AzureContext UpdateCurrentContext(string subscriptionId, string tenantId)
86+
{
87+
AzureSubscription newSubscription = null;
88+
AzureTenant newTenant = new AzureTenant();
89+
AzureAccount account = _profile.Context.Account;
90+
AzureEnvironment envrionment = _profile.Context.Environment;
91+
ShowDialog promptBehavior = ShowDialog.Auto;
92+
93+
if (!string.IsNullOrWhiteSpace(tenantId) &&
94+
!string.IsNullOrWhiteSpace(subscriptionId))
95+
{
96+
if(TryGetTenantSubscription(account, envrionment, tenantId, subscriptionId, null, promptBehavior, out newSubscription))
97+
{
98+
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
99+
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
100+
}
101+
}
102+
else if (!string.IsNullOrWhiteSpace(tenantId))
103+
{
104+
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
105+
account,
106+
envrionment,
107+
tenantId,
108+
null,
109+
promptBehavior,
110+
TokenCache.DefaultShared);
111+
112+
account.Properties[AzureAccount.Property.Tenants] = accessToken.TenantId;
113+
newTenant.Id = new Guid(accessToken.TenantId);
114+
_profile.Context = new AzureContext(account, envrionment, newTenant);
115+
}
116+
else if(!string.IsNullOrWhiteSpace(subscriptionId))
117+
{
118+
foreach (var tenant in ListAccountTenants(account, envrionment, null, promptBehavior))
119+
{
120+
if (TryGetTenantSubscription(account, envrionment, tenant, subscriptionId, null, promptBehavior, out newSubscription))
121+
{
122+
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
123+
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
124+
break;
125+
}
126+
}
127+
}
128+
else
129+
{
130+
throw new PSNotSupportedException();
131+
}
132+
133+
_profile.Context.TokenCache = TokenCache.DefaultShared.Serialize();
134+
135+
return _profile.Context;
136+
}
137+
83138
private bool TryGetTenantSubscription(
84139
AzureAccount account,
85140
AzureEnvironment environment,
@@ -148,10 +203,8 @@ private bool TryGetTenantSubscription(
148203
}
149204
}
150205

151-
private string[] ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password)
206+
private string[] ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
152207
{
153-
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
154-
155208
var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(
156209
account,
157210
environment,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
</ItemGroup>
180180
<ItemGroup>
181181
<Compile Include="LoginCmdletTests.cs" />
182+
<Compile Include="ContextCmdletTests.cs" />
182183
<Compile Include="ProfileCmdletTests.cs" />
183184
<Compile Include="Properties\AssemblyInfo.cs" />
184185
</ItemGroup>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 Microsoft.WindowsAzure.Commands.Utilities.Common;
16+
using Microsoft.Azure.Commands.Profile;
17+
using Microsoft.Azure.Commands.ResourceManager.Common;
18+
using Microsoft.Azure.Common.Authentication;
19+
using Microsoft.Azure.Common.Authentication.Models;
20+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using System.Linq;
23+
using Xunit;
24+
using System;
25+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
26+
27+
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
28+
{
29+
public class ContextCmdletTests : RMTestBase
30+
{
31+
private MemoryDataStore dataStore;
32+
private MockCommandRuntime commandRuntimeMock;
33+
34+
public ContextCmdletTests()
35+
{
36+
dataStore = new MemoryDataStore();
37+
AzureSession.DataStore = dataStore;
38+
commandRuntimeMock = new MockCommandRuntime();
39+
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
40+
}
41+
42+
[Fact]
43+
[Trait(Category.AcceptanceType, Category.CheckIn)]
44+
public void GetAzureContextInMemory()
45+
{
46+
var cmdlt = new GetAzureRMContextCommand();
47+
// Setup
48+
cmdlt.CommandRuntime = commandRuntimeMock;
49+
50+
// Act
51+
cmdlt.InvokeBeginProcessing();
52+
cmdlt.ExecuteCmdlet();
53+
cmdlt.InvokeEndProcessing();
54+
55+
// Verify
56+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
57+
var context = (AzureContext) commandRuntimeMock.OutputPipeline[0];
58+
Assert.Equal("test", context.Subscription.Name);
59+
}
60+
}
61+
}

src/Common/Commands.ResourceManager.Profile.Test/LoginCmdletTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public LoginCmdletTests()
4545
[Trait(Category.AcceptanceType, Category.LiveOnly)]
4646
public void LoginWithSubscriptionAndTenant()
4747
{
48-
var cmdlt = new LoginAzureRMAccount();
48+
var cmdlt = new LoginAzureRMAccountCommand();
4949
// Setup
5050
cmdlt.CommandRuntime = commandRuntimeMock;
5151
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
@@ -63,7 +63,7 @@ public void LoginWithSubscriptionAndTenant()
6363
[Trait(Category.AcceptanceType, Category.LiveOnly)]
6464
public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
6565
{
66-
var cmdlt = new LoginAzureRMAccount();
66+
var cmdlt = new LoginAzureRMAccountCommand();
6767
// Setup
6868
cmdlt.CommandRuntime = commandRuntimeMock;
6969
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5";
@@ -79,7 +79,7 @@ public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
7979
[Trait(Category.AcceptanceType, Category.LiveOnly)]
8080
public void LoginWithSubscriptionAndNoTenant()
8181
{
82-
var cmdlt = new LoginAzureRMAccount();
82+
var cmdlt = new LoginAzureRMAccountCommand();
8383
// Setup
8484
cmdlt.CommandRuntime = commandRuntimeMock;
8585
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
@@ -96,7 +96,7 @@ public void LoginWithSubscriptionAndNoTenant()
9696
[Trait(Category.AcceptanceType, Category.LiveOnly)]
9797
public void LoginWithNoSubscriptionAndNoTenant()
9898
{
99-
var cmdlt = new LoginAzureRMAccount();
99+
var cmdlt = new LoginAzureRMAccountCommand();
100100
// Setup
101101
cmdlt.CommandRuntime = commandRuntimeMock;
102102

@@ -112,7 +112,7 @@ public void LoginWithNoSubscriptionAndNoTenant()
112112
[Trait(Category.AcceptanceType, Category.LiveOnly)]
113113
public void LoginWithNoSubscriptionAndTenant()
114114
{
115-
var cmdlt = new LoginAzureRMAccount();
115+
var cmdlt = new LoginAzureRMAccountCommand();
116116
// Setup
117117
cmdlt.CommandRuntime = commandRuntimeMock;
118118
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";

src/Common/Commands.ResourceManager.Profile/Account/LoginAzureRMAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Microsoft.Azure.Commands.Profile
2626
/// </summary>
2727
[Cmdlet("Login", "AzureRMAccount", DefaultParameterSetName = "User")]
2828
[OutputType(typeof(AzureRMProfile))]
29-
public class LoginAzureRMAccount : AzureRMCmdlet
29+
public class LoginAzureRMAccountCommand : AzureRMCmdlet
3030
{
3131
[Parameter(Mandatory = false, HelpMessage = "Environment containing the account to log into")]
3232
[ValidateNotNullOrEmpty]

src/Common/Commands.ResourceManager.Profile/Context/GetAzureRMContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Microsoft.Azure.Commands.Profile
2323
/// </summary>
2424
[Cmdlet(VerbsCommon.Get, "AzureRMContext")]
2525
[OutputType(typeof(AzureContext))]
26-
public class GetAzureRMContext : AzureRMCmdlet
26+
public class GetAzureRMContextCommand : AzureRMCmdlet
2727
{
2828
protected override void ProcessRecord()
2929
{

src/Common/Commands.ResourceManager.Profile/Context/SelectAzureRMContext.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,29 @@ namespace Microsoft.Azure.Commands.Profile
2323
/// </summary>
2424
[Cmdlet(VerbsCommon.Select, "AzureRMContext")]
2525
[OutputType(typeof(AzureContext))]
26-
public class SelectAzureRMContext : AzureRMCmdlet
26+
public class SelectAzureRMContextCommand : AzureRMCmdlet
2727
{
28+
private const string TenantParameterSet = "Tenant";
29+
private const string SubscriptionParameterSet = "Subscription";
30+
private const string TenantAndSubscriptionParameterSet = "TenantAndSubscription";
31+
32+
[Parameter(ParameterSetName = TenantParameterSet, Mandatory = true, HelpMessage = "Tenant name or ID")]
33+
[Parameter(ParameterSetName = TenantAndSubscriptionParameterSet, Mandatory = true, HelpMessage = "Tenant name or ID")]
34+
[ValidateNotNullOrEmpty]
35+
public string Tenant { get; set; }
36+
37+
[Parameter(ParameterSetName = SubscriptionParameterSet, Mandatory = true, HelpMessage = "Subscription")]
38+
[Parameter(ParameterSetName = TenantAndSubscriptionParameterSet, Mandatory = true, HelpMessage = "Subscription")]
39+
[ValidateNotNullOrEmpty]
40+
public string SubscriptionId { get; set; }
41+
2842
protected override void ProcessRecord()
2943
{
30-
throw new PSNotImplementedException();
44+
var profileClient = new RMProfileClient(AzureRMCmdlet.DefaultProfile);
45+
46+
AzureRMCmdlet.DefaultProfile.Context = profileClient.UpdateCurrentContext(SubscriptionId, Tenant);
47+
48+
WriteObject(AzureRMCmdlet.DefaultProfile.Context);
3149
}
3250
}
3351
}

0 commit comments

Comments
 (0)