Adds methods for share and share_type
This methods allow calling api call from salt
Added share_type_present
to create or update share type
Added share_type_absent
to delete share_type
Add's example of sls
Change-Id: Ib349c1b495e8cb155195190afd614db55c55a238
Related-Issue: PROD-18221
diff --git a/_modules/manilang/__init__.py b/_modules/manilang/__init__.py
new file mode 100644
index 0000000..4d0b401
--- /dev/null
+++ b/_modules/manilang/__init__.py
@@ -0,0 +1,45 @@
+# Copyright 2018 Mirantis Inc
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+try:
+ import os_client_config
+ from keystoneauth1 import exceptions as ka_exceptions
+ REQUIREMENTS_MET = True
+except ImportError:
+ REQUIREMENTS_MET = False
+
+from manilang import share_types
+
+list_share_types = share_types.list_share_types
+create_share_type = share_types.create_share_type
+set_share_type_extra_specs = share_types.set_share_type_extra_specs
+unset_share_type_extra_specs = share_types.unset_share_type_extra_specs
+delete_share_type = share_types.delete_share_type
+
+
+__all__ = (
+ 'list_share_types', 'create_share_type',
+ 'set_share_type_extra_specs', 'unset_share_type_extra_specs',
+ 'delete_share_type',
+)
+
+
+def __virtual__():
+ """Only load manilang if requirements are available."""
+ if REQUIREMENTS_MET:
+ return 'manilang'
+ else:
+ return False, ("The manilang execution module cannot be loaded: "
+ "os_client_config or keystoneauth are unavailable.")
diff --git a/_modules/manilang/common.py b/_modules/manilang/common.py
new file mode 100644
index 0000000..47bcfd8
--- /dev/null
+++ b/_modules/manilang/common.py
@@ -0,0 +1,98 @@
+# Copyright 2018 Mirantis Inc
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import logging
+
+import os_client_config
+
+
+MANILA_HEADER = 'X-OpenStack-Manila-API-Version'
+
+
+log = logging.getLogger(__name__)
+
+
+class ManilaException(Exception):
+
+ _msg = "Manila module exception occured."
+
+ def __init__(self, message=None, **kwargs):
+ super(ManilaException, self).__init__(message or self._msg)
+
+
+class NoManilaEndpoint(ManilaException):
+ _msg = "Manila endpoint not found in keystone catalog."
+
+
+class NoAuthPluginConfigured(ManilaException):
+ _msg = ("You are using keystoneauth auth plugin that does not support "
+ "fetching endpoint list from token (noauth or admin_token).")
+
+
+class NoCredentials(ManilaException):
+ _msg = "Please provide cloud name present in clouds.yaml."
+
+
+def _get_raw_client(cloud_name):
+ service_type = 'sharev2'
+ adapter = os_client_config.make_rest_client(service_type,
+ cloud=cloud_name)
+ try:
+ access_info = adapter.session.auth.get_access(adapter.session)
+ endpoints = access_info.service_catalog.get_endpoints()
+ except (AttributeError, ValueError):
+ e = NoAuthPluginConfigured()
+ log.error('%s' % e)
+ raise e
+ if service_type not in endpoints:
+ service_type = None
+ for possible_type in ('share', 'shared-file-system'):
+ if possible_type in endpoints:
+ service_type = possible_type
+ break
+ if not service_type:
+ e = NoManilaEndpoint()
+ log.exception('%s' % e)
+ raise e
+ adapter = os_client_config.make_rest_client(service_type,
+ cloud=cloud_name)
+ log.debug("Using manila endpoint with type %s." % service_type)
+ return adapter
+
+
+def send(method, microversion_header=None):
+ def wrap(func):
+ def wrapped_f(*args, **kwargs):
+ headers = kwargs.pop('headers', {})
+ if kwargs.get('microversion'):
+ headers.setdefault(microversion_header,
+ kwargs.get('microversion'))
+ cloud_name = kwargs.pop('cloud_name')
+ if not cloud_name:
+ e = NoCredentials()
+ log.error('%s' % e)
+ raise e
+ adapter = _get_raw_client(cloud_name)
+ url, json = func(*args, **kwargs)
+ if json:
+ response = getattr(adapter, method)(url, headers=headers,
+ json=json)
+ else:
+ response = getattr(adapter, method)(url, headers=headers)
+ if not response.content:
+ return {}
+ return response.json()
+ return wrapped_f
+ return wrap
diff --git a/_modules/manilang/share_types.py b/_modules/manilang/share_types.py
new file mode 100644
index 0000000..51d4e23
--- /dev/null
+++ b/_modules/manilang/share_types.py
@@ -0,0 +1,107 @@
+# Copyright 2018 Mirantis Inc
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+from manilang.common import send, MANILA_HEADER
+
+
+@send('get', MANILA_HEADER)
+def list_share_types(**kwargs):
+ url = '/types'
+ return url, None
+
+
+@send('get', MANILA_HEADER)
+def get_default_share_types(**kwargs):
+ url = '/types/default'
+ return url, None
+
+
+@send('get', MANILA_HEADER)
+def get_share_type_detail(share_type_id, **kwargs):
+ url = '/types/{}'.format(share_type_id)
+ return url, None
+
+
+@send('get', MANILA_HEADER)
+def get_extra_specs(share_type_id, **kwargs):
+ url = '/types/{}/extra_specs'.format(share_type_id)
+ return url, None
+
+
+@send('post', MANILA_HEADER)
+def create_share_type(name, extra_specs, **kwargs):
+ json = {
+ 'share_type': {
+ 'extra_specs': extra_specs,
+ 'name': name,
+ }
+ }
+ # NOTE: passing share_type dictionary in kwargs will override anything
+ # that was constructed from function arguments. Use with caution.
+ # is_public attribute is special, as os-share-type-access:is_public
+ # always overrides share_type_access:is_public, no matter what
+ # microversion used (sic!).
+ json['share_type'].update(kwargs)
+ url = '/types'
+ return url, json
+
+
+@send('get', MANILA_HEADER)
+def get_share_type_access_details(share_type_id, **kwargs):
+ url = '/types/{}/share_type_access'.format(share_type_id)
+ return url, None
+
+
+@send('post', MANILA_HEADER)
+def set_share_type_extra_specs(share_type_id, extra_specs, **kwargs):
+ url = '/types/{}/extra_specs'.format(share_type_id)
+ json = {
+ 'extra_specs': extra_specs
+ }
+ return url, json
+
+
+@send('delete', MANILA_HEADER)
+def unset_share_type_extra_specs(share_type_id, extra_spec_key, **kwargs):
+ url = '/types/{}/extra_specs/{}'.format(share_type_id, extra_spec_key)
+ return url, None
+
+
+@send('post', MANILA_HEADER)
+def add_share_type_access(share_type_id, project, **kwargs):
+ url = '/types/{}/action'.format(share_type_id)
+ json = {
+ 'addProjectAccess': {
+ 'project': project
+ }
+ }
+ return url, json
+
+
+@send('post', MANILA_HEADER)
+def remove_share_type_access(share_type_id, project, **kwargs):
+ url = 'types/{}/action'.format(share_type_id)
+ json = {
+ 'removeProjectAccess': {
+ 'project': project
+ }
+ }
+ return url, json
+
+
+@send('delete', MANILA_HEADER)
+def delete_share_type(share_type_id, **kwargs):
+ url = '/types/{}'.format(share_type_id)
+ return url, None
diff --git a/_modules/manilang/shares.py b/_modules/manilang/shares.py
new file mode 100644
index 0000000..77e5f1a
--- /dev/null
+++ b/_modules/manilang/shares.py
@@ -0,0 +1,79 @@
+# Copyright 2018 Mirantis Inc
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import urllib
+
+from manilang.common import send, MANILA_HEADER
+
+
+@send('get', MANILA_HEADER)
+def list_shares(**kwargs):
+ url = '/shares?{}'.format(urllib.urlencode(kwargs))
+ return url, None
+
+
+@send('get', MANILA_HEADER)
+def list_shares_detailed(**kwargs):
+
+ url = '/shares/detail?{}'.format(urllib.urlencode(kwargs))
+ return url, None
+
+
+@send('get', MANILA_HEADER)
+def get_share_details(share_id, **kwargs):
+ url = '/shares/{}'.format(share_id)
+ return url, None
+
+
+@send('post', MANILA_HEADER)
+def create_share(share_proto, size, **kwargs):
+ url = '/shares'
+ json = {
+ 'share': {
+ 'share_proto': share_proto,
+ 'size': size,
+ },
+ }
+ json['share'].update(kwargs)
+ return url, json
+
+
+@send('post', MANILA_HEADER)
+def manage_share(protocol, export_path, service_host, **kwargs):
+ url = '/shares/manage'
+ json = {
+ 'share': {
+ 'protocol': protocol,
+ 'export_path': export_path,
+ 'service_host': service_host,
+ }
+ }
+ json['share'].update(kwargs)
+ return url, json
+
+
+@send('put', MANILA_HEADER)
+def update_share(share_id, **kwargs):
+ url = '/shares/{}'.format(share_id)
+ json = {
+ 'share': kwargs,
+ }
+ return url, json
+
+
+@send('delete', MANILA_HEADER)
+def delete_share(share_id, **kwargs):
+ url = '/shares/{}'.format(share_id)
+ return url, None