This repository was archived by the owner on Jul 25, 2024. It is now read-only.
forked from RedHatQE/openshift-python-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
45 lines (33 loc) · 1.11 KB
/
utils.py
File metadata and controls
45 lines (33 loc) · 1.11 KB
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
39
40
41
42
43
44
45
import re
import jinja2
import yaml
class MissingTemplateVariables(Exception):
def __init__(self, var, template):
self.var = var
self.template = template
def __str__(self):
return f"Missing variables {self.var} for template {self.template}"
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)