Add designate modules and states

Add basic work with zones via designate RESTful API

Change-Id: Ib8dca42fc00c96756f4137acdc57aeb81e95c06c
Related-Issue: PROD-24588
diff --git a/_modules/designatev2/__init__.py b/_modules/designatev2/__init__.py
new file mode 100644
index 0000000..0becffe
--- /dev/null
+++ b/_modules/designatev2/__init__.py
@@ -0,0 +1,29 @@
+try:
+    import os_client_config  # noqa
+    from keystoneauth1 import exceptions as ka_exceptions  # noqa
+    REQUIREMENTS_MET = True
+except ImportError:
+    REQUIREMENTS_MET = False
+
+from designatev2 import lists
+from designatev2 import zones
+
+zone_list = lists.zone_list
+zone_create = zones.zone_create
+zone_get_details = zones.zone_get_details
+zone_get_name_servers = zones.zone_get_name_servers
+zone_update = zones.zone_update
+zone_delete = zones.zone_delete
+
+
+__all__ = ('zone_list', 'zone_create', 'zone_delete', 'zone_update',
+           'zone_get_details', 'zone_get_name_servers',)
+
+
+def __virtual__():
+    """Only load designatev2 if requirements are available."""
+    if REQUIREMENTS_MET:
+        return 'designatev2'
+    else:
+        return False, ("The designatev2 execution module cannot be loaded: "
+                       "os_client_config or keystoneauth are unavailable.")
\ No newline at end of file
diff --git a/_modules/designatev2/arg_converter.py b/_modules/designatev2/arg_converter.py
new file mode 100644
index 0000000..76569e8
--- /dev/null
+++ b/_modules/designatev2/arg_converter.py
@@ -0,0 +1,53 @@
+import uuid
+from designatev2 import common
+from designatev2 import lists
+
+
+class CheckId(object):
+    def check_id(self, val):
+        try:
+            return str(uuid.UUID(val)).replace('-', '') == val
+        except (TypeError, ValueError, AttributeError):
+            return False
+
+
+resource_lists = {
+    'zone': lists.zone_list,
+}
+
+
+response_keys = {
+    'zone': 'zones',
+}
+
+
+def get_by_name_or_uuid_multiple(resource_arg_name_pairs):
+    def wrap(func):
+        def wrapped_f(*args, **kwargs):
+            results = []
+            args_start = 0
+            for index, (resource, arg_name) in enumerate(
+                    resource_arg_name_pairs):
+                if arg_name in kwargs:
+                    ref = kwargs.pop(arg_name, None)
+                else:
+                    ref = args[index]
+                    args_start += 1
+                cloud_name = kwargs['cloud_name']
+                checker = CheckId()
+                if checker.check_id(ref):
+                    results.append(ref)
+                else:
+                    # Then we have name not uuid
+                    resp_key = response_keys[resource]
+                    resp = resource_lists[resource](
+                        name=ref, cloud_name=cloud_name)[resp_key]
+                    if len(resp) == 0:
+                        raise common.ResourceNotFound(resp_key, ref)
+                    elif len(resp) > 1:
+                        raise common.MultipleResourcesFound(resp_key, ref)
+                    results.append(resp[0]['id'])
+                results.extend(args[args_start:])
+            return func(*results, **kwargs)
+        return wrapped_f
+    return wrap
diff --git a/_modules/designatev2/common.py b/_modules/designatev2/common.py
new file mode 100644
index 0000000..22e8ad3
--- /dev/null
+++ b/_modules/designatev2/common.py
@@ -0,0 +1,97 @@
+import logging
+import os_client_config
+
+log = logging.getLogger(__name__)
+
+
+class DesignateException(Exception):
+
+    _msg = "Designate module exception occured."
+
+    def __init__(self, message=None, **kwargs):
+        super(DesignateException, self).__init__(message or self._msg)
+
+
+class NoDesignateEndpoint(DesignateException):
+    _msg = "Designate endpoint not found in keystone catalog."
+
+
+class NoAuthPluginConfigured(DesignateException):
+    _msg = ("You are using keystoneauth auth plugin that does not support "
+            "fetching endpoint list from token (noauth or admin_token).")
+
+
+class NoCredentials(DesignateException):
+    _msg = "Please provide cloud name present in clouds.yaml."
+
+
+class ResourceNotFound(DesignateException):
+    _msg = "Uniq resource: {resource} with name: {name} not found."
+
+    def __init__(self, resource, name, **kwargs):
+        super(DesignateException, self).__init__(
+            self._msg.format(resource=resource, name=name))
+
+
+class MultipleResourcesFound(DesignateException):
+    _msg = "Multiple resource: {resource} with name: {name} found."
+
+    def __init__(self, resource, name, **kwargs):
+        super(DesignateException, self).__init__(
+            self._msg.format(resource=resource, name=name))
+
+
+def _get_raw_client(cloud_name):
+    service_type = 'dns'
+    config = os_client_config.OpenStackConfig()
+    cloud = config.get_one_cloud(cloud_name)
+    adapter = cloud.get_session_client(service_type)
+    adapter.version = 'v2'
+    try:
+        access_info = adapter.session.auth.get_access(adapter.session)
+        endpoints = access_info.service_catalog.get_endpoints()
+    except (AttributeError, ValueError):
+        e = NoAuthPluginConfigured()
+        log.exception('%s' % e)
+        raise e
+    if service_type not in endpoints:
+        if not service_type:
+            e = NoDesignateEndpoint()
+            log.error('%s' % e)
+            raise e
+    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)
+            # Remove salt internal kwargs
+            kwarg_keys = list(kwargs.keys())
+            for k in kwarg_keys:
+                if k.startswith('__'):
+                    kwargs.pop(k)
+            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 {}
+            try:
+                resp = response.json()
+            except:
+                resp = response.content
+            return resp
+        return wrapped_f
+    return wrap
diff --git a/_modules/designatev2/lists.py b/_modules/designatev2/lists.py
new file mode 100644
index 0000000..aebd573
--- /dev/null
+++ b/_modules/designatev2/lists.py
@@ -0,0 +1,12 @@
+from designatev2.common import send
+
+try:
+    from urllib.parse import urlencode
+except ImportError:
+    from urllib import urlencode
+
+
+@send('get')
+def zone_list(**kwargs):
+    url = '/zones?{}'.format(urlencode(kwargs))
+    return url, None
diff --git a/_modules/designatev2/zones.py b/_modules/designatev2/zones.py
new file mode 100644
index 0000000..8c8233d
--- /dev/null
+++ b/_modules/designatev2/zones.py
@@ -0,0 +1,41 @@
+from designatev2.common import send
+from designatev2.arg_converter import get_by_name_or_uuid_multiple
+
+
+@send('post')
+def zone_create(name, email, **kwargs):
+    url = '/zones'
+    json = {
+        'name': name,
+        'email': email,
+    }
+    json.update(kwargs)
+    return url, json
+
+
+@get_by_name_or_uuid_multiple([('zone', 'zone_id')])
+@send('get')
+def zone_get_details(zone_id, **kwargs):
+    url = '/zones/{}'.format(zone_id)
+    return url, None
+
+
+@get_by_name_or_uuid_multiple([('zone', 'zone_id')])
+@send('get')
+def zone_get_name_servers(zone_id, **kwargs):
+    url = '/zones/{}/nameservers'.format(zone_id)
+    return url, None
+
+
+@get_by_name_or_uuid_multiple([('zone', 'zone_id')])
+@send('patch')
+def zone_update(zone_id, **kwargs):
+    url = '/zones/{}'.format(zone_id)
+    return url, kwargs
+
+
+@get_by_name_or_uuid_multiple([('zone', 'zone_id')])
+@send('delete')
+def zone_delete(zone_id, **kwargs):
+    url = '/zones/{}'.format(zone_id)
+    return url, None
diff --git a/_states/designatev2.py b/_states/designatev2.py
new file mode 100644
index 0000000..3f98815
--- /dev/null
+++ b/_states/designatev2.py
@@ -0,0 +1,116 @@
+import logging
+
+
+def __virtual__():
+    return 'designatev2' if 'designatev2.zone_list' in __salt__ else False  # noqa
+
+
+log = logging.getLogger(__name__)
+
+
+def _designatev2_call(fname, *args, **kwargs):
+    return __salt__['designatev2.{}'.format(fname)](*args, **kwargs)  # noqa
+
+
+def _resource_present(resource, name, cloud_name, **kwargs):
+    try:
+        method_name = '{}_get_details'.format(resource)
+        exact_resource = _designatev2_call(
+            method_name, name, cloud_name=cloud_name
+        )[resource]
+    except Exception as e:
+        if 'ResourceNotFound' in repr(e):
+            try:
+                method_name = '{}_create'.format(resource)
+                resp = _designatev2_call(
+                    method_name, name=name, cloud_name=cloud_name, **kwargs
+                )
+            except Exception as e:
+                log.exception('Designate {0} create failed with {1}'.
+                    format(resource, e))
+                return _failed('create', name, resource)
+            return _succeeded('create', name, resource, resp)
+        elif 'MultipleResourcesFound' in repr(e):
+            return _failed('find', name, resource)
+        else:
+            raise
+
+    to_update = {}
+    for key in kwargs:
+        if key not in exact_resource or kwargs[key] != exact_resource[key]:
+            to_update[key] = kwargs[key]
+    try:
+        method_name = '{}_update'.format(resource)
+        resp = _designatev2_call(
+            method_name, name, cloud_name=cloud_name, **to_update
+        )
+    except Exception as e:
+        log.exception('Designate {0} update failed with {1}'.format(resource, e))
+        return _failed('update', name, resource)
+    return _succeeded('update', name, resource, resp)
+
+
+def _resource_absent(resource, name, cloud_name):
+    try:
+        method_name = '{}_get_details'.format(resource)
+        _designatev2_call(
+            method_name, name, cloud_name=cloud_name
+        )[resource]
+    except Exception as e:
+        if 'ResourceNotFound' in repr(e):
+            return _succeeded('absent', name, resource)
+        if 'MultipleResourcesFound' in repr(e):
+            return _failed('find', name, resource)
+    try:
+        method_name = '{}_delete'.format(resource)
+        _designatev2_call(
+            method_name, name, cloud_name=cloud_name
+        )
+    except Exception as e:
+        log.error('Designate delete {0} failed with {1}'.format(resource, e))
+        return _failed('delete', name, resource)
+    return _succeeded('delete', name, resource)
+
+
+def zone_present(name, cloud_name, email, **kwargs):
+    kwargs.update({'email': email})
+    return _resource_present('zone', name, cloud_name, **kwargs)
+
+
+def zone_absent(name, cloud_name):
+    return _resource_absent('zone', name, cloud_name)
+
+
+def _succeeded(op, name, resource, changes=None):
+    msg_map = {
+        'create': '{0} {1} created',
+        'delete': '{0} {1} removed',
+        'update': '{0} {1} updated',
+        'no_changes': '{0} {1} is in desired state',
+        'absent': '{0} {1} not present',
+        'resources_moved': '{1} resources were moved from {0}',
+    }
+    changes_dict = {
+        'name': name,
+        'result': True,
+        'comment': msg_map[op].format(resource, name),
+        'changes': changes or {},
+    }
+    return changes_dict
+
+
+def _failed(op, name, resource):
+    msg_map = {
+        'create': '{0} {1} failed to create',
+        'delete': '{0} {1} failed to delete',
+        'update': '{0} {1} failed to update',
+        'find': '{0} {1} found multiple {0}',
+        'resources_moved': 'failed to move {1} from {0}',
+    }
+    changes_dict = {
+        'name': name,
+        'result': False,
+        'comment': msg_map[op].format(resource, name),
+        'changes': {},
+    }
+    return changes_dict