Skip to content

Commit feb5369

Browse files
committed
Merge pull request Azure#1183 from hovsepm/dev
[#105517610] Added selection of first subscription (if there are any) if Set-AzureRmContext was called with only tenantId
2 parents 4b96991 + f3c185b commit feb5369

File tree

6 files changed

+142
-12
lines changed

6 files changed

+142
-12
lines changed

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

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,113 @@
2424
"Pragma": [
2525
"no-cache"
2626
],
27+
"x-ms-ratelimit-remaining-tenant-reads": [
28+
"14999"
29+
],
30+
"x-ms-request-id": [
31+
"e290eb91-61fb-486a-994d-6c7a34a93df7"
32+
],
33+
"x-ms-correlation-request-id": [
34+
"e290eb91-61fb-486a-994d-6c7a34a93df7"
35+
],
36+
"x-ms-routing-request-id": [
37+
"WESTUS:20151027T222509Z:e290eb91-61fb-486a-994d-6c7a34a93df7"
38+
],
39+
"Strict-Transport-Security": [
40+
"max-age=31536000; includeSubDomains"
41+
],
42+
"Cache-Control": [
43+
"no-cache"
44+
],
45+
"Date": [
46+
"Tue, 27 Oct 2015 22:25:08 GMT"
47+
]
48+
},
49+
"StatusCode": 200
50+
},
51+
{
52+
"RequestUri": "/subscriptions?api-version=2014-04-01-preview",
53+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnM/YXBpLXZlcnNpb249MjAxNC0wNC0wMS1wcmV2aWV3",
54+
"RequestMethod": "GET",
55+
"RequestBody": "",
56+
"RequestHeaders": {
57+
"User-Agent": [
58+
"Microsoft.Azure.Subscriptions.SubscriptionClient/2.0.0.0"
59+
]
60+
},
61+
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"id\": \"/subscriptions/00977cdb-163f-435f-9c32-39ec8ae61f4d\",\r\n \"subscriptionId\": \"00977cdb-163f-435f-9c32-39ec8ae61f4d\",\r\n \"displayName\": \"node\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n },\r\n {\r\n \"id\": \"/subscriptions/2c224e7e-3ef5-431d-a57b-e71f4662e3a6\",\r\n \"subscriptionId\": \"2c224e7e-3ef5-431d-a57b-e71f4662e3a6\",\r\n \"displayName\": \"Node CLI Test\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n },\r\n {\r\n \"id\": \"/subscriptions/6b085460-5f21-477e-ba44-1035046e9101\",\r\n \"subscriptionId\": \"6b085460-5f21-477e-ba44-1035046e9101\",\r\n \"displayName\": \"Azure SDK Infrastructure\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n },\r\n {\r\n \"id\": \"/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c\",\r\n \"subscriptionId\": \"db1ab6f0-4769-4b27-930e-01e2ef9c123c\",\r\n \"displayName\": \"Azure SDK sandbox\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n }\r\n ]\r\n}",
62+
"ResponseHeaders": {
63+
"Content-Length": [
64+
"1081"
65+
],
66+
"Content-Type": [
67+
"application/json; charset=utf-8"
68+
],
69+
"Expires": [
70+
"-1"
71+
],
72+
"Pragma": [
73+
"no-cache"
74+
],
75+
"x-ms-ratelimit-remaining-tenant-reads": [
76+
"14998"
77+
],
78+
"x-ms-request-id": [
79+
"9bc25859-ce5f-487b-a260-abcf61e181f1"
80+
],
81+
"x-ms-correlation-request-id": [
82+
"9bc25859-ce5f-487b-a260-abcf61e181f1"
83+
],
84+
"x-ms-routing-request-id": [
85+
"WESTUS:20151027T222509Z:9bc25859-ce5f-487b-a260-abcf61e181f1"
86+
],
87+
"Strict-Transport-Security": [
88+
"max-age=31536000; includeSubDomains"
89+
],
90+
"Cache-Control": [
91+
"no-cache"
92+
],
93+
"Date": [
94+
"Tue, 27 Oct 2015 22:25:09 GMT"
95+
]
96+
},
97+
"StatusCode": 200
98+
},
99+
{
100+
"RequestUri": "/subscriptions?api-version=2014-04-01-preview",
101+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnM/YXBpLXZlcnNpb249MjAxNC0wNC0wMS1wcmV2aWV3",
102+
"RequestMethod": "GET",
103+
"RequestBody": "",
104+
"RequestHeaders": {
105+
"User-Agent": [
106+
"Microsoft.Azure.Subscriptions.SubscriptionClient/2.0.0.0"
107+
]
108+
},
109+
"ResponseBody": "{\r\n \"value\": [\r\n {\r\n \"id\": \"/subscriptions/00977cdb-163f-435f-9c32-39ec8ae61f4d\",\r\n \"subscriptionId\": \"00977cdb-163f-435f-9c32-39ec8ae61f4d\",\r\n \"displayName\": \"node\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n },\r\n {\r\n \"id\": \"/subscriptions/2c224e7e-3ef5-431d-a57b-e71f4662e3a6\",\r\n \"subscriptionId\": \"2c224e7e-3ef5-431d-a57b-e71f4662e3a6\",\r\n \"displayName\": \"Node CLI Test\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n },\r\n {\r\n \"id\": \"/subscriptions/6b085460-5f21-477e-ba44-1035046e9101\",\r\n \"subscriptionId\": \"6b085460-5f21-477e-ba44-1035046e9101\",\r\n \"displayName\": \"Azure SDK Infrastructure\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n },\r\n {\r\n \"id\": \"/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c\",\r\n \"subscriptionId\": \"db1ab6f0-4769-4b27-930e-01e2ef9c123c\",\r\n \"displayName\": \"Azure SDK sandbox\",\r\n \"state\": \"Enabled\",\r\n \"subscriptionPolicies\": {\r\n \"locationPlacementId\": \"Internal_2014-09-01\",\r\n \"quotaId\": \"Internal_2014-09-01\"\r\n }\r\n }\r\n ]\r\n}",
110+
"ResponseHeaders": {
111+
"Content-Length": [
112+
"1081"
113+
],
114+
"Content-Type": [
115+
"application/json; charset=utf-8"
116+
],
117+
"Expires": [
118+
"-1"
119+
],
120+
"Pragma": [
121+
"no-cache"
122+
],
27123
"x-ms-ratelimit-remaining-tenant-reads": [
28124
"14997"
29125
],
30126
"x-ms-request-id": [
31-
"b56ad73a-247d-40a1-bc37-4962d0f0694e"
127+
"341c41a1-4b00-4082-872c-8eebcfbe8d77"
32128
],
33129
"x-ms-correlation-request-id": [
34-
"b56ad73a-247d-40a1-bc37-4962d0f0694e"
130+
"341c41a1-4b00-4082-872c-8eebcfbe8d77"
35131
],
36132
"x-ms-routing-request-id": [
37-
"WESTUS:20151026T230716Z:b56ad73a-247d-40a1-bc37-4962d0f0694e"
133+
"WESTUS:20151027T222510Z:341c41a1-4b00-4082-872c-8eebcfbe8d77"
38134
],
39135
"Strict-Transport-Security": [
40136
"max-age=31536000; includeSubDomains"
@@ -43,7 +139,7 @@
43139
"no-cache"
44140
],
45141
"Date": [
46-
"Mon, 26 Oct 2015 23:07:16 GMT"
142+
"Tue, 27 Oct 2015 22:25:09 GMT"
47143
]
48144
},
49145
"StatusCode": 200
@@ -76,13 +172,13 @@
76172
"14996"
77173
],
78174
"x-ms-request-id": [
79-
"ebe1c423-ed92-4e1d-9399-60233621d0c8"
175+
"c96fdf27-a910-4e8e-8cf7-c9f3b69d1521"
80176
],
81177
"x-ms-correlation-request-id": [
82-
"ebe1c423-ed92-4e1d-9399-60233621d0c8"
178+
"c96fdf27-a910-4e8e-8cf7-c9f3b69d1521"
83179
],
84180
"x-ms-routing-request-id": [
85-
"WESTUS:20151026T230716Z:ebe1c423-ed92-4e1d-9399-60233621d0c8"
181+
"WESTUS:20151027T222510Z:c96fdf27-a910-4e8e-8cf7-c9f3b69d1521"
86182
],
87183
"Strict-Transport-Security": [
88184
"max-age=31536000; includeSubDomains"
@@ -91,7 +187,7 @@
91187
"no-cache"
92188
],
93189
"Date": [
94-
"Mon, 26 Oct 2015 23:07:16 GMT"
190+
"Tue, 27 Oct 2015 22:25:10 GMT"
95191
]
96192
},
97193
"StatusCode": 200

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ function Test-SetAzureRmContextWithoutSubscription
9595
Set-AzureRmContext -TenantId $tenantId
9696
$context = Get-AzureRmContext
9797

98-
Assert-True { $context.Subscription -eq $null }
98+
Assert-True { $context.Subscription -ne $null }
9999
Assert-True { $context.Tenant -ne $null }
100100
Assert-AreEqual $context.Tenant.TenantId $firstSubscription.TenantId
101+
Assert-AreEqual $context.Subscription.SubscriptionId $firstSubscription.SubscriptionId
101102
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,23 @@ protected override void ProcessRecord()
104104
else
105105
{
106106
// authenticate with just tenant id
107-
var accessToken = profileClient.AcureAccessToken(tenantId);
108-
profileClient.SetCurrentContext(accessToken.TenantId);
107+
AzureTenant tenant;
108+
if(!profileClient.TryGetTenantAndSubscription(tenantId, null, out subscription, out tenant))
109+
{
110+
throw new ItemNotFoundException(
111+
string.Format(Resources.TenantIdNotFound, tenantId));
112+
}
113+
if (subscription == null)
114+
{
115+
profileClient.SetCurrentContext(tenant.Id.ToString());
116+
}
117+
else
118+
{
119+
profileClient.SetCurrentContext(
120+
subscription.Id.ToString(),
121+
tenant.Id.ToString(),
122+
verifySubscription: false);
123+
}
109124
}
110125
}
111126

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,21 @@ public List<AzureTenant> ListTenants(string tenant)
196196
}
197197

198198
public bool TryGetSubscription(string tenantId, string subscriptionId, out AzureSubscription subscription)
199+
{
200+
AzureTenant tenant;
201+
return TryGetTenantAndSubscription(tenantId, subscriptionId, out subscription, out tenant);
202+
}
203+
204+
public bool TryGetTenantAndSubscription(string tenantId, string subscriptionId, out AzureSubscription subscription, out AzureTenant tenant)
199205
{
200206
if (string.IsNullOrWhiteSpace(tenantId))
201207
{
202208
throw new ArgumentNullException("Please provide a valid tenant Id.");
203209
}
204210

205-
AzureTenant tenant;
206211
var token = AcquireAccessToken(_profile.Context.Account, _profile.Context.Environment,
207212
tenantId, null, ShowDialog.Never);
213+
208214
return TryGetTenantSubscription(token, _profile.Context.Account, _profile.Context.Environment,
209215
tenantId, subscriptionId, null, out subscription, out tenant);
210216
}

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

Lines changed: 9 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@
156156
<data name="TenantAuthFailed" xml:space="preserve">
157157
<value>Could not authenticate with tenant {0}. Please ensure that your account has access to this tenant and log in with Login-AzureRmAccount</value>
158158
</data>
159+
<data name="TenantIdNotFound" xml:space="preserve">
160+
<value>Unable to find tenant '{0}'.</value>
161+
</data>
159162
<data name="TypeNotAccessToken" xml:space="preserve">
160163
<value>To create an access token credential, you must provide an access token account.</value>
161164
</data>

0 commit comments

Comments
 (0)