Use os_client config for Openstack requests

salt-fornulas require os-client config ti be used instead of
pythonclients. so this patch add implementation of such modules
and states.
Closes-issue: PROD-20032

Change-Id: Iad35cc38afd7021bf8e368c7c9a7a770e47c4ce9
diff --git a/_modules/heatv1/stack.py b/_modules/heatv1/stack.py
new file mode 100644
index 0000000..ace3db0
--- /dev/null
+++ b/_modules/heatv1/stack.py
@@ -0,0 +1,147 @@
+from yaml import safe_load
+import json
+import common
+try:
+    from urllib.parse import urlencode
+except ImportError:
+    from urllib import urlencode
+
+HEAT_ROOT = "/srv/heat/env"
+
+TEMPLATE_PATH = "template"
+ENV_PATH = "env"
+
+
+def _read_env_file(name):
+    path = "/".join([
+        HEAT_ROOT,
+        ENV_PATH,
+        name])
+
+    return _read_file(path)
+
+
+def _read_template_file(name):
+    path = "/".join([
+        HEAT_ROOT,
+        TEMPLATE_PATH,
+        name])
+
+    return _read_file(path)
+
+
+def _read_file(full_path):
+    with open(full_path, 'r') as f:
+        data = safe_load(f)
+        return json.dumps(data, default=str)
+
+
+def _read_additional_file(path):
+    full_path = "/".join([
+        HEAT_ROOT,
+        path])
+    with open(full_path) as f:
+        return str(f.read())
+
+
+@common.send("get")
+def stack_list(**kwargs):
+    url = "/stacks?{}".format(urlencode(kwargs))
+    return url, {}
+
+
+@common.get_by_name_or_uuid(stack_list, 'stacks')
+@common.send("get")
+def stack_show(stack_id, **kwargs):
+    stack_name = kwargs.get("name")
+    url = "/stacks/{stack_name}/{stack_id}".format(
+        stack_name=stack_name, stack_id=stack_id)
+    return url, {}
+
+
+@common.get_by_name_or_uuid(stack_list, 'stacks')
+@common.send("delete")
+def stack_delete(stack_id, **kwargs):
+    stack_name = kwargs.get("name")
+    url = "/stacks/{stack_name}/{stack_id}".format(stack_name=stack_name,
+                                                   stack_id=stack_id)
+    return url, {}
+
+
+@common.send("post")
+def stack_create(name, template=None, environment=None, environment_files=None,
+                 files=None, parameters=None, template_url=None,
+                 timeout_mins=5, disable_rollback=True, **kwargs):
+    url = "/stacks"
+    request = {'stack_name': name,
+               'timeout_mins': timeout_mins,
+               'disable_rollback': disable_rollback}
+    if environment:
+        request["environment"] = environment
+    file_items = {}
+    if environment_files:
+        env_names = []
+        env_files = {}
+        for f_name in environment_files:
+            data = _read_env_file(f_name)
+            env_files[f_name] = data
+            env_names.append(f_name)
+        file_items.update(env_files)
+        request["environment_files"] = env_names
+    if files:
+        for f_name, path in files.items():
+            file_items.update((f_name, _read_additional_file(path)))
+    if file_items:
+        request["files"] = file_items
+    if parameters:
+        request["parameters"] = parameters
+    if template:
+        template_file = _read_template_file(template)
+        request["template"] = template_file
+    if template_url:
+        request["template_url"] = template_url
+    # Validate the template and get back the params.
+
+    return url, {"json": request}
+
+
+@common.get_by_name_or_uuid(stack_list, 'stacks')
+@common.send("put")
+def stack_update(stack_id, template=None, environment=None,
+                 environment_files=None, files=None, parameters=None,
+                 template_url=None, timeout_mins=5, disable_rollback=True,
+                 clear_parameters=None, **kwargs):
+    stack_name = kwargs.get("name")
+    url = "/stacks/{stack_name}/{stack_id}".format(
+        stack_name=stack_name, stack_id=stack_id
+    )
+    request = {'stack_name': stack_name,
+               'timeout_mins': timeout_mins,
+               'disable_rollback': disable_rollback}
+    if environment:
+        request["environment"] = environment
+    file_items = {}
+    if environment_files:
+        env_names = []
+        env_files = {}
+        for f_name in environment_files:
+            data = _read_env_file(f_name)
+            env_files[f_name] = data
+            env_names.append(f_name)
+        file_items.update(env_files)
+        request["environment_files"] = env_names
+    if files:
+        for f_name, path in files.items():
+            file_items.update((f_name, _read_additional_file(path)))
+    if file_items:
+        request["files"] = file_items
+    if parameters:
+        request["parameters"] = parameters
+    if template:
+        template_file = _read_template_file(template)
+        request["template"] = template_file
+    if template_url:
+        request["template_url"] = template_url
+    if clear_parameters:
+        request["clear_parameters"] = clear_parameters
+    return url, {"json": request}