-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RHTAPREL-392): add jinja2 and templating to the image
As part of RHTAPREL-392, we need to create advisories. The easiest way to do this is from a template using jinja2, so this commit adds jinja2 and a template to the repo. Some ansible filters are not present in vanilla jinja2, so a module providing those is added too. Signed-off-by: Johnny Bieren <[email protected]>
- Loading branch information
1 parent
ecbbfc8
commit b76ba3e
Showing
5 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pytest | ||
requests | ||
jinja2 | ||
jinja2-ansible-filters |
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,23 @@ | ||
apiVersion: rhtap.redhat.com/v1alpha1 | ||
kind: Advisory | ||
metadata: | ||
name: {{ advisory_name }} | ||
spec: | ||
product_id: {{ advisory.spec.product_id }} | ||
cpe: {{ advisory.spec.cpe }} | ||
type: {{ advisory.spec.type }} | ||
{%- if 'issues' in advisory.spec %} | ||
issues: | ||
{{ advisory.spec.issues | to_nice_yaml(indent=2) | indent(4) | trim }} | ||
{%- endif %} | ||
content: | ||
{{ advisory.spec.content | to_nice_yaml(indent=2) | indent(4) | trim }} | ||
synopsis: {{ advisory.spec.synopsis }} | ||
topic: >- | ||
{{ advisory.spec.topic | wordwrap(76) | indent(4) }} | ||
description: >- | ||
{{ advisory.spec.description | wordwrap(76) | indent(4) }} | ||
solution: >- | ||
{{ advisory.spec.solution | wordwrap(76) | indent(4) }} | ||
references: | ||
{{ advisory.spec.references | to_nice_yaml | indent(4) }} |
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,53 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
from jinja2 import Template | ||
from jinja2_ansible_filters import AnsibleCoreFiltersExtension | ||
import argparse | ||
import json | ||
from typing import Any | ||
|
||
|
||
def setup_argparser(args: Any) -> argparse.Namespace: # pragma: no cover | ||
"""Setup argument parser | ||
:return: Initialized argument parser | ||
""" | ||
|
||
parser = argparse.ArgumentParser(description="Applies a template.") | ||
|
||
parser.add_argument( | ||
"--data", | ||
help="JSON string containing data to use in the template.", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"--template", | ||
help="Path to the template file to use.", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"-o", | ||
"--output", | ||
help="The desired filename of the result.", | ||
required=True, | ||
) | ||
return parser.parse_args(args) | ||
|
||
|
||
def main(): # pragma: no cover | ||
"""Main func""" | ||
|
||
args = setup_argparser(sys.argv[1:]) | ||
|
||
with open(args.template) as t: | ||
template = Template(t.read(), extensions=[AnsibleCoreFiltersExtension]) | ||
content = template.render(json.loads(args.data)) | ||
|
||
filename = args.output | ||
with open(filename, mode="w", encoding="utf-8") as advisory: | ||
advisory.write(content) | ||
print(f"Wrote {filename}") | ||
|
||
|
||
if __name__ == "__main__": # pragma: no cover | ||
main() |
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,34 @@ | ||
import os | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from apply_template import setup_argparser, main | ||
|
||
|
||
def test_setup_argparser_proper_args(): | ||
args_in = ["--data", "{}", "--template", "somefile", "-o", "newfile"] | ||
args_out = setup_argparser(args_in) | ||
assert args_out.data == "{}" | ||
assert args_out.template == "somefile" | ||
assert args_out.output == "newfile" | ||
|
||
|
||
def test_setup_argparser_improper_args(): | ||
with pytest.raises(SystemExit): | ||
setup_argparser([]) | ||
|
||
|
||
@patch("apply_template.Template.render") | ||
@patch("apply_template.setup_argparser") | ||
def test_apply_template_advisory_template(mock_argparser: MagicMock, mock_render: MagicMock): | ||
args = MagicMock() | ||
args.template = "templates/advisory.yaml.jinja" | ||
args.data = "{}" | ||
args.output = "somefile" | ||
mock_argparser.return_value = args | ||
mock_render.return_value = "applied template file" | ||
|
||
# Act | ||
main() | ||
|
||
assert os.path.exists("somefile") | ||
os.remove("somefile") |