1515
1616import logging
1717
18- from osc_lib import exceptions
1918from osc_lib import utils
2019
2120from openstackclient .i18n import _
2221
23-
2422LOG = logging .getLogger (__name__ )
2523
2624DEFAULT_API_VERSION = '2.1'
2725API_VERSION_OPTION = 'os_compute_api_version'
2826API_NAME = 'compute'
2927API_VERSIONS = {
30- "2" : "novaclient.client" ,
31- "2.1" : "novaclient.client" ,
32- }
33-
34- COMPUTE_API_VERSIONS = {
35- '2' : 'openstackclient.api.compute_v2.APIv2' ,
28+ '2' : 'openstack.connection.Connection' ,
29+ '2.1' : 'openstack.connection.Connection' ,
3630}
3731
38- # Save the microversion if in use
39- _compute_api_version = None
40-
4132
4233def make_client (instance ):
4334 """Returns a compute service client."""
44-
45- # Defer client import until we actually need them
46- from novaclient import client as nova_client
47-
48- if _compute_api_version is not None :
49- version = _compute_api_version
50- else :
51- version = instance ._api_version [API_NAME ]
52- from novaclient import api_versions
53-
54- # convert to APIVersion object
55- version = api_versions .get_api_version (version )
56-
57- if version .is_latest ():
58- import novaclient
59-
60- # NOTE(RuiChen): executing version discovery make sense, but that need
61- # an initialized REST client, it's not available now,
62- # fallback to use the max version of novaclient side.
63- version = novaclient .API_MAX_VERSION
64-
65- # Set client http_log_debug to True if verbosity level is high enough
66- http_log_debug = utils .get_effective_log_level () <= logging .DEBUG
67-
68- extensions = [
69- ext
70- for ext in nova_client .discover_extensions (version )
71- if ext .name == "list_extensions"
72- ]
73-
74- # Remember interface only if it is set
75- kwargs = utils .build_kwargs_dict ('endpoint_type' , instance .interface )
76-
77- client = nova_client .Client (
78- version ,
79- session = instance .session ,
80- extensions = extensions ,
81- http_log_debug = http_log_debug ,
82- timings = instance .timing ,
83- region_name = instance .region_name ,
84- ** kwargs
35+ LOG .debug (
36+ 'Compute client initialized using OpenStack SDK: %s' ,
37+ instance .sdk_connection .compute ,
8538 )
86-
87- return client
39+ return instance .sdk_connection .compute
8840
8941
9042def build_option_parser (parser ):
@@ -93,48 +45,7 @@ def build_option_parser(parser):
9345 '--os-compute-api-version' ,
9446 metavar = '<compute-api-version>' ,
9547 default = utils .env ('OS_COMPUTE_API_VERSION' ),
96- help = _ (
97- "Compute API version, default=%s " "(Env: OS_COMPUTE_API_VERSION)"
98- )
48+ help = _ ("Compute API version, default=%s (Env: OS_COMPUTE_API_VERSION)" )
9949 % DEFAULT_API_VERSION ,
10050 )
10151 return parser
102-
103-
104- def check_api_version (check_version ):
105- """Validate version supplied by user
106-
107- Returns:
108-
109- * True if version is OK
110- * False if the version has not been checked and the previous plugin
111- check should be performed
112- * throws an exception if the version is no good
113-
114- TODO(dtroyer): make the exception thrown a version-related one
115- """
116-
117- # Defer client imports until we actually need them
118- import novaclient
119- from novaclient import api_versions
120-
121- global _compute_api_version
122-
123- # Copy some logic from novaclient 3.3.0 for basic version detection
124- # NOTE(dtroyer): This is only enough to resume operations using API
125- # version 2.0 or any valid version supplied by the user.
126- _compute_api_version = api_versions .get_api_version (check_version )
127-
128- # Bypass X.latest format microversion
129- if not _compute_api_version .is_latest ():
130- if _compute_api_version > api_versions .APIVersion ("2.0" ):
131- if not _compute_api_version .matches (
132- novaclient .API_MIN_VERSION ,
133- novaclient .API_MAX_VERSION ,
134- ):
135- msg = _ ("versions supported by client: %(min)s - %(max)s" ) % {
136- "min" : novaclient .API_MIN_VERSION .get_string (),
137- "max" : novaclient .API_MAX_VERSION .get_string (),
138- }
139- raise exceptions .CommandError (msg )
140- return True
0 commit comments