Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 1 | import time |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 2 | import logging |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 3 | import os_client_config |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 4 | |
| 5 | log = logging.getLogger(__name__) |
| 6 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 7 | |
| 8 | class 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_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 14 | |
| 15 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 16 | class NoCinderEndpoint(CinderException): |
| 17 | _msg = "Cinder endpoint not found in keystone catalog." |
| 18 | |
| 19 | |
| 20 | class 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 | |
| 25 | class NoCredentials(CinderException): |
| 26 | _msg = "Please provide cloud name present in clouds.yaml." |
| 27 | |
| 28 | |
| 29 | class 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 | |
| 37 | class 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 | |
| 45 | def _get_raw_client(cloud_name): |
| 46 | service_type = 'volumev3' |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 47 | config = os_client_config.OpenStackConfig() |
| 48 | cloud = config.get_one_cloud(cloud_name) |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 49 | adapter = cloud.get_session_client(service_type) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 50 | try: |
| 51 | access_info = adapter.session.auth.get_access(adapter.session) |
| 52 | endpoints = access_info.service_catalog.get_endpoints() |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 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 = NoCinderEndpoint() |
| 60 | log.error('%s' % e) |
| 61 | raise e |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 62 | return adapter |
| 63 | |
| 64 | |
| 65 | def send(method): |
| 66 | def wrap(func): |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 67 | def wrapped_f(*args, **kwargs): |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 68 | connect_retries = 30 |
| 69 | connect_retry_delay = 1 |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 70 | cloud_name = kwargs.pop('cloud_name') |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 71 | if not cloud_name: |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 72 | e = NoCredentials() |
| 73 | log.error('%s' % e) |
| 74 | raise e |
| 75 | adapter = _get_raw_client(cloud_name) |
| 76 | # Remove salt internal kwargs |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 77 | kwarg_keys = list(kwargs.keys()) |
| 78 | for k in kwarg_keys: |
| 79 | if k.startswith('__'): |
| 80 | kwargs.pop(k) |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 81 | url, json = func(*args, **kwargs) |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 82 | response = None |
| 83 | for i in range(connect_retries): |
| 84 | try: |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 85 | if json: |
| 86 | response = getattr(adapter, method)( |
| 87 | url, json=json, connect_retries=connect_retries) |
| 88 | else: |
| 89 | response = getattr(adapter, method)(url) |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 90 | except Exception as e: |
Oleksiy Petrenko | 61a84ff | 2018-12-07 19:14:28 +0200 | [diff] [blame^] | 91 | if not hasattr(e, 'http_status') or (e.http_status >= 500 |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 92 | or e.http_status == 0): |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 93 | msg = ("Got retriable exception when contacting " |
| 94 | "Cinder API. Sleeping for %ss. Attepmpts " |
| 95 | "%s of %s") |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 96 | log.error( |
| 97 | msg % (connect_retry_delay, i, connect_retries)) |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 98 | time.sleep(connect_retry_delay) |
| 99 | continue |
| 100 | break |
| 101 | if not response or not response.content: |
| 102 | return {} |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 103 | try: |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 104 | resp = response.json() |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 105 | except: |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 106 | resp = response.content |
| 107 | return resp |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 108 | return wrapped_f |
| 109 | return wrap |