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;
}
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;
}
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;
}
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;
}
Aggregations