|
| 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.ArrayList; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +import org.apache.commons.lang3.builder.EqualsBuilder; |
| 30 | +import org.apache.commons.lang3.builder.HashCodeBuilder; |
| 31 | +import org.apache.commons.lang3.builder.ToStringBuilder; |
| 32 | +import org.apache.commons.lang3.builder.ToStringStyle; |
| 33 | + |
| 34 | +import com.github.jknack.handlebars.Options; |
| 35 | + |
| 36 | +import io.wcm.devops.conga.generator.ContextProperties; |
| 37 | +import io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin; |
| 38 | +import io.wcm.devops.conga.generator.spi.handlebars.context.HelperContext; |
| 39 | +import io.wcm.devops.conga.model.environment.Tenant; |
| 40 | +import io.wcm.devops.conga.model.util.MapExpander; |
| 41 | + |
| 42 | +/** |
| 43 | + * Gets all tenants which have a "httpd.cloudManagerConditional" configuration set, grouped by targetEnvironment value. |
| 44 | + * The resulting map with environment name as key and list of conditional configuration maps as value is |
| 45 | + * set to a context variable with the given name. |
| 46 | + */ |
| 47 | +public final class AemTenantsCloudManagerConditionalHelper implements HelperPlugin<Object> { |
| 48 | + |
| 49 | + /** |
| 50 | + * Plugin/Helper name |
| 51 | + */ |
| 52 | + public static final String NAME = "aemTenantsCloudManagerConditional"; |
| 53 | + |
| 54 | + static final String CLOUD_MANAGER_CONDITIONAL_KEY = "httpd.cloudManagerConditional"; |
| 55 | + static final String TARGET_ENVIRONMENT_KEY = "targetEnvironment"; |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getName() { |
| 59 | + return NAME; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Object apply(Object context, Options options, HelperContext pluginContext) throws IOException { |
| 64 | + String variableName = null; |
| 65 | + if (context instanceof String) { |
| 66 | + variableName = (String)context; |
| 67 | + } |
| 68 | + |
| 69 | + // get tenants from context |
| 70 | + Object tenants = options.context.get(ContextProperties.TENANTS); |
| 71 | + |
| 72 | + // generate grouped result map |
| 73 | + Map<String, List<CloudManagerConditional>> result = getTenantConfigs(tenants).stream() |
| 74 | + .flatMap(tenantConfig -> tenantConfig.getCloudManagerConditionals().stream()) |
| 75 | + .filter(item -> item.getTargetEnvironment() != null) |
| 76 | + .collect(Collectors.groupingBy(CloudManagerConditional::getTargetEnvironment, LinkedHashMap::new, Collectors.toList())); |
| 77 | + |
| 78 | + // set variable |
| 79 | + if (variableName != null) { |
| 80 | + options.context.data(variableName, result); |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + private List<TenantConfig> getTenantConfigs(Object object) { |
| 88 | + List<TenantConfig> result = new ArrayList<>(); |
| 89 | + if (object instanceof List) { |
| 90 | + List<Tenant> tenants = (List<Tenant>)object; |
| 91 | + for (Tenant tenant : tenants) { |
| 92 | + result.add(new TenantConfig(tenant)); |
| 93 | + } |
| 94 | + } |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + private static class TenantConfig { |
| 99 | + |
| 100 | + private final Tenant tenant; |
| 101 | + |
| 102 | + TenantConfig(Tenant tenant) { |
| 103 | + this.tenant = tenant; |
| 104 | + } |
| 105 | + |
| 106 | + @SuppressWarnings("unchecked") |
| 107 | + public List<CloudManagerConditional> getCloudManagerConditionals() { |
| 108 | + List<CloudManagerConditional> result = new ArrayList<>(); |
| 109 | + Object object = MapExpander.getDeep(tenant.getConfig(), CLOUD_MANAGER_CONDITIONAL_KEY); |
| 110 | + if (object instanceof List) { |
| 111 | + List<Map<String, Object>> items = (List<Map<String, Object>>)object; |
| 112 | + for (Map<String, Object> item : items) { |
| 113 | + result.add(new CloudManagerConditional(item, tenant)); |
| 114 | + } |
| 115 | + } |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Cloud manager conditional result per tenant and target environment. |
| 123 | + */ |
| 124 | + public static class CloudManagerConditional { |
| 125 | + |
| 126 | + private final Map<String, Object> config; |
| 127 | + private final Tenant tenant; |
| 128 | + private final String targetEnvironment; |
| 129 | + |
| 130 | + CloudManagerConditional(Map<String, Object> config, Tenant tenant) { |
| 131 | + this.config = config; |
| 132 | + this.tenant = tenant; |
| 133 | + Object value = config.get(TARGET_ENVIRONMENT_KEY); |
| 134 | + if (value instanceof String) { |
| 135 | + this.targetEnvironment = (String)value; |
| 136 | + } |
| 137 | + else { |
| 138 | + this.targetEnvironment = null; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public String getTargetEnvironment() { |
| 143 | + return targetEnvironment; |
| 144 | + } |
| 145 | + |
| 146 | + public Map<String, Object> getConfig() { |
| 147 | + return config; |
| 148 | + } |
| 149 | + |
| 150 | + public Tenant getTenant() { |
| 151 | + return this.tenant; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public int hashCode() { |
| 156 | + return new HashCodeBuilder() |
| 157 | + .append(config) |
| 158 | + .append(tenant.getTenant()) |
| 159 | + .toHashCode(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public boolean equals(Object obj) { |
| 164 | + if (obj instanceof CloudManagerConditional) { |
| 165 | + CloudManagerConditional other = (CloudManagerConditional)obj; |
| 166 | + return new EqualsBuilder() |
| 167 | + .append(config, other.config) |
| 168 | + .append(tenant.getTenant(), other.tenant.getTenant()) |
| 169 | + .isEquals(); |
| 170 | + } |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public String toString() { |
| 176 | + return new ToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE) |
| 177 | + .append("config", config) |
| 178 | + .append("tenant", tenant.getTenant()) |
| 179 | + .toString(); |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments