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