Merge "Add Salt 2018.3 tests"
diff --git a/README.rst b/README.rst
index 5fcede9..e3e4bad 100644
--- a/README.rst
+++ b/README.rst
@@ -92,6 +92,25 @@
               - server2
             glusterfs_volume_pattern: manila-share-volume-d+$
 
+Client usage:
+=============
+
+The `manila.client` state provides ability to manage manila resources.
+
+Manage `share_type`
+
+.. code-block:: yaml
+
+
+    manila:
+      client:
+        enabled: true
+        server:
+          admin_identity:
+            share_type:
+              default:
+                extra_specs:
+                  driver_handles_share_servers: false
 
 More information
 ================
diff --git a/_modules/manilang.py b/_modules/manilang.py
deleted file mode 100644
index 633e363..0000000
--- a/_modules/manilang.py
+++ /dev/null
@@ -1,121 +0,0 @@
-import logging
-
-
-try:
-    import os_client_config
-    from keystoneauth1 import exceptions as ka_exceptions
-    REQUIREMENTS_MET = True
-except ImportError:
-    REQUIREMENTS_MET = False
-
-
-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.")
-
-
-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.error('%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 _add_microversion_header(microversion, headers):
-    if microversion:
-        headers.setdefault('X-OpenStack-Manila-API-Version', microversion)
-
-
-def create_adapter(fun):
-    def inner(*args, **kwargs):
-        headers = kwargs.pop('headers', {})
-        _add_microversion_header(kwargs.get('microversion'), headers)
-        cloud_name = kwargs.get('cloud_name')
-        if not cloud_name:
-            e = NoCredentials()
-            log.error('%s' % e)
-            raise e
-        adapter = _get_raw_client(cloud_name)
-        return fun(*args, adapter=adapter, headers=headers, **kwargs)
-    return inner
-
-
-@create_adapter
-def get_default_share_types(**kwargs):
-    adapter = kwargs.get('adapter')
-    try:
-        response = adapter.get('/types/default',
-                               headers=kwargs.get('headers', {}))
-    except ka_exceptions.NotFound:
-        log.debug("No default share type found.")
-        return None
-    return response.json()
-
-
-@create_adapter
-def create_share_type(name, driver_handles_share_servers, extra_specs=None,
-                      is_public=True, **kwargs):
-    adapter = kwargs.get('adapter')
-    extra_specs = extra_specs or {}
-    extra_specs['driver_handles_share_servers'] = driver_handles_share_servers
-    post_data = {
-        'share_type': {
-            'extra_specs': extra_specs, 'name': name,
-            'os-share-type-access:is_public': is_public}}
-    # 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!).
-    post_data['share_type'].update(kwargs.get('share_type', {}))
-    response = adapter.post('/types', json=post_data,
-                            headers=kwargs.get('headers', {}))
-    return response.json()['share_type']
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
diff --git a/_states/example.sls b/_states/example.sls
new file mode 100644
index 0000000..55afcba
--- /dev/null
+++ b/_states/example.sls
@@ -0,0 +1,13 @@
+present_example:
+  manilang.share_type_present:
+    - microversion: '2.4'
+    - cloud_name: admin
+    - extra_specs:
+        driver_handles_share_servers: false
+        snapshot_support : true
+        key: value
+
+absent_example:
+    manilang.share_type_absent:
+      - microversion: '2.4'
+      - cloud_name: admin
diff --git a/_states/manilang.py b/_states/manilang.py
new file mode 100644
index 0000000..964518a
--- /dev/null
+++ b/_states/manilang.py
@@ -0,0 +1,236 @@
+# 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 oslo_utils.strutils import bool_from_string
+import logging
+log = logging.getLogger(__name__)
+
+
+def __virtual__():
+    '''
+    Only load if manila module is present in __salt__
+    '''
+    return 'manilang' if 'manilang.list_share_types' in __salt__ else False
+
+
+manilang_func = {
+    'list_types': 'manilang.list_share_types',
+    'create_type': 'manilang.create_share_type',
+    'set_type_specs': 'manilang.set_share_type_extra_specs',
+    'unset_type_specs': 'manilang.unset_share_type_extra_specs',
+    'delete_type': 'manilang.delete_share_type',
+}
+
+
+def share_type_present(name, extra_specs, cloud_name, microversion=None,
+                       **kwargs):
+    """
+    Ensure that share_type is present and has desired parameters
+
+    This function will create the desired share type if one with the requested
+    name does not exist. If it does, it will be updated to correspond to
+    parameters passed to this function.
+
+    :param name: name of the share type.
+    :param extra_specs: dictionary of extra_specs that share type should have.
+    It contains one required parameter - driver_handles_share_servers.
+    :param kwargs: other arguments that will be pushed into share_type
+        dictionary to be POSTed, if specified.
+
+    """
+    for key in extra_specs:
+        try:
+            extra_specs[key] = str(
+                bool_from_string(extra_specs[key], strict=True)
+            )
+        except ValueError:
+            extra_specs[key] = str(extra_specs[key])
+
+    origin_share_types = __salt__[
+        manilang_func['list_types']
+    ](cloud_name=cloud_name)['share_types']
+    share_types = [
+        share_type
+        for share_type in origin_share_types if share_type['name'] == name
+    ]
+    if not share_types:
+        try:
+            res = __salt__[
+                manilang_func['create_type']
+            ](name, extra_specs, cloud_name=cloud_name,
+              microversion=microversion, **kwargs)
+        except Exception as e:
+            log.error('Manila share type create failed with {}'.format(e))
+            return _create_failed(name, 'resource')
+        return _created(name, 'share_type', res)
+
+    elif len(share_types) == 1:
+        exact_share_type = share_types[0]
+
+        api_extra_specs = exact_share_type['extra_specs']
+        api_keys = set(api_extra_specs)
+        sls_keys = set(extra_specs)
+
+        to_delete = api_keys - sls_keys
+        to_add = sls_keys - api_keys
+        to_update = sls_keys & api_keys
+        resp = {}
+
+        for key in to_delete:
+            try:
+                __salt__[
+                    manilang_func['unset_type_specs']
+                ](exact_share_type['id'], key, cloud_name=cloud_name,
+                  microversion=microversion)
+            except Exception as e:
+                log.error(
+                    'Manila share type delete '
+                    'extra specs failed with {}'.format(e)
+                )
+                return _update_failed(name, 'share_type_extra_specs')
+            resp.update({'deleted_extra_specs': tuple(to_delete)})
+
+        diff = {}
+
+        for key in to_add:
+            diff[key] = extra_specs[key]
+        for key in to_update:
+            if extra_specs[key] != api_extra_specs[key]:
+                diff[key] = extra_specs[key]
+        if diff:
+            try:
+                resp.update(
+                    __salt__[
+                        manilang_func['set_type_specs']
+                    ](exact_share_type['id'],  diff,
+                      cloud_name=cloud_name, microversion=microversion)
+                )
+            except Exception as e:
+                log.error(
+                    'Manila share type update '
+                    'extra specs failed with {}'.format(e)
+                )
+                return _update_failed(name, 'share_type_extra_specs')
+        if to_delete or diff:
+            return _updated(name, 'share_type', resp)
+        return _no_changes(name, 'share_type')
+    else:
+        return _find_failed(name, 'share_type')
+
+
+def share_type_absent(name, cloud_name):
+    origin_share_types = __salt__[
+        manilang_func['list_types']
+    ](cloud_name=cloud_name)['share_types']
+    share_types = [
+        share_type
+        for share_type in origin_share_types if share_type['name'] == name
+    ]
+    if not share_types:
+        return _absent(name, 'share_type')
+    elif len(share_types) == 1:
+        try:
+            __salt__[manilang_func['delete_type']](share_types[0]['id'],
+                                                   cloud_name=cloud_name)
+        except Exception as e:
+            log.error('Manila share type delete failed with {}'.format(e))
+            return _delete_failed(name, 'share_type')
+        return _deleted(name, 'share_type')
+    else:
+        return _find_failed(name, 'share_type')
+
+
+def _created(name, resource, resource_definition):
+    changes_dict = {
+        'name': name,
+        'changes': resource_definition,
+        'result': True,
+        'comment': '{}{} created'.format(resource, name)
+    }
+    return changes_dict
+
+
+def _updated(name, resource, resource_definition):
+    changes_dict = {
+        'name': name,
+        'changes': resource_definition,
+        'result': True,
+        'comment': '{}{} updated'.format(resource, name)
+    }
+    return changes_dict
+
+
+def _no_changes(name, resource):
+    changes_dict = {
+        'name': name,
+        'changes': {},
+        'result': True,
+        'comment': '{}{} is in desired state'.format(resource, name)
+    }
+    return changes_dict
+
+
+def _deleted(name, resource):
+    changes_dict = {
+        'name': name,
+        'changes': {},
+        'result': True,
+        'comment': '{}{} removed'.format(resource, name)
+    }
+    return changes_dict
+
+
+def _absent(name, resource):
+    changes_dict = {'name': name,
+                    'changes': {},
+                    'comment': '{0} {1} not present'.format(resource, name),
+                    'result': True}
+    return changes_dict
+
+
+def _delete_failed(name, resource):
+    changes_dict = {'name': name,
+                    'changes': {},
+                    'comment': '{0} {1} failed to delete'.format(resource,
+                                                                 name),
+                    'result': False}
+    return changes_dict
+
+
+def _create_failed(name, resource):
+    changes_dict = {'name': name,
+                    'changes': {},
+                    'comment': '{0} {1} failed to create'.format(resource,
+                                                                 name),
+                    'result': False}
+    return changes_dict
+
+
+def _update_failed(name, resource):
+    changes_dict = {'name': name,
+                    'changes': {},
+                    'comment': '{0} {1} failed to update'.format(resource,
+                                                                 name),
+                    'result': False}
+    return changes_dict
+
+
+def _find_failed(name, resource):
+    changes_dict = {
+        'name': name,
+        'changes': {},
+        'comment': '{0} {1} found multiple {0}'.format(resource, name),
+        'result': False,
+    }
+    return changes_dict
diff --git a/manila/client.sls b/manila/client.sls
index 378f831..85f9b82 100644
--- a/manila/client.sls
+++ b/manila/client.sls
@@ -6,4 +6,21 @@
     - names: {{ client.pkgs }}
     - install_recommends: False
 
+{%- for identity_name, identity in client.server.iteritems() %}
+{%- if identity.share_type is defined %}
+{%- for share_type_name, share_type in identity.share_type.iteritems() %}
+
+manila_share_type_{{ share_type_name }}:
+  manilang.share_type_present:
+    - cloud_name: {{ identity_name }}
+    - name: {{ share_type_name }}
+    - extra_specs: {{ share_type.extra_specs }}
+    {%- if share_type.microversion is defined %}
+    - microversion: {{ share_type.microversion |string }}
+    {%- endif %}
+
+{%- endfor %}
+{%- endif %}
+{%- endfor %}
+
 {%- endif %}
diff --git a/metadata/service/client/init.yml b/metadata/service/client/init.yml
new file mode 100644
index 0000000..d91f9b9
--- /dev/null
+++ b/metadata/service/client/init.yml
@@ -0,0 +1,6 @@
+applications:
+  - manila
+parameters:
+  manila:
+    client:
+      enabled: true