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 |
Oleksiy Petrenko | b110da9 | 2019-02-06 12:52:56 +0200 | [diff] [blame] | 3 | |
| 4 | try: |
| 5 | import os_client_config |
| 6 | except ImportError: |
| 7 | os_client_config = None |
| 8 | from salt import exceptions |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 9 | |
| 10 | log = logging.getLogger(__name__) |
| 11 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 12 | |
| 13 | class CinderException(Exception): |
| 14 | |
| 15 | _msg = "Cinder module exception occured." |
| 16 | |
| 17 | def __init__(self, message=None, **kwargs): |
| 18 | super(CinderException, self).__init__(message or self._msg) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 19 | |
| 20 | |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 21 | class NoCinderEndpoint(CinderException): |
| 22 | _msg = "Cinder endpoint not found in keystone catalog." |
| 23 | |
| 24 | |
| 25 | class NoAuthPluginConfigured(CinderException): |
| 26 | _msg = ("You are using keystoneauth auth plugin that does not support " |
| 27 | "fetching endpoint list from token (noauth or admin_token).") |
| 28 | |
| 29 | |
| 30 | class NoCredentials(CinderException): |
| 31 | _msg = "Please provide cloud name present in clouds.yaml." |
| 32 | |
| 33 | |
| 34 | class ResourceNotFound(CinderException): |
| 35 | _msg = "Uniq resource: {resource} with name: {name} not found." |
| 36 | |
| 37 | def __init__(self, resource, name, **kwargs): |
| 38 | super(CinderException, self).__init__( |
| 39 | self._msg.format(resource=resource, name=name)) |
| 40 | |
| 41 | |
| 42 | class MultipleResourcesFound(CinderException): |
| 43 | _msg = "Multiple resource: {resource} with name: {name} found." |
| 44 | |
| 45 | def __init__(self, resource, name, **kwargs): |
| 46 | super(CinderException, self).__init__( |
| 47 | self._msg.format(resource=resource, name=name)) |
| 48 | |
| 49 | |
| 50 | def _get_raw_client(cloud_name): |
Oleksiy Petrenko | b110da9 | 2019-02-06 12:52:56 +0200 | [diff] [blame] | 51 | if not os_client_config: |
| 52 | raise exceptions.SaltInvocationError( |
| 53 | "Cannot load os-client-config. Please check your environment " |
| 54 | "configuration.") |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 55 | service_type = 'volumev3' |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 56 | config = os_client_config.OpenStackConfig() |
| 57 | cloud = config.get_one_cloud(cloud_name) |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 58 | adapter = cloud.get_session_client(service_type) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 59 | try: |
| 60 | access_info = adapter.session.auth.get_access(adapter.session) |
| 61 | endpoints = access_info.service_catalog.get_endpoints() |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 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 = NoCinderEndpoint() |
| 69 | log.error('%s' % e) |
| 70 | raise e |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 71 | return adapter |
| 72 | |
| 73 | |
| 74 | def send(method): |
| 75 | def wrap(func): |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 76 | def wrapped_f(*args, **kwargs): |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 77 | cloud_name = kwargs.pop('cloud_name') |
root | 0840265 | 2018-12-28 15:04:23 +0000 | [diff] [blame] | 78 | connection_params = kwargs.pop('connection_params', {}) or {} |
| 79 | connect_retries = connection_params.get('connect_retries', 60) |
| 80 | connect_retry_delay = connection_params.get('connect_retry_delay', |
| 81 | 1) |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 82 | if not cloud_name: |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 83 | e = NoCredentials() |
| 84 | log.error('%s' % e) |
| 85 | raise e |
| 86 | adapter = _get_raw_client(cloud_name) |
| 87 | # Remove salt internal kwargs |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 88 | kwarg_keys = list(kwargs.keys()) |
| 89 | for k in kwarg_keys: |
| 90 | if k.startswith('__'): |
| 91 | kwargs.pop(k) |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 92 | url, json = func(*args, **kwargs) |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 93 | response = None |
| 94 | for i in range(connect_retries): |
| 95 | try: |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 96 | if json: |
| 97 | response = getattr(adapter, method)( |
| 98 | url, json=json, connect_retries=connect_retries) |
| 99 | else: |
| 100 | response = getattr(adapter, method)(url) |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 101 | except Exception as e: |
Oleksiy Petrenko | 61a84ff | 2018-12-07 19:14:28 +0200 | [diff] [blame] | 102 | if not hasattr(e, 'http_status') or (e.http_status >= 500 |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 103 | or e.http_status == 0): |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 104 | msg = ("Got retriable exception when contacting " |
| 105 | "Cinder API. Sleeping for %ss. Attepmpts " |
| 106 | "%s of %s") |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 107 | log.error( |
| 108 | msg % (connect_retry_delay, i, connect_retries)) |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 109 | time.sleep(connect_retry_delay) |
| 110 | continue |
| 111 | break |
| 112 | if not response or not response.content: |
| 113 | return {} |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 114 | try: |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 115 | resp = response.json() |
Oleksiy Petrenko | e38f5a6 | 2018-11-21 12:58:07 +0200 | [diff] [blame] | 116 | except: |
Oleh Hryhorov | 1aeb0c8 | 2018-10-26 14:51:30 +0000 | [diff] [blame] | 117 | resp = response.content |
| 118 | return resp |
kairat_kushaev | 5c8626d | 2018-06-09 18:15:15 +0400 | [diff] [blame] | 119 | return wrapped_f |
| 120 | return wrap |