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;
});
}
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`");
}
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;
}
Aggregations