|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
15 | 15 | using Hyak.Common;
|
16 |
| -using Microsoft.Azure.Commands.ResourceManager.Common.Properties; |
17 | 16 | using Microsoft.Azure.Common.Authentication;
|
18 | 17 | using Microsoft.Azure.Common.Authentication.Factories;
|
19 | 18 | using Microsoft.Azure.Common.Authentication.Models;
|
20 | 19 | using Microsoft.Azure.Subscriptions;
|
21 | 20 | using System;
|
22 | 21 | using System.Collections.Generic;
|
23 | 22 | using System.Linq;
|
| 23 | +using System.Management.Automation; |
24 | 24 | using System.Security;
|
25 |
| -using System.Text; |
26 |
| -using System.Threading.Tasks; |
27 | 25 |
|
28 | 26 | namespace Microsoft.Azure.Commands.ResourceManager.Common
|
29 | 27 | {
|
30 | 28 | public class RMProfileClient
|
31 | 29 | {
|
32 | 30 | private AzureRMProfile _profile;
|
| 31 | + public Action<string> WarningLog; |
33 | 32 |
|
34 | 33 | public RMProfileClient(AzureRMProfile profile)
|
35 | 34 | {
|
36 | 35 | _profile = profile;
|
37 | 36 | }
|
38 | 37 |
|
39 |
| - public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId) |
| 38 | + public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, SecureString password) |
40 | 39 | {
|
41 |
| - var tenant = string.IsNullOrEmpty(tenantId) ? AuthenticationFactory.CommonAdTenant : tenantId; |
| 40 | + AzureSubscription newSubscription = null; |
| 41 | + AzureTenant newTenant = new AzureTenant(); |
42 | 42 |
|
43 |
| - var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(account, environment, tenant, null, ShowDialog.Auto); |
44 |
| - |
45 |
| - using (SubscriptionClient SubscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( |
46 |
| - new TokenCloudCredentials(commonTenantToken.AccessToken), |
47 |
| - environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement))) |
| 43 | + // (tenant and subscription are present) OR |
| 44 | + // (tenant is present and subscription is not provided) |
| 45 | + if (!string.IsNullOrEmpty(tenantId)) |
48 | 46 | {
|
49 |
| - var tenantListResult = SubscriptionClient.Tenants.List(); |
50 |
| - |
51 |
| - if (!string.IsNullOrEmpty(tenantId) && !tenantListResult.TenantIds.Any(s => s.TenantId.Equals(tenantId, StringComparison.OrdinalIgnoreCase))) |
| 47 | + newTenant.Id = new Guid(tenantId); |
| 48 | + ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never; |
| 49 | + TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription); |
| 50 | + } |
| 51 | + // (tenant is not provided and subscription is present) OR |
| 52 | + // (tenant is not provided and subscription is not provided) |
| 53 | + else |
| 54 | + { |
| 55 | + foreach(var tenant in ListAccountTenants(account, environment, password)) |
52 | 56 | {
|
53 |
| - throw new ArgumentException(string.Format(Resources.TenantNotFound, tenantId)); |
| 57 | + if (TryGetTenantSubscription(account, environment, tenant, subscriptionId, password, ShowDialog.Auto, out newSubscription)) |
| 58 | + { |
| 59 | + newTenant.Id = new Guid(tenant); |
| 60 | + break; |
| 61 | + } |
54 | 62 | }
|
55 | 63 |
|
56 |
| - _profile. |
57 |
| - //ListResourceManagerSubscriptions |
58 |
| - |
| 64 | + } |
59 | 65 |
|
60 |
| - //_profile.DefaultContext = new AzureContext(); |
| 66 | + if (newSubscription == null) |
| 67 | + { |
| 68 | + throw new PSInvalidOperationException("Subscription was not found."); |
61 | 69 | }
|
62 | 70 |
|
| 71 | + _profile.DefaultContext = new AzureContext(newSubscription, account, environment, newTenant); |
| 72 | + |
63 | 73 | return _profile;
|
64 | 74 | }
|
65 | 75 |
|
66 |
| - public IEnumerable<string> GetTenantSubscriptions(AzureAccount account, AzureEnvironment environment, string tenantId, SecureString password) |
| 76 | + private bool TryGetTenantSubscription( |
| 77 | + AzureAccount account, |
| 78 | + AzureEnvironment environment, |
| 79 | + string tenantId, |
| 80 | + string subscriptionId, |
| 81 | + SecureString password, |
| 82 | + ShowDialog promptBehavior, |
| 83 | + out AzureSubscription subscription) |
67 | 84 | {
|
68 |
| - try |
| 85 | + var accessToken = AzureSession.AuthenticationFactory.Authenticate( |
| 86 | + account, |
| 87 | + environment, |
| 88 | + tenantId, |
| 89 | + password, |
| 90 | + promptBehavior); |
| 91 | + using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( |
| 92 | + new TokenCloudCredentials(accessToken.AccessToken), |
| 93 | + environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) |
69 | 94 | {
|
70 |
| - var tenantAccount = new AzureAccount(); |
71 |
| - CopyAccount(account, tenantAccount); |
72 |
| - var tenantToken = AzureSession.AuthenticationFactory.Authenticate(tenantAccount, environment, tenantId, password, ShowDialog.Never); |
73 |
| - if (string.Equals(tenantAccount.Id, account.Id, StringComparison.InvariantCultureIgnoreCase)) |
| 95 | + Subscriptions.Models.Subscription subscriptionFromServer = null; |
| 96 | + |
| 97 | + try |
74 | 98 | {
|
75 |
| - tenantAccount = account; |
| 99 | + if (subscriptionId != null) |
| 100 | + { |
| 101 | + subscriptionFromServer = subscriptionClient.Subscriptions.Get(subscriptionId).Subscription; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions; |
| 106 | + if (subscriptions != null) |
| 107 | + { |
| 108 | + if (subscriptions.Count > 1) |
| 109 | + { |
| 110 | + WriteWarningMessage(string.Format( |
| 111 | + "Tenant '{0}' contains more than one subscription. First one will be selected for further use.", |
| 112 | + tenantId)); |
| 113 | + } |
| 114 | + subscriptionFromServer = subscriptions.First(); |
| 115 | + } |
| 116 | + } |
76 | 117 | }
|
77 |
| - |
78 |
| - tenantAccount.SetOrAppendProperty(AzureAccount.Property.Tenants, new string[] { tenantId }); |
79 |
| - |
80 |
| - using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( |
81 |
| - new TokenCloudCredentials(tenantToken.AccessToken), |
82 |
| - environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) |
| 118 | + catch (CloudException ex) |
83 | 119 | {
|
84 |
| - var subscriptionListResult = subscriptionClient.Subscriptions.List(); |
85 |
| - |
86 |
| - return subscriptionListResult.Subscriptions.Select(s => s.SubscriptionId); |
| 120 | + WriteWarningMessage(ex.Message); |
87 | 121 | }
|
88 | 122 |
|
| 123 | + if (subscriptionFromServer != null) |
| 124 | + { |
| 125 | + subscription = new AzureSubscription |
| 126 | + { |
| 127 | + Id = new Guid(subscriptionFromServer.SubscriptionId), |
| 128 | + Account = accessToken.UserId, |
| 129 | + Environment = environment.Name, |
| 130 | + Name = subscriptionFromServer.DisplayName, |
| 131 | + Properties = new Dictionary<AzureSubscription.Property, string> { { AzureSubscription.Property.Tenants, accessToken.TenantId } } |
| 132 | + }; |
| 133 | + return true; |
| 134 | + } |
89 | 135 |
|
90 |
| - } |
91 |
| - catch (CloudException cEx) |
92 |
| - { |
93 |
| - WriteOrThrowAadExceptionMessage(cEx); |
94 |
| - } |
95 |
| - catch (AadAuthenticationException aadEx) |
96 |
| - { |
97 |
| - WriteOrThrowAadExceptionMessage(aadEx); |
| 136 | + subscription = null; |
| 137 | + return false; |
98 | 138 | }
|
99 | 139 | }
|
100 | 140 |
|
101 |
| - private void CopyAccount(AzureAccount sourceAccount, AzureAccount targetAccount) |
| 141 | + private string[] ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password) |
102 | 142 | {
|
103 |
| - targetAccount.Id = sourceAccount.Id; |
104 |
| - targetAccount.Type = sourceAccount.Type; |
105 |
| - } |
| 143 | + ShowDialog promptBehavior = password == null ? ShowDialog.Always : ShowDialog.Never; |
106 | 144 |
|
107 |
| - private void WriteOrThrowAadExceptionMessage(AadAuthenticationException aadEx) |
108 |
| - { |
109 |
| - if (aadEx is AadAuthenticationFailedWithoutPopupException) |
110 |
| - { |
111 |
| - WriteDebugMessage(aadEx.Message); |
112 |
| - } |
113 |
| - else if (aadEx is AadAuthenticationCanceledException) |
| 145 | + var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(account, environment, |
| 146 | + AuthenticationFactory.CommonAdTenant, password, promptBehavior); |
| 147 | + |
| 148 | + using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( |
| 149 | + new TokenCloudCredentials(commonTenantToken.AccessToken), |
| 150 | + environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) |
114 | 151 | {
|
115 |
| - WriteWarningMessage(aadEx.Message); |
| 152 | + return subscriptionClient.Tenants.List().TenantIds.Select(ti => ti.TenantId).ToArray(); |
116 | 153 | }
|
117 |
| - else |
| 154 | + } |
| 155 | + |
| 156 | + private void WriteWarningMessage(string message) |
| 157 | + { |
| 158 | + if (WarningLog != null) |
118 | 159 | {
|
119 |
| - throw aadEx; |
| 160 | + WarningLog(message); |
120 | 161 | }
|
121 | 162 | }
|
122 | 163 | }
|
|
0 commit comments