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