Skip to content

Commit 0454ab4

Browse files
committed
change cloudManagerConditional to map
1 parent be0b42e commit 0454ab4

3 files changed

Lines changed: 24 additions & 32 deletions

File tree

conga-aem-plugin/src/main/java/io/wcm/devops/conga/plugins/aem/handlebars/helper/AbstractCloudManagerConditionalHelper.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package io.wcm.devops.conga.plugins.aem.handlebars.helper;
2121

2222
import java.io.IOException;
23-
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
2625
import java.util.stream.Collectors;
@@ -30,6 +29,7 @@
3029
import com.github.jknack.handlebars.Context;
3130
import com.github.jknack.handlebars.Options;
3231
import com.github.jknack.handlebars.Options.Buffer;
32+
import com.google.common.collect.ImmutableList;
3333
import com.google.common.collect.ImmutableMap;
3434

3535
import io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin;
@@ -45,7 +45,7 @@ abstract class AbstractCloudManagerConditionalHelper implements HelperPlugin<Obj
4545

4646
static final String HTTPD_KEY = "httpd";
4747
static final String CLOUD_MANAGER_CONDITIONAL_KEY = "cloudManagerConditional";
48-
static final String TARGET_ENVIRONMENT_KEY = "targetEnvironment";
48+
static final List<String> ENVIRONMENTS = ImmutableList.of("dev", "stage", "prod");
4949

5050
@Override
5151
@SuppressWarnings("unchecked")
@@ -60,13 +60,13 @@ public final Object apply(Object context, Options options, HelperContext pluginC
6060
// get tenants from context
6161
Object cloudManagerConditional = currentContext.get(HTTPD_KEY + "." + CLOUD_MANAGER_CONDITIONAL_KEY);
6262

63-
if (!(cloudManagerConditional instanceof List)) {
63+
if (!(cloudManagerConditional instanceof Map)) {
6464
// no conditional statement - just render the body
6565
buffer.append(options.fn(currentContext));
6666
}
6767
else {
6868
// render body for each environment in conditional block with a merged context
69-
List<CloudManagerConditional> items = getCloudManagerConditional((List<Map<String, Object>>)cloudManagerConditional);
69+
List<CloudManagerConditional> items = getCloudManagerConditional((Map<String, Object>)cloudManagerConditional);
7070
for (CloudManagerConditional item : items) {
7171

7272
// config inside httpd.cloudManagerConditional is considered to be prefixed with "httpd.", so wrap it around here for merging
@@ -94,10 +94,9 @@ public final Object apply(Object context, Options options, HelperContext pluginC
9494
protected abstract void renderBodyContent(Buffer buffer, CharSequence bodyContent,
9595
String targetEnvironment) throws IOException;
9696

97-
private List<CloudManagerConditional> getCloudManagerConditional(List<Map<String, Object>> configs) {
98-
return configs.stream()
99-
.map(config -> new CloudManagerConditional(config))
100-
.filter(item -> item.getTargetEnvironment() != null)
97+
private List<CloudManagerConditional> getCloudManagerConditional(Map<String, Object> cloudManagerConditional) {
98+
return ENVIRONMENTS.stream()
99+
.map(env -> new CloudManagerConditional(env, cloudManagerConditional.getOrDefault(env, ImmutableMap.of())))
101100
.collect(Collectors.toList());
102101
}
103102

@@ -109,16 +108,15 @@ private static class CloudManagerConditional {
109108
private final Map<String, Object> config;
110109
private final String targetEnvironment;
111110

112-
CloudManagerConditional(Map<String, Object> config) {
113-
this.config = new HashMap<>(config);
114-
Object value = this.config.get(AbstractCloudManagerConditionalHelper.TARGET_ENVIRONMENT_KEY);
115-
if (value instanceof String) {
116-
this.targetEnvironment = (String)value;
111+
@SuppressWarnings("unchecked")
112+
CloudManagerConditional(String targetEnvironment, Object value) {
113+
this.targetEnvironment = targetEnvironment;
114+
if (value instanceof Map) {
115+
this.config = (Map<String, Object>)value;
117116
}
118117
else {
119-
this.targetEnvironment = null;
118+
this.config = ImmutableMap.of();
120119
}
121-
this.config.remove(AbstractCloudManagerConditionalHelper.TARGET_ENVIRONMENT_KEY);
122120
}
123121

124122
public String getTargetEnvironment() {

conga-aem-plugin/src/test/java/io/wcm/devops/conga/plugins/aem/handlebars/helper/HttpdCloudManagerConditionalHelperTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Test;
3030

31-
import com.google.common.collect.ImmutableList;
3231
import com.google.common.collect.ImmutableMap;
3332

3433
import io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin;
@@ -64,11 +63,10 @@ void testApplySeparateModel() throws IOException {
6463
void testApplyWithCloudManagerConditional() throws IOException {
6564
Map<String, Object> model = ImmutableMap.of("httpd",
6665
ImmutableMap.of("serverName", "host0",
67-
"cloudManagerConditional", ImmutableList.of(
68-
ImmutableMap.of("targetEnvironment", "dev", "serverName", "host1"),
69-
ImmutableMap.of("targetEnvironment", "stage", "serverName", "host2"),
70-
ImmutableMap.of("targetEnvironment", "prod"),
71-
ImmutableMap.of("serverName", "host3"))));
66+
"cloudManagerConditional", ImmutableMap.of(
67+
"dev", ImmutableMap.of("serverName", "host1"),
68+
"stage", ImmutableMap.of("serverName", "host2"),
69+
"prod", ImmutableMap.of())));
7270
MockOptions options = new MockOptions(model);
7371

7472
Map<String, Object> model_dev = ImmutableMap.of("httpd", ImmutableMap.of("serverName", "host1"));

conga-aem-plugin/src/test/java/io/wcm/devops/conga/plugins/aem/handlebars/helper/WithAllCloudManagerConditionalHelperTest.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Test;
3030

31-
import com.google.common.collect.ImmutableList;
3231
import com.google.common.collect.ImmutableMap;
3332

3433
import io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin;
@@ -64,11 +63,10 @@ void testApplySeparateModel() throws IOException {
6463
void testApplyWithCloudManagerConditional() throws IOException {
6564
Map<String, Object> model = ImmutableMap.of("httpd",
6665
ImmutableMap.of("serverName", "host0",
67-
"cloudManagerConditional", ImmutableList.of(
68-
ImmutableMap.of("targetEnvironment", "dev", "serverName", "host1"),
69-
ImmutableMap.of("targetEnvironment", "stage", "serverName", "host2"),
70-
ImmutableMap.of("targetEnvironment", "prod"),
71-
ImmutableMap.of("serverName", "host3"))));
66+
"cloudManagerConditional", ImmutableMap.of(
67+
"dev", ImmutableMap.of("serverName", "host1"),
68+
"stage", ImmutableMap.of("serverName", "host2"),
69+
"prod", ImmutableMap.of())));
7270
MockOptions options = new MockOptions(model);
7371

7472
Map<String, Object> model_dev = ImmutableMap.of("httpd", ImmutableMap.of("serverName", "host1"));
@@ -84,11 +82,9 @@ void testApplyWithCloudManagerConditional() throws IOException {
8482
void testApplyWithCloudManagerConditionalSeparateModel() throws IOException {
8583
Map<String, Object> model = ImmutableMap.of("httpd",
8684
ImmutableMap.of("serverName", "host0",
87-
"cloudManagerConditional", ImmutableList.of(
88-
ImmutableMap.of("targetEnvironment", "dev", "serverName", "host1"),
89-
ImmutableMap.of("targetEnvironment", "stage", "serverName", "host2"),
90-
ImmutableMap.of("targetEnvironment", "prod"),
91-
ImmutableMap.of("serverName", "host3"))));
85+
"cloudManagerConditional", ImmutableMap.of(
86+
"dev", ImmutableMap.of("serverName", "host1"),
87+
"stage", ImmutableMap.of("serverName", "host2"))));
9288
MockOptions options = new MockOptions();
9389

9490
Map<String, Object> model_dev = ImmutableMap.of("httpd", ImmutableMap.of("serverName", "host1"));

0 commit comments

Comments
 (0)