1717
1818import argparse
1919import logging
20+ import re
2021
2122from cliff import lister
2223from cliff import show
@@ -326,7 +327,10 @@ class ShowCommand(QuantumCommand, show.ShowOne):
326327 """Show information of a given resource
327328
328329 """
329-
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}' ])
330334 api = 'network'
331335 resource = None
332336 log = None
@@ -336,22 +340,70 @@ def get_parser(self, prog_name):
336340 add_show_list_common_argument (parser )
337341 parser .add_argument (
338342 'id' , metavar = '%s_id' % self .resource ,
339- help = 'ID of %s to look up' % self .resource )
340-
343+ help = 'ID or name of %s to look up' % self .resource )
341344 return parser
342345
343346 def get_data (self , parsed_args ):
344347 self .log .debug ('get_data(%s)' % parsed_args )
345348 quantum_client = self .get_client ()
346349 quantum_client .format = parsed_args .request_format
350+
347351 params = {}
348352 if parsed_args .show_details :
349353 params = {'verbose' : 'True' }
350354 if parsed_args .fields :
351355 params = {'fields' : parsed_args .fields }
352- obj_showor = getattr (quantum_client ,
353- "show_%s" % self .resource )
354- data = obj_showor (parsed_args .id , ** params )
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 ]}
355407 if self .resource in data :
356408 for k , v in data [self .resource ].iteritems ():
357409 if isinstance (v , list ):
0 commit comments