Skip to content

Commit be0b42e

Browse files
committed
Add httpdCloudManagerConditional and withAllCloudManagerConditional handlebars helper to build envrionment-conditional configuration for AEM cloud service.
1 parent fc469aa commit be0b42e

11 files changed

Lines changed: 455 additions & 302 deletions

changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
<release version="1.12.0" date="not released">
2727
<action type="add" dev="sseifert">
28-
Add aemTenantsCloudManagerConditional handlebar helper to build conditional sling mapping definitions for AEM cloud service with a sling dispatcher configuration.
28+
Add httpdCloudManagerConditional and withAllCloudManagerConditional handlebars helper to build envrionment-conditional configuration for AEM cloud service..
2929
</action>
3030
<action type="add" dev="sseifert">
3131
conga-aem-maven-plugin: Add new parameter autoDependenciesMode which replaces/supercedes autoDependencies and autoDependenciesSeparateMutable flags.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2020 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package io.wcm.devops.conga.plugins.aem.handlebars.helper;
21+
22+
import java.io.IOException;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.stream.Collectors;
27+
28+
import org.apache.commons.lang3.StringUtils;
29+
30+
import com.github.jknack.handlebars.Context;
31+
import com.github.jknack.handlebars.Options;
32+
import com.github.jknack.handlebars.Options.Buffer;
33+
import com.google.common.collect.ImmutableMap;
34+
35+
import io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin;
36+
import io.wcm.devops.conga.generator.spi.handlebars.context.HelperContext;
37+
import io.wcm.devops.conga.model.util.MapMerger;
38+
39+
/**
40+
* Checks if there is a conditional statement 'httpd.cloudManagerConditional' in the current context.
41+
* If yes iterates over all environments and renders the body for each with a merged model.
42+
* If no the body is rendered once with the default model.
43+
*/
44+
abstract class AbstractCloudManagerConditionalHelper implements HelperPlugin<Object> {
45+
46+
static final String HTTPD_KEY = "httpd";
47+
static final String CLOUD_MANAGER_CONDITIONAL_KEY = "cloudManagerConditional";
48+
static final String TARGET_ENVIRONMENT_KEY = "targetEnvironment";
49+
50+
@Override
51+
@SuppressWarnings("unchecked")
52+
public final Object apply(Object context, Options options, HelperContext pluginContext) throws IOException {
53+
Buffer buffer = options.buffer();
54+
55+
Context currentContext = options.context;
56+
if (context != null) {
57+
currentContext = Context.copy(options.context, context);
58+
}
59+
60+
// get tenants from context
61+
Object cloudManagerConditional = currentContext.get(HTTPD_KEY + "." + CLOUD_MANAGER_CONDITIONAL_KEY);
62+
63+
if (!(cloudManagerConditional instanceof List)) {
64+
// no conditional statement - just render the body
65+
buffer.append(options.fn(currentContext));
66+
}
67+
else {
68+
// render body for each environment in conditional block with a merged context
69+
List<CloudManagerConditional> items = getCloudManagerConditional((List<Map<String, Object>>)cloudManagerConditional);
70+
for (CloudManagerConditional item : items) {
71+
72+
// config inside httpd.cloudManagerConditional is considered to be prefixed with "httpd.", so wrap it around here for merging
73+
Map<String, Object> httpdWrapper = ImmutableMap.of(HTTPD_KEY, item.getConfig());
74+
Map<String, Object> mergedModel = MapMerger.merge(httpdWrapper, (Map<String, Object>)currentContext.model());
75+
76+
// remove cloudManagerConditional after merging the models
77+
Map<String, Object> mergedHttpd = (Map<String, Object>)mergedModel.get(HTTPD_KEY);
78+
if (mergedHttpd != null) {
79+
mergedHttpd.remove(CLOUD_MANAGER_CONDITIONAL_KEY);
80+
}
81+
82+
Context mergedContext = Context.copy(currentContext, mergedModel);
83+
CharSequence bodyContent = options.fn(mergedContext);
84+
85+
if (StringUtils.isNotBlank(bodyContent)) {
86+
renderBodyContent(buffer, bodyContent, item.getTargetEnvironment());
87+
}
88+
}
89+
}
90+
91+
return buffer;
92+
}
93+
94+
protected abstract void renderBodyContent(Buffer buffer, CharSequence bodyContent,
95+
String targetEnvironment) throws IOException;
96+
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)
101+
.collect(Collectors.toList());
102+
}
103+
104+
/**
105+
* Cloud manager conditional result per tenant and target environment.
106+
*/
107+
private static class CloudManagerConditional {
108+
109+
private final Map<String, Object> config;
110+
private final String targetEnvironment;
111+
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;
117+
}
118+
else {
119+
this.targetEnvironment = null;
120+
}
121+
this.config.remove(AbstractCloudManagerConditionalHelper.TARGET_ENVIRONMENT_KEY);
122+
}
123+
124+
public String getTargetEnvironment() {
125+
return targetEnvironment;
126+
}
127+
128+
public Map<String, Object> getConfig() {
129+
return config;
130+
}
131+
132+
}
133+
134+
}

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

Lines changed: 0 additions & 184 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2020 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package io.wcm.devops.conga.plugins.aem.handlebars.helper;
21+
22+
import java.io.IOException;
23+
24+
import org.apache.commons.lang3.StringUtils;
25+
26+
import com.github.jknack.handlebars.Options.Buffer;
27+
28+
/**
29+
* Checks if there is a conditional statement 'httpd.cloudManagerConditional' in the current context.
30+
* If yes iterates over all environments and renders the body for each with a merged model,
31+
* each enclosed in an IfDefine statement.
32+
* If no the body is rendered once with the default model.
33+
*/
34+
public final class HttpdCloudManagerConditionalHelper extends AbstractCloudManagerConditionalHelper {
35+
36+
/**
37+
* Plugin/Helper name
38+
*/
39+
public static final String NAME = "httpdCloudManagerConditional";
40+
41+
@Override
42+
public String getName() {
43+
return NAME;
44+
}
45+
46+
@Override
47+
protected void renderBodyContent(Buffer buffer, CharSequence bodyContent,
48+
String targetEnvironment) throws IOException {
49+
buffer.append("<IfDefine ENVIRONMENT_").append(StringUtils.upperCase(targetEnvironment)).append(">");
50+
buffer.append(bodyContent);
51+
buffer.append("</IfDefine>\n");
52+
}
53+
54+
}

0 commit comments

Comments
 (0)