|
| 1 | +# Copyright 2012-2013 OpenStack Foundation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import logging |
| 17 | +from typing import TYPE_CHECKING, Any |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from manilaclient import client |
| 21 | + |
| 22 | +from osc_lib import exceptions |
| 23 | +from osc_lib import utils |
| 24 | + |
| 25 | +from openstackclient.i18n import _ |
| 26 | + |
| 27 | +LOG = logging.getLogger(__name__) |
| 28 | + |
| 29 | +# global variables used when building the shell |
| 30 | +DEFAULT_API_VERSION = '2' |
| 31 | +API_VERSION_OPTION = 'os_share_api_version' |
| 32 | +API_NAME = 'share' |
| 33 | + |
| 34 | +# Save the microversion if in use |
| 35 | +_share_api_version = None |
| 36 | + |
| 37 | + |
| 38 | +def make_client(instance: Any) -> 'client.Client': |
| 39 | + """Returns a manilaclient.client.Client instance.""" |
| 40 | + from manilaclient import api_versions |
| 41 | + from manilaclient import client |
| 42 | + |
| 43 | + check_version = instance._api_version[API_NAME] |
| 44 | + if check_version.isdigit(): |
| 45 | + check_version = f"{check_version}.0" |
| 46 | + |
| 47 | + version = api_versions.get_api_version(check_version) |
| 48 | + |
| 49 | + instance.setup_auth() |
| 50 | + |
| 51 | + LOG.debug('Instantiating Shared File System client: %s', client.Client) |
| 52 | + LOG.debug('Shared File System API version: %s', version) |
| 53 | + |
| 54 | + return client.Client( |
| 55 | + version, |
| 56 | + session=instance.session, |
| 57 | + endpoint_type=instance.interface, |
| 58 | + region_name=instance.region_name, |
| 59 | + auth=instance.auth, |
| 60 | + cacert=instance.cacert, |
| 61 | + cert=instance.cert, |
| 62 | + insecure=not instance.verify, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def build_option_parser( |
| 67 | + parser: argparse.ArgumentParser, |
| 68 | +) -> argparse.ArgumentParser: |
| 69 | + """Hook to add global options""" |
| 70 | + parser.add_argument( |
| 71 | + '--os-share-api-version', |
| 72 | + metavar='<share-api-version>', |
| 73 | + default=utils.env('OS_SHARE_API_VERSION'), |
| 74 | + help=_( |
| 75 | + "Shared File System API version, default=%s " |
| 76 | + "(Env: OS_SHARE_API_VERSION)" |
| 77 | + ) |
| 78 | + % DEFAULT_API_VERSION, |
| 79 | + ) |
| 80 | + return parser |
| 81 | + |
| 82 | + |
| 83 | +def check_api_version(check_version: str) -> bool: |
| 84 | + # Defer client imports until we actually need them |
| 85 | + from manilaclient import api_versions |
| 86 | + |
| 87 | + global _share_api_version |
| 88 | + |
| 89 | + # TODO(stephenfin): Other clients (novaclient, cinderclient, ...) do this |
| 90 | + # normalization for us |
| 91 | + if check_version.isdigit(): |
| 92 | + check_version = f"{check_version}.0" |
| 93 | + |
| 94 | + _share_api_version = api_versions.get_api_version(check_version) |
| 95 | + |
| 96 | + # Bypass X.latest format microversion |
| 97 | + if not _share_api_version.is_latest(): |
| 98 | + if _share_api_version > api_versions.APIVersion("2.0"): |
| 99 | + if not _share_api_version.matches( |
| 100 | + api_versions.MIN_VERSION, |
| 101 | + api_versions.MAX_VERSION, |
| 102 | + ): |
| 103 | + msg = _("versions supported by client: %(min)s - %(max)s") % { |
| 104 | + "min": api_versions.MIN_VERSION, |
| 105 | + "max": api_versions.MAX_VERSION, |
| 106 | + } |
| 107 | + raise exceptions.CommandError(msg) |
| 108 | + |
| 109 | + return True |
| 110 | + |
| 111 | + return False |
0 commit comments