-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Attila Mészáros <[email protected]>
- Loading branch information
Showing
1 changed file
with
14 additions
and
17 deletions.
There are no files selected for viewing
31 changes: 14 additions & 17 deletions
31
src/main/java/io/csviri/operator/resourceglue/templating/GenericTemplateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,43 @@ | ||
package io.csviri.operator.resourceglue.templating; | ||
|
||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.csviri.operator.resourceglue.Utils; | ||
import io.csviri.operator.resourceglue.customresource.glue.Glue; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.quarkus.qute.Engine; | ||
import io.quarkus.qute.Template; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.mustachejava.DefaultMustacheFactory; | ||
import com.github.mustachejava.MustacheFactory; | ||
|
||
public class GenericTemplateHandler { | ||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
private static final MustacheFactory mustacheFactory = new DefaultMustacheFactory(); | ||
|
||
public static final String WORKFLOW_METADATA_KEY = "glueMetadata"; | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
private static final Engine engine = Engine.builder().addDefaults().build(); | ||
|
||
public String processTemplate(String template, Glue primary, | ||
Context<Glue> context) { | ||
|
||
// to precompile? | ||
var mustache = mustacheFactory.compile(new StringReader(template), "desired"); | ||
Template hello = engine.parse(template); | ||
|
||
var mustacheContext = createMustacheContextWithResources(primary, context); | ||
return mustache.execute(new StringWriter(), mustacheContext).toString(); | ||
return hello.data(createDataWithResources(primary, context)).render(); | ||
} | ||
|
||
private static Map<String, Map> createMustacheContextWithResources(Glue primary, | ||
@SuppressWarnings("rawtypes") | ||
private static Map<String, Map> createDataWithResources(Glue primary, | ||
Context<Glue> context) { | ||
Map<String, Map> res = new HashMap<>(); | ||
var actualResourcesByName = Utils.getActualResourcesByNameInWorkflow(context, primary); | ||
|
||
Map<String, Map> mustacheContext = new HashMap<>(); | ||
|
||
actualResourcesByName.entrySet().stream().forEach(e -> mustacheContext.put(e.getKey(), | ||
e.getValue() == null ? null : objectMapper.convertValue(e.getValue(), Map.class))); | ||
actualResourcesByName.forEach((key, value) -> res.put(key, | ||
value == null ? null : objectMapper.convertValue(value, Map.class))); | ||
|
||
mustacheContext.put(WORKFLOW_METADATA_KEY, | ||
res.put(WORKFLOW_METADATA_KEY, | ||
objectMapper.convertValue(primary.getMetadata(), Map.class)); | ||
|
||
return mustacheContext; | ||
return res; | ||
} | ||
} |