1818import six
1919
2020from openstackclient .common import command
21+ from openstackclient .common import exceptions
2122from openstackclient .common import parseractions
2223from openstackclient .common import utils
2324
2425
26+ def _find_flavor (compute_client , flavor ):
27+ try :
28+ return compute_client .flavors .get (flavor )
29+ except Exception as ex :
30+ if type (ex ).__name__ == 'NotFound' :
31+ pass
32+ else :
33+ raise
34+ try :
35+ return compute_client .flavors .find (name = flavor , is_public = None )
36+ except Exception as ex :
37+ if type (ex ).__name__ == 'NotFound' :
38+ msg = "No flavor with a name or ID of '%s' exists." % flavor
39+ raise exceptions .CommandError (msg )
40+ else :
41+ raise
42+
43+
2544class CreateFlavor (command .ShowOne ):
2645 """Create new flavor"""
2746
@@ -132,8 +151,7 @@ def get_parser(self, prog_name):
132151
133152 def take_action (self , parsed_args ):
134153 compute_client = self .app .client_manager .compute
135- flavor = utils .find_resource (compute_client .flavors ,
136- parsed_args .flavor )
154+ flavor = _find_flavor (compute_client , parsed_args .flavor )
137155 compute_client .flavors .delete (flavor .id )
138156
139157
@@ -239,8 +257,7 @@ def get_parser(self, prog_name):
239257
240258 def take_action (self , parsed_args ):
241259 compute_client = self .app .client_manager .compute
242- flavor = utils .find_resource (compute_client .flavors ,
243- parsed_args .flavor )
260+ flavor = _find_flavor (compute_client , parsed_args .flavor )
244261 flavor .set_keys (parsed_args .property )
245262
246263
@@ -258,8 +275,7 @@ def get_parser(self, prog_name):
258275
259276 def take_action (self , parsed_args ):
260277 compute_client = self .app .client_manager .compute
261- resource_flavor = utils .find_resource (compute_client .flavors ,
262- parsed_args .flavor )
278+ resource_flavor = _find_flavor (compute_client , parsed_args .flavor )
263279 flavor = resource_flavor ._info .copy ()
264280 flavor .pop ("links" , None )
265281
@@ -290,6 +306,5 @@ def get_parser(self, prog_name):
290306
291307 def take_action (self , parsed_args ):
292308 compute_client = self .app .client_manager .compute
293- flavor = utils .find_resource (compute_client .flavors ,
294- parsed_args .flavor )
309+ flavor = _find_flavor (compute_client , parsed_args .flavor )
295310 flavor .unset_keys (parsed_args .property )
0 commit comments