Vladyslav Drok | ffb6f71 | 2018-03-22 19:56:00 +0200 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | |
| 4 | try: |
| 5 | import os_client_config |
| 6 | from keystoneauth1 import exceptions as ka_exceptions |
| 7 | REQUIREMENTS_MET = True |
| 8 | except ImportError: |
| 9 | REQUIREMENTS_MET = False |
| 10 | |
| 11 | |
| 12 | def __virtual__(): |
| 13 | """Only load manilang if requirements are available.""" |
| 14 | if REQUIREMENTS_MET: |
| 15 | return 'manilang' |
| 16 | else: |
| 17 | return False, ("The manilang execution module cannot be loaded: " |
| 18 | "os_client_config or keystoneauth are unavailable.") |
| 19 | |
| 20 | |
| 21 | log = logging.getLogger(__name__) |
| 22 | |
| 23 | |
| 24 | class ManilaException(Exception): |
| 25 | |
| 26 | _msg = "Manila module exception occured." |
| 27 | |
| 28 | def __init__(self, message=None, **kwargs): |
| 29 | super(ManilaException, self).__init__(message or self._msg) |
| 30 | |
| 31 | |
| 32 | class NoManilaEndpoint(ManilaException): |
| 33 | _msg = "Manila endpoint not found in keystone catalog." |
| 34 | |
| 35 | |
| 36 | class NoAuthPluginConfigured(ManilaException): |
| 37 | _msg = ("You are using keystoneauth auth plugin that does not support " |
| 38 | "fetching endpoint list from token (noauth or admin_token).") |
| 39 | |
| 40 | |
| 41 | class NoCredentials(ManilaException): |
| 42 | _msg = "Please provide cloud name present in clouds.yaml." |
| 43 | |
| 44 | |
| 45 | def _get_raw_client(cloud_name): |
| 46 | service_type = 'sharev2' |
| 47 | adapter = os_client_config.make_rest_client(service_type, |
| 48 | cloud=cloud_name) |
| 49 | try: |
| 50 | access_info = adapter.session.auth.get_access(adapter.session) |
| 51 | endpoints = access_info.service_catalog.get_endpoints() |
| 52 | except (AttributeError, ValueError): |
| 53 | e = NoAuthPluginConfigured() |
| 54 | log.error('%s' % e) |
| 55 | raise e |
| 56 | if service_type not in endpoints: |
| 57 | service_type = None |
| 58 | for possible_type in ('share', 'shared-file-system'): |
| 59 | if possible_type in endpoints: |
| 60 | service_type = possible_type |
| 61 | break |
| 62 | if not service_type: |
| 63 | e = NoManilaEndpoint() |
| 64 | log.error('%s' % e) |
| 65 | raise e |
| 66 | adapter = os_client_config.make_rest_client(service_type, |
| 67 | cloud=cloud_name) |
| 68 | log.debug("Using manila endpoint with type %s." % service_type) |
| 69 | return adapter |
| 70 | |
| 71 | |
| 72 | def _add_microversion_header(microversion, headers): |
| 73 | if microversion: |
| 74 | headers.setdefault('X-OpenStack-Manila-API-Version', microversion) |
| 75 | |
| 76 | |
| 77 | def create_adapter(fun): |
| 78 | def inner(*args, **kwargs): |
| 79 | headers = kwargs.pop('headers', {}) |
| 80 | _add_microversion_header(kwargs.get('microversion'), headers) |
| 81 | cloud_name = kwargs.get('cloud_name') |
| 82 | if not cloud_name: |
| 83 | e = NoCredentials() |
| 84 | log.error('%s' % e) |
| 85 | raise e |
| 86 | adapter = _get_raw_client(cloud_name) |
| 87 | return fun(*args, adapter=adapter, headers=headers, **kwargs) |
| 88 | return inner |
| 89 | |
| 90 | |
| 91 | @create_adapter |
| 92 | def get_default_share_types(**kwargs): |
| 93 | adapter = kwargs.get('adapter') |
| 94 | try: |
| 95 | response = adapter.get('/types/default', |
| 96 | headers=kwargs.get('headers', {})) |
| 97 | except ka_exceptions.NotFound: |
| 98 | log.debug("No default share type found.") |
| 99 | return None |
| 100 | return response.json() |
| 101 | |
| 102 | |
| 103 | @create_adapter |
| 104 | def create_share_type(name, driver_handles_share_servers, extra_specs=None, |
| 105 | is_public=True, **kwargs): |
| 106 | adapter = kwargs.get('adapter') |
| 107 | extra_specs = extra_specs or {} |
| 108 | extra_specs['driver_handles_share_servers'] = driver_handles_share_servers |
| 109 | post_data = { |
| 110 | 'share_type': { |
| 111 | 'extra_specs': extra_specs, 'name': name, |
| 112 | 'os-share-type-access:is_public': is_public}} |
| 113 | # NOTE: passing share_type dictionary in kwargs will override anything |
| 114 | # that was constructed from function arguments. Use with caution. |
| 115 | # is_public attribute is special, as os-share-type-access:is_public |
| 116 | # always overrides share_type_access:is_public, no matter what |
| 117 | # microversion used (sic!). |
| 118 | post_data['share_type'].update(kwargs.get('share_type', {})) |
| 119 | response = adapter.post('/types', json=post_data, |
| 120 | headers=kwargs.get('headers', {})) |
| 121 | return response.json()['share_type'] |