blob: 001dc1d65aa8a2620d90b38b7214c8a2b80a5fbe [file] [log] [blame]
kairat_kushaev66852622018-06-07 17:27:35 +04001from yaml import safe_load
2import json
Oleksiy Petrenko5e6ebc92018-08-09 15:03:30 +03003from heatv1 import common
kairat_kushaev66852622018-06-07 17:27:35 +04004try:
5 from urllib.parse import urlencode
6except ImportError:
7 from urllib import urlencode
8
9HEAT_ROOT = "/srv/heat/env"
10
11TEMPLATE_PATH = "template"
12ENV_PATH = "env"
13
14
15def _read_env_file(name):
16 path = "/".join([
17 HEAT_ROOT,
18 ENV_PATH,
19 name])
20
21 return _read_file(path)
22
23
24def _read_template_file(name):
25 path = "/".join([
26 HEAT_ROOT,
27 TEMPLATE_PATH,
28 name])
29
30 return _read_file(path)
31
32
33def _read_file(full_path):
34 with open(full_path, 'r') as f:
35 data = safe_load(f)
36 return json.dumps(data, default=str)
37
38
39def _read_additional_file(path):
40 full_path = "/".join([
41 HEAT_ROOT,
42 path])
43 with open(full_path) as f:
44 return str(f.read())
45
46
47@common.send("get")
48def stack_list(**kwargs):
49 url = "/stacks?{}".format(urlencode(kwargs))
50 return url, {}
51
52
53@common.get_by_name_or_uuid(stack_list, 'stacks')
54@common.send("get")
55def stack_show(stack_id, **kwargs):
56 stack_name = kwargs.get("name")
57 url = "/stacks/{stack_name}/{stack_id}".format(
58 stack_name=stack_name, stack_id=stack_id)
59 return url, {}
60
61
62@common.get_by_name_or_uuid(stack_list, 'stacks')
63@common.send("delete")
64def stack_delete(stack_id, **kwargs):
65 stack_name = kwargs.get("name")
66 url = "/stacks/{stack_name}/{stack_id}".format(stack_name=stack_name,
67 stack_id=stack_id)
68 return url, {}
69
70
71@common.send("post")
72def stack_create(name, template=None, environment=None, environment_files=None,
73 files=None, parameters=None, template_url=None,
74 timeout_mins=5, disable_rollback=True, **kwargs):
75 url = "/stacks"
76 request = {'stack_name': name,
77 'timeout_mins': timeout_mins,
78 'disable_rollback': disable_rollback}
79 if environment:
80 request["environment"] = environment
81 file_items = {}
82 if environment_files:
83 env_names = []
84 env_files = {}
85 for f_name in environment_files:
86 data = _read_env_file(f_name)
87 env_files[f_name] = data
88 env_names.append(f_name)
89 file_items.update(env_files)
90 request["environment_files"] = env_names
91 if files:
92 for f_name, path in files.items():
93 file_items.update((f_name, _read_additional_file(path)))
94 if file_items:
95 request["files"] = file_items
96 if parameters:
97 request["parameters"] = parameters
98 if template:
99 template_file = _read_template_file(template)
100 request["template"] = template_file
101 if template_url:
102 request["template_url"] = template_url
103 # Validate the template and get back the params.
104
105 return url, {"json": request}
106
107
108@common.get_by_name_or_uuid(stack_list, 'stacks')
109@common.send("put")
110def stack_update(stack_id, template=None, environment=None,
111 environment_files=None, files=None, parameters=None,
112 template_url=None, timeout_mins=5, disable_rollback=True,
113 clear_parameters=None, **kwargs):
114 stack_name = kwargs.get("name")
115 url = "/stacks/{stack_name}/{stack_id}".format(
116 stack_name=stack_name, stack_id=stack_id
117 )
118 request = {'stack_name': stack_name,
119 'timeout_mins': timeout_mins,
120 'disable_rollback': disable_rollback}
121 if environment:
122 request["environment"] = environment
123 file_items = {}
124 if environment_files:
125 env_names = []
126 env_files = {}
127 for f_name in environment_files:
128 data = _read_env_file(f_name)
129 env_files[f_name] = data
130 env_names.append(f_name)
131 file_items.update(env_files)
132 request["environment_files"] = env_names
133 if files:
134 for f_name, path in files.items():
135 file_items.update((f_name, _read_additional_file(path)))
136 if file_items:
137 request["files"] = file_items
138 if parameters:
139 request["parameters"] = parameters
140 if template:
141 template_file = _read_template_file(template)
142 request["template"] = template_file
143 if template_url:
144 request["template_url"] = template_url
145 if clear_parameters:
146 request["clear_parameters"] = clear_parameters
147 return url, {"json": request}