|
| 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.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import org.apache.commons.lang3.StringUtils; |
| 28 | + |
| 29 | +import com.github.jknack.handlebars.Context; |
| 30 | +import com.github.jknack.handlebars.Options; |
| 31 | +import com.github.jknack.handlebars.Options.Buffer; |
| 32 | +import com.google.common.collect.ImmutableList; |
| 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 List<String> ENVIRONMENTS = ImmutableList.of("dev", "stage", "prod"); |
| 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 Map)) { |
| 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((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(Map<String, Object> cloudManagerConditional) { |
| 98 | + return ENVIRONMENTS.stream() |
| 99 | + .map(env -> new CloudManagerConditional(env, cloudManagerConditional.getOrDefault(env, ImmutableMap.of()))) |
| 100 | + .collect(Collectors.toList()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Cloud manager conditional result per tenant and target environment. |
| 105 | + */ |
| 106 | + private static class CloudManagerConditional { |
| 107 | + |
| 108 | + private final Map<String, Object> config; |
| 109 | + private final String targetEnvironment; |
| 110 | + |
| 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; |
| 116 | + } |
| 117 | + else { |
| 118 | + this.config = ImmutableMap.of(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public String getTargetEnvironment() { |
| 123 | + return targetEnvironment; |
| 124 | + } |
| 125 | + |
| 126 | + public Map<String, Object> getConfig() { |
| 127 | + return config; |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments