Skip to content

Commit cc8f3d0

Browse files
committed
Merge pull request #911 from hovsepm/dev
[#102922544] Added Select-AzureRMContext and Get-AzureRMContext cmdlets
2 parents aad156c + d844b33 commit cc8f3d0

File tree

176 files changed

+534
-196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+534
-196
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</Reference>
5959
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6060
<SpecificVersion>False</SpecificVersion>
61-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
61+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>

src/Common/Commands.Common.Storage/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
5-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
5+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
88
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</Reference>
5959
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6060
<SpecificVersion>False</SpecificVersion>
61-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
61+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Common.NetFramework">
6464
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>

src/Common/Commands.Common.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
5-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
5+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
88
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
</Reference>
6666
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6767
<SpecificVersion>False</SpecificVersion>
68-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
68+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6969
</Reference>
7070
<Reference Include="Microsoft.Azure.Common.NetFramework">
7171
<SpecificVersion>False</SpecificVersion>

src/Common/Commands.Common/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="Microsoft.ApplicationInsights" version="1.1.1-beta" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
7-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
7+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
1010
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.dll</HintPath>
5555
</Reference>
5656
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
57+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
5858
<Private>True</Private>
5959
</Reference>
6060
<Reference Include="Microsoft.Azure.Common.NetFramework">

src/Common/Commands.Profile/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<packages>
33
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
5-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
5+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
88
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>
65-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
65+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6666
</Reference>
6767
<Reference Include="Microsoft.Azure.Common.NetFramework">
6868
<SpecificVersion>False</SpecificVersion>

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.Common/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="Microsoft.ApplicationInsights" version="1.1.1-beta" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
66
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
7-
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
7+
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
1010
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
</Reference>
6060
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6161
<SpecificVersion>False</SpecificVersion>
62-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
62+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6363
</Reference>
6464
<Reference Include="Microsoft.Azure.Common.NetFramework">
6565
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
@@ -178,7 +178,9 @@
178178
</Reference>
179179
</ItemGroup>
180180
<ItemGroup>
181+
<Compile Include="ContextCmdletTests.Live.cs" />
181182
<Compile Include="LoginCmdletTests.cs" />
183+
<Compile Include="ContextCmdletTests.Mocked.cs" />
182184
<Compile Include="ProfileCmdletTests.cs" />
183185
<Compile Include="Properties\AssemblyInfo.cs" />
184186
</ItemGroup>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
using System.Management.Automation;
27+
28+
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
29+
{
30+
public class ContextCmdletTestsLive
31+
{
32+
private MemoryDataStore dataStore;
33+
private MockCommandRuntime commandRuntimeMock;
34+
35+
public ContextCmdletTestsLive()
36+
{
37+
dataStore = new MemoryDataStore();
38+
AzureSession.DataStore = dataStore;
39+
commandRuntimeMock = new MockCommandRuntime();
40+
AzureRMCmdlet.DefaultProfile = new AzureRMProfile();
41+
}
42+
43+
[Fact]
44+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
45+
public void SelectAzureContextWithSubscriptionAndTenant()
46+
{
47+
var cmdlt = new SelectAzureRMContextCommand();
48+
// Setup
49+
cmdlt.CommandRuntime = commandRuntimeMock;
50+
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
51+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
52+
53+
// Act
54+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
55+
cmdlt.InvokeBeginProcessing();
56+
cmdlt.ExecuteCmdlet();
57+
cmdlt.InvokeEndProcessing();
58+
59+
// 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());
63+
}
64+
65+
[Fact]
66+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
67+
public void SelectAzureContextWithSubscriptionAndNoTenant()
68+
{
69+
var cmdlt = new SelectAzureRMContextCommand();
70+
// Setup
71+
cmdlt.CommandRuntime = commandRuntimeMock;
72+
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
73+
74+
// Act
75+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
76+
cmdlt.InvokeBeginProcessing();
77+
cmdlt.ExecuteCmdlet();
78+
cmdlt.InvokeEndProcessing();
79+
80+
// Verify
81+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
82+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
83+
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
84+
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
85+
}
86+
87+
[Fact]
88+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
89+
public void SelectAzureContextWithNoSubscriptionAndTenant()
90+
{
91+
var cmdlt = new SelectAzureRMContextCommand();
92+
// Setup
93+
cmdlt.CommandRuntime = commandRuntimeMock;
94+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
95+
96+
// Act
97+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
98+
cmdlt.InvokeBeginProcessing();
99+
cmdlt.ExecuteCmdlet();
100+
cmdlt.InvokeEndProcessing();
101+
102+
// 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());
107+
}
108+
109+
[Fact]
110+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
111+
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
112+
{
113+
var cmdlt = new SelectAzureRMContextCommand();
114+
// Setup
115+
cmdlt.CommandRuntime = commandRuntimeMock;
116+
117+
// Act
118+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
119+
cmdlt.InvokeBeginProcessing();
120+
121+
// Verify
122+
Assert.Throws<PSNotSupportedException>(() => cmdlt.ExecuteCmdlet());
123+
cmdlt.InvokeEndProcessing();
124+
}
125+
126+
private void Login(string subscriptionId, string tenantId)
127+
{
128+
var cmdlt = new LoginAzureRMAccountCommand();
129+
// Setup
130+
cmdlt.CommandRuntime = commandRuntimeMock;
131+
cmdlt.SubscriptionId = subscriptionId;
132+
cmdlt.Tenant = tenantId;
133+
134+
// Act
135+
cmdlt.InvokeBeginProcessing();
136+
cmdlt.ExecuteCmdlet();
137+
cmdlt.InvokeEndProcessing();
138+
139+
Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)