Skip to content

Commit 1e535ab

Browse files
committed
Support autocomplete on subscription ID and tenant ID
1 parent a36114f commit 1e535ab

File tree

5 files changed

+109
-35
lines changed

5 files changed

+109
-35
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Xunit;
16-
using System;
1715
using Microsoft.Azure.Commands.Common.Authentication.Models;
1816
using Microsoft.Azure.Commands.Profile.Models;
19-
using Microsoft.Azure.ServiceManagemenet.Common;
17+
using Microsoft.Azure.ServiceManagemenet.Common.Models;
2018
using Microsoft.WindowsAzure.Commands.Common;
2119
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2220
using Microsoft.WindowsAzure.Commands.Utilities.Common;
23-
using Xunit.Extensions;
21+
using System;
22+
using Xunit;
2423
using Xunit.Abstractions;
25-
using Microsoft.Azure.ServiceManagemenet.Common.Models;
2624

2725
namespace Microsoft.Azure.Commands.Profile.Test
2826
{

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
15+
using Microsoft.Azure.Commands.Common.Authentication;
16+
using Microsoft.Azure.Commands.Common.Authentication.Models;
1617
using Microsoft.Azure.Commands.Profile;
18+
using Microsoft.Azure.Commands.Profile.Models;
19+
using Microsoft.Azure.ServiceManagemenet.Common.Models;
20+
using Microsoft.WindowsAzure.Commands.Common;
1721
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
1822
using Microsoft.WindowsAzure.Commands.ScenarioTest;
19-
using Xunit;
20-
using Microsoft.Azure.Commands.Profile.Models;
2123
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
22-
using Microsoft.Azure.Commands.Common.Authentication;
23-
using Microsoft.Azure.Commands.Common.Authentication.Models;
24+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
25+
using System.Management.Automation;
26+
using Xunit;
2427
using Xunit.Abstractions;
25-
using Microsoft.Azure.ServiceManagemenet.Common.Models;
2628

2729
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
2830
{
2931
public class ContextCmdletTests : RMTestBase
3032
{
3133
private MemoryDataStore dataStore;
3234
private MockCommandRuntime commandRuntimeMock;
33-
35+
3436
public ContextCmdletTests(ITestOutputHelper output)
3537
{
3638
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
@@ -45,6 +47,7 @@ public ContextCmdletTests(ITestOutputHelper output)
4547
public void GetAzureContext()
4648
{
4749
var cmdlt = new GetAzureRMContextCommand();
50+
4851
// Setup
4952
cmdlt.CommandRuntime = commandRuntimeMock;
5053

@@ -55,18 +58,28 @@ public void GetAzureContext()
5558

5659
// Verify
5760
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
58-
var context = (PSAzureContext) commandRuntimeMock.OutputPipeline[0];
61+
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
5962
Assert.Equal("test", context.Subscription.SubscriptionName);
6063
}
61-
64+
6265
[Fact]
6366
[Trait(Category.AcceptanceType, Category.CheckIn)]
6467
public void SelectAzureContextWithNoSubscriptionAndTenant()
6568
{
6669
var cmdlt = new SetAzureRMContextCommand();
70+
var tenantToSet = "72f988bf-86f1-41af-91ab-2d7cd011db47";
71+
6772
// Setup
6873
cmdlt.CommandRuntime = commandRuntimeMock;
69-
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
74+
75+
// Make sure that the tenant ID we are attempting to set is
76+
// valid for the account
77+
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
78+
var existingTenants = account.GetProperty(AzureAccount.Property.Tenants);
79+
var allowedTenants = existingTenants == null ? tenantToSet : existingTenants + "," + tenantToSet;
80+
account.SetProperty(AzureAccount.Property.Tenants, allowedTenants);
81+
82+
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters())["TenantId"].Value = tenantToSet;
7083

7184
// Act
7285
cmdlt.InvokeBeginProcessing();
@@ -76,15 +89,17 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
7689
// Verify
7790
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
7891
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
92+
7993
// TenantId is not sufficient to change the context.
80-
Assert.NotEqual("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
94+
Assert.NotEqual(tenantToSet, context.Tenant.TenantId);
8195
}
8296

8397
[Fact]
8498
[Trait(Category.AcceptanceType, Category.CheckIn)]
8599
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
86100
{
87101
var cmdlt = new SetAzureRMContextCommand();
102+
88103
// Setup
89104
cmdlt.CommandRuntime = commandRuntimeMock;
90105

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function Test-SetAzureRmContextEndToEnd
8181
$context = Get-AzureRmContext
8282
Assert-AreEqual $context.Subscription.SubscriptionId $secondSubscription.SubscriptionId
8383
$junkSubscriptionId = "49BC3D95-9A30-40F8-81E0-3CDEF0C3F8A5"
84-
Assert-ThrowsContains {Set-AzureRmContext -SubscriptionId $junkSubscriptionId} "does not exist"
84+
Assert-ThrowsContains {Set-AzureRmContext -SubscriptionId $junkSubscriptionId} "does not belong"
8585
}
8686

8787
function Test-SetAzureRmContextWithoutSubscription
@@ -95,7 +95,7 @@ function Test-SetAzureRmContextWithoutSubscription
9595

9696
Set-AzureRmContext -TenantId $tenantId
9797
$context = Get-AzureRmContext
98-
98+
9999
Assert-True { $context.Subscription -ne $null }
100100
Assert-True { $context.Tenant -ne $null }
101101
Assert-AreEqual $context.Tenant.TenantId $firstSubscription.TenantId

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

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,30 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.Management.Automation;
15+
using Microsoft.Azure.Commands.Common.Authentication.Models;
1616
using Microsoft.Azure.Commands.Profile.Models;
17+
using Microsoft.Azure.Commands.Profile.Properties;
1718
using Microsoft.Azure.Commands.ResourceManager.Common;
1819
using Microsoft.WindowsAzure.Commands.Common;
19-
using Microsoft.Azure.Commands.Profile.Properties;
2020
using System;
21-
using Microsoft.Azure.Commands.Common.Authentication.Models;
21+
using System.Collections.ObjectModel;
22+
using System.Management.Automation;
2223

2324
namespace Microsoft.Azure.Commands.Profile
2425
{
2526
/// <summary>
26-
/// Cmdlet to change current Azure context.
27+
/// Cmdlet to change current Azure context.
2728
/// </summary>
2829
[Cmdlet(VerbsCommon.Set, "AzureRmContext", DefaultParameterSetName = SubscriptionNameParameterSet)]
2930
[Alias("Select-AzureRmSubscription")]
3031
[OutputType(typeof(PSAzureContext))]
31-
public class SetAzureRMContextCommand : AzureRMCmdlet
32+
public class SetAzureRMContextCommand : AzureRMCmdlet, IDynamicParameters
3233
{
3334
private const string SubscriptionNameParameterSet = "SubscriptionName";
3435
private const string SubscriptionIdParameterSet = "SubscriptionId";
3536
private const string ContextParameterSet = "Context";
36-
37-
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
38-
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "TenantId name or ID", ValueFromPipelineByPropertyName = true)]
39-
[Alias("Domain")]
40-
[ValidateNotNullOrEmpty]
41-
public string TenantId { get; set; }
42-
43-
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false, HelpMessage = "Subscription", ValueFromPipelineByPropertyName = true)]
44-
[ValidateNotNullOrEmpty]
45-
public string SubscriptionId { get; set; }
37+
private RuntimeDefinedParameter _tenantId;
38+
private RuntimeDefinedParameter _subscriptionId;
4639

4740
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
4841
[ValidateNotNullOrEmpty]
@@ -51,6 +44,22 @@ public class SetAzureRMContextCommand : AzureRMCmdlet
5144
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline = true)]
5245
public PSAzureContext Context { get; set; }
5346

47+
private string TenantId
48+
{
49+
get
50+
{
51+
return _tenantId == null ? null : (string)_tenantId.Value;
52+
}
53+
}
54+
55+
private string SubscriptionId
56+
{
57+
get
58+
{
59+
return _subscriptionId == null ? null : (string)_subscriptionId.Value;
60+
}
61+
}
62+
5463
public override void ExecuteCmdlet()
5564
{
5665
if (ParameterSetName == ContextParameterSet)
@@ -60,7 +69,7 @@ public override void ExecuteCmdlet()
6069
}
6170
else if (ParameterSetName == SubscriptionNameParameterSet || ParameterSetName == SubscriptionIdParameterSet)
6271
{
63-
if (string.IsNullOrWhiteSpace(SubscriptionId)
72+
if (string.IsNullOrWhiteSpace(SubscriptionId)
6473
&& string.IsNullOrWhiteSpace(SubscriptionName)
6574
&& string.IsNullOrWhiteSpace(TenantId))
6675
{
@@ -82,7 +91,7 @@ public override void ExecuteCmdlet()
8291
AzureRmProfileProvider.Instance.Profile.Context.Subscription != null &&
8392
AzureRmProfileProvider.Instance.Profile.Context.Subscription.State != null &&
8493
!AzureRmProfileProvider.Instance.Profile.Context.Subscription.State.Equals(
85-
"Enabled",
94+
"Enabled",
8695
StringComparison.OrdinalIgnoreCase))
8796
{
8897
WriteWarning(string.Format(
@@ -91,5 +100,55 @@ public override void ExecuteCmdlet()
91100
}
92101
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
93102
}
103+
104+
public object GetDynamicParameters()
105+
{
106+
return CreateDynamicParameterDictionary();
107+
}
108+
109+
private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
110+
{
111+
var runtimeDefinedParameterDictionary = new RuntimeDefinedParameterDictionary();
112+
113+
var subscriptionIdAttributes = new Collection<Attribute>
114+
{
115+
new ParameterAttribute
116+
{
117+
ParameterSetName = SubscriptionIdParameterSet,
118+
Mandatory = false,
119+
HelpMessage = "Subscription",
120+
ValueFromPipelineByPropertyName = true
121+
},
122+
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Subscriptions)),
123+
};
124+
125+
var tenantIdAttributes = new Collection<Attribute>
126+
{
127+
new ParameterAttribute
128+
{
129+
ParameterSetName = SubscriptionNameParameterSet,
130+
Mandatory = false,
131+
HelpMessage = "TenantId name or ID",
132+
ValueFromPipelineByPropertyName = true
133+
},
134+
new ParameterAttribute
135+
{
136+
ParameterSetName = SubscriptionIdParameterSet,
137+
Mandatory = false,
138+
HelpMessage = "TenantId name or ID",
139+
ValueFromPipelineByPropertyName = true
140+
},
141+
new AliasAttribute("Domain"),
142+
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants)),
143+
};
144+
145+
_tenantId = new RuntimeDefinedParameter("TenantId", typeof(string), tenantIdAttributes);
146+
_subscriptionId = new RuntimeDefinedParameter("SubscriptionId", typeof(string), subscriptionIdAttributes);
147+
148+
runtimeDefinedParameterDictionary.Add("SubscriptionId", _subscriptionId);
149+
runtimeDefinedParameterDictionary.Add("TenantId", _tenantId);
150+
151+
return runtimeDefinedParameterDictionary;
152+
}
94153
}
95154
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
513513
.Where(s => "enabled".Equals(s.State, StringComparison.OrdinalIgnoreCase) ||
514514
"warned".Equals(s.State, StringComparison.OrdinalIgnoreCase));
515515

516+
account.SetProperty(AzureAccount.Property.Subscriptions, subscriptions.Select(i => i.SubscriptionId).ToArray());
517+
516518
if (subscriptions.Any())
517519
{
518520
if (subscriptionName != null)

0 commit comments

Comments
 (0)