kairat_kushaev | 6685262 | 2018-06-07 17:27:35 +0400 | [diff] [blame] | 1 | from yaml import safe_load |
| 2 | import json |
Oleksiy Petrenko | 5e6ebc9 | 2018-08-09 15:03:30 +0300 | [diff] [blame] | 3 | from heatv1 import common |
kairat_kushaev | 6685262 | 2018-06-07 17:27:35 +0400 | [diff] [blame] | 4 | try: |
| 5 | from urllib.parse import urlencode |
| 6 | except ImportError: |
| 7 | from urllib import urlencode |
| 8 | |
| 9 | HEAT_ROOT = "/srv/heat/env" |
| 10 | |
| 11 | TEMPLATE_PATH = "template" |
| 12 | ENV_PATH = "env" |
| 13 | |
| 14 | |
| 15 | def _read_env_file(name): |
| 16 | path = "/".join([ |
| 17 | HEAT_ROOT, |
| 18 | ENV_PATH, |
| 19 | name]) |
| 20 | |
| 21 | return _read_file(path) |
| 22 | |
| 23 | |
| 24 | def _read_template_file(name): |
| 25 | path = "/".join([ |
| 26 | HEAT_ROOT, |
| 27 | TEMPLATE_PATH, |
| 28 | name]) |
| 29 | |
| 30 | return _read_file(path) |
| 31 | |
| 32 | |
| 33 | def _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 | |
| 39 | def _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") |
| 48 | def 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") |
| 55 | def 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") |
| 64 | def 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") |
| 72 | def 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") |
| 110 | def 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} |