1111# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212# License for the specific language governing permissions and limitations
1313# under the License.
14- #
1514
1615import argparse
1716import logging
1817from typing import Any
1918
20- from osc_lib import exceptions
2119from osc_lib import utils
2220
2321from openstackclient .i18n import _
2422
2523LOG = logging .getLogger (__name__ )
2624
25+ # global variables used when building the shell
2726DEFAULT_API_VERSION = '3'
2827API_VERSION_OPTION = 'os_volume_api_version'
2928API_NAME = 'volume'
30- API_VERSIONS = {
31- '2' : 'cinderclient.v2.client.Client' ,
32- '3' : 'cinderclient.v3.client.Client' ,
33- }
34-
35- # Save the microversion if in use
36- _volume_api_version = None
29+ API_VERSIONS = ('2' , '3' )
3730
3831
3932def make_client (instance : Any ) -> Any :
4033 """Returns a volume service client."""
41-
42- # Defer client imports until we actually need them
43- from cinderclient import extension
44- from cinderclient .v3 .contrib import list_extensions
45- from cinderclient .v3 import volume_snapshots
46- from cinderclient .v3 import volumes
47-
48- try :
49- from cinderclient .v2 import services # noqa
50- except Exception :
51- del API_VERSIONS ['2' ]
52-
53- if _volume_api_version is not None :
54- version = _volume_api_version
55- else :
56- version = instance ._api_version [API_NAME ]
57- from cinderclient import api_versions
58-
59- # convert to APIVersion object
60- version = api_versions .get_api_version (version )
61-
62- if version .ver_major == '1' :
63- # Monkey patch for v1 cinderclient
64- volumes .Volume .NAME_ATTR = 'display_name'
65- volume_snapshots .Snapshot .NAME_ATTR = 'display_name'
66-
67- volume_client = utils .get_client_class (
68- API_NAME , version .ver_major , API_VERSIONS
69- )
70- LOG .debug ('Instantiating volume client: %s' , volume_client )
71-
72- # Set client http_log_debug to True if verbosity level is high enough
73- http_log_debug = utils .get_effective_log_level () <= logging .DEBUG
74-
75- extensions = [extension .Extension ('list_extensions' , list_extensions )]
76-
77- # Remember interface only if it is set
78- kwargs = utils .build_kwargs_dict ('endpoint_type' , instance .interface )
79-
80- endpoint_override = instance .sdk_connection .config .get_endpoint (
81- 'block-storage'
82- )
83-
84- client = volume_client (
85- session = instance .session ,
86- extensions = extensions ,
87- http_log_debug = http_log_debug ,
88- region_name = instance .region_name ,
89- endpoint_override = endpoint_override ,
90- api_version = version ,
91- ** kwargs ,
34+ LOG .debug (
35+ 'Volume client initialized using OpenStack SDK: %s' ,
36+ instance .sdk_connection .block_storage ,
9237 )
93-
94- return client
38+ return instance .sdk_connection .block_storage
9539
9640
9741def build_option_parser (
@@ -109,36 +53,5 @@ def build_option_parser(
10953
11054
11155def check_api_version (check_version : str ) -> bool :
112- """Validate version supplied by user
113-
114- Returns:
115-
116- * True if version is OK
117- * False if the version has not been checked and the previous plugin
118- check should be performed
119- * throws an exception if the version is no good
120- """
121-
122- # Defer client imports until we actually need them
123- from cinderclient import api_versions
124-
125- global _volume_api_version
126-
127- _volume_api_version = api_versions .get_api_version (check_version )
128-
129- # Bypass X.latest format microversion
130- if not _volume_api_version .is_latest ():
131- if _volume_api_version > api_versions .APIVersion ('3.0' ):
132- if not _volume_api_version .matches (
133- api_versions .MIN_VERSION ,
134- api_versions .MAX_VERSION ,
135- ):
136- msg = _ ('versions supported by client: %(min)s - %(max)s' ) % {
137- 'min' : api_versions .MIN_VERSION ,
138- 'max' : api_versions .MAX_VERSION ,
139- }
140- raise exceptions .CommandError (msg )
141-
142- return True
143-
144- return False
56+ # SDK supports auto-negotiation for us: always return True
57+ return True
0 commit comments