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 | |
Oleksiy Petrenko | a47678d | 2019-02-06 13:07:20 +0200 | [diff] [blame] | 18 | try: |
| 19 | import os_client_config |
| 20 | except ImportError: |
| 21 | os_client_config = None |
| 22 | from salt import exceptions |
Oleksiy Petrenko | e2c8da2 | 2018-03-30 18:27:58 +0300 | [diff] [blame] | 23 | |
| 24 | |
| 25 | MANILA_HEADER = 'X-OpenStack-Manila-API-Version' |
| 26 | |
| 27 | |
| 28 | log = logging.getLogger(__name__) |
| 29 | |
| 30 | |
| 31 | class ManilaException(Exception): |
| 32 | |
| 33 | _msg = "Manila module exception occured." |
| 34 | |
| 35 | def __init__(self, message=None, **kwargs): |
| 36 | super(ManilaException, self).__init__(message or self._msg) |
| 37 | |
| 38 | |
| 39 | class NoManilaEndpoint(ManilaException): |
| 40 | _msg = "Manila endpoint not found in keystone catalog." |
| 41 | |
| 42 | |
| 43 | class NoAuthPluginConfigured(ManilaException): |
| 44 | _msg = ("You are using keystoneauth auth plugin that does not support " |
| 45 | "fetching endpoint list from token (noauth or admin_token).") |
| 46 | |
| 47 | |
| 48 | class NoCredentials(ManilaException): |
| 49 | _msg = "Please provide cloud name present in clouds.yaml." |
| 50 | |
| 51 | |
| 52 | def _get_raw_client(cloud_name): |
Oleksiy Petrenko | a47678d | 2019-02-06 13:07:20 +0200 | [diff] [blame] | 53 | if not os_client_config: |
| 54 | raise exceptions.SaltInvocationError( |
| 55 | "Cannot load os-client-config. Please check your environment " |
| 56 | "configuration.") |
Oleksiy Petrenko | e2c8da2 | 2018-03-30 18:27:58 +0300 | [diff] [blame] | 57 | service_type = 'sharev2' |
| 58 | adapter = os_client_config.make_rest_client(service_type, |
| 59 | cloud=cloud_name) |
| 60 | try: |
| 61 | access_info = adapter.session.auth.get_access(adapter.session) |
| 62 | endpoints = access_info.service_catalog.get_endpoints() |
| 63 | except (AttributeError, ValueError): |
| 64 | e = NoAuthPluginConfigured() |
| 65 | log.error('%s' % e) |
| 66 | raise e |
| 67 | if service_type not in endpoints: |
| 68 | service_type = None |
| 69 | for possible_type in ('share', 'shared-file-system'): |
| 70 | if possible_type in endpoints: |
| 71 | service_type = possible_type |
| 72 | break |
| 73 | if not service_type: |
| 74 | e = NoManilaEndpoint() |
| 75 | log.exception('%s' % e) |
| 76 | raise e |
| 77 | adapter = os_client_config.make_rest_client(service_type, |
| 78 | cloud=cloud_name) |
| 79 | log.debug("Using manila endpoint with type %s." % service_type) |
| 80 | return adapter |
| 81 | |
| 82 | |
| 83 | def send(method, microversion_header=None): |
| 84 | def wrap(func): |
| 85 | def wrapped_f(*args, **kwargs): |
| 86 | headers = kwargs.pop('headers', {}) |
| 87 | if kwargs.get('microversion'): |
| 88 | headers.setdefault(microversion_header, |
| 89 | kwargs.get('microversion')) |
| 90 | cloud_name = kwargs.pop('cloud_name') |
| 91 | if not cloud_name: |
| 92 | e = NoCredentials() |
| 93 | log.error('%s' % e) |
| 94 | raise e |
| 95 | adapter = _get_raw_client(cloud_name) |
| 96 | url, json = func(*args, **kwargs) |
| 97 | if json: |
| 98 | response = getattr(adapter, method)(url, headers=headers, |
| 99 | json=json) |
| 100 | else: |
| 101 | response = getattr(adapter, method)(url, headers=headers) |
| 102 | if not response.content: |
| 103 | return {} |
| 104 | return response.json() |
| 105 | return wrapped_f |
| 106 | return wrap |