blob: bc77cd9c54ee12b3bbf8f74f375f0e6ea5e873b5 [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):
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000068 connect_retries = 30
69 connect_retry_delay = 1
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020070 cloud_name = kwargs.pop('cloud_name')
kairat_kushaev5c8626d2018-06-09 18:15:15 +040071 if not cloud_name:
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020072 e = NoCredentials()
73 log.error('%s' % e)
74 raise e
75 adapter = _get_raw_client(cloud_name)
76 # Remove salt internal kwargs
kairat_kushaev5c8626d2018-06-09 18:15:15 +040077 kwarg_keys = list(kwargs.keys())
78 for k in kwarg_keys:
79 if k.startswith('__'):
80 kwargs.pop(k)
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020081 url, json = func(*args, **kwargs)
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000082 response = None
83 for i in range(connect_retries):
84 try:
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020085 if json:
86 response = getattr(adapter, method)(
87 url, json=json, connect_retries=connect_retries)
88 else:
89 response = getattr(adapter, method)(url)
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000090 except Exception as e:
Oleksiy Petrenko61a84ff2018-12-07 19:14:28 +020091 if not hasattr(e, 'http_status') or (e.http_status >= 500
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020092 or e.http_status == 0):
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000093 msg = ("Got retriable exception when contacting "
94 "Cinder API. Sleeping for %ss. Attepmpts "
95 "%s of %s")
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +020096 log.error(
97 msg % (connect_retry_delay, i, connect_retries))
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +000098 time.sleep(connect_retry_delay)
99 continue
100 break
101 if not response or not response.content:
102 return {}
kairat_kushaev5c8626d2018-06-09 18:15:15 +0400103 try:
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +0000104 resp = response.json()
Oleksiy Petrenkoe38f5a62018-11-21 12:58:07 +0200105 except:
Oleh Hryhorov1aeb0c82018-10-26 14:51:30 +0000106 resp = response.content
107 return resp
kairat_kushaev5c8626d2018-06-09 18:15:15 +0400108 return wrapped_f
109 return wrap