-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipelinetemplate): Adding a Jinja template renderer alternative (#…
- Loading branch information
1 parent
352b385
commit 1f565cb
Showing
13 changed files
with
518 additions
and
87 deletions.
There are no files selected for viewing
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
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
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
90 changes: 90 additions & 0 deletions
90
.../main/java/com/netflix/spinnaker/orca/pipelinetemplate/v1schema/render/JinjaRenderer.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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.hubspot.jinjava.Jinjava; | ||
import com.hubspot.jinjava.JinjavaConfig; | ||
import com.hubspot.jinjava.interpret.InterpretException; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.loader.ResourceLocator; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.exceptions.TemplateRenderException; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.tags.ModuleTag; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors.Error; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
|
||
public class JinjaRenderer implements Renderer { | ||
|
||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
private Jinjava jinja; | ||
|
||
private RenderedValueConverter renderedValueConverter; | ||
|
||
public JinjaRenderer(ObjectMapper pipelineTemplateObjectMapper) { | ||
this(new JsonRenderedValueConverter(pipelineTemplateObjectMapper), pipelineTemplateObjectMapper); | ||
} | ||
|
||
public JinjaRenderer(RenderedValueConverter renderedValueConverter, ObjectMapper pipelineTemplateObjectMapper) { | ||
this.renderedValueConverter = renderedValueConverter; | ||
|
||
JinjavaConfig config = new JinjavaConfig(); | ||
jinja = new Jinjava(config); | ||
jinja.setResourceLocator(new NoopResourceLocator()); | ||
jinja.getGlobalContext().registerTag(new ModuleTag(this, pipelineTemplateObjectMapper)); | ||
|
||
log.info("PipelineTemplates: Using JinjaRenderer"); | ||
} | ||
|
||
@Override | ||
public String render(String template, RenderContext context) { | ||
String rendered; | ||
try { | ||
rendered = jinja.render(template, context.getVariables()); | ||
} catch (InterpretException e) { | ||
log.error("Failed rendering jinja template", e); | ||
throw new TemplateRenderException(new Error() | ||
.withMessage("failed rendering jinja template") | ||
.withCause(e.getMessage()) | ||
.withLocation(context.getLocation()) | ||
); | ||
} | ||
|
||
rendered = rendered.trim().replaceAll("\n", ""); | ||
|
||
if (!template.equals(rendered)) { | ||
log.debug("rendered '" + template + "' -> '" + rendered + "'"); | ||
} | ||
|
||
return rendered; | ||
} | ||
|
||
@Override | ||
public Object renderGraph(String template, RenderContext context) { | ||
return renderedValueConverter.convertRenderedValue(render(template, context)); | ||
} | ||
|
||
private static class NoopResourceLocator implements ResourceLocator { | ||
@Override | ||
public String getString(String fullName, Charset encoding, JinjavaInterpreter interpreter) throws IOException { | ||
return null; | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...m/netflix/spinnaker/orca/pipelinetemplate/v1schema/render/JsonRenderedValueConverter.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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.exceptions.TemplateRenderException; | ||
import org.apache.commons.lang3.math.NumberUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
|
||
public class JsonRenderedValueConverter implements RenderedValueConverter { | ||
|
||
private ObjectMapper pipelineTemplateObjectMapper; | ||
|
||
public JsonRenderedValueConverter(ObjectMapper pipelineTemplateObjectMapper) { | ||
this.pipelineTemplateObjectMapper = pipelineTemplateObjectMapper; | ||
} | ||
|
||
@Override | ||
public Object convertRenderedValue(String rendered) { | ||
if (NumberUtils.isNumber(rendered)) { | ||
if (rendered.contains(".")) { | ||
return NumberUtils.createDouble(rendered); | ||
} | ||
try { | ||
return NumberUtils.createInteger(rendered); | ||
} catch (NumberFormatException ignored) { | ||
return NumberUtils.createLong(rendered); | ||
} | ||
} else if (rendered.equals("true") || rendered.equals("false")) { | ||
return Boolean.parseBoolean(rendered); | ||
} else if (rendered.startsWith("{{") || (!rendered.startsWith("{") && !rendered.startsWith("["))) { | ||
return rendered; | ||
} | ||
|
||
JsonNode node; | ||
try { | ||
node = pipelineTemplateObjectMapper.readTree(rendered); | ||
} catch (IOException e) { | ||
throw new TemplateRenderException("template produced invalid json", e); | ||
} | ||
|
||
try { | ||
if (node.isArray()) { | ||
return pipelineTemplateObjectMapper.readValue(rendered, Collection.class); | ||
} | ||
if (node.isObject()) { | ||
return pipelineTemplateObjectMapper.readValue(rendered, HashMap.class); | ||
} | ||
if (node.isBoolean()) { | ||
return Boolean.parseBoolean(node.asText()); | ||
} | ||
if (node.isDouble()) { | ||
return node.doubleValue(); | ||
} | ||
if (node.canConvertToInt()) { | ||
return node.intValue(); | ||
} | ||
if (node.canConvertToLong()) { | ||
return node.longValue(); | ||
} | ||
if (node.isTextual()) { | ||
return node.textValue(); | ||
} | ||
if (node.isNull()) { | ||
return null; | ||
} | ||
} catch (IOException e) { | ||
throw new TemplateRenderException("template produced invalid json", e); | ||
} | ||
|
||
throw new TemplateRenderException("unknown rendered object type"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...a/com/netflix/spinnaker/orca/pipelinetemplate/v1schema/render/RenderedValueConverter.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render; | ||
|
||
public interface RenderedValueConverter { | ||
|
||
Object convertRenderedValue(String renderedValue); | ||
} |
Oops, something went wrong.