Merge "Move to Mirantis owned docker images for tests         - This is a temporary solution until we have our own images"
diff --git a/_modules/aodhv2/__init__.py b/_modules/aodhv2/__init__.py
new file mode 100644
index 0000000..7eb1f85
--- /dev/null
+++ b/_modules/aodhv2/__init__.py
@@ -0,0 +1,32 @@
+try:
+    import os_client_config  # noqa
+    from keystoneauth1 import exceptions as ka_exceptions  # noqa
+    REQUIREMENTS_MET = True
+except ImportError:
+    REQUIREMENTS_MET = False
+
+from aodhv2 import lists
+from aodhv2 import alarms
+
+alarm_list = lists.alarm_list
+alarm_create = alarms.alarm_create
+alarm_get_details = alarms.alarm_get_details
+alarm_update = alarms.alarm_update
+alarm_delete = alarms.alarm_delete
+alarm_history_get = alarms.alarm_history_get
+alarm_state_set = alarms.alarm_state_set
+alarm_state_get = alarms.alarm_state_get
+
+
+__all__ = ('alarm_list', 'alarm_create', 'alarm_delete', 'alarm_get_details',
+           'alarm_history_get', 'alarm_state_get', 'alarm_state_set',
+           'alarm_update')
+
+
+def __virtual__():
+    """Only load aodhv2 if requirements are available."""
+    if REQUIREMENTS_MET:
+        return 'aodhv2'
+    else:
+        return False, ("The aodhv2 execution module cannot be loaded: "
+                       "os_client_config or keystoneauth are unavailable.")
diff --git a/_modules/aodhv2/alarms.py b/_modules/aodhv2/alarms.py
new file mode 100644
index 0000000..9d8a6dc
--- /dev/null
+++ b/_modules/aodhv2/alarms.py
@@ -0,0 +1,50 @@
+from aodhv2.common import send
+from aodhv2.arg_converter import get_by_name_or_uuid_multiple
+
+
+@send('post')
+def alarm_create(**kwargs):
+    url = '/alarms'
+    return url, kwargs
+
+
+@get_by_name_or_uuid_multiple([('alarm', 'alarm_id')])
+@send('get')
+def alarm_get_details(alarm_id, **kwargs):
+    url = '/alarms/{}'.format(alarm_id)
+    return url, None
+
+
+@get_by_name_or_uuid_multiple([('alarm', 'alarm_id')])
+@send('put')
+def alarm_update(alarm_id, **kwargs):
+    url = '/alarms/{}'.format(alarm_id)
+    return url, kwargs
+
+
+@get_by_name_or_uuid_multiple([('alarm', 'alarm_id')])
+@send('delete')
+def alarm_delete(alarm_id, **kwargs):
+    url = '/alarms/{}'.format(alarm_id)
+    return url, None
+
+
+@get_by_name_or_uuid_multiple([('alarm', 'alarm_id')])
+@send('get')
+def alarm_history_get(alarm_id, **kwargs):
+    url = '/alarms/{}/history'.format(alarm_id)
+    return url, None
+
+
+@get_by_name_or_uuid_multiple([('alarm', 'alarm_id')])
+@send('put')
+def alarm_state_set(alarm_id, state, **kwargs):
+    url = '/alarms/{}/state'.format(alarm_id)
+    return url, {'state': state}
+
+
+@get_by_name_or_uuid_multiple([('alarm', 'alarm_id')])
+@send('get')
+def alarm_state_get(alarm_id, **kwargs):
+    url = '/alarms/{}/state'.format(alarm_id)
+    return url, None
diff --git a/_modules/aodhv2/arg_converter.py b/_modules/aodhv2/arg_converter.py
new file mode 100644
index 0000000..ee6937b
--- /dev/null
+++ b/_modules/aodhv2/arg_converter.py
@@ -0,0 +1,47 @@
+import uuid
+from aodhv2 import common
+from aodhv2 import queries
+
+
+class CheckId(object):
+    def check_id(self, val):
+        try:
+            return str(uuid.UUID(val)).replace('-', '') == val
+        except (TypeError, ValueError, AttributeError):
+            return False
+
+
+resource_lists = {
+    'alarm': queries.query_post,
+}
+
+
+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 = resource_lists[resource](
+                        name=ref, cloud_name=cloud_name)
+                    if len(resp) == 0:
+                        raise common.ResourceNotFound(resource, ref)
+                    elif len(resp) > 1:
+                        raise common.MultipleResourcesFound(resource, ref)
+                    results.append(resp[0]['alarm_id'])
+                results.extend(args[args_start:])
+            return func(*results, **kwargs)
+        return wrapped_f
+    return wrap
diff --git a/_modules/aodhv2/common.py b/_modules/aodhv2/common.py
new file mode 100644
index 0000000..e507457
--- /dev/null
+++ b/_modules/aodhv2/common.py
@@ -0,0 +1,103 @@
+import logging
+import os_client_config
+
+log = logging.getLogger(__name__)
+
+
+class AodhException(Exception):
+
+    _msg = "Aodh module exception occured."
+
+    def __init__(self, message=None, **kwargs):
+        super(AodhException, self).__init__(message or self._msg)
+
+
+class NoAodhEndpoint(AodhException):
+    _msg = "Aodh endpoint not found in keystone catalog."
+
+
+class NoAuthPluginConfigured(AodhException):
+    _msg = ("You are using keystoneauth auth plugin that does not support "
+            "fetching endpoint list from token (noauth or admin_token).")
+
+
+class NoCredentials(AodhException):
+    _msg = "Please provide cloud name present in clouds.yaml."
+
+
+class ResourceNotFound(AodhException):
+    _msg = "Uniq resource: {resource} with name: {name} not found."
+
+    def __init__(self, resource, name, **kwargs):
+        super(AodhException, self).__init__(
+            self._msg.format(resource=resource, name=name))
+
+
+class MultipleResourcesFound(AodhException):
+    _msg = "Multiple resource: {resource} with name: {name} found."
+
+    def __init__(self, resource, name, **kwargs):
+        super(AodhException, self).__init__(
+            self._msg.format(resource=resource, name=name))
+
+
+def _get_raw_client(cloud_name):
+    service_type = 'alarming'
+    config = os_client_config.OpenStackConfig()
+    cloud = config.get_one_cloud(cloud_name)
+    api_version = 'v2'
+    try:
+        # NOTE(pas-ha) for Queens and later,
+        # 'version' kwarg in absent for Pike and older
+        adapter = cloud.get_session_client(service_type, version=api_version)
+    except TypeError:
+        adapter = cloud.get_session_client(service_type)
+        adapter.version = api_version
+    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 = NoAodhEndpoint()
+            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
\ No newline at end of file
diff --git a/_modules/aodhv2/lists.py b/_modules/aodhv2/lists.py
new file mode 100644
index 0000000..708d4c1
--- /dev/null
+++ b/_modules/aodhv2/lists.py
@@ -0,0 +1,12 @@
+from aodhv2.common import send
+
+try:
+    from urllib.parse import urlencode
+except ImportError:
+    from urllib import urlencode
+
+
+@send('get')
+def alarm_list(**kwargs):
+    url = '/alarms'
+    return url, None
\ No newline at end of file
diff --git a/_modules/aodhv2/queries.py b/_modules/aodhv2/queries.py
new file mode 100644
index 0000000..df7b5b6
--- /dev/null
+++ b/_modules/aodhv2/queries.py
@@ -0,0 +1,11 @@
+from aodhv2.common import send
+
+
+# NOTE(opetrenko): currently we support only by name filtration
+@send('post')
+def query_post(name, **kwargs):
+    url = '/query/alarms'
+    json = {
+        'filter': "{\"=\":{\"name\": \"%s\"}}" % name
+    }
+    return url, json
diff --git a/_states/aodhv2.py b/_states/aodhv2.py
new file mode 100644
index 0000000..5dff5c2
--- /dev/null
+++ b/_states/aodhv2.py
@@ -0,0 +1,115 @@
+import logging
+
+
+def __virtual__():
+    return 'aodhv2' if 'aodhv2.alarm_list' in __salt__ else False  # noqa
+
+
+log = logging.getLogger(__name__)
+
+
+def _aodhv2_call(fname, *args, **kwargs):
+    return __salt__['aodhv2.{}'.format(fname)](*args, **kwargs)  # noqa
+
+
+def _resource_present(resource, name, cloud_name, **kwargs):
+    try:
+        method_name = '{}_get_details'.format(resource)
+        exact_resource = _aodhv2_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 = _aodhv2_call(
+                    method_name, name=name, cloud_name=cloud_name, **kwargs
+                )
+            except Exception as e:
+                log.exception('Aodh {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 = _aodhv2_call(
+            method_name, name, cloud_name=cloud_name, **to_update
+        )
+    except Exception as e:
+        log.exception('Aodh {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)
+        _aodhv2_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)
+        _aodhv2_call(
+            method_name, name, cloud_name=cloud_name
+        )
+    except Exception as e:
+        log.error('Aodh delete {0} failed with {1}'.format(resource, e))
+        return _failed('delete', name, resource)
+    return _succeeded('delete', name, resource)
+
+
+def alarm_present(name, cloud_name, **kwargs):
+    return _resource_present('alarm', name, cloud_name, **kwargs)
+
+
+def alarm_absent(name, cloud_name):
+    return _resource_absent('alarm', 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
diff --git a/aodh/files/queens/aodh.conf.Debian b/aodh/files/queens/aodh.conf.Debian
index 54f23bc..88c50fc 100644
--- a/aodh/files/queens/aodh.conf.Debian
+++ b/aodh/files/queens/aodh.conf.Debian
@@ -257,5 +257,8 @@
 [keystone_authtoken]
 {%- set _data = server.identity %}
 {%- set auth_type = _data.get('auth_type', 'password') %}
+{%- if server.get('cache',{}).members is defined and 'cache' not in _data.keys() %}
+{% do _data.update({'cache': server.cache}) %}
+{%- endif %}
 {%- include "oslo_templates/files/queens/keystonemiddleware/_auth_token.conf" %}
 {%- include "oslo_templates/files/queens/keystoneauth/_type_" + auth_type + ".conf" %}
diff --git a/aodh/upgrade/verify/_api.sls b/aodh/upgrade/verify/_api.sls
index e34edf5..d2467a7 100644
--- a/aodh/upgrade/verify/_api.sls
+++ b/aodh/upgrade/verify/_api.sls
@@ -1,7 +1,34 @@
+{%- from "aodh/map.jinja" import server with context %}
 
 aodh_upgrade_verify_api:
   test.show_notification:
     - text: "Running aodh.upgrade.verify.api"
 
-# TODO: Implement api checks when aodh salt
-# modules are implemented
+{%- if server.get('role', 'primary') == 'primary' %}
+{% set Alarm_Name = 'testupgrade_alarm_name' %}
+{% set Random_Uuid = salt['cmd.run']('cat /proc/sys/kernel/random/uuid') %}
+
+aodhv2_alarm_present:
+  aodhv2.alarm_present:
+  - cloud_name: admin_identity
+  - name: {{ Alarm_Name }}
+  - type: gnocchi_resources_threshold
+  - gnocchi_resources_threshold_rule:
+      metric: cpu_util
+      aggregation_method: mean
+      threshold: '70.0'
+      resource_id: {{ Random_Uuid }}
+      resource_type: instance
+
+aodhv2_alarm_list:
+  module.run:
+    - name: aodhv2.alarm_list
+    - kwargs:
+        cloud_name: admin_identity
+
+aodhv2_alarm_absent:
+  aodhv2.alarm_absent:
+  - cloud_name: admin_identity
+  - name: {{ Alarm_Name }}
+
+{%- endif %}