| Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 1 | import functools | 
| Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 2 | import logging | 
| Vasyl Saienko | cb788d4 | 2018-09-26 10:34:50 +0000 | [diff] [blame] | 3 | import time | 
| Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 4 |  | 
| Oleksiy Petrenko | a089471 | 2019-02-06 13:10:24 +0200 | [diff] [blame] | 5 | try: | 
|  | 6 | import os_client_config | 
|  | 7 | except ImportError: | 
|  | 8 | os_client_config = None | 
|  | 9 | from salt import exceptions | 
|  | 10 |  | 
| Oleh Hryhorov | 4ce5d2c | 2018-11-08 18:41:20 +0200 | [diff] [blame] | 11 |  | 
| Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 12 | log = logging.getLogger(__name__) | 
|  | 13 |  | 
|  | 14 | NEUTRON_VERSION_HEADER = 'x-openstack-networking-version' | 
|  | 15 | ADAPTER_VERSION = '2.0' | 
|  | 16 |  | 
|  | 17 |  | 
|  | 18 | class NeutronException(Exception): | 
|  | 19 |  | 
|  | 20 | _msg = "Neutron module exception occured." | 
|  | 21 |  | 
|  | 22 | def __init__(self, message=None, **kwargs): | 
|  | 23 | super(NeutronException, self).__init__(message or self._msg) | 
|  | 24 |  | 
|  | 25 |  | 
|  | 26 | class NoNeutronEndpoint(NeutronException): | 
|  | 27 | _msg = "Neutron endpoint not found in keystone catalog." | 
|  | 28 |  | 
|  | 29 |  | 
|  | 30 | class NoAuthPluginConfigured(NeutronException): | 
|  | 31 | _msg = ("You are using keystoneauth auth plugin that does not support " | 
|  | 32 | "fetching endpoint list from token (noauth or admin_token).") | 
|  | 33 |  | 
|  | 34 |  | 
|  | 35 | class NoCredentials(NeutronException): | 
|  | 36 | _msg = "Please provide cloud name present in clouds.yaml." | 
|  | 37 |  | 
|  | 38 |  | 
|  | 39 | class ResourceNotFound(NeutronException): | 
|  | 40 | _msg = "Uniq resource: {resource} with name: {name} not found." | 
|  | 41 |  | 
|  | 42 | def __init__(self, resource, name, **kwargs): | 
|  | 43 | super(NeutronException, self).__init__( | 
|  | 44 | self._msg.format(resource=resource, name=name)) | 
|  | 45 |  | 
|  | 46 |  | 
|  | 47 | class MultipleResourcesFound(NeutronException): | 
|  | 48 | _msg = "Multiple resource: {resource} with name: {name} found." | 
|  | 49 |  | 
|  | 50 | def __init__(self, resource, name, **kwargs): | 
|  | 51 | super(NeutronException, self).__init__( | 
|  | 52 | self._msg.format(resource=resource, name=name)) | 
|  | 53 |  | 
|  | 54 |  | 
|  | 55 | def _get_raw_client(cloud_name): | 
| Oleksiy Petrenko | a089471 | 2019-02-06 13:10:24 +0200 | [diff] [blame] | 56 | if not os_client_config: | 
|  | 57 | raise exceptions.SaltInvocationError( | 
|  | 58 | "Cannot load os-client-config. Please check your environment " | 
|  | 59 | "configuration.") | 
| Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 60 | service_type = 'network' | 
|  | 61 | config = os_client_config.OpenStackConfig() | 
|  | 62 | cloud = config.get_one_cloud(cloud_name) | 
|  | 63 | adapter = cloud.get_session_client(service_type) | 
|  | 64 | adapter.version = ADAPTER_VERSION | 
|  | 65 | try: | 
|  | 66 | access_info = adapter.session.auth.get_access(adapter.session) | 
|  | 67 | access_info.service_catalog.get_endpoints() | 
|  | 68 | except (AttributeError, ValueError): | 
|  | 69 | e = NoAuthPluginConfigured() | 
|  | 70 | log.exception('%s' % e) | 
|  | 71 | raise e | 
|  | 72 | return adapter | 
|  | 73 |  | 
|  | 74 |  | 
|  | 75 | def send(method): | 
|  | 76 | def wrap(func): | 
| Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 77 | @functools.wraps(func) | 
| Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 78 | def wrapped_f(*args, **kwargs): | 
|  | 79 | cloud_name = kwargs.pop('cloud_name') | 
| Vasyl Saienko | 65fb5d3 | 2018-10-24 12:51:51 +0000 | [diff] [blame] | 80 | connect_retries =  30 | 
| Vasyl Saienko | cb788d4 | 2018-09-26 10:34:50 +0000 | [diff] [blame] | 81 | connect_retry_delay = 1 | 
| Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 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, request_kwargs = func(*args, **kwargs) | 
|  | 93 | if 'microversion' in kwargs: | 
|  | 94 | request_kwargs['headers'][ | 
|  | 95 | NEUTRON_VERSION_HEADER] = kwargs['microversion'] | 
| Vasyl Saienko | 65fb5d3 | 2018-10-24 12:51:51 +0000 | [diff] [blame] | 96 | response = None | 
| Vasyl Saienko | cb788d4 | 2018-09-26 10:34:50 +0000 | [diff] [blame] | 97 | for i in range(connect_retries): | 
|  | 98 | try: | 
|  | 99 | response = getattr(adapter, method)( | 
|  | 100 | url, connect_retries=connect_retries, | 
|  | 101 | **request_kwargs) | 
|  | 102 | except Exception as e: | 
| Oleh Hryhorov | 42a930e | 2018-12-21 14:37:20 +0200 | [diff] [blame] | 103 | if not hasattr(e, 'http_status') or (e.http_status >= 500 | 
| Vasyl Saienko | cb788d4 | 2018-09-26 10:34:50 +0000 | [diff] [blame] | 104 | or e.http_status == 0): | 
|  | 105 | msg = ("Got retriable exception when contacting " | 
|  | 106 | "Neutron API. Sleeping for %ss. Attepmpts " | 
|  | 107 | "%s of %s") | 
|  | 108 | log.error(msg % (connect_retry_delay, i, connect_retries)) | 
|  | 109 | time.sleep(connect_retry_delay) | 
|  | 110 | continue | 
|  | 111 | break | 
| Vasyl Saienko | 65fb5d3 | 2018-10-24 12:51:51 +0000 | [diff] [blame] | 112 | if not response or not response.content: | 
| Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 113 | return {} | 
|  | 114 | try: | 
|  | 115 | resp = response.json() | 
|  | 116 | except ValueError: | 
|  | 117 | resp = response.content | 
|  | 118 | return resp | 
|  | 119 | return wrapped_f | 
|  | 120 | return wrap | 
| Oleh Hryhorov | 4ce5d2c | 2018-11-08 18:41:20 +0200 | [diff] [blame] | 121 |  | 
|  | 122 | def wait_for_api_ready(cloud_name, retries=1, retry_timeout=10, **kwargs): | 
| Pavlo Shchelokovskyy | 5b088b9 | 2020-09-11 11:00:03 +0300 | [diff] [blame^] | 123 | # Remove salt internal kwargs | 
|  | 124 | kwarg_keys = list(kwargs.keys()) | 
|  | 125 | for k in kwarg_keys: | 
|  | 126 | if k.startswith('__'): | 
|  | 127 | kwargs.pop(k) | 
| Oleh Hryhorov | 4ce5d2c | 2018-11-08 18:41:20 +0200 | [diff] [blame] | 128 | adapter = _get_raw_client(cloud_name) | 
|  | 129 | response = None | 
|  | 130 | for i in range(1, retries+1): | 
|  | 131 | try: | 
|  | 132 | response = getattr(adapter, 'get')( | 
|  | 133 | '/', connect_retries=retries, | 
|  | 134 | **kwargs) | 
|  | 135 | except Exception as e: | 
|  | 136 | msg = ("Error: %s " | 
|  | 137 | "Sleeping for %ss. " | 
|  | 138 | "Attempts %s of %s ") | 
|  | 139 | log.error(msg % (e, retry_timeout, i, retries)) | 
|  | 140 | time.sleep(retry_timeout) | 
|  | 141 | continue | 
|  | 142 | break | 
|  | 143 | if not response or not response.content: | 
|  | 144 | if e: | 
| Oleksiy Petrenko | a089471 | 2019-02-06 13:10:24 +0200 | [diff] [blame] | 145 | raise exceptions.CommandExecutionError(e) | 
| Oleh Hryhorov | 4ce5d2c | 2018-11-08 18:41:20 +0200 | [diff] [blame] | 146 | else: | 
|  | 147 | return {} | 
|  | 148 | try: | 
|  | 149 | resp = response.json() | 
|  | 150 | except ValueError: | 
|  | 151 | resp = response.content | 
|  | 152 | return resp | 
|  | 153 |  |