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