Skip to content

Commit

Permalink
[github #4555][devops #1761663] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wangmingliang-ms committed Sep 3, 2020
1 parent 9c238a9 commit 7e3bb93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -145,6 +148,14 @@ protected String getTenantId() {
return delegateADAuthManager.getCommonTenantId();
}

public List<Subscription> getSubscriptions(String tenantId) {
return getSubscriptions(authTenant(tenantId));
}

public List<Tenant> getTenants(String tenantId) {
return getTenants(authTenant(tenantId));
}

@Override
public KeyVaultClient getKeyVaultClient(String tid) {
ServiceClientCredentials creds = new KeyVaultCredentials() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -108,29 +109,27 @@ protected <T extends AzureConfigurable<T>> T buildAzureManager(AzureConfigurable

@Override
public List<Subscription> getSubscriptions() throws IOException {
List<Subscription> sl = new LinkedList<Subscription>();
// could be multi tenant - return all subscriptions for the current account
List<Tenant> 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<Pair<Subscription, Tenant>> getSubscriptionsWithTenant() throws IOException {
List<Pair<Subscription, Tenant>> 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<Pair<Subscription, Tenant>> subscriptions = new LinkedList<>();
final Azure.Authenticated authentication = authTenant(getTenantId());
// could be multi tenant - return all subscriptions for the current account
final List<Tenant> tenants = getTenants(authentication);
for (Tenant tenant : tenants) {
final Azure.Authenticated tenantAuthentication = authTenant(tenant.tenantId());
final List<Subscription> tenantSubscriptions = getSubscriptions(tenantAuthentication);
for (Subscription subscription : tenantSubscriptions) {
subscriptions.add(new Pair<>(subscription, tenant));
}
}
return stl;
return subscriptions;
}

private List<Subscription> getSubscriptions(String tid) {
return authTenant(tid).subscriptions().listAsync()
protected List<Subscription> getSubscriptions(Azure.Authenticated tenantAuthentication) {
return tenantAuthentication.subscriptions().listAsync()
.onErrorResumeNext(err -> {
LOGGER.warning(err.getMessage());
return Observable.empty();
Expand All @@ -140,8 +139,8 @@ private List<Subscription> getSubscriptions(String tid) {
.singleOrDefault(Collections.emptyList());
}

private List<Tenant> getTenants(String tid) {
return authTenant(tid).tenants().listAsync()
protected List<Tenant> getTenants(Azure.Authenticated authentication) {
return authentication.tenants().listAsync()
.onErrorResumeNext(err -> {
LOGGER.warning(err.getMessage());
return Observable.empty();
Expand Down

0 comments on commit 7e3bb93

Please sign in to comment.