Skip to content

Commit f0d2d3e

Browse files
markcowlcormacpayne
authored andcommitted
Removing dynamic parameters from Set-AzureRMContext cmdlet, fixes #2554 (#2712)
1 parent 3fb9fc3 commit f0d2d3e

File tree

3 files changed

+12
-145
lines changed

3 files changed

+12
-145
lines changed

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

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,7 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
8989
account.SetProperty(AzureAccount.Property.Tenants, allowedTenants);
9090
account.SetProperty(AzureAccount.Property.Subscriptions, new string[0]);
9191

92-
var paramDictionary =
93-
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters());
94-
var tenantParam = paramDictionary["TenantId"];
95-
Assert.True(tenantParam.Attributes.Any(a => a is ValidateSetAttribute
96-
&& ((ValidateSetAttribute)a).ValidValues.Any(v => string.Equals(v, tenantToSet, StringComparison.OrdinalIgnoreCase))));
97-
Assert.False(paramDictionary["SubscriptionId"].Attributes.Any(a => a is ValidateSetAttribute));
98-
tenantParam.Value = tenantToSet;
92+
cmdlt.TenantId = tenantToSet;
9993

10094
// Act
10195
cmdlt.InvokeBeginProcessing();
@@ -110,50 +104,6 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
110104
Assert.NotEqual(tenantToSet, context.Tenant.TenantId);
111105
}
112106

113-
[Theory]
114-
[InlineData(null, null)]
115-
[InlineData(new string[0], new string[0])]
116-
[InlineData(new string[] { guid1}, new string[] {guid2})]
117-
[InlineData(new string[] { guid1, guid2 }, new string[] { guid3, guid4 })]
118-
[Trait(Category.AcceptanceType, Category.CheckIn)]
119-
public void SetsDynamicParametersForContext(string[] subscriptions, string[] tenants)
120-
{
121-
var cmdlt = new SetAzureRMContextCommand();
122-
123-
// Setup
124-
cmdlt.CommandRuntime = commandRuntimeMock;
125-
126-
// Make sure that the tenant ID we are attempting to set is
127-
// valid for the account
128-
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
129-
account.SetProperty(AzureAccount.Property.Tenants, tenants);
130-
account.SetProperty(AzureAccount.Property.Subscriptions, subscriptions);
131-
132-
var paramDictionary =
133-
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters());
134-
var subscriptionParams = paramDictionary["SubscriptionId"];
135-
VerifyValidationAttribute(subscriptionParams, subscriptions);
136-
var tenantParams = paramDictionary["TenantId"];
137-
VerifyValidationAttribute(tenantParams, tenants);
138-
}
139-
140-
private void VerifyValidationAttribute(RuntimeDefinedParameter parameter, string[] expectedValues)
141-
{
142-
if (expectedValues != null && expectedValues.Length > 0)
143-
{
144-
var validateAttribute = parameter.Attributes.First(a => a is ValidateSetAttribute) as ValidateSetAttribute;
145-
Assert.NotNull(validateAttribute);
146-
foreach (var expectedValue in expectedValues)
147-
{
148-
Assert.Contains(expectedValue, validateAttribute.ValidValues, StringComparer.OrdinalIgnoreCase);
149-
}
150-
}
151-
else
152-
{
153-
Assert.False(parameter.Attributes.Any(a => a is ValidateSetAttribute));
154-
}
155-
}
156-
157107
[Fact]
158108
[Trait(Category.AcceptanceType, Category.CheckIn)]
159109
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
@@ -173,7 +123,5 @@ public void SelectAzureContextWithNoSubscriptionAndNoTenant()
173123
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
174124
Assert.NotNull(context);
175125
}
176-
177-
178126
}
179127
}

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

Lines changed: 1 addition & 1 deletion
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 belong"
84+
Assert-ThrowsContains {Set-AzureRmContext -SubscriptionId $junkSubscriptionId} "does not exist"
8585
}
8686

8787
function Test-SetAzureRmContextWithoutSubscription

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

Lines changed: 10 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ namespace Microsoft.Azure.Commands.Profile
3131
SupportsShouldProcess=true)]
3232
[Alias("Select-AzureRmSubscription")]
3333
[OutputType(typeof(PSAzureContext))]
34-
public class SetAzureRMContextCommand : AzureRMCmdlet, IDynamicParameters
34+
public class SetAzureRMContextCommand : AzureRMCmdlet
3535
{
3636
private const string SubscriptionNameParameterSet = "SubscriptionName";
3737
private const string SubscriptionIdParameterSet = "SubscriptionId";
3838
private const string ContextParameterSet = "Context";
39-
private RuntimeDefinedParameter _tenantId;
40-
private RuntimeDefinedParameter _subscriptionId;
4139

4240
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false, HelpMessage = "Subscription Name", ValueFromPipelineByPropertyName = true)]
4341
[ValidateNotNullOrEmpty]
@@ -46,21 +44,16 @@ public class SetAzureRMContextCommand : AzureRMCmdlet, IDynamicParameters
4644
[Parameter(ParameterSetName = ContextParameterSet, Mandatory = true, HelpMessage = "Context", ValueFromPipeline = true)]
4745
public PSAzureContext Context { get; set; }
4846

49-
private string TenantId
50-
{
51-
get
52-
{
53-
return _tenantId == null ? null : (string)_tenantId.Value;
54-
}
55-
}
47+
[Parameter(ParameterSetName = SubscriptionNameParameterSet, Mandatory = false,
48+
HelpMessage = "Tenant name or ID", ValueFromPipelineByPropertyName = true)]
49+
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false,
50+
HelpMessage = "Tenant name or ID", ValueFromPipelineByPropertyName = true)]
51+
[Alias("Domain")]
52+
public string TenantId { get; set; }
5653

57-
private string SubscriptionId
58-
{
59-
get
60-
{
61-
return _subscriptionId == null ? null : (string)_subscriptionId.Value;
62-
}
63-
}
54+
[Parameter(ParameterSetName = SubscriptionIdParameterSet, Mandatory = false,
55+
HelpMessage = "Subscription Identifer (GUID)", ValueFromPipelineByPropertyName = true)]
56+
public string SubscriptionId { get; set; }
6457

6558
public override void ExecuteCmdlet()
6659
{
@@ -127,79 +120,5 @@ private void CompleteContextProcessing()
127120
}
128121
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
129122
}
130-
131-
public object GetDynamicParameters()
132-
{
133-
return CreateDynamicParameterDictionary();
134-
}
135-
136-
private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
137-
{
138-
var runtimeDefinedParameterDictionary = new RuntimeDefinedParameterDictionary();
139-
140-
var subscriptionIdAttributes = new Collection<Attribute>
141-
{
142-
new ParameterAttribute
143-
{
144-
ParameterSetName = SubscriptionIdParameterSet,
145-
Mandatory = false,
146-
HelpMessage = "Subscription",
147-
ValueFromPipelineByPropertyName = true
148-
}
149-
};
150-
151-
var tenantIdAttributes = new Collection<Attribute>
152-
{
153-
new ParameterAttribute
154-
{
155-
ParameterSetName = SubscriptionNameParameterSet,
156-
Mandatory = false,
157-
HelpMessage = "Tenant name or ID",
158-
ValueFromPipelineByPropertyName = true
159-
},
160-
new ParameterAttribute
161-
{
162-
ParameterSetName = SubscriptionIdParameterSet,
163-
Mandatory = false,
164-
HelpMessage = "Tenant name or ID",
165-
ValueFromPipelineByPropertyName = true
166-
},
167-
new AliasAttribute("Domain")
168-
};
169-
170-
if (AzureRmProfileProvider.Instance != null
171-
&& AzureRmProfileProvider.Instance.Profile != null
172-
&& AzureRmProfileProvider.Instance.Profile.Context != null
173-
&& AzureRmProfileProvider.Instance.Profile.Context.Account != null)
174-
{
175-
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
176-
if (account.IsPropertySet(AzureAccount.Property.Subscriptions))
177-
{
178-
var subscriptions = account.GetPropertyAsArray(AzureAccount.Property.Subscriptions);
179-
if (subscriptions != null && subscriptions.Length > 0)
180-
{
181-
subscriptionIdAttributes.Add(
182-
new ValidateSetAttribute(subscriptions));
183-
}
184-
}
185-
if (account.IsPropertySet(AzureAccount.Property.Tenants))
186-
{
187-
var tenants = account.GetPropertyAsArray(AzureAccount.Property.Tenants);
188-
if (tenants != null && tenants.Length > 0)
189-
{
190-
tenantIdAttributes.Add(
191-
new ValidateSetAttribute(tenants));
192-
}
193-
}
194-
}
195-
196-
_tenantId = new RuntimeDefinedParameter("TenantId", typeof(string), tenantIdAttributes);
197-
_subscriptionId = new RuntimeDefinedParameter("SubscriptionId", typeof(string), subscriptionIdAttributes);
198-
199-
runtimeDefinedParameterDictionary.Add("SubscriptionId", _subscriptionId);
200-
runtimeDefinedParameterDictionary.Add("TenantId", _tenantId);
201-
202-
return runtimeDefinedParameterDictionary;
203-
}
204123
}
205124
}

0 commit comments

Comments
 (0)