Search in sources :

Example 1 with AzureCliSubscription

use of com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription in project azure-maven-plugins by microsoft.

the class AzureCliAccount method preLoginCheck.

protected Mono<Boolean> preLoginCheck() {
    return Mono.fromCallable(() -> {
        AzureCliUtils.ensureMinimumCliVersion();
        AzureCliUtils.executeAzureCli("az account get-access-token --output json");
        List<AzureCliSubscription> subscriptions = AzureCliUtils.listSubscriptions();
        if (subscriptions.isEmpty()) {
            throw new AzureToolkitAuthenticationException("Cannot find any subscriptions in current account.");
        }
        AzureCliSubscription defaultSubscription = subscriptions.stream().filter(AzureCliSubscription::isSelected).findFirst().orElse(subscriptions.get(0));
        AzureEnvironment configEnv = Azure.az(AzureCloud.class).get();
        if (configEnv != null && defaultSubscription.getEnvironment() != configEnv) {
            throw new AzureToolkitAuthenticationException(String.format("The azure cloud from azure cli '%s' doesn't match with your auth configuration, " + "you can change it by executing 'az cloud set --name=%s' command to change the cloud in azure cli.", AzureEnvironmentUtils.getCloudNameForAzureCli(defaultSubscription.getEnvironment()), AzureEnvironmentUtils.getCloudNameForAzureCli(configEnv)));
        }
        this.entity.setEnvironment(defaultSubscription.getEnvironment());
        this.entity.setEmail(defaultSubscription.getEmail());
        subscriptions = subscriptions.stream().filter(s -> StringUtils.equals(this.entity.getEmail(), s.getEmail())).collect(Collectors.toList());
        // use the tenant who has one or more subscriptions
        this.entity.setTenantIds(subscriptions.stream().map(Subscription::getTenantId).distinct().collect(Collectors.toList()));
        this.entity.setSubscriptions(subscriptions.stream().filter(distinctByKey(t -> StringUtils.lowerCase(t.getId()))).map(AzureCliAccount::toSubscription).collect(Collectors.toList()));
        // set initial selection of subscriptions
        this.entity.setSelectedSubscriptionIds(subscriptions.stream().filter(Subscription::isSelected).map(Subscription::getId).distinct().collect(Collectors.toList()));
        return true;
    });
}
Also used : AzureEnvironment(com.azure.core.management.AzureEnvironment) AzureCloud(com.microsoft.azure.toolkit.lib.auth.AzureCloud) AzureCliSubscription(com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription) AzureCliSubscription(com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)

Example 2 with AzureCliSubscription

use of com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription in project azure-maven-plugins by microsoft.

the class AzureCliUtils method listSubscriptions.

@Nonnull
public static List<AzureCliSubscription> listSubscriptions() {
    final String jsonString = executeAzureCli("az account list --output json");
    final JsonArray result = JsonUtils.getGson().fromJson(jsonString, JsonArray.class);
    final List<AzureCliSubscription> list = new ArrayList<>();
    if (result != null) {
        result.forEach(j -> {
            JsonObject accountObject = j.getAsJsonObject();
            if (!accountObject.has("id")) {
                return;
            }
            // TODO: use utility to handle the json mapping
            String tenantId = accountObject.get("tenantId").getAsString();
            String subscriptionId = accountObject.get("id").getAsString();
            String subscriptionName = accountObject.get("name").getAsString();
            String state = accountObject.get("state").getAsString();
            String cloud = accountObject.get("cloudName").getAsString();
            String email = accountObject.get("user").getAsJsonObject().get("name").getAsString();
            if (StringUtils.equals(state, "Enabled") && StringUtils.isNoneBlank(subscriptionId, subscriptionName)) {
                AzureCliSubscription entity = new AzureCliSubscription();
                entity.setId(subscriptionId);
                entity.setName(subscriptionName);
                entity.setSelected(accountObject.get("isDefault").getAsBoolean());
                entity.setTenantId(tenantId);
                entity.setEmail(email);
                entity.setEnvironment(AzureEnvironmentUtils.stringToAzureEnvironment(cloud));
                list.add(entity);
            }
        });
        return list;
    }
    throw new AzureToolkitAuthenticationException("list subscriptions by command `az account list` failed, please make sure you have signed in Azure Cli using `az login`");
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) AzureCliSubscription(com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription) AzureToolkitAuthenticationException(com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException) Nonnull(javax.annotation.Nonnull)

Example 3 with AzureCliSubscription

use of com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription in project azure-maven-plugins by microsoft.

the class AzureCliAccount method toSubscription.

private static Subscription toSubscription(AzureCliSubscription s) {
    Subscription subscription = new Subscription();
    subscription.setId(s.getId());
    subscription.setName(s.getName());
    subscription.setSelected(s.isSelected());
    subscription.setTenantId(s.getTenantId());
    return subscription;
}
Also used : AzureCliSubscription(com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription)

Aggregations

AzureCliSubscription (com.microsoft.azure.toolkit.lib.auth.model.AzureCliSubscription)3 AzureToolkitAuthenticationException (com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException)2 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)2 AzureEnvironment (com.azure.core.management.AzureEnvironment)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 AzureCloud (com.microsoft.azure.toolkit.lib.auth.AzureCloud)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1