From 590a2de227cacdd456347df9a891ccf95b1744c0 Mon Sep 17 00:00:00 2001 From: Ivan Straka Date: Thu, 8 Jun 2023 15:00:47 +0200 Subject: [PATCH] Allow config properties in template definitions & allow empty parameters --- .../main/java/sunstone/aws/impl/AwsSunstoneDeployer.java | 2 +- .../java/sunstone/azure/impl/AzureSunstoneDeployer.java | 2 +- .../java/sunstone/core/AbstractSunstoneCloudDeployer.java | 5 ++++- core/src/main/java/sunstone/core/SunstoneConfig.java | 8 ++++++-- core/src/main/java/sunstone/core/SunstoneExtension.java | 3 ++- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/aws/src/main/java/sunstone/aws/impl/AwsSunstoneDeployer.java b/aws/src/main/java/sunstone/aws/impl/AwsSunstoneDeployer.java index 00085b70..23c90397 100644 --- a/aws/src/main/java/sunstone/aws/impl/AwsSunstoneDeployer.java +++ b/aws/src/main/java/sunstone/aws/impl/AwsSunstoneDeployer.java @@ -53,7 +53,7 @@ private void deployCfTemplate(WithAwsCfTemplate awsTemplateDefinition, AwsSunsto AwsCloudFormationCloudDeploymentManager deploymentManager = store.getAwsCfDemploymentManagerOrCreate(); try { - String content = getResourceContent(awsTemplateDefinition.template()); + String content = getResourceContent(SunstoneConfig.resolveExpressionToString(awsTemplateDefinition.template())); Map parameters = getParameters(awsTemplateDefinition.parameters()); String region = SunstoneConfig.resolveExpressionToString(awsTemplateDefinition.region()); if (region == null) { diff --git a/azure/src/main/java/sunstone/azure/impl/AzureSunstoneDeployer.java b/azure/src/main/java/sunstone/azure/impl/AzureSunstoneDeployer.java index b5471478..5cfb1664 100644 --- a/azure/src/main/java/sunstone/azure/impl/AzureSunstoneDeployer.java +++ b/azure/src/main/java/sunstone/azure/impl/AzureSunstoneDeployer.java @@ -51,7 +51,7 @@ private void deployArmTemplate(WithAzureArmTemplate armTemplateDefinition, Azure String content = null; try { - content = getResourceContent(armTemplateDefinition.template()); + content = getResourceContent(SunstoneConfig.resolveExpressionToString(armTemplateDefinition.template())); String group = SunstoneConfig.resolveExpressionToString(armTemplateDefinition.group()); if (group == null) { throw new IllegalArgumentSunstoneException("Resource group for Azure ARM template is not defined. " diff --git a/core/src/main/java/sunstone/core/AbstractSunstoneCloudDeployer.java b/core/src/main/java/sunstone/core/AbstractSunstoneCloudDeployer.java index 6a0bddee..f46acc8e 100644 --- a/core/src/main/java/sunstone/core/AbstractSunstoneCloudDeployer.java +++ b/core/src/main/java/sunstone/core/AbstractSunstoneCloudDeployer.java @@ -12,6 +12,9 @@ import java.util.HashMap; import java.util.Map; +import static sunstone.core.SunstoneConfig.resolveExpression; +import static sunstone.core.SunstoneConfig.resolveOptionalExpression; + /** * Abstract class for providing common functionality for deploy operation to clouds. Tha class focuses on utilizing * work flow regarding getting resources, parameters and so on. @@ -25,7 +28,7 @@ protected static Map getParameters(Parameter[] parameters) { for (int i = 0; i < parameters.length; i++) { parametersMap.put(parameters[i].k(), parameters[i].v()); } - parametersMap.forEach((key, value) -> parametersMap.put(key, SunstoneConfig.resolveExpressionToString(value))); + parametersMap.forEach((key, value) -> parametersMap.put(resolveExpression(key, String.class), resolveOptionalExpression(value, String.class).orElse(""))); return Collections.unmodifiableMap(parametersMap); } diff --git a/core/src/main/java/sunstone/core/SunstoneConfig.java b/core/src/main/java/sunstone/core/SunstoneConfig.java index a01edb84..9e4e84c5 100644 --- a/core/src/main/java/sunstone/core/SunstoneConfig.java +++ b/core/src/main/java/sunstone/core/SunstoneConfig.java @@ -4,11 +4,11 @@ import io.smallrye.config.SmallRyeConfig; import io.smallrye.config.SmallRyeConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigSource; -import org.junit.platform.commons.util.StringUtils; import java.io.IOException; import java.io.InputStream; import java.util.Objects; +import java.util.Optional; import java.util.Properties; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -89,6 +89,10 @@ public static V resolveExpression(String expression, Class clazz) { return config.getValue(SunstoneExpressionSource.registerExpression(expression), clazz); } + public static Optional resolveOptionalExpression(String expression, Class clazz) { + return config.getOptionalValue(SunstoneExpressionSource.registerExpression(expression), clazz); + } + /** * @@ -116,7 +120,7 @@ public static V resolveExpression(String expression, Class clazz) { * */ public static String resolveExpressionToString(String expression) { - return StringUtils.isBlank(expression) ? expression : resolveExpression(expression, String.class); + return resolveExpression(expression, String.class); } /** diff --git a/core/src/main/java/sunstone/core/SunstoneExtension.java b/core/src/main/java/sunstone/core/SunstoneExtension.java index ca76b50d..abc09a04 100644 --- a/core/src/main/java/sunstone/core/SunstoneExtension.java +++ b/core/src/main/java/sunstone/core/SunstoneExtension.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.extension.TestInstancePostProcessor; import org.junit.platform.commons.support.AnnotationSupport; import org.junit.platform.commons.support.HierarchyTraversalMode; +import org.junit.platform.commons.util.StringUtils; import sunstone.annotation.AbstractSetupTask; import sunstone.annotation.Setup; import sunstone.annotation.SunstoneCloudDeployAnnotation; @@ -191,7 +192,7 @@ static void performDeploymentOperation(ExtensionContext ctx) throws SunstoneExce throw new IllegalArgumentSunstoneException("Deployment method must have 0 parameters"); } Deployment annotation = method.getAnnotation(Deployment.class); - String deploymentName = SunstoneConfig.resolveExpressionToString(annotation.name()); + String deploymentName = StringUtils.isBlank(annotation.name()) ? "" : SunstoneConfig.resolveExpressionToString(annotation.name()); method.setAccessible(true); Object invoke = method.invoke(null);