forked from RedHatQE/openshift-python-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
38 lines (28 loc) · 959 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import re
import jinja2
import yaml
from ocp_resources.exceptions import MissingTemplateVariables
def generate_yaml_from_template(**kwargs):
"""
Generate JSON from yaml file_
Keyword Args:
name (str):
image (str):
Returns:
dict: Generated from template file
Raises:
MissingTemplateVariables: If not all template variables exists
Examples:
generate_yaml_from_template(file_='path/to/file/name', name='vm-name-1')
"""
file_ = "tests/manifests/vm.yaml"
with open(file_, "r") as stream:
data = stream.read()
# Find all template variables
template_vars = [i.split()[1] for i in re.findall(r"{{ .* }}", data)]
for var in template_vars:
if var not in kwargs.keys():
raise MissingTemplateVariables(var=var, template=file_)
template = jinja2.Template(source=data)
out = template.render(**kwargs)
return yaml.safe_load(stream=out)