Search in sources :

Example 1 with Tenant

use of com.microsoft.azure.management.resources.Tenant in project azure-tools-for-java by Microsoft.

the class AccessTokenAzureManager method getSubscriptions.

@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("common");
    for (Tenant t : tl) {
        sl.addAll(getSubscriptions(t.tenantId()));
    }
    return sl;
}
Also used : Tenant(com.microsoft.azure.management.resources.Tenant) Subscription(com.microsoft.azure.management.resources.Subscription) LinkedList(java.util.LinkedList)

Example 2 with Tenant

use of com.microsoft.azure.management.resources.Tenant in project azure-tools-for-java by Microsoft.

the class AdAuthManager method signIn.

public AuthenticationResult signIn() throws IOException {
    // build token cache for azure and graph api
    // using azure sdk directly
    cleanCache();
    String commonTid = "common";
    AuthContext ac = new AuthContext(String.format("%s/%s", Constants.authority, commonTid), cache);
    AuthenticationResult result = ac.acquireToken(AzureEnvironment.AZURE.resourceManagerEndpoint(), Constants.clientId, Constants.redirectUri, PromptBehavior.Always, null);
    String displayableId = result.getUserInfo().getDisplayableId();
    UserIdentifier uid = new UserIdentifier(displayableId, UserIdentifierType.RequiredDisplayableId);
    Map<String, List<String>> tidToSidsMap = new HashMap<>();
    //        List<Tenant> tenants = AccessTokenAzureManager.authTid(commonTid).tenants().list();
    List<Tenant> tenants = AccessTokenAzureManager.getTenants(commonTid);
    for (Tenant t : tenants) {
        String tid = t.tenantId();
        AuthContext ac1 = new AuthContext(String.format("%s/%s", Constants.authority, tid), cache);
        // put tokens into the cache
        ac1.acquireToken(AzureEnvironment.AZURE.resourceManagerEndpoint(), Constants.clientId, Constants.redirectUri, PromptBehavior.Auto, uid);
        ac1.acquireToken(AzureEnvironment.AZURE.graphEndpoint(), Constants.clientId, Constants.redirectUri, PromptBehavior.Auto, uid);
        ac1.acquireToken(Constants.resourceVault, Constants.clientId, Constants.redirectUri, PromptBehavior.Auto, uid);
        List<String> sids = new LinkedList<>();
        for (Subscription s : AccessTokenAzureManager.getSubscriptions(tid)) {
            sids.add(s.subscriptionId());
        }
        tidToSidsMap.put(t.tenantId(), sids);
    }
    // save account email
    String accountEmail = displayableId;
    if (accountEmail == null) {
        throw new IllegalArgumentException("accountEmail is null");
    }
    adAuthDetails.setAccountEmail(accountEmail);
    adAuthDetails.setTidToSidsMap(tidToSidsMap);
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Tenant(com.microsoft.azure.management.resources.Tenant) List(java.util.List) LinkedList(java.util.LinkedList) Subscription(com.microsoft.azure.management.resources.Subscription)

Example 3 with Tenant

use of com.microsoft.azure.management.resources.Tenant in project azure-tools-for-java by Microsoft.

the class SubscriptionManager method updateAccountSubscriptionList.

protected List<SubscriptionDetail> updateAccountSubscriptionList() throws IOException {
    System.out.println(Thread.currentThread().getId() + " SubscriptionManager.updateAccountSubscriptionList()");
    if (azureManager == null) {
        throw new IllegalArgumentException("azureManager is null");
    }
    System.out.println("Getting subscription list from Azure");
    List<SubscriptionDetail> sdl = new ArrayList<>();
    List<Pair<Subscription, Tenant>> stpl = azureManager.getSubscriptionsWithTenant();
    for (Pair<Subscription, Tenant> stp : stpl) {
        sdl.add(new SubscriptionDetail(stp.first().subscriptionId(), stp.first().displayName(), stp.second().tenantId(), true));
    }
    return sdl;
}
Also used : Tenant(com.microsoft.azure.management.resources.Tenant) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription) Pair(com.microsoft.azuretools.utils.Pair)

Example 4 with Tenant

use of com.microsoft.azure.management.resources.Tenant in project azure-tools-for-java by Microsoft.

the class AzureManagerBase method getSubscriptionsWithTenant.

@Override
@AzureOperation(name = "account|subscription.list.tenant|authorized", type = AzureOperation.Type.SERVICE)
public List<Pair<Subscription, Tenant>> getSubscriptionsWithTenant() {
    final List<Pair<Subscription, Tenant>> subscriptions = new LinkedList<>();
    final Azure.Authenticated authentication = authTenant(getCurrentTenantId());
    // could be multi tenant - return all subscriptions for the current account
    final List<Tenant> tenants = getTenants(authentication);
    final List<String> failedTenantIds = new ArrayList<>();
    for (final Tenant tenant : tenants) {
        try {
            final Azure.Authenticated tenantAuthentication = authTenant(tenant.tenantId());
            final List<Subscription> tenantSubscriptions = getSubscriptions(tenantAuthentication);
            for (final Subscription subscription : tenantSubscriptions) {
                subscriptions.add(new Pair<>(subscription, tenant));
            }
        } catch (final Exception e) {
            // just skip for cases user failing to get subscriptions of tenants he/she has no permission to get access token.
            LOGGER.log(Level.WARNING, e.getMessage(), e);
            failedTenantIds.add(tenant.tenantId());
        }
    }
    if (!failedTenantIds.isEmpty()) {
        final INotification nw = CommonSettings.getUiFactory().getNotificationWindow();
        nw.deliver("Lack permission for some tenants", "You don't have permission on the tenant(s): " + StringUtils.join(failedTenantIds, ","));
    }
    return subscriptions;
}
Also used : Azure(com.microsoft.azure.management.Azure) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Tenant(com.microsoft.azure.management.resources.Tenant) INotification(com.microsoft.azuretools.authmanage.interact.INotification) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) Pair(com.microsoft.azuretools.utils.Pair) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

Tenant (com.microsoft.azure.management.resources.Tenant)4 Subscription (com.microsoft.azure.management.resources.Subscription)3 LinkedList (java.util.LinkedList)3 Pair (com.microsoft.azuretools.utils.Pair)2 Azure (com.microsoft.azure.management.Azure)1 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)1 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)1 INotification (com.microsoft.azuretools.authmanage.interact.INotification)1 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1