Skip to content

Commit a59a8b3

Browse files
committed
Merge pull request Azure#1022 from amarzavery/context
Set-AzureRmContext usability improvements
2 parents 157095e + 2c66fa8 commit a59a8b3

File tree

11 files changed

+422
-130
lines changed

11 files changed

+422
-130
lines changed

src/ResourceManager/Common/Commands.ResourceManager.Common/RMProfileClient.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
6161
// (tenant is not provided and subscription is not provided)
6262
else
6363
{
64-
foreach(var tenant in ListAccountTenants(account, environment, password, promptBehavior))
64+
foreach (var tenant in ListAccountTenants(account, environment, password, promptBehavior))
6565
{
6666
AzureTenant tempTenant;
6767
AzureSubscription tempSubscription;
6868
var token = AcquireAccessToken(account, environment, tenant.Id.ToString(), password,
6969
ShowDialog.Auto);
7070
if (newTenant == null && TryGetTenantSubscription(token, account, environment, tenant.Id.ToString(), subscriptionId, out tempSubscription, out tempTenant) &&
7171
newTenant == null)
72-
{
72+
{
7373
newTenant = tempTenant;
7474
newSubscription = tempSubscription;
7575
}
@@ -93,8 +93,8 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
9393
{
9494
_profile.Context = new AzureContext(
9595
_profile.Context.Subscription,
96-
_profile.Context.Account,
97-
_profile.Context.Environment,
96+
_profile.Context.Account,
97+
_profile.Context.Environment,
9898
new AzureTenant() { Id = new Guid(tenantId) });
9999

100100
if (_profile.Context.Account != null)
@@ -107,10 +107,17 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
107107
}
108108
}
109109

110-
if(!string.IsNullOrWhiteSpace(subscriptionId))
110+
if (!string.IsNullOrWhiteSpace(subscriptionId))
111111
{
112+
if (ListSubscriptions(_profile.Context.Tenant.Id.ToString()).FirstOrDefault(s =>
113+
String.Compare(s.Id.ToString(), subscriptionId, StringComparison.OrdinalIgnoreCase) == 0) == null)
114+
{
115+
throw new ArgumentException(string.Format(
116+
"Provided subscription {0} does not exist under current tenant {1}", subscriptionId, _profile.Context.Tenant.Id));
117+
}
118+
112119
var newSubscription = new AzureSubscription { Id = new Guid(subscriptionId) };
113-
if(_profile.Context.Subscription != null)
120+
if (_profile.Context.Subscription != null)
114121
{
115122
newSubscription.Account = _profile.Context.Subscription.Account;
116123
newSubscription.Environment = _profile.Context.Subscription.Environment;
@@ -121,7 +128,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
121128
_profile.Context = new AzureContext(
122129
newSubscription,
123130
_profile.Context.Account,
124-
_profile.Context.Environment,
131+
_profile.Context.Environment,
125132
_profile.Context.Tenant);
126133
}
127134

@@ -131,7 +138,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
131138
public List<AzureTenant> ListTenants(string tenant)
132139
{
133140
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Auto)
134-
.Where(t => tenant == null ||
141+
.Where(t => tenant == null ||
135142
tenant.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase) ||
136143
tenant.Equals(t.Domain, StringComparison.OrdinalIgnoreCase))
137144
.ToList();
@@ -279,7 +286,7 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
279286
out AzureSubscription subscription,
280287
out AzureTenant tenant)
281288
{
282-
289+
283290
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
284291
new TokenCloudCredentials(accessToken.AccessToken),
285292
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
@@ -339,15 +346,15 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
339346

340347
private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
341348
{
342-
var commonTenantToken = AcquireAccessToken( account, environment, AuthenticationFactory.CommonAdTenant,
349+
var commonTenantToken = AcquireAccessToken(account, environment, AuthenticationFactory.CommonAdTenant,
343350
password, promptBehavior);
344351

345352
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
346353
new TokenCloudCredentials(commonTenantToken.AccessToken),
347354
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
348355
{
349356
return subscriptionClient.Tenants.List().TenantIds
350-
.Select(ti => new AzureTenant() { Id = new Guid(ti.TenantId), Domain = commonTenantToken.GetDomain() } )
357+
.Select(ti => new AzureTenant() { Id = new Guid(ti.TenantId), Domain = commonTenantToken.GetDomain() })
351358
.ToList();
352359
}
353360
}
@@ -361,7 +368,7 @@ public IEnumerable<AzureTenant> ListTenants()
361368
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Never);
362369
}
363370

364-
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
371+
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
365372
SecureString password, ShowDialog promptBehavior, string tenantId)
366373
{
367374
var accessToken = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="xunit.runner.console" version="2.0.0" />
4+
</packages>

src/ResourceManager/HDInsight/Commands.HDInsight.Test/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net45" />
2020
<package id="Microsoft.Rest.ClientRuntime" version="1.2.0" targetFramework="net45" />
2121
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="0.9.3" targetFramework="net45" />
22+
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
2223
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
2324
<package id="xunit" version="1.9.2" targetFramework="net45" />
2425
<package id="xunit.extensions" version="1.9.2" targetFramework="net45" />
26+
<package id="xunit.runner.visualstudio" version="2.1.0-beta4-build1109" targetFramework="net45" />
2527
</packages>

src/ResourceManager/Profile/Commands.Profile.Test/Commands.Profile.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@
196196
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.SubscriptionCmdletTests\AllParameterSetsSucceed.json">
197197
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
198198
</None>
199+
<None Include="SessionRecords\Microsoft.Azure.Commands.Profile.Test.SubscriptionCmdletTests\SetAzureRmContextWorks.json">
200+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
201+
</None>
199202
</ItemGroup>
200203
<ItemGroup>
201204
<ProjectReference Include="..\..\..\Common\Commands.Common\Commands.Common.csproj">

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

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,48 +59,6 @@ public void GetAzureContext()
5959
Assert.Equal("test", context.Subscription.SubscriptionName);
6060
}
6161

62-
[Fact]
63-
[Trait(Category.AcceptanceType, Category.CheckIn)]
64-
public void SelectAzureContextWithSubscriptionAndTenant()
65-
{
66-
var cmdlt = new SetAzureRMContextCommand();
67-
// Setup
68-
cmdlt.CommandRuntime = commandRuntimeMock;
69-
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
70-
cmdlt.TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
71-
72-
// Act
73-
cmdlt.InvokeBeginProcessing();
74-
cmdlt.ExecuteCmdlet();
75-
cmdlt.InvokeEndProcessing();
76-
77-
// Verify
78-
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
79-
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
80-
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.SubscriptionId);
81-
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
82-
}
83-
84-
[Fact]
85-
[Trait(Category.AcceptanceType, Category.CheckIn)]
86-
public void SelectAzureContextWithSubscriptionAndNoTenant()
87-
{
88-
var cmdlt = new SetAzureRMContextCommand();
89-
// Setup
90-
cmdlt.CommandRuntime = commandRuntimeMock;
91-
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
92-
93-
// Act
94-
cmdlt.InvokeBeginProcessing();
95-
cmdlt.ExecuteCmdlet();
96-
cmdlt.InvokeEndProcessing();
97-
98-
// Verify
99-
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
100-
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
101-
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.SubscriptionId);
102-
}
103-
10462
[Fact]
10563
[Trait(Category.AcceptanceType, Category.CheckIn)]
10664
public void SelectAzureContextWithNoSubscriptionAndTenant()

0 commit comments

Comments
 (0)