Use os_client config for Openstack requests
we need to use os_client config and direct api calls
because it is very stable.
Closes-Issue: PROD-20033
Change-Id: I3645f0426030bbbfa63b7bbc75a41f8d0df86dba
diff --git a/_modules/cinderv3/__init__.py b/_modules/cinderv3/__init__.py
new file mode 100644
index 0000000..650a6a2
--- /dev/null
+++ b/_modules/cinderv3/__init__.py
@@ -0,0 +1,33 @@
+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 volume
+
+volume_list = volume.volume_list
+volume_type_list = volume.volume_type_list
+volume_type_get = volume.volume_type_get
+volume_type_create = volume.volume_type_create
+volume_type_delete = volume.volume_type_delete
+keys_volume_type_get = volume.keys_volume_type_get
+keys_volume_type_set = volume.keys_volume_type_set
+
+__all__ = ('volume_list', 'volume_type_list', 'volume_type_get',
+ 'volume_type_create', 'keys_volume_type_get',
+ 'keys_volume_type_set', 'volume_type_delete')
+
+
+def __virtual__():
+ if REQUIREMENTS_MET:
+ return 'cinderv3'
+ else:
+ return False, ("The cinderv3 execution module cannot be loaded: "
+ "os_client_config are unavailable.")
diff --git a/_modules/cinderv3/common.py b/_modules/cinderv3/common.py
new file mode 100644
index 0000000..93477a3
--- /dev/null
+++ b/_modules/cinderv3/common.py
@@ -0,0 +1,106 @@
+import six
+import logging
+import uuid
+
+import os_client_config
+from salt import exceptions
+
+
+log = logging.getLogger(__name__)
+
+SERVICE_KEY = 'volumev3'
+
+
+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 = '3'
+ 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 cinder endpoint in "
+ "environment endpoint list.")
+ return adapter
+
+
+def send(method):
+ def wrap(func):
+ @six.wraps(func)
+ def wrapped_f(*args, **kwargs):
+ cloud_name = kwargs.pop('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)
+ 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)}
+ return {"result": True,
+ "body": response.json() if response.content else {},
+ "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.pop('name', None)
+ start_arg = 0
+ else:
+ start_arg = 1
+ ref = args[0]
+ item_id = None
+ if _check_uuid(ref):
+ item_id = ref
+ else:
+ cloud_name = kwargs['cloud_name']
+ # seems no filtering on volume type name in cinder
+ resp = resource_list(cloud_name=cloud_name)["body"][resp_key]
+ # so need to search in list directly
+ for item in resp:
+ if item["name"] == ref:
+ if item_id is not None:
+ msg = ("Multiple resource: {resource} "
+ "with name: {name} found ").format(
+ resource=resp_key, name=ref)
+ return {"result": False,
+ "body": msg,
+ "status_code": 400}
+ item_id = item["id"]
+ if not item_id:
+ msg = ("Uniq {resource} resource "
+ "with name={name} not found.").format(
+ resource=resp_key, name=ref)
+ return {"result": False,
+ "body": msg,
+ "status_code": 404}
+ return func(item_id, *args[start_arg:], **kwargs)
+ return wrapped_f
+ return wrap
diff --git a/_modules/cinderv3/volume.py b/_modules/cinderv3/volume.py
new file mode 100644
index 0000000..deaaf6d
--- /dev/null
+++ b/_modules/cinderv3/volume.py
@@ -0,0 +1,95 @@
+from __future__ import absolute_import
+
+import common
+
+try:
+ from urllib.parse import urlencode
+except ImportError:
+ from urllib import urlencode
+
+
+@common.send("get")
+def volume_list(**kwargs):
+ """
+ Return list of cinder volumes.
+ """
+ url = '/volumes?{}'.format(urlencode(kwargs))
+ return url, {}
+
+
+@common.send("get")
+def volume_type_list(**kwargs):
+ """
+ Return list of volume types
+ """
+ url = '/types?{}'.format(urlencode(kwargs))
+ return url, {}
+
+
+@common.get_by_name_or_uuid(volume_type_list, 'volume_types')
+@common.send("get")
+def volume_type_get(volume_type_id, **kwargs):
+ """
+ Returns id of the specified volume type name
+ """
+ url = "/types/{volume_type_id}".format(volume_type_id=volume_type_id)
+ return url, {}
+
+
+@common.get_by_name_or_uuid(volume_type_list, 'volume_types')
+@common.send("delete")
+def volume_type_delete(volume_type_id, **kwargs):
+ """
+ delete the specified volume type
+ """
+ url = "/types/{volume_type_id}".format(volume_type_id=volume_type_id)
+ return url, {}
+
+
+@common.send("post")
+def volume_type_create(name, **kwargs):
+ """
+ Create cinder volume type
+ """
+ url = "/types"
+ req = {"volume_type": {"name": name}}
+ return url, {'json': req}
+
+
+@common.get_by_name_or_uuid(volume_type_list, 'volume_types')
+@common.send("get")
+def keys_volume_type_get(volume_type_id, **kwargs):
+ """
+ Return extra specs of the specified volume type.
+ """
+ url = "/types/{volume_type_id}/extra_specs".format(
+ volume_type_id=volume_type_id)
+ return url, {}
+
+
+@common.send("put")
+def _key_volume_type_set(type_id, key, value, **kwargs):
+ url = "/types/{volume_type_id}/extra_specs/{key}".format(
+ volume_type_id=type_id, key=key)
+ return url, {'json': {str(key): str(value)}}
+
+
+@common.get_by_name_or_uuid(volume_type_list, 'volume_types')
+def keys_volume_type_set(volume_type_id, keys=None, **kwargs):
+ """
+ Set extra specs of the specified volume type.
+ """
+ if keys is None:
+ keys = {}
+ cloud_name = kwargs["cloud_name"]
+ cur_keys = keys_volume_type_get(
+ volume_type_id, cloud_name=cloud_name)["body"]["extra_specs"]
+
+ for k, v in keys.items():
+ if (k, v) in cur_keys.items():
+ continue
+ resp = _key_volume_type_set(volume_type_id, k, v, cloud_name=cloud_name)
+ if resp.get("result") is False:
+ return resp
+
+ return {"result": True}
diff --git a/_states/cinderv3.py b/_states/cinderv3.py
new file mode 100644
index 0000000..c45e970
--- /dev/null
+++ b/_states/cinderv3.py
@@ -0,0 +1,99 @@
+"""
+Management of Cinder resources
+"""
+
+import ast
+import logging
+
+LOG = logging.getLogger(__name__)
+
+
+def __virtual__():
+ return 'cinderv3'
+
+
+def _cinder_call(fname, *args, **kwargs):
+ return __salt__['cinderv3.{}'.format(fname)](*args, **kwargs)
+
+
+def volume_type_present(name=None, cloud_name=None):
+ """
+ Ensures that the specified volume type is present.
+ """
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': True,
+ 'comment': 'Volume type "{0}" already exists'.format(name)
+ }
+ type_req = _cinder_call('volume_type_get', name=name, cloud_name=cloud_name)
+ if type_req.get("result"):
+ return ret
+ else:
+ create_req = _cinder_call('volume_type_create', name=name,
+ cloud_name=cloud_name)
+ if create_req.get("result") is False:
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': 'Volume type "{0}" failed to create'.format(name)
+ }
+ else:
+ ret['comment'] = 'Volume type {0} has been created'.format(name)
+ ret['changes']['Volume type'] = 'Created'
+ return ret
+
+
+def volume_type_absent(name=None, cloud_name=None):
+ """
+ Ensures that the specified volume type is absent.
+ """
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': True,
+ 'comment': 'Volume type "{0}" not found'.format(name)
+ }
+ type_req = _cinder_call('volume_type_get', name=name, cloud_name=cloud_name)
+ if not type_req.get("result"):
+ return ret
+ else:
+ delete_req = _cinder_call('volume_type_delete', name=name,
+ cloud_name=cloud_name)
+ if delete_req.get("result") is False:
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': 'Volume type "{0}" failed to delete'.format(name)
+ }
+ else:
+ ret['comment'] = 'Volume type {0} has been deleted'.format(name)
+ ret['changes']['Volume type'] = 'Deleted'
+ return ret
+
+
+def volume_type_key_present(name=None, key=None, value=None, cloud_name=None):
+ """
+ Ensures that the extra specs are present on a volume type.
+ """
+ keys = "{u'" + key + "': u'" + value + "'}"
+ keys = ast.literal_eval(keys)
+ signal_create = _cinder_call('keys_volume_type_set',
+ name=name, keys=keys, cloud_name=cloud_name)
+ if signal_create["result"] is True:
+ ret = {
+ 'name': name,
+ 'changes': keys,
+ 'result': True,
+ 'comment': 'Volume type "{0}" was updated'.format(name)
+ }
+ else:
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': signal_create.get("comment")
+ }
+ return ret