blob: 92d227ee5b21864d93aa3090921b1fcc593bd6a6 [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 Saienkocb788d42018-09-26 10:34:50 +000070 connect_retries = 15
71 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 Saienkocb788d42018-09-26 10:34:50 +000086 for i in range(connect_retries):
87 try:
88 response = getattr(adapter, method)(
89 url, connect_retries=connect_retries,
90 **request_kwargs)
91 except Exception as e:
92 if hasattr(e, 'http_status') and (e.http_status >= 500
93 or e.http_status == 0):
94 msg = ("Got retriable exception when contacting "
95 "Neutron API. Sleeping for %ss. Attepmpts "
96 "%s of %s")
97 log.error(msg % (connect_retry_delay, i, connect_retries))
98 time.sleep(connect_retry_delay)
99 continue
100 break
Oleksiy Petrenkocaad2032018-04-20 14:42:46 +0300101 if not response.content:
102 return {}
103 try:
104 resp = response.json()
105 except ValueError:
106 resp = response.content
107 return resp
108 return wrapped_f
109 return wrap