Skip to content
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

[#105517922] Update Select-AzureRmSubscription to pass in subscriptionId only or subscriptionName only #1151

Merged
merged 4 commits into from
Oct 22, 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 @@ -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

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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
return _profile;
}

public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
public AzureContext SetCurrentContext(string subscriptionId, string tenantId, bool verifySubscription = true)
{
if (!string.IsNullOrWhiteSpace(tenantId))
{
Expand All @@ -123,8 +123,12 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)

if (!string.IsNullOrWhiteSpace(subscriptionId))
{
if (!ListSubscriptions(_profile.Context.Tenant.Id.ToString()).Any(s =>
string.Equals(s.Id.ToString(), subscriptionId, StringComparison.OrdinalIgnoreCase) ) )
var subscription = ListSubscriptions(_profile.Context.Tenant.Id.ToString())
.FirstOrDefault(s =>
string.Equals(s.Id.ToString(), subscriptionId, StringComparison.OrdinalIgnoreCase));

if (verifySubscription &&
subscription == null)
{
throw new ArgumentException(string.Format(
"Provided subscription {0} does not exist under current tenant {1}", subscriptionId, _profile.Context.Tenant.Id));
Expand All @@ -136,7 +140,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
newSubscription.Account = _profile.Context.Subscription.Account;
newSubscription.Environment = _profile.Context.Subscription.Environment;
newSubscription.Properties = _profile.Context.Subscription.Properties;
newSubscription.Name = null;
newSubscription.Name = (subscription == null) ? null : subscription.Name;
}

_profile.SetContextWithCache(new AzureContext(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<data name="CommonTenantAuthFailed" xml:space="preserve">
<value>Could not authenticate your user account {0} with the common tenant. Please login again using Login-AzureRmAccount.</value>
</data>
<data name="CurrentTenantInUse" xml:space="preserve">
<value>Current tenant with Id '{0}' will be used.</value>
</data>
<data name="InvalidSubscriptionId" xml:space="preserve">
<value>The provided subscription ID "{0}" is not a valid Guid.</value>
</data>
Expand All @@ -141,6 +144,9 @@
<data name="NoValidTenant" xml:space="preserve">
<value>Please provide a valid tenant Id on the command line or execute Login-AzureRmAccount.</value>
</data>
<data name="SubscriptionNameNotFoundError" xml:space="preserve">
<value>Unable to find subscription with name '{0}'.</value>
</data>
<data name="SubscriptionNotFoundError" xml:space="preserve">
<value>Subscription {0} was not found in tenant {1}. Please verify that the subscription exists in this tenant.</value>
</data>
Expand Down