Oleksiy Petrenko | e2c8da2 | 2018-03-30 18:27:58 +0300 | [diff] [blame] | 1 | # Copyright 2018 Mirantis Inc |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import logging |
| 17 | |
| 18 | import os_client_config |
| 19 | |
| 20 | |
| 21 | MANILA_HEADER = 'X-OpenStack-Manila-API-Version' |
| 22 | |
| 23 | |
| 24 | log = logging.getLogger(__name__) |
| 25 | |
| 26 | |
| 27 | class ManilaException(Exception): |
| 28 | |
| 29 | _msg = "Manila module exception occured." |
| 30 | |
| 31 | def __init__(self, message=None, **kwargs): |
| 32 | super(ManilaException, self).__init__(message or self._msg) |
| 33 | |
| 34 | |
| 35 | class NoManilaEndpoint(ManilaException): |
| 36 | _msg = "Manila endpoint not found in keystone catalog." |
| 37 | |
| 38 | |
| 39 | class NoAuthPluginConfigured(ManilaException): |
| 40 | _msg = ("You are using keystoneauth auth plugin that does not support " |
| 41 | "fetching endpoint list from token (noauth or admin_token).") |
| 42 | |
| 43 | |
| 44 | class NoCredentials(ManilaException): |
| 45 | _msg = "Please provide cloud name present in clouds.yaml." |
| 46 | |
| 47 | |
| 48 | def _get_raw_client(cloud_name): |
| 49 | service_type = 'sharev2' |
| 50 | adapter = os_client_config.make_rest_client(service_type, |
| 51 | cloud=cloud_name) |
| 52 | try: |
| 53 | access_info = adapter.session.auth.get_access(adapter.session) |
| 54 | endpoints = access_info.service_catalog.get_endpoints() |
| 55 | except (AttributeError, ValueError): |
| 56 | e = NoAuthPluginConfigured() |
| 57 | log.error('%s' % e) |
| 58 | raise e |
| 59 | if service_type not in endpoints: |
| 60 | service_type = None |
| 61 | for possible_type in ('share', 'shared-file-system'): |
| 62 | if possible_type in endpoints: |
| 63 | service_type = possible_type |
| 64 | break |
| 65 | if not service_type: |
| 66 | e = NoManilaEndpoint() |
| 67 | log.exception('%s' % e) |
| 68 | raise e |
| 69 | adapter = os_client_config.make_rest_client(service_type, |
| 70 | cloud=cloud_name) |
| 71 | log.debug("Using manila endpoint with type %s." % service_type) |
| 72 | return adapter |
| 73 | |
| 74 | |
| 75 | def send(method, microversion_header=None): |
| 76 | def wrap(func): |
| 77 | def wrapped_f(*args, **kwargs): |
| 78 | headers = kwargs.pop('headers', {}) |
| 79 | if kwargs.get('microversion'): |
| 80 | headers.setdefault(microversion_header, |
| 81 | kwargs.get('microversion')) |
| 82 | cloud_name = kwargs.pop('cloud_name') |
| 83 | if not cloud_name: |
| 84 | e = NoCredentials() |
| 85 | log.error('%s' % e) |
| 86 | raise e |
| 87 | adapter = _get_raw_client(cloud_name) |
| 88 | url, json = func(*args, **kwargs) |
| 89 | if json: |
| 90 | response = getattr(adapter, method)(url, headers=headers, |
| 91 | json=json) |
| 92 | else: |
| 93 | response = getattr(adapter, method)(url, headers=headers) |
| 94 | if not response.content: |
| 95 | return {} |
| 96 | return response.json() |
| 97 | return wrapped_f |
| 98 | return wrap |