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 |
| 3 | import os_client_config |
Vasyl Saienko | cb788d4 | 2018-09-26 10:34:50 +0000 | [diff] [blame] | 4 | import time |
Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 5 | |
| 6 | log = logging.getLogger(__name__) |
| 7 | |
| 8 | NEUTRON_VERSION_HEADER = 'x-openstack-networking-version' |
| 9 | ADAPTER_VERSION = '2.0' |
| 10 | |
| 11 | |
| 12 | class NeutronException(Exception): |
| 13 | |
| 14 | _msg = "Neutron module exception occured." |
| 15 | |
| 16 | def __init__(self, message=None, **kwargs): |
| 17 | super(NeutronException, self).__init__(message or self._msg) |
| 18 | |
| 19 | |
| 20 | class NoNeutronEndpoint(NeutronException): |
| 21 | _msg = "Neutron endpoint not found in keystone catalog." |
| 22 | |
| 23 | |
| 24 | class NoAuthPluginConfigured(NeutronException): |
| 25 | _msg = ("You are using keystoneauth auth plugin that does not support " |
| 26 | "fetching endpoint list from token (noauth or admin_token).") |
| 27 | |
| 28 | |
| 29 | class NoCredentials(NeutronException): |
| 30 | _msg = "Please provide cloud name present in clouds.yaml." |
| 31 | |
| 32 | |
| 33 | class ResourceNotFound(NeutronException): |
| 34 | _msg = "Uniq resource: {resource} with name: {name} not found." |
| 35 | |
| 36 | def __init__(self, resource, name, **kwargs): |
| 37 | super(NeutronException, self).__init__( |
| 38 | self._msg.format(resource=resource, name=name)) |
| 39 | |
| 40 | |
| 41 | class MultipleResourcesFound(NeutronException): |
| 42 | _msg = "Multiple resource: {resource} with name: {name} found." |
| 43 | |
| 44 | def __init__(self, resource, name, **kwargs): |
| 45 | super(NeutronException, self).__init__( |
| 46 | self._msg.format(resource=resource, name=name)) |
| 47 | |
| 48 | |
| 49 | def _get_raw_client(cloud_name): |
| 50 | service_type = 'network' |
| 51 | config = os_client_config.OpenStackConfig() |
| 52 | cloud = config.get_one_cloud(cloud_name) |
| 53 | adapter = cloud.get_session_client(service_type) |
| 54 | adapter.version = ADAPTER_VERSION |
| 55 | try: |
| 56 | access_info = adapter.session.auth.get_access(adapter.session) |
| 57 | access_info.service_catalog.get_endpoints() |
| 58 | except (AttributeError, ValueError): |
| 59 | e = NoAuthPluginConfigured() |
| 60 | log.exception('%s' % e) |
| 61 | raise e |
| 62 | return adapter |
| 63 | |
| 64 | |
| 65 | def send(method): |
| 66 | def wrap(func): |
Oleksiy Petrenko | 5bfb8bc | 2018-08-23 15:08:17 +0300 | [diff] [blame] | 67 | @functools.wraps(func) |
Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 68 | def wrapped_f(*args, **kwargs): |
| 69 | cloud_name = kwargs.pop('cloud_name') |
Vasyl Saienko | 65fb5d3 | 2018-10-24 12:51:51 +0000 | [diff] [blame] | 70 | connect_retries = 30 |
Vasyl Saienko | cb788d4 | 2018-09-26 10:34:50 +0000 | [diff] [blame] | 71 | connect_retry_delay = 1 |
Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 72 | if not cloud_name: |
| 73 | e = NoCredentials() |
| 74 | log.error('%s' % e) |
| 75 | raise e |
| 76 | adapter = _get_raw_client(cloud_name) |
| 77 | # Remove salt internal kwargs |
| 78 | kwarg_keys = list(kwargs.keys()) |
| 79 | for k in kwarg_keys: |
| 80 | if k.startswith('__'): |
| 81 | kwargs.pop(k) |
| 82 | url, request_kwargs = func(*args, **kwargs) |
| 83 | if 'microversion' in kwargs: |
| 84 | request_kwargs['headers'][ |
| 85 | NEUTRON_VERSION_HEADER] = kwargs['microversion'] |
Vasyl Saienko | 65fb5d3 | 2018-10-24 12:51:51 +0000 | [diff] [blame] | 86 | response = None |
Vasyl Saienko | cb788d4 | 2018-09-26 10:34:50 +0000 | [diff] [blame] | 87 | for i in range(connect_retries): |
| 88 | try: |
| 89 | response = getattr(adapter, method)( |
| 90 | url, connect_retries=connect_retries, |
| 91 | **request_kwargs) |
| 92 | except Exception as e: |
| 93 | if hasattr(e, 'http_status') and (e.http_status >= 500 |
| 94 | or e.http_status == 0): |
| 95 | msg = ("Got retriable exception when contacting " |
| 96 | "Neutron API. Sleeping for %ss. Attepmpts " |
| 97 | "%s of %s") |
| 98 | log.error(msg % (connect_retry_delay, i, connect_retries)) |
| 99 | time.sleep(connect_retry_delay) |
| 100 | continue |
| 101 | break |
Vasyl Saienko | 65fb5d3 | 2018-10-24 12:51:51 +0000 | [diff] [blame] | 102 | if not response or not response.content: |
Oleksiy Petrenko | caad203 | 2018-04-20 14:42:46 +0300 | [diff] [blame] | 103 | return {} |
| 104 | try: |
| 105 | resp = response.json() |
| 106 | except ValueError: |
| 107 | resp = response.content |
| 108 | return resp |
| 109 | return wrapped_f |
| 110 | return wrap |