add resource lib: resource group

This commit is contained in:
andxu 2021-05-08 08:27:01 +08:00
Родитель 7da8e9f5bc
Коммит 0b5e5ede72
5 изменённых файлов: 201 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>azure-toolkit-libs</artifactId>
<groupId>com.microsoft.azure</groupId>
<version>0.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>azure-toolkit-resource-lib</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<show>private</show>
<failOnError>false</failOnError>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalparam>${javadoc.opts}</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-toolkit-common-lib</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-toolkit-auth-lib</artifactId>
</dependency>
</dependencies>
</project>

Просмотреть файл

@ -0,0 +1,96 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
package com.microsoft.azure.toolkit.lib.resource;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.resources.ResourceManager;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceId;
import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.AzureConfiguration;
import com.microsoft.azure.toolkit.lib.AzureService;
import com.microsoft.azure.toolkit.lib.SubscriptionScoped;
import com.microsoft.azure.toolkit.lib.auth.Account;
import com.microsoft.azure.toolkit.lib.auth.AzureAccount;
import com.microsoft.azure.toolkit.lib.common.cache.Cacheable;
import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException;
import com.microsoft.azure.toolkit.lib.common.model.Subscription;
import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Flux;
import javax.annotation.Nonnull;
import java.util.List;
public class AzureGroup extends SubscriptionScoped<AzureGroup> implements AzureService {
public AzureGroup() { // for SPI
super(AzureGroup::new);
}
private AzureGroup(@Nonnull final List<Subscription> subscriptions) {
super(AzureGroup::new, subscriptions);
}
public List<ResourceGroupEntity> list() {
return Flux.fromIterable(getSubscriptions()).parallel()
.map(subscription -> getResourceManager(subscription.getId()))
.flatMap(azureResourceManager -> azureResourceManager.resourceGroups().listAsync())
.map(ResourceGroupEntity::fromResource)
.sequential().collectList().block();
}
public ResourceGroupEntity getById(@Nonnull String idStr) {
final ResourceId id = ResourceId.fromString(idStr);
return get(id.subscriptionId(), id.resourceGroupName());
}
public ResourceGroupEntity getByName(@Nonnull String name) {
return get(getDefaultSubscription().getId(), name);
}
public ResourceGroupEntity get(@Nonnull String subscriptionId, @Nonnull String name) {
return ResourceGroupEntity.fromResource(getResourceManager(subscriptionId).resourceGroups().getByName(name));
}
public ResourceGroupEntity create(String name, String region) {
if (StringUtils.isNoneBlank(name, region)) {
final com.azure.resourcemanager.resources.models.ResourceGroup result = getResourceManager(getDefaultSubscription().getId())
.resourceGroups().define(name)
.withRegion(region).create();
return ResourceGroupEntity.fromResource(result);
}
throw new AzureToolkitRuntimeException("Please provide both name and region to create a resource group.");
}
public void delete(String name) {
getResourceManager(getDefaultSubscription().getId())
.resourceGroups().deleteByName(name);
}
@Cacheable(cacheName = "ResourceManager", key = "$subscriptionId")
public ResourceManager getResourceManager(String subscriptionId) {
final Account account = Azure.az(AzureAccount.class).account();
final AzureConfiguration config = Azure.az().config();
final String userAgent = config.getUserAgent();
final HttpLogDetailLevel logDetailLevel = config.getLogLevel() == null ?
HttpLogDetailLevel.NONE : HttpLogDetailLevel.valueOf(config.getLogLevel());
final AzureProfile azureProfile = new AzureProfile(account.getEnvironment());
return ResourceManager.configure()
.withLogLevel(logDetailLevel)
.withPolicy(getUserAgentPolicy(userAgent)) // set user agent with policy
.authenticate(account.getTokenCredential(subscriptionId), azureProfile)
.withSubscription(subscriptionId);
}
private HttpPipelinePolicy getUserAgentPolicy(String userAgent) {
return (httpPipelineCallContext, httpPipelineNextPolicy) -> {
final String previousUserAgent = httpPipelineCallContext.getHttpRequest().getHeaders().getValue("User-Agent");
httpPipelineCallContext.getHttpRequest().setHeader("User-Agent", String.format("%s %s", userAgent, previousUserAgent));
return httpPipelineNextPolicy.process();
};
}
}

Просмотреть файл

@ -0,0 +1,44 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
package com.microsoft.azure.toolkit.lib.resource;
import com.azure.resourcemanager.resources.models.ResourceGroup;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceId;
import com.microsoft.azure.toolkit.lib.common.entity.IAzureResourceEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import javax.annotation.Nonnull;
@Getter
@Setter(AccessLevel.PRIVATE)
@SuperBuilder(toBuilder = true)
public class ResourceGroupEntity implements IAzureResourceEntity {
private final String subscriptionId;
private final String name;
private final String id;
private final String region;
@Getter(AccessLevel.PACKAGE)
private ResourceGroup inner;
private ResourceGroupEntity(@Nonnull ResourceGroup resource) {
this.inner = resource;
final ResourceId resourceId = ResourceId.fromString(this.inner.id());
this.subscriptionId = resourceId.subscriptionId();
this.name = resource.name();
this.region = resource.regionName();
this.id = resource.id();
}
public static ResourceGroupEntity fromResource(@Nonnull ResourceGroup resource) {
return new ResourceGroupEntity(resource);
}
}

Просмотреть файл

@ -0,0 +1 @@
com.microsoft.azure.toolkit.lib.resource.AzureGroup

Просмотреть файл

@ -36,6 +36,7 @@
<modules>
<module>azure-toolkit-common-lib</module>
<module>azure-toolkit-auth-lib</module>
<module>azure-toolkit-resource-lib</module>
<module>azure-toolkit-springcloud-lib</module>
<module>azure-toolkit-appservice-lib</module>
</modules>