Skip to content

[#105517610] Added selection of first subscription (if there are any) if Set-AzureRmContext was called with only tenantId #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,113 @@
"Pragma": [
"no-cache"
],
"x-ms-ratelimit-remaining-tenant-reads": [
"14999"
],
"x-ms-request-id": [
"e290eb91-61fb-486a-994d-6c7a34a93df7"
],
"x-ms-correlation-request-id": [
"e290eb91-61fb-486a-994d-6c7a34a93df7"
],
"x-ms-routing-request-id": [
"WESTUS:20151027T222509Z:e290eb91-61fb-486a-994d-6c7a34a93df7"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"Cache-Control": [
"no-cache"
],
"Date": [
"Tue, 27 Oct 2015 22:25:08 GMT"
]
},
"StatusCode": 200
},
{
"RequestUri": "/subscriptions?api-version=2014-04-01-preview",
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnM/YXBpLXZlcnNpb249MjAxNC0wNC0wMS1wcmV2aWV3",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"User-Agent": [
"Microsoft.Azure.Subscriptions.SubscriptionClient/2.0.0.0"
]
},
"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}",
"ResponseHeaders": {
"Content-Length": [
"1081"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Pragma": [
"no-cache"
],
"x-ms-ratelimit-remaining-tenant-reads": [
"14998"
],
"x-ms-request-id": [
"9bc25859-ce5f-487b-a260-abcf61e181f1"
],
"x-ms-correlation-request-id": [
"9bc25859-ce5f-487b-a260-abcf61e181f1"
],
"x-ms-routing-request-id": [
"WESTUS:20151027T222509Z:9bc25859-ce5f-487b-a260-abcf61e181f1"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"Cache-Control": [
"no-cache"
],
"Date": [
"Tue, 27 Oct 2015 22:25:09 GMT"
]
},
"StatusCode": 200
},
{
"RequestUri": "/subscriptions?api-version=2014-04-01-preview",
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnM/YXBpLXZlcnNpb249MjAxNC0wNC0wMS1wcmV2aWV3",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"User-Agent": [
"Microsoft.Azure.Subscriptions.SubscriptionClient/2.0.0.0"
]
},
"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}",
"ResponseHeaders": {
"Content-Length": [
"1081"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Pragma": [
"no-cache"
],
"x-ms-ratelimit-remaining-tenant-reads": [
"14997"
],
"x-ms-request-id": [
"b56ad73a-247d-40a1-bc37-4962d0f0694e"
"341c41a1-4b00-4082-872c-8eebcfbe8d77"
],
"x-ms-correlation-request-id": [
"b56ad73a-247d-40a1-bc37-4962d0f0694e"
"341c41a1-4b00-4082-872c-8eebcfbe8d77"
],
"x-ms-routing-request-id": [
"WESTUS:20151026T230716Z:b56ad73a-247d-40a1-bc37-4962d0f0694e"
"WESTUS:20151027T222510Z:341c41a1-4b00-4082-872c-8eebcfbe8d77"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
Expand All @@ -43,7 +139,7 @@
"no-cache"
],
"Date": [
"Mon, 26 Oct 2015 23:07:16 GMT"
"Tue, 27 Oct 2015 22:25:09 GMT"
]
},
"StatusCode": 200
Expand Down Expand Up @@ -76,13 +172,13 @@
"14996"
],
"x-ms-request-id": [
"ebe1c423-ed92-4e1d-9399-60233621d0c8"
"c96fdf27-a910-4e8e-8cf7-c9f3b69d1521"
],
"x-ms-correlation-request-id": [
"ebe1c423-ed92-4e1d-9399-60233621d0c8"
"c96fdf27-a910-4e8e-8cf7-c9f3b69d1521"
],
"x-ms-routing-request-id": [
"WESTUS:20151026T230716Z:ebe1c423-ed92-4e1d-9399-60233621d0c8"
"WESTUS:20151027T222510Z:c96fdf27-a910-4e8e-8cf7-c9f3b69d1521"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
Expand All @@ -91,7 +187,7 @@
"no-cache"
],
"Date": [
"Mon, 26 Oct 2015 23:07:16 GMT"
"Tue, 27 Oct 2015 22:25:10 GMT"
]
},
"StatusCode": 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ function Test-SetAzureRmContextWithoutSubscription
Set-AzureRmContext -TenantId $tenantId
$context = Get-AzureRmContext

Assert-True { $context.Subscription -eq $null }
Assert-True { $context.Subscription -ne $null }
Assert-True { $context.Tenant -ne $null }
Assert-AreEqual $context.Tenant.TenantId $firstSubscription.TenantId
Assert-AreEqual $context.Subscription.SubscriptionId $firstSubscription.SubscriptionId
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,23 @@ protected override void ProcessRecord()
else
{
// authenticate with just tenant id
var accessToken = profileClient.AcureAccessToken(tenantId);
profileClient.SetCurrentContext(accessToken.TenantId);
AzureTenant tenant;
if(!profileClient.TryGetTenantAndSubscription(tenantId, null, out subscription, out tenant))
{
throw new ItemNotFoundException(
string.Format(Resources.TenantIdNotFound, tenantId));
}
if (subscription == null)
{
profileClient.SetCurrentContext(tenant.Id.ToString());
}
else
{
profileClient.SetCurrentContext(
subscription.Id.ToString(),
tenant.Id.ToString(),
verifySubscription: false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,21 @@ public List<AzureTenant> ListTenants(string tenant)
}

public bool TryGetSubscription(string tenantId, string subscriptionId, out AzureSubscription subscription)
{
AzureTenant tenant;
return TryGetTenantAndSubscription(tenantId, subscriptionId, out subscription, out tenant);
}

public bool TryGetTenantAndSubscription(string tenantId, string subscriptionId, out AzureSubscription subscription, out AzureTenant tenant)
{
if (string.IsNullOrWhiteSpace(tenantId))
{
throw new ArgumentNullException("Please provide a valid tenant Id.");
}

AzureTenant tenant;
var token = AcquireAccessToken(_profile.Context.Account, _profile.Context.Environment,
tenantId, null, ShowDialog.Never);

return TryGetTenantSubscription(token, _profile.Context.Account, _profile.Context.Environment,
tenantId, subscriptionId, null, out subscription, out tenant);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<data name="TenantAuthFailed" xml:space="preserve">
<value>Could not authenticate with tenant {0}. Please ensure that your account has access to this tenant and log in with Login-AzureRmAccount</value>
</data>
<data name="TenantIdNotFound" xml:space="preserve">
<value>Unable to find tenant '{0}'.</value>
</data>
<data name="TypeNotAccessToken" xml:space="preserve">
<value>To create an access token credential, you must provide an access token account.</value>
</data>
Expand Down