Search in sources :

Example 1 with ProviderResourceType

use of com.azure.resourcemanager.resources.models.ProviderResourceType in project azure-maven-plugins by microsoft.

the class AzureService method listSupportedRegions.

default List<Region> listSupportedRegions(String subscriptionId) {
    String[] names = StringUtils.split(name(), "/");
    if (names.length != 2) {
        throw new AzureToolkitRuntimeException(String.format("It is illegal to list regions for service '%s'.", name()));
    }
    final String provider = names[0];
    final String resourceType = names[1];
    List<Region> allRegionList = az(IAzureAccount.class).listRegions(subscriptionId);
    List<Region> result = new ArrayList<>();
    final ResourceManager resourceManager = getResourceManager(subscriptionId);
    resourceManager.providers().getByName(provider).resourceTypes().stream().filter(type -> StringUtils.equalsIgnoreCase(type.resourceType(), resourceType)).findAny().map(ProviderResourceType::locations).ifPresent(list -> result.addAll(list.stream().map(Region::fromName).filter(allRegionList::contains).collect(Collectors.toList())));
    return result.isEmpty() ? allRegionList : result;
}
Also used : IAzureBaseResource(com.microsoft.azure.toolkit.lib.common.entity.IAzureBaseResource) NettyAsyncHttpClientBuilder(com.azure.core.http.netty.NettyAsyncHttpClientBuilder) ProviderResourceType(com.azure.resourcemanager.resources.models.ProviderResourceType) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) StringUtils(org.apache.commons.lang3.StringUtils) HttpPipelinePolicy(com.azure.core.http.policy.HttpPipelinePolicy) ArrayList(java.util.ArrayList) Region(com.microsoft.azure.toolkit.lib.common.model.Region) NoopAddressResolverGroup(io.netty.resolver.NoopAddressResolverGroup) ProxyOptions(com.azure.core.http.ProxyOptions) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable) HttpLogDetailLevel(com.azure.core.http.policy.HttpLogDetailLevel) Subscription(com.microsoft.azure.toolkit.lib.common.model.Subscription) ResourceManager(com.azure.resourcemanager.resources.ResourceManager) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureProfile(com.azure.core.management.profile.AzureProfile) IAccount(com.microsoft.azure.toolkit.lib.account.IAccount) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Azure.az(com.microsoft.azure.toolkit.lib.Azure.az) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) List(java.util.List) HttpClient(com.azure.core.http.HttpClient) ProviderRegistrationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy) Optional(java.util.Optional) DefaultAddressResolverGroup(io.netty.resolver.DefaultAddressResolverGroup) Providers(com.azure.resourcemanager.resources.models.Providers) IAzureModule(com.microsoft.azure.toolkit.lib.common.entity.IAzureModule) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) ArrayList(java.util.ArrayList) Region(com.microsoft.azure.toolkit.lib.common.model.Region) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) ResourceManager(com.azure.resourcemanager.resources.ResourceManager)

Example 2 with ProviderResourceType

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

the class LocationCache method getLocation.

public static Set<String> getLocation(AzureResourceManager azureClient, String key) throws Exception {
    if (regions.containsKey(key) && !regions.get(key).isEmpty() && System.currentTimeMillis() < achieveTimeInMillis.get(key) + EXPIRE_TIME_IN_MILLIS) {
        return regions.get(key);
    } else {
        synchronized (LocationCache.class) {
            if (regions.containsKey(key) && !regions.get(key).isEmpty() && System.currentTimeMillis() < achieveTimeInMillis.get(key) + EXPIRE_TIME_IN_MILLIS) {
                return regions.get(key);
            } else {
                Provider byName = azureClient.providers().getByName("Microsoft.Compute");
                ProviderResourceType resourceType = byName.resourceTypes().stream().filter(type -> type.resourceType().equalsIgnoreCase("virtualMachines")).findFirst().orElse(null);
                if (resourceType == null) {
                    throw new RuntimeException("Virtual machines provider not registered");
                }
                Set<String> locations = new HashSet<>(resourceType.locations());
                achieveTimeInMillis.put(key, System.currentTimeMillis());
                regions.put(key, locations);
                return locations;
            }
        }
    }
}
Also used : ProviderResourceType(com.azure.resourcemanager.resources.models.ProviderResourceType) Provider(com.azure.resourcemanager.resources.models.Provider) HashSet(java.util.HashSet)

Aggregations

ProviderResourceType (com.azure.resourcemanager.resources.models.ProviderResourceType)2 HttpClient (com.azure.core.http.HttpClient)1 ProxyOptions (com.azure.core.http.ProxyOptions)1 NettyAsyncHttpClientBuilder (com.azure.core.http.netty.NettyAsyncHttpClientBuilder)1 HttpLogDetailLevel (com.azure.core.http.policy.HttpLogDetailLevel)1 HttpPipelinePolicy (com.azure.core.http.policy.HttpPipelinePolicy)1 AzureProfile (com.azure.core.management.profile.AzureProfile)1 ResourceManager (com.azure.resourcemanager.resources.ResourceManager)1 ProviderRegistrationPolicy (com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy)1 Provider (com.azure.resourcemanager.resources.models.Provider)1 Providers (com.azure.resourcemanager.resources.models.Providers)1 Azure.az (com.microsoft.azure.toolkit.lib.Azure.az)1 IAccount (com.microsoft.azure.toolkit.lib.account.IAccount)1 IAzureAccount (com.microsoft.azure.toolkit.lib.account.IAzureAccount)1 Cacheable (com.microsoft.azure.toolkit.lib.common.cache.Cacheable)1 IAzureBaseResource (com.microsoft.azure.toolkit.lib.common.entity.IAzureBaseResource)1 IAzureModule (com.microsoft.azure.toolkit.lib.common.entity.IAzureModule)1 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)1 Region (com.microsoft.azure.toolkit.lib.common.model.Region)1 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)1