blob: df419e9a7d3bdeb7369855d0f960da6c706cf8f7 [file] [log] [blame]
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +00001import time
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +02002import logging
kairat_kushaev5c8626d2018-06-09 18:15:15 +04003import os_client_config
kairat_kushaev5c8626d2018-06-09 18:15:15 +04004
5log = logging.getLogger(__name__)
6
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +02007
8class CinderException(Exception):
9
10 _msg = "Cinder module exception occured."
11
12 def __init__(self, message=None, **kwargs):
13 super(CinderException, self).__init__(message or self._msg)
kairat_kushaev5c8626d2018-06-09 18:15:15 +040014
15
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020016class NoCinderEndpoint(CinderException):
17 _msg = "Cinder endpoint not found in keystone catalog."
18
19
20class NoAuthPluginConfigured(CinderException):
21 _msg = ("You are using keystoneauth auth plugin that does not support "
22 "fetching endpoint list from token (noauth or admin_token).")
23
24
25class NoCredentials(CinderException):
26 _msg = "Please provide cloud name present in clouds.yaml."
27
28
29class ResourceNotFound(CinderException):
30 _msg = "Uniq resource: {resource} with name: {name} not found."
31
32 def __init__(self, resource, name, **kwargs):
33 super(CinderException, self).__init__(
34 self._msg.format(resource=resource, name=name))
35
36
37class MultipleResourcesFound(CinderException):
38 _msg = "Multiple resource: {resource} with name: {name} found."
39
40 def __init__(self, resource, name, **kwargs):
41 super(CinderException, self).__init__(
42 self._msg.format(resource=resource, name=name))
43
44
45def _get_raw_client(cloud_name):
46 service_type = 'volumev3'
kairat_kushaev5c8626d2018-06-09 18:15:15 +040047 config = os_client_config.OpenStackConfig()
48 cloud = config.get_one_cloud(cloud_name)
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020049 adapter = cloud.get_session_client(service_type)
kairat_kushaev5c8626d2018-06-09 18:15:15 +040050 try:
51 access_info = adapter.session.auth.get_access(adapter.session)
52 endpoints = access_info.service_catalog.get_endpoints()
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020053 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 = NoCinderEndpoint()
60 log.error('%s' % e)
61 raise e
kairat_kushaev5c8626d2018-06-09 18:15:15 +040062 return adapter
63
64
65def send(method):
66 def wrap(func):
kairat_kushaev5c8626d2018-06-09 18:15:15 +040067 def wrapped_f(*args, **kwargs):
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020068 cloud_name = kwargs.pop('cloud_name')
root08402652018-12-28 15:04:23 +000069 connection_params = kwargs.pop('connection_params', {}) or {}
70 connect_retries = connection_params.get('connect_retries', 60)
71 connect_retry_delay = connection_params.get('connect_retry_delay',
72 1)
kairat_kushaev5c8626d2018-06-09 18:15:15 +040073 if not cloud_name:
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020074 e = NoCredentials()
75 log.error('%s' % e)
76 raise e
77 adapter = _get_raw_client(cloud_name)
78 # Remove salt internal kwargs
kairat_kushaev5c8626d2018-06-09 18:15:15 +040079 kwarg_keys = list(kwargs.keys())
80 for k in kwarg_keys:
81 if k.startswith('__'):
82 kwargs.pop(k)
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020083 url, json = func(*args, **kwargs)
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000084 response = None
85 for i in range(connect_retries):
86 try:
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020087 if json:
88 response = getattr(adapter, method)(
89 url, json=json, connect_retries=connect_retries)
90 else:
91 response = getattr(adapter, method)(url)
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000092 except Exception as e:
Oleksiy Petrenko61a84ff2018-12-07 19:14:28 +020093 if not hasattr(e, 'http_status') or (e.http_status >= 500
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020094 or e.http_status == 0):
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000095 msg = ("Got retriable exception when contacting "
96 "Cinder API. Sleeping for %ss. Attepmpts "
97 "%s of %s")
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020098 log.error(
99 msg % (connect_retry_delay, i, connect_retries))
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +0000100 time.sleep(connect_retry_delay)
101 continue
102 break
103 if not response or not response.content:
104 return {}
kairat_kushaev5c8626d2018-06-09 18:15:15 +0400105 try:
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +0000106 resp = response.json()
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +0200107 except:
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +0000108 resp = response.content
109 return resp
kairat_kushaev5c8626d2018-06-09 18:15:15 +0400110 return wrapped_f
111 return wrap