io.linguarobot.aws.cdk.maven.context.SsmContextProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-cdk-maven-plugin Show documentation
Show all versions of aws-cdk-maven-plugin Show documentation
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.
package io.linguarobot.aws.cdk.maven.context;
import io.linguarobot.aws.cdk.maven.CdkPluginException;
import software.amazon.awssdk.services.ssm.SsmClient;
import software.amazon.awssdk.services.ssm.model.GetParameterRequest;
import software.amazon.awssdk.services.ssm.model.GetParameterResponse;
import software.amazon.awssdk.services.ssm.model.Parameter;
import software.amazon.awssdk.services.ssm.model.ParameterNotFoundException;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonValue;
import java.util.Optional;
public class SsmContextProvider implements ContextProvider {
public static final String KEY = "ssm";
private final AwsClientProvider awsClientProvider;
public SsmContextProvider(AwsClientProvider awsClientProvider) {
this.awsClientProvider = awsClientProvider;
}
@Override
public JsonValue getContextValue(JsonObject properties) {
String environment = ContextProviders.buildEnvironment(properties);
String parameterName = ContextProviders.getRequiredProperty(properties, "parameterName");
try (SsmClient ssmClient = awsClientProvider.getClient(SsmClient.class, environment)) {
String value;
try {
GetParameterResponse response = ssmClient.getParameter(parameterRequest(parameterName));
value = Optional.of(response)
.map(GetParameterResponse::parameter)
.map(Parameter::value)
.orElse(null);
} catch (ParameterNotFoundException e) {
value = null;
}
if (value == null) {
throw new CdkPluginException("The SSM parameter '" + parameterName + "' is not available for the " +
"following environment: " + environment);
}
return Json.createValue(value);
}
}
private GetParameterRequest parameterRequest(String parameterName) {
return GetParameterRequest.builder()
.name(parameterName)
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy