2020import os
2121import sys
2222
23- from novaclient import api_versions
23+ from openstack import utils as sdk_utils
2424from osc_lib .command import command
2525from osc_lib import exceptions
2626from osc_lib import utils
3232LOG = logging .getLogger (__name__ )
3333
3434
35+ def _get_keypair_columns (item , hide_pub_key = False , hide_priv_key = False ):
36+ # To maintain backwards compatibility we need to rename sdk props to
37+ # whatever OSC was using before
38+ column_map = {}
39+ hidden_columns = ['links' , 'location' ]
40+ if hide_pub_key :
41+ hidden_columns .append ('public_key' )
42+ if hide_priv_key :
43+ hidden_columns .append ('private_key' )
44+ return utils .get_osc_show_columns_for_sdk_resource (
45+ item , column_map , hidden_columns )
46+
47+
3548class CreateKeypair (command .ShowOne ):
3649 _description = _ ("Create new public or private key for server ssh access" )
3750
@@ -76,9 +89,13 @@ def get_parser(self, prog_name):
7689 return parser
7790
7891 def take_action (self , parsed_args ):
79- compute_client = self .app .client_manager .compute
92+ compute_client = self .app .client_manager .sdk_connection . compute
8093 identity_client = self .app .client_manager .identity
8194
95+ kwargs = {
96+ 'name' : parsed_args .name
97+ }
98+
8299 public_key = parsed_args .public_key
83100 if public_key :
84101 try :
@@ -91,12 +108,10 @@ def take_action(self, parsed_args):
91108 "exception" : e }
92109 )
93110
94- kwargs = {
95- 'name' : parsed_args .name ,
96- 'public_key' : public_key ,
97- }
111+ kwargs ['public_key' ] = public_key
112+
98113 if parsed_args .type :
99- if compute_client . api_version < api_versions . APIVersion ( '2.2' ):
114+ if not sdk_utils . supports_microversion ( compute_client , '2.2' ):
100115 msg = _ (
101116 '--os-compute-api-version 2.2 or greater is required to '
102117 'support the --type option'
@@ -106,7 +121,7 @@ def take_action(self, parsed_args):
106121 kwargs ['key_type' ] = parsed_args .type
107122
108123 if parsed_args .user :
109- if compute_client . api_version < api_versions . APIVersion ( '2.10' ):
124+ if not sdk_utils . supports_microversion ( compute_client , '2.10' ):
110125 msg = _ (
111126 '--os-compute-api-version 2.10 or greater is required to '
112127 'support the --user option'
@@ -119,7 +134,7 @@ def take_action(self, parsed_args):
119134 parsed_args .user_domain ,
120135 ).id
121136
122- keypair = compute_client .keypairs . create (** kwargs )
137+ keypair = compute_client .create_keypair (** kwargs )
123138
124139 private_key = parsed_args .private_key
125140 # Save private key into specified file
@@ -139,14 +154,12 @@ def take_action(self, parsed_args):
139154 # NOTE(dtroyer): how do we want to handle the display of the private
140155 # key when it needs to be communicated back to the user
141156 # For now, duplicate nova keypair-add command output
142- info = {}
143157 if public_key or private_key :
144- info .update (keypair ._info )
145- if 'public_key' in info :
146- del info ['public_key' ]
147- if 'private_key' in info :
148- del info ['private_key' ]
149- return zip (* sorted (info .items ()))
158+ display_columns , columns = _get_keypair_columns (
159+ keypair , hide_pub_key = True , hide_priv_key = True )
160+ data = utils .get_item_properties (keypair , columns )
161+
162+ return (display_columns , data )
150163 else :
151164 sys .stdout .write (keypair .private_key )
152165 return ({}, {})
@@ -175,14 +188,14 @@ def get_parser(self, prog_name):
175188 return parser
176189
177190 def take_action (self , parsed_args ):
178- compute_client = self .app .client_manager .compute
191+ compute_client = self .app .client_manager .sdk_connection . compute
179192 identity_client = self .app .client_manager .identity
180193
181194 kwargs = {}
182195 result = 0
183196
184197 if parsed_args .user :
185- if compute_client . api_version < api_versions . APIVersion ( '2.10' ):
198+ if not sdk_utils . supports_microversion ( compute_client , '2.10' ):
186199 msg = _ (
187200 '--os-compute-api-version 2.10 or greater is required to '
188201 'support the --user option'
@@ -197,9 +210,8 @@ def take_action(self, parsed_args):
197210
198211 for n in parsed_args .name :
199212 try :
200- data = utils .find_resource (
201- compute_client .keypairs , n )
202- compute_client .keypairs .delete (data .name , ** kwargs )
213+ compute_client .delete_keypair (
214+ n , ** kwargs , ignore_missing = False )
203215 except Exception as e :
204216 result += 1
205217 LOG .error (_ ("Failed to delete key with name "
@@ -240,11 +252,11 @@ def get_parser(self, prog_name):
240252 return parser
241253
242254 def take_action (self , parsed_args ):
243- compute_client = self .app .client_manager .compute
255+ compute_client = self .app .client_manager .sdk_connection . compute
244256 identity_client = self .app .client_manager .identity
245257
246258 if parsed_args .project :
247- if compute_client . api_version < api_versions . APIVersion ( '2.10' ):
259+ if not sdk_utils . supports_microversion ( compute_client , '2.10' ):
248260 msg = _ (
249261 '--os-compute-api-version 2.10 or greater is required to '
250262 'support the --project option'
@@ -263,9 +275,9 @@ def take_action(self, parsed_args):
263275
264276 data = []
265277 for user in users :
266- data .extend (compute_client .keypairs . list (user_id = user .id ))
278+ data .extend (compute_client .keypairs (user_id = user .id ))
267279 elif parsed_args .user :
268- if compute_client . api_version < api_versions . APIVersion ( '2.10' ):
280+ if not sdk_utils . supports_microversion ( compute_client , '2.10' ):
269281 msg = _ (
270282 '--os-compute-api-version 2.10 or greater is required to '
271283 'support the --user option'
@@ -278,16 +290,16 @@ def take_action(self, parsed_args):
278290 parsed_args .user_domain ,
279291 )
280292
281- data = compute_client .keypairs . list (user_id = user .id )
293+ data = compute_client .keypairs (user_id = user .id )
282294 else :
283- data = compute_client .keypairs . list ()
295+ data = compute_client .keypairs ()
284296
285297 columns = (
286298 "Name" ,
287299 "Fingerprint"
288300 )
289301
290- if compute_client . api_version >= api_versions . APIVersion ( '2.2' ):
302+ if sdk_utils . supports_microversion ( compute_client , '2.2' ):
291303 columns += ("Type" , )
292304
293305 return (
@@ -324,13 +336,13 @@ def get_parser(self, prog_name):
324336 return parser
325337
326338 def take_action (self , parsed_args ):
327- compute_client = self .app .client_manager .compute
339+ compute_client = self .app .client_manager .sdk_connection . compute
328340 identity_client = self .app .client_manager .identity
329341
330342 kwargs = {}
331343
332344 if parsed_args .user :
333- if compute_client . api_version < api_versions . APIVersion ( '2.10' ):
345+ if not sdk_utils . supports_microversion ( compute_client , '2.10' ):
334346 msg = _ (
335347 '--os-compute-api-version 2.10 or greater is required to '
336348 'support the --user option'
@@ -343,16 +355,14 @@ def take_action(self, parsed_args):
343355 parsed_args .user_domain ,
344356 ).id
345357
346- keypair = utils . find_resource (
347- compute_client . keypairs , parsed_args .name , ** kwargs )
358+ keypair = compute_client . find_keypair (
359+ parsed_args .name , ** kwargs , ignore_missing = False )
348360
349- info = {}
350- info .update (keypair ._info )
351361 if not parsed_args .public_key :
352- del info ['public_key' ]
353- return zip (* sorted (info .items ()))
362+ display_columns , columns = _get_keypair_columns (
363+ keypair , hide_pub_key = True )
364+ data = utils .get_item_properties (keypair , columns )
365+ return (display_columns , data )
354366 else :
355- # NOTE(dtroyer): a way to get the public key in a similar form
356- # as the private key in the create command
357367 sys .stdout .write (keypair .public_key )
358368 return ({}, {})
0 commit comments