def main(self, argv): parser = self.get_subcommand_parser() args = parser.parse_args(argv) if args.cli_action == "list": print getattr(self.endpoint, pluralize(args.cli_noun)) if args.cli_action == "show": if args.id is not None: #has ID, show individual item self.do_show(args, pluralize(args.cli_noun)) else: #no ID specified, list all. print getattr(self.endpoint, pluralize(args.cli_noun)) if args.cli_action == "create": self.do_create(args, pluralize(args.cli_noun)) if args.cli_action == "delete": self.do_delete(args, pluralize(args.cli_noun)) if args.cli_action == "execute": self.do_execute(args, pluralize(args.cli_noun)) if args.cli_action == "filter": self.do_filter(args, pluralize(args.cli_noun)) if args.cli_action == "adventures": self.do_adventures(args, pluralize(args.cli_noun)) if args.cli_action == "logs": self.do_logs(args, pluralize(args.cli_noun))
def validate_id_or_name(self, obj_type, id_or_name): """Get object ID from type and name, or validate that the specified ID is valid. Returns the ID as an int, not an OpenCenter object. """ obj = getattr(self.endpoint, pluralize(obj_type)) try: id = int(id_or_name) return obj[id].id except ValueError: # can't convert string to int, try lookup by name matches = [x[1] for x in obj.filter( "name='%s'" % id_or_name).items()] if len(matches) == 1: return matches[0].id elif len(matches) == 0: raise ValueError('No %s found for id or name %s' % (obj_type, id_or_name)) elif len(matches) > 1: match_string = "\n".join(map(str, matches)) raise ValueError("Multiple %ss matched name %s, " "please specify an ID " "instead.\n\nMatches:\n%s" % (obj_type, id_or_name, match_string)) except KeyError: #obj[id] lookup failed, so ID is an int but not a valid ID. raise ValueError('No %s found for ID %s' % (obj_type, id_or_name))
def validate_id_or_name(self, obj_type, id_or_name): """Get object ID from type and name, or validate that the specified ID is valid. Returns the ID as an int, not an OpenCenter object. """ obj = getattr(self.endpoint, pluralize(obj_type)) try: id = int(id_or_name) return obj[id].id except ValueError: # can't convert string to int, try lookup by name matches = [ x[1] for x in obj.filter("name='%s'" % id_or_name).items() ] if len(matches) == 1: return matches[0].id elif len(matches) == 0: raise ValueError('No %s found for id or name %s' % (obj_type, id_or_name)) elif len(matches) > 1: match_string = "\n".join(map(str, matches)) raise ValueError("Multiple %ss matched name %s, " "please specify an ID " "instead.\n\nMatches:\n%s" % (obj_type, id_or_name, match_string)) except KeyError: #obj[id] lookup failed, so ID is an int but not a valid ID. raise ValueError('No %s found for ID %s' % (obj_type, id_or_name))
def main(self, argv): parser = self.get_subcommand_parser() args = parser.parse_args(argv) if args.cli_action == "list": print getattr(self.endpoint, pluralize(args.cli_noun)) if args.cli_action == "show": self.do_show(args, pluralize(args.cli_noun)) if args.cli_action == "create": self.do_create(args, pluralize(args.cli_noun)) if args.cli_action == "delete": self.do_delete(args, pluralize(args.cli_noun)) if args.cli_action == "execute": self.do_execute(args, pluralize(args.cli_noun))
self.validate_id_or_name(obj_type, value) ) except ValueError, e: print e return #Adventure has an arg called args, this conflicts with the arg_tree # structure, so I called the args arg arguments. At this point it # can be renamed back to args. if hasattr(args, 'arguments'): args.args = args.arguments del args.arguments if args.cli_action == "list": print getattr(self.endpoint, pluralize(args.cli_noun)) if args.cli_action == "show": #has ID, show individual item self.do_show(args, pluralize(args.cli_noun)) if args.cli_action == "create": self.do_create(args, pluralize(args.cli_noun)) if args.cli_action == "delete": self.do_delete(args, pluralize(args.cli_noun)) if args.cli_action == "update": self.do_create(args, pluralize(args.cli_noun)) if args.cli_action == "adventure":
setattr(args, attr_name, self.validate_id_or_name(obj_type, value)) except ValueError, e: print e return #Adventure has an arg called args, this conflicts with the arg_tree # structure, so I called the args arg arguments. At this point it # can be renamed back to args. if hasattr(args, 'arguments'): args.args = args.arguments del args.arguments if args.cli_action == "list": print getattr(self.endpoint, pluralize(args.cli_noun)) if args.cli_action == "show": #has ID, show individual item self.do_show(args, pluralize(args.cli_noun)) if args.cli_action == "create": self.do_create(args, pluralize(args.cli_noun)) if args.cli_action == "delete": self.do_delete(args, pluralize(args.cli_noun)) if args.cli_action == "update": self.do_create(args, pluralize(args.cli_noun)) if args.cli_action == "adventure":