blob: 7b62122b019720420fdf7518c426874970af7e04 [file] [log] [blame]
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +03001import functools
Oleksiy Petrenkocaad2032018-04-20 14:42:46 +03002import logging
3import os_client_config
Vasyl Saienkocb788d42018-09-26 10:34:50 +00004import time
Oleksiy Petrenkocaad2032018-04-20 14:42:46 +03005
6log = logging.getLogger(__name__)
7
8NEUTRON_VERSION_HEADER = 'x-openstack-networking-version'
9ADAPTER_VERSION = '2.0'
10
11
12class 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
20class NoNeutronEndpoint(NeutronException):
21 _msg = "Neutron endpoint not found in keystone catalog."
22
23
24class 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
29class NoCredentials(NeutronException):
30 _msg = "Please provide cloud name present in clouds.yaml."
31
32
33class 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
41class 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
49def _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
65def send(method):
66 def wrap(func):
Oleksiy Petrenko5bfb8bc2018-08-23 15:08:17 +030067 @functools.wraps(func)
Oleksiy Petrenkocaad2032018-04-20 14:42:46 +030068 def wrapped_f(*args, **kwargs):
69 cloud_name = kwargs.pop('cloud_name')
Vasyl Saienko65fb5d32018-10-24 12:51:51 +000070 connect_retries = 30
Vasyl Saienkocb788d42018-09-26 10:34:50 +000071 connect_retry_delay = 1
Oleksiy Petrenkocaad2032018-04-20 14:42:46 +030072 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 Saienko65fb5d32018-10-24 12:51:51 +000086 response = None
Vasyl Saienkocb788d42018-09-26 10:34:50 +000087 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 Saienko65fb5d32018-10-24 12:51:51 +0000102 if not response or not response.content:
Oleksiy Petrenkocaad2032018-04-20 14:42:46 +0300103 return {}
104 try:
105 resp = response.json()
106 except ValueError:
107 resp = response.content
108 return resp
109 return wrapped_f
110 return wrap