Skip to content

. #170

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 9 commits into from
Oct 23, 2015
Merged

. #170

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 @@ -24,6 +24,7 @@
using System;
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public void GetAzureContext()
var context = (PSAzureContext) commandRuntimeMock.OutputPipeline[0];
Assert.Equal("test", context.Subscription.SubscriptionName);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SelectAzureContextWithNoSubscriptionAndTenant()
Expand All @@ -76,7 +77,8 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
// Verify
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
// TenantId is not sufficient to change the context.
Assert.NotEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void LoginWithSubscriptionAndTenant()
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Act
cmdlt.InvokeBeginProcessing();
Expand All @@ -69,7 +69,7 @@ public void LoginWithInvalidSubscriptionAndTenantThrowsCloudException()
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a5";
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Act
cmdlt.InvokeBeginProcessing();
Expand Down Expand Up @@ -119,7 +119,7 @@ public void LoginWithNoSubscriptionAndTenant()
var cmdlt = new AddAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Act
cmdlt.InvokeBeginProcessing();
Expand All @@ -137,7 +137,7 @@ public void LoginWithSubscriptionname()
var cmdlt = new AddAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.SubscriptionName = "Node CLI Test";

// Act
Expand All @@ -156,7 +156,7 @@ public void LoginWithBothSubscriptionIdAndNameThrowsCloudException()
var cmdlt = new AddAzureRMAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.SubscriptionName = "Node CLI Test";
cmdlt.SubscriptionId = "2c224e7e-3ef5-431d-a57b-e71f4662e3a6";

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function Test-PipingWithContext
$name = $firstSubscription.SubscriptionName
$nameContext = Get-AzureRmSubscription -SubscriptionName $name | Set-AzureRmContext
$idContext = Get-AzureRmSubscription -SubscriptionId $id | Set-AzureRmContext
$contextByName = Set-AzureRmContext -SubscriptionName $name
Assert-True { $nameContext -ne $null }
Assert-True { $nameContext.Subscription -ne $null }
Assert-True { $nameContext.Subscription.SubscriptionId -ne $null }
Expand All @@ -62,6 +63,11 @@ function Test-PipingWithContext
Assert-True { $idContext.Subscription.SubscriptionName -ne $null }
Assert-AreEqual $idContext.Subscription.SubscriptionId $nameContext.Subscription.SubscriptionId
Assert-AreEqual $idContext.Subscription.SubscriptionName $nameContext.Subscription.SubscriptionName
Assert-True { $contextByName -ne $null }
Assert-True { $contextByName.Subscription -ne $null }
Assert-True { $contextByName.Subscription.SubscriptionId -ne $null }
Assert-True { $contextByName.Subscription.SubscriptionName -ne $null }
Assert-AreEqual $contextByName.Subscription.SubscriptionName $nameContext.Subscription.SubscriptionName
}

function Test-SetAzureRmContextEndToEnd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void GetTenantWithTenantParameter()
var cmdlt = new GetAzureRMTenantCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";

// Act
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
Expand All @@ -69,7 +69,7 @@ public void GetTenantWithDomainParameter()
var cmdlt = new GetAzureRMTenantCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Tenant = "microsoft.com";
cmdlt.TenantId = "microsoft.com";

// Act
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
Expand Down Expand Up @@ -107,7 +107,7 @@ private void Login(string subscriptionId, string tenantId)
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SubscriptionId = subscriptionId;
cmdlt.Tenant = tenantId;
cmdlt.TenantId = tenantId;

// Act
cmdlt.InvokeBeginProcessing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public class AddAzureRMAccountCommand : AzureRMCmdlet , IModuleAssemblyInitializ
[Parameter(ParameterSetName = "User", Mandatory = false, HelpMessage = "Optional tenant name or ID")]
[Parameter(ParameterSetName = "ServicePrincipal", Mandatory = true, HelpMessage = "TenantId name or ID")]
[Parameter(ParameterSetName = "AccessToken", Mandatory = false, HelpMessage = "TenantId name or ID")]
[Alias("Tenant")]
[ValidateNotNullOrEmpty]
public string Tenant { get; set; }
public string TenantId { get; set; }

[Parameter(ParameterSetName = "AccessToken", Mandatory = true, HelpMessage = "AccessToken")]
[ValidateNotNullOrEmpty]
Expand Down Expand Up @@ -126,9 +127,9 @@ protected override void ProcessRecord()
password = Credential.Password;
}

if (!string.IsNullOrEmpty(Tenant))
if (!string.IsNullOrEmpty(TenantId))
{
azureAccount.SetProperty(AzureAccount.Property.Tenants, new[] { Tenant });
azureAccount.SetProperty(AzureAccount.Property.Tenants, new[] { TenantId });
}

if( AzureRmProfileProvider.Instance.Profile == null)
Expand All @@ -138,7 +139,7 @@ protected override void ProcessRecord()

var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);

WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, Tenant, SubscriptionId,
WriteObject((PSAzureProfile)profileClient.Login(azureAccount, Environment, TenantId, SubscriptionId,
SubscriptionName, password));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,36 @@
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.Azure.Commands.Profile.Properties;

namespace Microsoft.Azure.Commands.Profile
{
/// <summary>
/// Cmdlet to change current Azure context.
/// </summary>
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName =TenantIdAndSubscriptionIdParameterSet)]
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName = SubscriptionNameParameterSet)]
[Alias("Select-AzureRmSubscription")]
[OutputType(typeof(PSAzureContext))]
public class SetAzureRMContextCommand : AzureRMCmdlet
{
private const string TenantIdParameterSet = "TenantId";
private const string SubscriptionIdParameterSet = "Subscription";
private const string TenantIdAndSubscriptionIdParameterSet = "TenantIdAndSubscriptionId";
private const string SubscriptionNameParameterSet = "SubscriptionName";
private const string SubscriptionIdParameterSet = "SubscriptionId";
private const string ContextParameterSet = "Context";

[Parameter(ParameterSetName = TenantIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string TenantId { get; set; }

[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName=true)]
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName=true)]

[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string SubscriptionId { get; set; }

[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName=true)]
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName=true)]
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = true, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string SubscriptionName{ get; set; }
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline=true)]
public string SubscriptionName { get; set; }

[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline = true)]
public PSAzureContext Context { get; set; }

protected override void ProcessRecord()
Expand All @@ -58,14 +56,40 @@ protected override void ProcessRecord()
AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription, Context.Account,
Context.Environment, Context.Tenant));
}
else
else if (ParameterSetName == SubscriptionNameParameterSet)
{
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
profileClient.SetCurrentContext(SubscriptionId, TenantId);
if (!string.IsNullOrWhiteSpace(SubscriptionName))
AzureSubscription subscription = null;
string tenantId = AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString();

if (string.IsNullOrWhiteSpace(TenantId))
{
WriteVerbose(
string.Format(
Resources.CurrentTenantInUse,
tenantId));
}
else
{
AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name = SubscriptionName;
tenantId = TenantId;
}

if (!profileClient.TryGetSubscriptionByName(
tenantId,
SubscriptionName,
out subscription))
{
throw new ItemNotFoundException(
string.Format(Resources.SubscriptionNameNotFoundError, SubscriptionName));
}

profileClient.SetCurrentContext(subscription.Id.ToString(), tenantId, verifySubscription: false);
AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name = SubscriptionName;
}
else if (ParameterSetName == SubscriptionIdParameterSet)
{
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
profileClient.SetCurrentContext(SubscriptionId, TenantId);
}
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
}
Expand Down
Loading