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/__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}