Skip to content

[#102922544] Added Select-AzureRMContext and Get-AzureRMContext cmdlets #911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Storage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<SpecificVersion>False</SpecificVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="Microsoft.ApplicationInsights" version="1.1.1-beta" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Profile/Commands.Profile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Profile/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<SpecificVersion>False</SpecificVersion>
Expand Down
81 changes: 67 additions & 14 deletions src/Common/Commands.ResourceManager.Common/RMProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,38 @@ public class RMProfileClient
public RMProfileClient(AzureRMProfile profile)
{
_profile = profile;

if (_profile != null && _profile.Context != null &&
_profile.Context.TokenCache != null && _profile.Context.TokenCache.Length > 0)
{
TokenCache.DefaultShared.Deserialize(_profile.Context.TokenCache);
}
}

public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password)
{
AzureSubscription newSubscription = null;
AzureTenant newTenant = new AzureTenant();

if (_profile != null && _profile.Context != null &&
_profile.Context.TokenCache != null && _profile.Context.TokenCache.Length > 0)
{
TokenCache.DefaultShared.Deserialize(_profile.Context.TokenCache);
}
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;

// (tenant and subscription are present) OR
// (tenant is present and subscription is not provided)
if (!string.IsNullOrEmpty(tenantId))
{
newTenant.Id = new Guid(tenantId);
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;
TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription);
if (TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription))
{
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
}
}
// (tenant is not provided and subscription is present) OR
// (tenant is not provided and subscription is not provided)
else
{
foreach(var tenant in ListAccountTenants(account, environment, password))
foreach(var tenant in ListAccountTenants(account, environment, password, promptBehavior))
{
if (TryGetTenantSubscription(account, environment, tenant, subscriptionId, password, ShowDialog.Auto, out newSubscription))
{
newTenant.Id = new Guid(tenant);
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
break;
}
}
Expand All @@ -80,6 +82,59 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
return _profile;
}

public AzureContext UpdateCurrentContext(string subscriptionId, string tenantId)
{
AzureSubscription newSubscription = null;
AzureTenant newTenant = new AzureTenant();
AzureAccount account = _profile.Context.Account;
AzureEnvironment envrionment = _profile.Context.Environment;
ShowDialog promptBehavior = ShowDialog.Auto;

if (!string.IsNullOrWhiteSpace(tenantId) &&
!string.IsNullOrWhiteSpace(subscriptionId))
{
if(TryGetTenantSubscription(account, envrionment, tenantId, subscriptionId, null, promptBehavior, out newSubscription))
{
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
}
}
else if (!string.IsNullOrWhiteSpace(tenantId))
{
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
account,
envrionment,
tenantId,
null,
promptBehavior,
TokenCache.DefaultShared);

account.Properties[AzureAccount.Property.Tenants] = accessToken.TenantId;
newTenant.Id = new Guid(accessToken.TenantId);
_profile.Context = new AzureContext(account, envrionment, newTenant);
}
else if(!string.IsNullOrWhiteSpace(subscriptionId))
{
foreach (var tenant in ListAccountTenants(account, envrionment, null, promptBehavior))
{
if (TryGetTenantSubscription(account, envrionment, tenant, subscriptionId, null, promptBehavior, out newSubscription))
{
newTenant.Id = new Guid(account.Properties[AzureAccount.Property.Tenants]);
_profile.Context = new AzureContext(newSubscription, account, envrionment, newTenant);
break;
}
}
}
else
{
throw new PSNotSupportedException();
}

_profile.Context.TokenCache = TokenCache.DefaultShared.Serialize();

return _profile.Context;
}

private bool TryGetTenantSubscription(
AzureAccount account,
AzureEnvironment environment,
Expand Down Expand Up @@ -148,10 +203,8 @@ private bool TryGetTenantSubscription(
}
}

private string[] ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password)
private string[] ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
{
ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never;

var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(
account,
environment,
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.ResourceManager.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="Microsoft.ApplicationInsights" version="1.1.1-beta" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.2.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.3.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.2.2-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.3.0-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
Expand Down Expand Up @@ -178,7 +178,9 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ContextCmdletTests.Live.cs" />
<Compile Include="LoginCmdletTests.cs" />
<Compile Include="ContextCmdletTests.Mocked.cs" />
<Compile Include="ProfileCmdletTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.Azure.Commands.Profile;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using System.Linq;
using Xunit;
using System;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
{
public class ContextCmdletTestsLive
{
private MemoryDataStore dataStore;
private MockCommandRuntime commandRuntimeMock;

public ContextCmdletTestsLive()
{
dataStore = new MemoryDataStore();
AzureSession.DataStore = dataStore;
commandRuntimeMock = new MockCommandRuntime();
AzureRMCmdlet.DefaultProfile = new AzureRMProfile();
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void SelectAzureContextWithSubscriptionAndTenant()
{
var cmdlt = new SelectAzureRMContextCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Act
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

// Verify
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void SelectAzureContextWithSubscriptionAndNoTenant()
{
var cmdlt = new SelectAzureRMContextCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";

// Act
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

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

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void SelectAzureContextWithNoSubscriptionAndTenant()
{
var cmdlt = new SelectAzureRMContextCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Act
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

// Verify
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
Assert.Null(context.Subscription);
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
{
var cmdlt = new SelectAzureRMContextCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;

// Act
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
cmdlt.InvokeBeginProcessing();

// Verify
Assert.Throws<PSNotSupportedException>(() => cmdlt.ExecuteCmdlet());
cmdlt.InvokeEndProcessing();
}

private void Login(string subscriptionId, string tenantId)
{
var cmdlt = new LoginAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SubscriptionId = subscriptionId;
cmdlt.Tenant = tenantId;

// Act
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
}
}
}
Loading