Skip to content

Commit 17a3330

Browse files
committed
Fix for #2387, #2388 fix subscription and tenant validate sets when no subscriptions or tenants selected
1 parent 9bdf159 commit 17a3330

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

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

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

15+
using System;
16+
using System.CodeDom;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Linq.Expressions;
1520
using Microsoft.Azure.Commands.Common.Authentication;
1621
using Microsoft.Azure.Commands.Common.Authentication.Models;
1722
using Microsoft.Azure.Commands.Profile;
@@ -25,14 +30,18 @@
2530
using System.Management.Automation;
2631
using Xunit;
2732
using Xunit.Abstractions;
33+
using Xunit.Sdk;
2834

2935
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
3036
{
3137
public class ContextCmdletTests : RMTestBase
3238
{
3339
private MemoryDataStore dataStore;
3440
private MockCommandRuntime commandRuntimeMock;
35-
41+
const string guid1 = "a0cc8bd7-2c6a-47e9-a4c4-3f6ed136e240";
42+
const string guid2 = "eab635c0-a35a-4f70-9e46-e5351c7b5c8b";
43+
const string guid3 = "52f66548-2550-417b-941e-9d6e04f3ac8d";
44+
const string guid4 = "40e67ee2-1a1a-4517-9253-ab6f93c5710f";
3645
public ContextCmdletTests(ITestOutputHelper output)
3746
{
3847
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
@@ -78,8 +87,15 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
7887
var existingTenants = account.GetProperty(AzureAccount.Property.Tenants);
7988
var allowedTenants = existingTenants == null ? tenantToSet : existingTenants + "," + tenantToSet;
8089
account.SetProperty(AzureAccount.Property.Tenants, allowedTenants);
90+
account.SetProperty(AzureAccount.Property.Subscriptions, new string[0]);
8191

82-
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters())["TenantId"].Value = tenantToSet;
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;
8399

84100
// Act
85101
cmdlt.InvokeBeginProcessing();
@@ -94,6 +110,50 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
94110
Assert.NotEqual(tenantToSet, context.Tenant.TenantId);
95111
}
96112

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+
97157
[Fact]
98158
[Trait(Category.AcceptanceType, Category.CheckIn)]
99159
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
@@ -113,5 +173,7 @@ public void SelectAzureContextWithNoSubscriptionAndNoTenant()
113173
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
114174
Assert.NotNull(context);
115175
}
176+
177+
116178
}
117179
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
119119
HelpMessage = "Subscription",
120120
ValueFromPipelineByPropertyName = true
121121
},
122-
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Subscriptions)),
123122
};
124123

125124
var tenantIdAttributes = new Collection<Attribute>
@@ -139,9 +138,34 @@ private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
139138
ValueFromPipelineByPropertyName = true
140139
},
141140
new AliasAttribute("Domain"),
142-
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants)),
143141
};
144142

143+
if (AzureRmProfileProvider.Instance != null
144+
&& AzureRmProfileProvider.Instance.Profile != null
145+
&& AzureRmProfileProvider.Instance.Profile.Context != null
146+
&& AzureRmProfileProvider.Instance.Profile.Context.Account != null)
147+
{
148+
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
149+
if (account.IsPropertySet(AzureAccount.Property.Subscriptions))
150+
{
151+
var subscriptions = account.GetPropertyAsArray(AzureAccount.Property.Subscriptions);
152+
if (subscriptions != null && subscriptions.Length > 0)
153+
{
154+
subscriptionIdAttributes.Add(
155+
new ValidateSetAttribute(subscriptions));
156+
}
157+
}
158+
if (account.IsPropertySet(AzureAccount.Property.Tenants))
159+
{
160+
var tenants = account.GetPropertyAsArray(AzureAccount.Property.Tenants);
161+
if (tenants != null && tenants.Length > 0)
162+
{
163+
tenantIdAttributes.Add(
164+
new ValidateSetAttribute(tenants));
165+
}
166+
}
167+
}
168+
145169
_tenantId = new RuntimeDefinedParameter("TenantId", typeof(string), tenantIdAttributes);
146170
_subscriptionId = new RuntimeDefinedParameter("SubscriptionId", typeof(string), subscriptionIdAttributes);
147171

0 commit comments

Comments
 (0)