All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.linguarobot.aws.cdk.maven.context.AvailabilityZonesContextProvider Maven / Gradle / Ivy

Go to download

The AWS CDK Maven plugin produces and deploys CloudFormation templates based on the cloud infrastructure defined by means of CDK. The goal of the project is to improve the experience of Java developers while working with CDK by eliminating the need for installing Node.js and interacting with the CDK application by means of CDK Toolkit.

There is a newer version: 0.0.8
Show newest version
package io.linguarobot.aws.cdk.maven.context;

import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.AvailabilityZone;
import software.amazon.awssdk.services.ec2.model.AvailabilityZoneState;
import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonValue;
import java.util.stream.Collector;
import java.util.stream.Stream;

public class AvailabilityZonesContextProvider implements ContextProvider {

    public static final String KEY = "availability-zones";

    private final AwsClientProvider awsClientProvider;

    public AvailabilityZonesContextProvider(AwsClientProvider awsClientProvider) {
        this.awsClientProvider = awsClientProvider;
    }

    @Override
    public JsonValue getContextValue(JsonObject properties) {
        String environment = ContextProviders.buildEnvironment(properties);
        try (Ec2Client ec2Client = awsClientProvider.getClient(Ec2Client.class, environment)) {
            return Stream.of(ec2Client.describeAvailabilityZones())
                    .filter(DescribeAvailabilityZonesResponse::hasAvailabilityZones)
                    .flatMap(availabilityZone -> availabilityZone.availabilityZones().stream())
                    .filter(availabilityZone -> availabilityZone.state() == AvailabilityZoneState.AVAILABLE)
                    .map(AvailabilityZone::zoneName)
                    .map(Json::createValue)
                    .collect(toJsonArray());
        }
    }

    private static Collector toJsonArray() {
        return Collector.of(
                Json::createArrayBuilder,
                JsonArrayBuilder::add,
                JsonArrayBuilder::add,
                JsonArrayBuilder::build
        );
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy