Skip to content

Commit

Permalink
logging(template-mcp): adding more logging around templating (datahub…
Browse files Browse the repository at this point in the history
  • Loading branch information
david-leifker authored Nov 5, 2024
1 parent edb87ff commit 26529f2
Showing 1 changed file with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
Expand Down Expand Up @@ -109,13 +110,29 @@ static List<ObjectNode> resolveMCPTemplate(
AuditStamp auditStamp)
throws IOException {

String template = loadTemplate(mcpTemplate.getMcps_location());
Mustache mustache = MUSTACHE_FACTORY.compile(new StringReader(template), mcpTemplate.getName());
final String template = loadTemplate(mcpTemplate.getMcps_location());
Map<String, Object> scopeValues = resolveValues(opContext, mcpTemplate, auditStamp);

StringWriter writer = new StringWriter();
mustache.execute(writer, scopeValues);
try {
Mustache mustache =
MUSTACHE_FACTORY.compile(new StringReader(template), mcpTemplate.getName());
mustache.execute(writer, scopeValues);
} catch (Exception e) {
log.error(
"Failed to apply mustache template. Template: {} Values: {}",
template,
resolveEnv(mcpTemplate));
throw e;
}

return opContext.getYamlMapper().readValue(writer.toString(), new TypeReference<>() {});
final String yaml = writer.toString();
try {
return opContext.getYamlMapper().readValue(yaml, new TypeReference<>() {});
} catch (Exception e) {
log.error("Failed to parse rendered MCP bootstrap yaml: {}", yaml);
throw e;
}
}

static Map<String, Object> resolveValues(
Expand All @@ -128,13 +145,21 @@ static Map<String, Object> resolveValues(
// built-in
scopeValues.put("auditStamp", RecordUtils.toJsonString(auditStamp));

String envValue = resolveEnv(mcpTemplate);
if (envValue != null) {
scopeValues.putAll(opContext.getObjectMapper().readValue(envValue, new TypeReference<>() {}));
}
return scopeValues;
}

@Nullable
private static String resolveEnv(BootstrapMCPConfigFile.MCPTemplate mcpTemplate) {
if (mcpTemplate.getValues_env() != null
&& !mcpTemplate.getValues_env().isEmpty()
&& System.getenv().containsKey(mcpTemplate.getValues_env())) {
String envValue = System.getenv(mcpTemplate.getValues_env());
scopeValues.putAll(opContext.getObjectMapper().readValue(envValue, new TypeReference<>() {}));
return System.getenv(mcpTemplate.getValues_env());
}
return scopeValues;
return null;
}

private static String loadTemplate(String source) throws IOException {
Expand Down

0 comments on commit 26529f2

Please sign in to comment.