Search in sources :

Example 1 with Subscription

use of com.azure.resourcemanager.resources.models.Subscription in project azure-vm-agents-plugin by jenkinsci.

the class AzureUtil method isValidSubscriptionId.

/**
 * Checks if the subscription id is valid in the current Azure client.
 *
 * @param credentialId credentials used to create an Azure client
 * @param subscriptionId target subscription id
 * @return true, if the subscription id is valid
 */
public static boolean isValidSubscriptionId(String credentialId, String subscriptionId) {
    if (StringUtils.isEmpty(subscriptionId)) {
        return true;
    }
    AzureResourceManager defaultClient = AzureResourceManagerCache.get(credentialId);
    PagedIterable<Subscription> subscriptions = defaultClient.subscriptions().list();
    boolean isSubscriptionIdValid = false;
    for (Subscription subscription : subscriptions) {
        if (subscription.subscriptionId().equals(subscriptionId)) {
            isSubscriptionIdValid = true;
        }
    }
    return isSubscriptionIdValid;
}
Also used : AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) Subscription(com.azure.resourcemanager.resources.models.Subscription)

Example 2 with Subscription

use of com.azure.resourcemanager.resources.models.Subscription in project azure-credentials-plugin by jenkinsci.

the class AzureImdsCredentials method validate.

public boolean validate() throws AzureCredentials.ValidationException {
    try {
        final String credentialSubscriptionId = getSubscriptionId();
        AzureProfile profile = new AzureProfile(AzureEnvUtil.resolveAzureEnv(getAzureEnvName()));
        ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder().build();
        AzureResourceManager azure = AzureResourceManager.configure().withHttpClient(HttpClientRetriever.get()).authenticate(credential, profile).withSubscription(credentialSubscriptionId);
        PagedIterable<Subscription> subscriptions = azure.subscriptions().list();
        if (subscriptionId != null) {
            for (Subscription subscription : subscriptions) {
                if (subscription.subscriptionId().equalsIgnoreCase(credentialSubscriptionId)) {
                    return true;
                }
            }
        } else {
            return true;
        }
    } catch (Exception e) {
        throw new AzureCredentials.ValidationException(Messages.Azure_CantValidate() + ": " + e.getMessage(), e);
    }
    throw new AzureCredentials.ValidationException(Messages.Azure_Invalid_SubscriptionId());
}
Also used : AzureProfile(com.azure.core.management.profile.AzureProfile) ManagedIdentityCredential(com.azure.identity.ManagedIdentityCredential) AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) Subscription(com.azure.resourcemanager.resources.models.Subscription) ManagedIdentityCredentialBuilder(com.azure.identity.ManagedIdentityCredentialBuilder)

Aggregations

AzureResourceManager (com.azure.resourcemanager.AzureResourceManager)2 Subscription (com.azure.resourcemanager.resources.models.Subscription)2 AzureProfile (com.azure.core.management.profile.AzureProfile)1 ManagedIdentityCredential (com.azure.identity.ManagedIdentityCredential)1 ManagedIdentityCredentialBuilder (com.azure.identity.ManagedIdentityCredentialBuilder)1