Merge "Use os_client config for Openstack requests"
diff --git a/_modules/heatv1/__init__.py b/_modules/heatv1/__init__.py
new file mode 100644
index 0000000..51d90b4
--- /dev/null
+++ b/_modules/heatv1/__init__.py
@@ -0,0 +1,38 @@
+"""
+Module for handling Heat stacks.
+
+:depends:   - os_client_config
+:configuration: This module is not usable until the following are specified
+"""
+
+try:
+    import os_client_config
+    REQUIREMENTS_MET = True
+except ImportError:
+    REQUIREMENTS_MET = False
+
+import os
+import sys
+
+# i failed to load module witjout this
+# seems bugs in salt or it is only me
+sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
+
+import stack
+
+stack_create = stack.stack_create
+stack_delete = stack.stack_delete
+stack_list = stack.stack_list
+stack_show = stack.stack_show
+stack_update = stack.stack_update
+
+__all__ = ('stack_create', 'stack_list', 'stack_delete', 'stack_show',
+           'stack_update')
+
+
+def __virtual__():
+    if REQUIREMENTS_MET:
+        return 'heatv1'
+    else:
+        return False, ("The heat execution module cannot be loaded: "
+                       "os_client_config is not available.")
diff --git a/_modules/heatv1/common.py b/_modules/heatv1/common.py
new file mode 100644
index 0000000..06eabff
--- /dev/null
+++ b/_modules/heatv1/common.py
@@ -0,0 +1,109 @@
+import logging
+import six
+import uuid
+
+import os_client_config
+from salt import exceptions
+
+
+log = logging.getLogger(__name__)
+
+SERVICE_KEY = 'orchestration'
+
+
+def get_raw_client(cloud_name):
+    config = os_client_config.OpenStackConfig()
+    cloud = config.get_one_cloud(cloud_name)
+    adapter = cloud.get_session_client(SERVICE_KEY)
+    adapter.version = '1'
+    try:
+        access_info = adapter.session.auth.get_access(adapter.session)
+        endpoints = access_info.service_catalog.get_endpoints()
+    except (AttributeError, ValueError) as exc:
+        six.raise_from(exc, exceptions.SaltInvocationError(
+            "Cannot load keystoneauth plugin. Please check your environment "
+            "configuration."))
+    if SERVICE_KEY not in endpoints:
+        raise exceptions.SaltInvocationError("Cannot find heat endpoint in "
+                                             "environment endpoint list.")
+    return adapter
+
+
+def send(method):
+    def wrap(func):
+        @six.wraps(func)
+        def wrapped_f(*args, **kwargs):
+            cloud_name = kwargs.get('cloud_name', None)
+            if not cloud_name:
+                raise exceptions.SaltInvocationError(
+                    "No cloud_name specified. Please provide cloud_name "
+                    "parameter")
+            adapter = get_raw_client(cloud_name)
+            # Remove salt internal kwargs
+            kwarg_keys = list(kwargs.keys())
+            for k in kwarg_keys:
+                if k.startswith('__'):
+                    kwargs.pop(k)
+            url, request_kwargs = func(*args, **kwargs)
+            try:
+                response = getattr(adapter, method.lower())(url,
+                                                            **request_kwargs)
+            except Exception as e:
+                log.exception("Error occured when executing request")
+                return {"result": False,
+                        "comment": str(e),
+                        "status_code": getattr(e, "http_status", 500)}
+            try:
+                resp_body = response.json() if response.content else {}
+            except:
+                resp_body = str(response.content)
+            return {"result": True,
+                    "body": resp_body,
+                    "status_code": response.status_code}
+        return wrapped_f
+    return wrap
+
+
+def _check_uuid(val):
+    try:
+        return str(uuid.UUID(val)) == val
+    except (TypeError, ValueError, AttributeError):
+        return False
+
+
+def get_by_name_or_uuid(resource_list, resp_key):
+    def wrap(func):
+        @six.wraps(func)
+        def wrapped_f(*args, **kwargs):
+            if 'name' in kwargs:
+                ref = kwargs.get('name', None)
+                start_arg = 0
+            else:
+                start_arg = 1
+                ref = args[0]
+                kwargs["name"] = ref
+            if _check_uuid(ref):
+                uuid = ref
+            else:
+                # Then we have name not uuid
+                cloud_name = kwargs['cloud_name']
+                resp = resource_list(
+                    name=ref, cloud_name=cloud_name)["body"][resp_key]
+                if len(resp) == 0:
+                    msg = ("Uniq {resource} resource "
+                           "with name={name} not found.").format(
+                        resource=resp_key, name=ref)
+                    return {"result": False,
+                            "body": msg,
+                            "status_code": 404}
+                elif len(resp) > 1:
+                    msg = ("Multiple resource: {resource} "
+                           "with name: {name} found ").format(
+                        resource=resp_key, name=ref)
+                    return {"result": False,
+                            "body": msg,
+                            "status_code": 400}
+                uuid = resp[0]['id']
+            return func(uuid, *args[start_arg:], **kwargs)
+        return wrapped_f
+    return wrap
\ No newline at end of file
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}
diff --git a/_states/heatv1.py b/_states/heatv1.py
new file mode 100644
index 0000000..4ee8cf5
--- /dev/null
+++ b/_states/heatv1.py
@@ -0,0 +1,131 @@
+# Import Python libs
+from __future__ import absolute_import, print_function, unicode_literals
+import logging
+import time
+
+LOG = logging.getLogger(__name__)
+
+
+def __virtual__():
+    return 'heatv1'
+
+
+def _heat_call(fname, *args, **kwargs):
+    return __salt__['heatv1.{}'.format(fname)](*args, **kwargs)
+
+
+def _poll_for_complete(stack_name, cloud_name=None, action=None,
+                       poll_period=5, timeout=60):
+    if action:
+        stop_status = ('{0}_FAILED'.format(action), '{0}_COMPLETE'.format(action))
+        stop_check = lambda a: a in stop_status
+    else:
+        stop_check = lambda a: a.endswith('_COMPLETE') or a.endswith('_FAILED')
+    timeout_sec = timeout * 60
+    msg_template = '\n Stack %(name)s %(status)s \n'
+    while True:
+        stack = _heat_call('stack_show',
+                           name=stack_name,
+                           cloud_name=cloud_name)
+        if not stack["result"]:
+            raise Exception("request for stack failed")
+
+        stack = stack["body"]["stack"]
+        stack_status = stack["stack_status"]
+        msg = msg_template % dict(
+            name=stack_name, status=stack_status)
+        if stop_check(stack_status):
+            return stack_status, msg
+
+        time.sleep(poll_period)
+        timeout_sec -= poll_period
+        if timeout_sec <= 0:
+            stack_status = '{0}_FAILED'.format(action)
+            msg = 'Timeout expired'
+            return stack_status, msg
+
+
+def stack_present(name, cloud_name, template=None,
+                  environment=None, params=None, poll=5, rollback=False,
+                  timeout=60, profile=None, **connection_args):
+    LOG.debug('Deployed with(' +
+              '{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8} {9})'
+              .format(name, cloud_name, template, environment, params,
+                      poll, rollback, timeout, profile, connection_args))
+    ret = {'name': None,
+           'comment': '',
+           'changes': {},
+           'result': True}
+
+    if not name:
+        ret['result'] = False
+        ret['comment'] = 'Name is not valid'
+        return ret
+
+    ret['name'] = name,
+
+    existing_stack = _heat_call('stack_show', name=name,
+                                cloud_name=cloud_name)
+
+    if existing_stack['result']:
+        _heat_call('stack_update', name=name,
+                   template=template,
+                   cloud_name=cloud_name,
+                   environment=environment,
+                   parameters=params,
+                   disable_rollback=not rollback,
+                   timeout=timeout)
+        ret['changes']['comment'] = 'Updated stack'
+        status, res = _poll_for_complete(stack_name=name,
+                                         cloud_name=cloud_name,
+                                         action="UPDATE", timeout=timeout)
+        ret["result"] = status == "UPDATE_COMPLETE"
+        ret['comment'] = res
+    else:
+        _heat_call('stack_create',
+                   name=name,
+                   template=template,
+                   cloud_name=cloud_name,
+                   environment=environment,
+                   parameters=params,
+                   disable_rollback=not rollback,
+                   timeout=timeout)
+        status, res = _poll_for_complete(stack_name=name,
+                                         cloud_name=cloud_name,
+                                         action="CREATE", timeout=timeout)
+        ret["result"] = status == "CREATE_COMPLETE"
+        ret['comment'] = res
+    ret['changes']['stack_name'] = name
+    return ret
+
+
+def stack_absent(name, cloud_name, poll=5, timeout=60):
+    LOG.debug('Absent with(' +
+              '{0}, {1}, {2})'.format(name, poll, cloud_name))
+    ret = {'name': None,
+           'comment': '',
+           'changes': {},
+           'result': True}
+    if not name:
+        ret['result'] = False
+        ret['comment'] = 'Name is not valid'
+        return ret
+
+    ret['name'] = name,
+
+    existing_stack = _heat_call('stack_show',
+                                name=name, cloud_name=cloud_name)
+
+    if not existing_stack['result']:
+        ret['result'] = True
+        ret['comment'] = 'Stack does not exist'
+        return ret
+
+    _heat_call('stack_delete', name=name, cloud_name=cloud_name)
+    status, comment = _poll_for_complete(stack_name=name,
+                                         cloud_name=cloud_name,
+                                         action="DELETE", timeout=timeout)
+    ret['result'] = status == "DELETE_COMPLETE"
+    ret['comment'] = comment
+    ret['changes']['stack_name'] = name
+    return ret