blob: 0ce0907996ebe3ff43de5f63386d61ba0c1e6ca6 [file] [log] [blame]
Vasyl Saienko4eda4f22018-04-26 19:30:39 +03001import logging
Oleksiy Petrenko77a7e9c2019-02-06 13:03:53 +02002
3try:
4 import os_client_config
5except ImportError:
6 os_client_config = None
7from salt import exceptions
Vasyl Saienko4eda4f22018-04-26 19:30:39 +03008
9log = logging.getLogger(__name__)
10
11
12class KeystoneException(Exception):
13
14 _msg = "Keystone module exception occured."
15
16 def __init__(self, message=None, **kwargs):
17 super(KeystoneException, self).__init__(message or self._msg)
18
19
20class NoKeystoneEndpoint(KeystoneException):
21 _msg = "Keystone endpoint not found in keystone catalog."
22
23
24class NoAuthPluginConfigured(KeystoneException):
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(KeystoneException):
30 _msg = "Please provide cloud name present in clouds.yaml."
31
32
33class ResourceNotFound(KeystoneException):
34 _msg = "Uniq resource: {resource} with name: {name} not found."
35
36 def __init__(self, resource, name, **kwargs):
37 super(KeystoneException, self).__init__(
38 self._msg.format(resource=resource, name=name))
39
40
41class MultipleResourcesFound(KeystoneException):
42 _msg = "Multiple resource: {resource} with name: {name} found."
43
44 def __init__(self, resource, name, **kwargs):
45 super(KeystoneException, self).__init__(
46 self._msg.format(resource=resource, name=name))
47
48
49def _get_raw_client(cloud_name):
Oleksiy Petrenko77a7e9c2019-02-06 13:03:53 +020050 if not os_client_config:
51 raise exceptions.SaltInvocationError(
52 "Cannot load os-client-config. Please check your environment "
53 "configuration.")
Vasyl Saienko4eda4f22018-04-26 19:30:39 +030054 service_type = 'identity'
55 config = os_client_config.OpenStackConfig()
56 cloud = config.get_one_cloud(cloud_name)
57 adapter = cloud.get_session_client(service_type)
58 adapter.version = '3'
59 try:
60 access_info = adapter.session.auth.get_access(adapter.session)
61 endpoints = access_info.service_catalog.get_endpoints()
62 except (AttributeError, ValueError):
63 e = NoAuthPluginConfigured()
64 log.exception('%s' % e)
65 raise e
66 if service_type not in endpoints:
67 if not service_type:
68 e = NoKeystoneEndpoint()
69 log.error('%s' % e)
70 raise e
71 return adapter
72
73
74def send(method, microversion_header=None):
75 def wrap(func):
76 def wrapped_f(*args, **kwargs):
77 headers = kwargs.pop('headers', {})
78 if kwargs.get('microversion'):
79 headers.setdefault(microversion_header,
80 kwargs.get('microversion'))
81 cloud_name = kwargs.pop('cloud_name')
82 if not cloud_name:
83 e = NoCredentials()
84 log.error('%s' % e)
85 raise e
86 adapter = _get_raw_client(cloud_name)
87 # Remove salt internal kwargs
88 kwarg_keys = list(kwargs.keys())
89 for k in kwarg_keys:
90 if k.startswith('__'):
91 kwargs.pop(k)
92 url, json = func(*args, **kwargs)
93 if json:
94 response = getattr(adapter, method)(url, headers=headers,
95 json=json)
96 else:
97 response = getattr(adapter, method)(url, headers=headers)
98 if not response.content:
99 return {}
100 try:
101 resp = response.json()
102 except:
103 resp = response.content
104 return resp
105 return wrapped_f
106 return wrap