Skip to content

Commit bd1cfc2

Browse files
author
Hovsep
committed
Merge pull request Azure#1151 from hovsepm/dev
[#105517922] Update Select-AzureRmSubscription to pass in subscriptionId only or subscriptionName only
2 parents 0716fc9 + 3f2656b commit bd1cfc2

File tree

7 files changed

+308
-56
lines changed

7 files changed

+308
-56
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/ContextCmdletTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System;
2525
using Microsoft.Azure.Commands.Profile.Models;
2626
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
27+
using System.Management.Automation;
2728

2829
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
2930
{
@@ -58,7 +59,7 @@ public void GetAzureContext()
5859
var context = (PSAzureContext) commandRuntimeMock.OutputPipeline[0];
5960
Assert.Equal("test", context.Subscription.SubscriptionName);
6061
}
61-
62+
6263
[Fact]
6364
[Trait(Category.AcceptanceType, Category.CheckIn)]
6465
public void SelectAzureContextWithNoSubscriptionAndTenant()
@@ -76,7 +77,8 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
7677
// Verify
7778
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
7879
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
79-
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
80+
// TenantId is not sufficient to change the context.
81+
Assert.NotEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
8082
}
8183

8284
[Fact]

src/ResourceManager/Profile/Commands.Profile.Test/SessionRecords/Microsoft.Azure.Commands.Profile.Test.SubscriptionCmdletTests/PipingWithRmContextWorks.json

Lines changed: 224 additions & 32 deletions
Large diffs are not rendered by default.

src/ResourceManager/Profile/Commands.Profile.Test/SubscriptionCmdletTests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function Test-PipingWithContext
5252
$name = $firstSubscription.SubscriptionName
5353
$nameContext = Get-AzureRmSubscription -SubscriptionName $name | Set-AzureRmContext
5454
$idContext = Get-AzureRmSubscription -SubscriptionId $id | Set-AzureRmContext
55+
$contextByName = Set-AzureRmContext -SubscriptionName $name
5556
Assert-True { $nameContext -ne $null }
5657
Assert-True { $nameContext.Subscription -ne $null }
5758
Assert-True { $nameContext.Subscription.SubscriptionId -ne $null }
@@ -62,6 +63,11 @@ function Test-PipingWithContext
6263
Assert-True { $idContext.Subscription.SubscriptionName -ne $null }
6364
Assert-AreEqual $idContext.Subscription.SubscriptionId $nameContext.Subscription.SubscriptionId
6465
Assert-AreEqual $idContext.Subscription.SubscriptionName $nameContext.Subscription.SubscriptionName
66+
Assert-True { $contextByName -ne $null }
67+
Assert-True { $contextByName.Subscription -ne $null }
68+
Assert-True { $contextByName.Subscription.SubscriptionId -ne $null }
69+
Assert-True { $contextByName.Subscription.SubscriptionName -ne $null }
70+
Assert-AreEqual $contextByName.Subscription.SubscriptionName $nameContext.Subscription.SubscriptionName
6571
}
6672

6773
function Test-SetAzureRmContextEndToEnd

src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,36 @@
1717
using Microsoft.Azure.Commands.Profile.Models;
1818
using Microsoft.Azure.Commands.ResourceManager.Common;
1919
using Microsoft.WindowsAzure.Commands.Common;
20+
using Microsoft.Azure.Commands.Profile.Properties;
2021

2122
namespace Microsoft.Azure.Commands.Profile
2223
{
2324
/// <summary>
2425
/// Cmdlet to change current Azure context.
2526
/// </summary>
26-
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName =TenantIdAndSubscriptionIdParameterSet)]
27+
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName = SubscriptionNameParameterSet)]
2728
[Alias("Select-AzureRmSubscription")]
2829
[OutputType(typeof(PSAzureContext))]
2930
public class SetAzureRMContextCommand : AzureRMCmdlet
3031
{
31-
private const string TenantIdParameterSet = "TenantId";
32-
private const string SubscriptionIdParameterSet = "Subscription";
33-
private const string TenantIdAndSubscriptionIdParameterSet = "TenantIdAndSubscriptionId";
32+
private const string SubscriptionNameParameterSet = "SubscriptionName";
33+
private const string SubscriptionIdParameterSet = "SubscriptionId";
3434
private const string ContextParameterSet = "Context";
3535

36-
[Parameter(ParameterSetName = TenantIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
37-
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName=true)]
36+
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
37+
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
3838
[ValidateNotNullOrEmpty]
3939
public string TenantId { get; set; }
40-
41-
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName=true)]
42-
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName=true)]
40+
41+
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = true, HelpMessage = "Subscription", ValueFromPipelineByPropertyName = true)]
4342
[ValidateNotNullOrEmpty]
4443
public string SubscriptionId { get; set; }
4544

46-
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName=true)]
47-
[Parameter(ParameterSetName = TenantIdAndSubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName=true)]
45+
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = true, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
4846
[ValidateNotNullOrEmpty]
49-
public string SubscriptionName{ get; set; }
50-
51-
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline=true)]
47+
public string SubscriptionName { get; set; }
48+
49+
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline = true)]
5250
public PSAzureContext Context { get; set; }
5351

5452
protected override void ProcessRecord()
@@ -58,14 +56,40 @@ protected override void ProcessRecord()
5856
AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription, Context.Account,
5957
Context.Environment, Context.Tenant));
6058
}
61-
else
59+
else if (ParameterSetName == SubscriptionNameParameterSet)
6260
{
6361
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
64-
profileClient.SetCurrentContext(SubscriptionId, TenantId);
65-
if (!string.IsNullOrWhiteSpace(SubscriptionName))
62+
AzureSubscription subscription = null;
63+
string tenantId = AzureRmProfileProvider.Instance.Profile.Context.Tenant.Id.ToString();
64+
65+
if (string.IsNullOrWhiteSpace(TenantId))
66+
{
67+
WriteVerbose(
68+
string.Format(
69+
Resources.CurrentTenantInUse,
70+
tenantId));
71+
}
72+
else
6673
{
67-
AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name = SubscriptionName;
74+
tenantId = TenantId;
6875
}
76+
77+
if (!profileClient.TryGetSubscriptionByName(
78+
tenantId,
79+
SubscriptionName,
80+
out subscription))
81+
{
82+
throw new ItemNotFoundException(
83+
string.Format(Resources.SubscriptionNameNotFoundError, SubscriptionName));
84+
}
85+
86+
profileClient.SetCurrentContext(subscription.Id.ToString(), tenantId, verifySubscription: false);
87+
AzureRmProfileProvider.Instance.Profile.Context.Subscription.Name = SubscriptionName;
88+
}
89+
else if (ParameterSetName == SubscriptionIdParameterSet)
90+
{
91+
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
92+
profileClient.SetCurrentContext(SubscriptionId, TenantId);
6993
}
7094
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
7195
}

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
101101
return _profile;
102102
}
103103

104-
public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
104+
public AzureContext SetCurrentContext(string subscriptionId, string tenantId, bool verifySubscription = true)
105105
{
106106
if (!string.IsNullOrWhiteSpace(tenantId))
107107
{
@@ -123,8 +123,12 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
123123

124124
if (!string.IsNullOrWhiteSpace(subscriptionId))
125125
{
126-
if (!ListSubscriptions(_profile.Context.Tenant.Id.ToString()).Any(s =>
127-
string.Equals(s.Id.ToString(), subscriptionId, StringComparison.OrdinalIgnoreCase) ) )
126+
var subscription = ListSubscriptions(_profile.Context.Tenant.Id.ToString())
127+
.FirstOrDefault(s =>
128+
string.Equals(s.Id.ToString(), subscriptionId, StringComparison.OrdinalIgnoreCase));
129+
130+
if (verifySubscription &&
131+
subscription == null)
128132
{
129133
throw new ArgumentException(string.Format(
130134
"Provided subscription {0} does not exist under current tenant {1}", subscriptionId, _profile.Context.Tenant.Id));
@@ -136,7 +140,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
136140
newSubscription.Account = _profile.Context.Subscription.Account;
137141
newSubscription.Environment = _profile.Context.Subscription.Environment;
138142
newSubscription.Properties = _profile.Context.Subscription.Properties;
139-
newSubscription.Name = null;
143+
newSubscription.Name = (subscription == null) ? null : subscription.Name;
140144
}
141145

142146
_profile.SetContextWithCache(new AzureContext(

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
<data name="CommonTenantAuthFailed" xml:space="preserve">
133133
<value>Could not authenticate your user account {0} with the common tenant. Please login again using Login-AzureRmAccount.</value>
134134
</data>
135+
<data name="CurrentTenantInUse" xml:space="preserve">
136+
<value>Current tenant with Id '{0}' will be used.</value>
137+
</data>
135138
<data name="InvalidSubscriptionId" xml:space="preserve">
136139
<value>The provided subscription ID "{0}" is not a valid Guid.</value>
137140
</data>
@@ -141,6 +144,9 @@
141144
<data name="NoValidTenant" xml:space="preserve">
142145
<value>Please provide a valid tenant Id on the command line or execute Login-AzureRmAccount.</value>
143146
</data>
147+
<data name="SubscriptionNameNotFoundError" xml:space="preserve">
148+
<value>Unable to find subscription with name '{0}'.</value>
149+
</data>
144150
<data name="SubscriptionNotFoundError" xml:space="preserve">
145151
<value>Subscription {0} was not found in tenant {1}. Please verify that the subscription exists in this tenant.</value>
146152
</data>

0 commit comments

Comments
 (0)