Oleksiy Petrenko | 777d782 | 2018-11-06 13:32:44 +0200 | [diff] [blame^] | 1 | import logging |
| 2 | import os_client_config |
| 3 | |
| 4 | log = logging.getLogger(__name__) |
| 5 | |
| 6 | |
| 7 | class 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 | |
| 15 | class NoDesignateEndpoint(DesignateException): |
| 16 | _msg = "Designate endpoint not found in keystone catalog." |
| 17 | |
| 18 | |
| 19 | class 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 | |
| 24 | class NoCredentials(DesignateException): |
| 25 | _msg = "Please provide cloud name present in clouds.yaml." |
| 26 | |
| 27 | |
| 28 | class 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 | |
| 36 | class 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 | |
| 44 | def _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 | |
| 65 | def 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 |