Add modules for AODH
Change-Id: Ifa9fb205f3043ea99b8153fd67184d13da84ca0f
Related-Issue: PROD-25008 (PROD:25008)
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