2626from quantumclient .common import exceptions
2727from quantumclient .common import utils
2828
29+ HEX_ELEM = '[0-9A-Fa-f]'
30+ UUID_PATTERN = '-' .join ([HEX_ELEM + '{8}' , HEX_ELEM + '{4}' ,
31+ HEX_ELEM + '{4}' , HEX_ELEM + '{4}' ,
32+ HEX_ELEM + '{12}' ])
33+
34+
35+ def find_resourceid_by_name_or_id (client , resource , name_or_id ):
36+ obj_lister = getattr (client , "list_%ss" % resource )
37+ # perform search by id only if we are passing a valid UUID
38+ match = re .match (UUID_PATTERN , name_or_id )
39+ collection = resource + "s"
40+ if match :
41+ data = obj_lister (id = name_or_id , fields = 'id' )
42+ if data and data [collection ]:
43+ return data [collection ][0 ]['id' ]
44+ return _find_resourceid_by_name (client , resource , name_or_id )
45+
46+
47+ def _find_resourceid_by_name (client , resource , name ):
48+ obj_lister = getattr (client , "list_%ss" % resource )
49+ data = obj_lister (name = name , fields = 'id' )
50+ collection = resource + "s"
51+ info = data [collection ]
52+ if len (info ) > 1 :
53+ msg = (_ ("Multiple %(resource)s matches found for '%(name)s',"
54+ " use an ID to be more specific." ) %
55+ {'resource' : resource , 'name' : name })
56+ raise exceptions .QuantumClientException (
57+ message = msg )
58+ elif len (info ) == 0 :
59+ not_found_message = (_ ("Unable to find %(resource)s with '%(name)s'" ) %
60+ {'resource' : resource , 'name' : name })
61+ # 404 is used to simulate server side behavior
62+ raise exceptions .QuantumClientException (
63+ message = not_found_message , status_code = 404 )
64+ else :
65+ return info [0 ]['id' ]
66+
2967
3068def add_show_list_common_argument (parser ):
3169 parser .add_argument (
@@ -222,8 +260,8 @@ class UpdateCommand(QuantumCommand):
222260 def get_parser (self , prog_name ):
223261 parser = super (UpdateCommand , self ).get_parser (prog_name )
224262 parser .add_argument (
225- 'id' , metavar = '%s_id' % self .resource ,
226- help = 'ID of %s to update' % self .resource )
263+ 'id' , metavar = self .resource ,
264+ help = 'ID or name of %s to update' % self .resource )
227265 add_extra_argument (parser , 'value_specs' ,
228266 'new values for the %s' % self .resource )
229267 return parser
@@ -237,9 +275,12 @@ def run(self, parsed_args):
237275 raise exceptions .CommandError (
238276 "Must specify new values to update %s" % self .resource )
239277 data = {self .resource : parse_args_to_dict (value_specs )}
278+ _id = find_resourceid_by_name_or_id (quantum_client ,
279+ self .resource ,
280+ parsed_args .id )
240281 obj_updator = getattr (quantum_client ,
241282 "update_%s" % self .resource )
242- obj_updator (parsed_args . id , data )
283+ obj_updator (_id , data )
243284 print >> self .app .stdout , (
244285 _ ('Updated %(resource)s: %(id)s' ) %
245286 {'id' : parsed_args .id , 'resource' : self .resource })
@@ -258,8 +299,8 @@ class DeleteCommand(QuantumCommand):
258299 def get_parser (self , prog_name ):
259300 parser = super (DeleteCommand , self ).get_parser (prog_name )
260301 parser .add_argument (
261- 'id' , metavar = '%s_id' % self .resource ,
262- help = 'ID of %s to delete' % self .resource )
302+ 'id' , metavar = self .resource ,
303+ help = 'ID or name of %s to delete' % self .resource )
263304 return parser
264305
265306 def run (self , parsed_args ):
@@ -268,7 +309,10 @@ def run(self, parsed_args):
268309 quantum_client .format = parsed_args .request_format
269310 obj_deleter = getattr (quantum_client ,
270311 "delete_%s" % self .resource )
271- obj_deleter (parsed_args .id )
312+ _id = find_resourceid_by_name_or_id (quantum_client ,
313+ self .resource ,
314+ parsed_args .id )
315+ obj_deleter (_id )
272316 print >> self .app .stdout , (_ ('Deleted %(resource)s: %(id)s' )
273317 % {'id' : parsed_args .id ,
274318 'resource' : self .resource })
@@ -327,10 +371,7 @@ class ShowCommand(QuantumCommand, show.ShowOne):
327371 """Show information of a given resource
328372
329373 """
330- HEX_ELEM = '[0-9A-Fa-f]'
331- UUID_PATTERN = '-' .join ([HEX_ELEM + '{8}' , HEX_ELEM + '{4}' ,
332- HEX_ELEM + '{4}' , HEX_ELEM + '{4}' ,
333- HEX_ELEM + '{12}' ])
374+
334375 api = 'network'
335376 resource = None
336377 log = None
@@ -339,7 +380,7 @@ def get_parser(self, prog_name):
339380 parser = super (ShowCommand , self ).get_parser (prog_name )
340381 add_show_list_common_argument (parser )
341382 parser .add_argument (
342- 'id' , metavar = '%s_id' % self .resource ,
383+ 'id' , metavar = self .resource ,
343384 help = 'ID or name of %s to look up' % self .resource )
344385 return parser
345386
@@ -353,57 +394,10 @@ def get_data(self, parsed_args):
353394 params = {'verbose' : 'True' }
354395 if parsed_args .fields :
355396 params = {'fields' : parsed_args .fields }
356-
357- data = None
358- # Error message to be used in case both search by id and name are
359- # unsuccessful (if list by name fails it does not return an error)
360- not_found_message = "Unable to find resource:%s" % parsed_args .id
361-
362- # perform search by id only if we are passing a valid UUID
363- match = re .match (self .UUID_PATTERN , parsed_args .id )
364- if match :
365- try :
366- obj_shower = getattr (quantum_client ,
367- "show_%s" % self .resource )
368- data = obj_shower (parsed_args .id , ** params )
369- except exceptions .QuantumClientException as ex :
370- logging .debug ("Show operation failed with code:%s" ,
371- ex .status_code )
372- not_found_message = ex .message
373- if ex .status_code != 404 :
374- logging .exception ("Unable to perform show operation" )
375- raise
376-
377- # If data is empty, then we got a 404. Try to interpret Id as a name
378- if not data :
379- logging .debug ("Trying to interpret %s as a %s name" ,
380- parsed_args .id ,
381- self .resource )
382- # build search_opts for the name
383- search_opts = parse_args_to_dict (["--name=%s" % parsed_args .id ])
384- search_opts .update (params )
385- obj_lister = getattr (quantum_client ,
386- "list_%ss" % self .resource )
387- data = obj_lister (** search_opts )
388- info = []
389- collection = self .resource + "s"
390- if collection in data :
391- info = data [collection ]
392- if len (info ) > 1 :
393- logging .info ("Multiple occurrences found for: %s" ,
394- parsed_args .id )
395- _columns = ['id' ]
396- # put all ids in a single string as formatter for show
397- # command will print on record only
398- id_string = "\n " .join (utils .get_item_properties (
399- s , _columns )[0 ] for s in info )
400- return (_columns , (id_string , ), )
401- elif len (info ) == 0 :
402- #Nothing was found
403- raise exceptions .QuantumClientException (
404- message = not_found_message )
405- else :
406- data = {self .resource : info [0 ]}
397+ _id = find_resourceid_by_name_or_id (quantum_client , self .resource ,
398+ parsed_args .id )
399+ obj_shower = getattr (quantum_client , "show_%s" % self .resource )
400+ data = obj_shower (_id , ** params )
407401 if self .resource in data :
408402 for k , v in data [self .resource ].iteritems ():
409403 if isinstance (v , list ):
0 commit comments