diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AccessTokenAzureManager.java b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AccessTokenAzureManager.java index 410a95cf91..0183a0489f 100644 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AccessTokenAzureManager.java +++ b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AccessTokenAzureManager.java @@ -27,6 +27,8 @@ import com.microsoft.azure.management.Azure; import com.microsoft.azure.management.applicationinsights.v2015_05_01.implementation.InsightsManager; import com.microsoft.azure.management.appplatform.v2019_05_01_preview.implementation.AppPlatformManager; +import com.microsoft.azure.management.resources.Subscription; +import com.microsoft.azure.management.resources.Tenant; import com.microsoft.azuretools.adauth.PromptBehavior; import com.microsoft.azuretools.adauth.StringUtils; import com.microsoft.azuretools.authmanage.AdAuthManagerBuilder; @@ -41,6 +43,7 @@ import com.microsoft.rest.credentials.ServiceClientCredentials; import java.io.IOException; +import java.util.List; import static com.microsoft.azuretools.Constants.FILE_NAME_SUBSCRIPTIONS_DETAILS_AT; import static org.apache.commons.lang3.StringUtils.isBlank; @@ -145,6 +148,14 @@ protected String getTenantId() { return delegateADAuthManager.getCommonTenantId(); } + public List getSubscriptions(String tenantId) { + return getSubscriptions(authTenant(tenantId)); + } + + public List getTenants(String tenantId) { + return getTenants(authTenant(tenantId)); + } + @Override public KeyVaultClient getKeyVaultClient(String tid) { ServiceClientCredentials creds = new KeyVaultCredentials() { diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java index cee7bd5b55..1bd385be24 100644 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java +++ b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java @@ -44,6 +44,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; +import java.util.stream.Collectors; import static com.microsoft.azuretools.authmanage.Environment.*; @@ -108,29 +109,27 @@ protected > T buildAzureManager(AzureConfigurable @Override public List getSubscriptions() throws IOException { - List sl = new LinkedList(); - // could be multi tenant - return all subscriptions for the current account - List tl = getTenants(getTenantId()); - for (Tenant t : tl) { - sl.addAll(getSubscriptions(t.tenantId())); - } - return sl; + return getSubscriptionsWithTenant().stream().map(Pair::first).collect(Collectors.toList()); } @Override public List> getSubscriptionsWithTenant() throws IOException { - List> stl = new LinkedList<>(); - for (Tenant t : getTenants(getTenantId())) { - String tid = t.tenantId(); - for (Subscription s : getSubscriptions(tid)) { - stl.add(new Pair<>(s, t)); + final List> subscriptions = new LinkedList<>(); + final Azure.Authenticated authentication = authTenant(getTenantId()); + // could be multi tenant - return all subscriptions for the current account + final List tenants = getTenants(authentication); + for (Tenant tenant : tenants) { + final Azure.Authenticated tenantAuthentication = authTenant(tenant.tenantId()); + final List tenantSubscriptions = getSubscriptions(tenantAuthentication); + for (Subscription subscription : tenantSubscriptions) { + subscriptions.add(new Pair<>(subscription, tenant)); } } - return stl; + return subscriptions; } - private List getSubscriptions(String tid) { - return authTenant(tid).subscriptions().listAsync() + protected List getSubscriptions(Azure.Authenticated tenantAuthentication) { + return tenantAuthentication.subscriptions().listAsync() .onErrorResumeNext(err -> { LOGGER.warning(err.getMessage()); return Observable.empty(); @@ -140,8 +139,8 @@ private List getSubscriptions(String tid) { .singleOrDefault(Collections.emptyList()); } - private List getTenants(String tid) { - return authTenant(tid).tenants().listAsync() + protected List getTenants(Azure.Authenticated authentication) { + return authentication.tenants().listAsync() .onErrorResumeNext(err -> { LOGGER.warning(err.getMessage()); return Observable.empty();