Oleksiy Petrenko | 5013e68 | 2018-11-16 14:26:36 +0200 | [diff] [blame] | 1 | import logging |
| 2 | import os_client_config |
| 3 | |
| 4 | log = logging.getLogger(__name__) |
| 5 | |
| 6 | |
| 7 | class AodhException(Exception): |
| 8 | |
| 9 | _msg = "Aodh module exception occured." |
| 10 | |
| 11 | def __init__(self, message=None, **kwargs): |
| 12 | super(AodhException, self).__init__(message or self._msg) |
| 13 | |
| 14 | |
| 15 | class NoAodhEndpoint(AodhException): |
| 16 | _msg = "Aodh endpoint not found in keystone catalog." |
| 17 | |
| 18 | |
| 19 | class NoAuthPluginConfigured(AodhException): |
| 20 | _msg = ("You are using keystoneauth auth plugin that does not support " |
| 21 | "fetching endpoint list from token (noauth or admin_token).") |
| 22 | |
| 23 | |
| 24 | class NoCredentials(AodhException): |
| 25 | _msg = "Please provide cloud name present in clouds.yaml." |
| 26 | |
| 27 | |
| 28 | class ResourceNotFound(AodhException): |
| 29 | _msg = "Uniq resource: {resource} with name: {name} not found." |
| 30 | |
| 31 | def __init__(self, resource, name, **kwargs): |
| 32 | super(AodhException, self).__init__( |
| 33 | self._msg.format(resource=resource, name=name)) |
| 34 | |
| 35 | |
| 36 | class MultipleResourcesFound(AodhException): |
| 37 | _msg = "Multiple resource: {resource} with name: {name} found." |
| 38 | |
| 39 | def __init__(self, resource, name, **kwargs): |
| 40 | super(AodhException, self).__init__( |
| 41 | self._msg.format(resource=resource, name=name)) |
| 42 | |
| 43 | |
| 44 | def _get_raw_client(cloud_name): |
| 45 | service_type = 'alarming' |
| 46 | config = os_client_config.OpenStackConfig() |
| 47 | cloud = config.get_one_cloud(cloud_name) |
| 48 | api_version = 'v2' |
| 49 | try: |
| 50 | # NOTE(pas-ha) for Queens and later, |
| 51 | # 'version' kwarg in absent for Pike and older |
| 52 | adapter = cloud.get_session_client(service_type, version=api_version) |
| 53 | except TypeError: |
| 54 | adapter = cloud.get_session_client(service_type) |
| 55 | adapter.version = api_version |
| 56 | try: |
| 57 | access_info = adapter.session.auth.get_access(adapter.session) |
| 58 | endpoints = access_info.service_catalog.get_endpoints() |
| 59 | except (AttributeError, ValueError): |
| 60 | e = NoAuthPluginConfigured() |
| 61 | log.exception('%s' % e) |
| 62 | raise e |
| 63 | if service_type not in endpoints: |
| 64 | if not service_type: |
| 65 | e = NoAodhEndpoint() |
| 66 | log.error('%s' % e) |
| 67 | raise e |
| 68 | return adapter |
| 69 | |
| 70 | |
| 71 | def send(method, microversion_header=None): |
| 72 | def wrap(func): |
| 73 | def wrapped_f(*args, **kwargs): |
| 74 | headers = kwargs.pop('headers', {}) |
| 75 | if kwargs.get('microversion'): |
| 76 | headers.setdefault(microversion_header, |
| 77 | kwargs.get('microversion')) |
| 78 | cloud_name = kwargs.pop('cloud_name') |
| 79 | if not cloud_name: |
| 80 | e = NoCredentials() |
| 81 | log.error('%s' % e) |
| 82 | raise e |
| 83 | adapter = _get_raw_client(cloud_name) |
| 84 | # Remove salt internal kwargs |
| 85 | kwarg_keys = list(kwargs.keys()) |
| 86 | for k in kwarg_keys: |
| 87 | if k.startswith('__'): |
| 88 | kwargs.pop(k) |
| 89 | url, json = func(*args, **kwargs) |
| 90 | if json: |
| 91 | response = getattr(adapter, method)(url, headers=headers, |
| 92 | json=json) |
| 93 | else: |
| 94 | response = getattr(adapter, method)(url, headers=headers) |
| 95 | if not response.content: |
| 96 | return {} |
| 97 | try: |
| 98 | resp = response.json() |
| 99 | except: |
| 100 | resp = response.content |
| 101 | return resp |
| 102 | return wrapped_f |
| 103 | return wrap |