blob: 22e8ad3397aa176c8455d4b7ea4430b1f1663624 [file] [log] [blame]
Oleksiy Petrenko777d7822018-11-06 13:32:44 +02001import logging
2import os_client_config
3
4log = logging.getLogger(__name__)
5
6
7class DesignateException(Exception):
8
9 _msg = "Designate module exception occured."
10
11 def __init__(self, message=None, **kwargs):
12 super(DesignateException, self).__init__(message or self._msg)
13
14
15class NoDesignateEndpoint(DesignateException):
16 _msg = "Designate endpoint not found in keystone catalog."
17
18
19class NoAuthPluginConfigured(DesignateException):
20 _msg = ("You are using keystoneauth auth plugin that does not support "
21 "fetching endpoint list from token (noauth or admin_token).")
22
23
24class NoCredentials(DesignateException):
25 _msg = "Please provide cloud name present in clouds.yaml."
26
27
28class ResourceNotFound(DesignateException):
29 _msg = "Uniq resource: {resource} with name: {name} not found."
30
31 def __init__(self, resource, name, **kwargs):
32 super(DesignateException, self).__init__(
33 self._msg.format(resource=resource, name=name))
34
35
36class MultipleResourcesFound(DesignateException):
37 _msg = "Multiple resource: {resource} with name: {name} found."
38
39 def __init__(self, resource, name, **kwargs):
40 super(DesignateException, self).__init__(
41 self._msg.format(resource=resource, name=name))
42
43
44def _get_raw_client(cloud_name):
45 service_type = 'dns'
46 config = os_client_config.OpenStackConfig()
47 cloud = config.get_one_cloud(cloud_name)
48 adapter = cloud.get_session_client(service_type)
49 adapter.version = 'v2'
50 try:
51 access_info = adapter.session.auth.get_access(adapter.session)
52 endpoints = access_info.service_catalog.get_endpoints()
53 except (AttributeError, ValueError):
54 e = NoAuthPluginConfigured()
55 log.exception('%s' % e)
56 raise e
57 if service_type not in endpoints:
58 if not service_type:
59 e = NoDesignateEndpoint()
60 log.error('%s' % e)
61 raise e
62 return adapter
63
64
65def send(method, microversion_header=None):
66 def wrap(func):
67 def wrapped_f(*args, **kwargs):
68 headers = kwargs.pop('headers', {})
69 if kwargs.get('microversion'):
70 headers.setdefault(microversion_header,
71 kwargs.get('microversion'))
72 cloud_name = kwargs.pop('cloud_name')
73 if not cloud_name:
74 e = NoCredentials()
75 log.error('%s' % e)
76 raise e
77 adapter = _get_raw_client(cloud_name)
78 # Remove salt internal kwargs
79 kwarg_keys = list(kwargs.keys())
80 for k in kwarg_keys:
81 if k.startswith('__'):
82 kwargs.pop(k)
83 url, json = func(*args, **kwargs)
84 if json:
85 response = getattr(adapter, method)(url, headers=headers,
86 json=json)
87 else:
88 response = getattr(adapter, method)(url, headers=headers)
89 if not response.content:
90 return {}
91 try:
92 resp = response.json()
93 except:
94 resp = response.content
95 return resp
96 return wrapped_f
97 return wrap