private List <AzureTenant> ListAccountTenants( IAzureAccount account, IAzureEnvironment environment, SecureString password, string promptBehavior, Action <string> promptAction) { IList <AzureTenant> result = new List <AzureTenant>(); var commonTenant = account.GetCommonTenant(); try { var commonTenantToken = AcquireAccessToken( account, environment, commonTenant, password, promptBehavior, promptAction); result = SubscriptionAndTenantClient?.ListAccountTenants(commonTenantToken, environment); } catch { WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, commonTenant)); if (account.IsPropertySet(AzureAccount.Property.Tenants)) { result = account.GetPropertyAsArray(AzureAccount.Property.Tenants) .Select(ti => { var tenant = new AzureTenant(); Guid guid; if (Guid.TryParse(ti, out guid)) { tenant.Id = ti; tenant.Directory = AccessTokenExtensions.GetDomain(account.Id); } else { tenant.Directory = ti; } return(tenant); }).ToList(); } if (!result.Any()) { throw; } } return(result.ToList()); }
private IEnumerable <AzureSubscription> ListAllSubscriptionsForTenant( string tenantId) { IAzureAccount account = _profile.DefaultContext.Account; IAzureEnvironment environment = _profile.DefaultContext.Environment; SecureString password = null; string promptBehavior = ShowDialog.Never; IAccessToken accessToken = null; try { accessToken = AcquireAccessToken(account, environment, tenantId, password, promptBehavior, null); } catch { WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, tenantId)); return(new List <AzureSubscription>()); } return(SubscriptionAndTenantClient?.ListAllSubscriptionsForTenant(accessToken, account, environment)); }
private bool TryGetTenantSubscription(IAccessToken accessToken, IAzureAccount account, IAzureEnvironment environment, string subscriptionId, string subscriptionName, bool isTenantPresent, out IAzureSubscription subscription, out IAzureTenant tenant) { subscription = null; if (accessToken != null) { try { if (!string.IsNullOrEmpty(subscriptionId)) { subscription = SubscriptionAndTenantClient?.GetSubscriptionById(subscriptionId, accessToken, new AzureAccount { Id = accessToken.UserId }, environment); } else { var subscriptions = SubscriptionAndTenantClient?.ListAllSubscriptionsForTenant(accessToken, new AzureAccount { Id = accessToken.UserId }, environment)?.ToList() .Where(s => "enabled".Equals(s.State.ToString(), StringComparison.OrdinalIgnoreCase) || "warned".Equals(s.State.ToString(), StringComparison.OrdinalIgnoreCase)); account.SetProperty(AzureAccount.Property.Subscriptions, subscriptions.Select(i => i.GetId().ToString()).ToArray()); if (subscriptions.Any()) { if (!string.IsNullOrEmpty(subscriptionName)) { subscription = subscriptions.FirstOrDefault( s => s.Name.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase)); } else { if (subscriptions.Count() > 1) { WriteWarningMessage(string.Format( "TenantId '{0}' contains more than one active subscription. First one will be selected for further use. " + "To select another subscription, use Set-AzContext.", accessToken.TenantId)); } subscription = subscription ?? subscriptions.First(); } } } } catch (CloudException ex) { //Error "InvalidAuthenticationTokenTenant" means tenant and subscription mismatches. //If tenant is not present, we're iterating all tenants until finding right tenant for specified subscription, //in this case, InvalidAuthenticationTokenTenant message is expected and we should ignore it. if (isTenantPresent || !string.Equals(ex.Body?.Code, "InvalidAuthenticationTokenTenant", StringComparison.OrdinalIgnoreCase)) { WriteWarningMessage(ex.Message); } } if (subscription != null) { subscription.SetAccount(accessToken.UserId); subscription.SetEnvironment(environment.Name); subscription.SetTenant(accessToken.TenantId); tenant = new AzureTenant(); tenant.Id = accessToken.TenantId; return(true); } subscription = null; if (accessToken != null && accessToken.TenantId != null) { tenant = new AzureTenant(); tenant.Id = accessToken.TenantId; return(true); } } tenant = null; return(false); }
private bool TryGetTenantSubscription(IAccessToken accessToken, IAzureAccount account, IAzureEnvironment environment, string subscriptionId, string subscriptionName, out IAzureSubscription subscription, out IAzureTenant tenant) { subscription = null; if (accessToken != null) { try { if (!string.IsNullOrEmpty(subscriptionId)) { subscription = SubscriptionAndTenantClient?.GetSubscriptionById(subscriptionId, accessToken, new AzureAccount { Id = accessToken.UserId }, environment); } else { var subscriptions = SubscriptionAndTenantClient?.ListAllSubscriptionsForTenant(accessToken, new AzureAccount { Id = accessToken.UserId }, environment)?.ToList() .Where(s => "enabled".Equals(s.State.ToString(), StringComparison.OrdinalIgnoreCase) || "warned".Equals(s.State.ToString(), StringComparison.OrdinalIgnoreCase)); account.SetProperty(AzureAccount.Property.Subscriptions, subscriptions.Select(i => i.GetId().ToString()).ToArray()); if (subscriptions.Any()) { if (!string.IsNullOrEmpty(subscriptionName)) { subscription = subscriptions.FirstOrDefault( s => s.Name.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase)); } else { if (subscriptions.Count() > 1) { WriteWarningMessage(string.Format( "TenantId '{0}' contains more than one active subscription. First one will be selected for further use. " + "To select another subscription, use Set-AzContext.", accessToken.TenantId)); } subscription = subscription ?? subscriptions.First(); } } } } catch (CloudException ex) { WriteWarningMessage(ex.Message); } if (subscription != null) { subscription.SetAccount(accessToken.UserId); subscription.SetEnvironment(environment.Name); subscription.SetTenant(accessToken.TenantId); tenant = new AzureTenant(); tenant.Id = accessToken.TenantId; return(true); } subscription = null; if (accessToken != null && accessToken.TenantId != null) { tenant = new AzureTenant(); tenant.Id = accessToken.TenantId; return(true); } } tenant = null; return(false); }