def do_add_server(self, args): '''Add a server with given name in the inventory.''' parser = argparse.ArgumentParser() parser.add_argument('-r', '--range', action="store", default=False, type=self.parseNumList, dest="range") parser.add_argument('-p', '--prefix', action="store", dest="prefix") parser.add_argument('-s', '--set', action="store", dest="set") args = parser.parse_args(args.split()) if (args.range): print args.range for i in args.range: if(args.prefix is None): print "Error No prefix specified" else: myInventory.add(inventory, serverobj, name=args.prefix, ip_address=self.get_prefix(args.prefix, i))
def do_add(self, args): '''Instead of having add_server and add_services, combine them into a single add method''' parser = argparse.ArgumentParser() # positional argument immediately after add parser.add_argument("firstargument") parser.add_argument('-n', '--name', action="store", dest="name") parser.add_argument('-r', '--range', action="store", default=False, type=self.parseNumList, dest="range") parser.add_argument( '-s', '--servicename', action="store", dest="servicename") parser.add_argument('-p', '--prefix', action="store", default="i" + self.placeholder + ".iu.edu", dest="prefix") args = parser.parse_args(args.split()) print args.firstargument if (args.firstargument.lower() == "server"): # print 'in server' if (args.range): for i in args.range: if((args.name is None) and (self.setcommands['server'] != '')): myInventory.add( inventory, serverobj, name=self.setcommands[ 'server'], prefix=self.get_prefix(args.prefix, i)) else: myInventory.add(inventory, serverobj, name=args.name, prefix=self.get_prefix(args.prefix, i)) elif (self.isassigned('server')): myInventory.add( inventory, serverobj, name=self.setcommands['server'], prefix=self.get_prefix(args.prefix, 1)) elif ((args.name) and (args.prefix)): myInventory.add(inventory, serverobj, name=args.name, prefix=self.get_prefix(args.prefix, 1)) # setting the prefix to 1 by default return elif (args.firstargument.lower() == "service"): # print 'services called' if (args.range): # print args.range for i in args.range: if (args.servicename is None) and (args.name is None): if (not self.isassigned('server')) and (not self.isassigned('service')): print "Error No server and/or servicename specified" return else: # assuming that server and servicename are assigned myInventory.add( inventory, serviceobj, name=self.setcommands[ 'server'], service_name=self.setcommands[ 'service'], prefix=self.get_prefix(args.prefix, i)) elif(args.servicename is not None) and (args.name is not None): myInventory.add( inventory, serviceobj, name=args.name, service_name=args.servicename, prefix=self.get_prefix(args.prefix, i)) elif((args.name) and self.isassigned('service')): myInventory.add( inventory, serviceobj, name=args.name, service_name=self.setcommands[ 'service'], prefix=self.get_prefix(args.prefix, i)) if((args.servicename) and self.isassigned('server')): myInventory.add( inventory, serviceobj, name=self.setcommands[ 'server'], service_name=args.servicename, prefix=self.get_prefix(args.prefix, i)) else: myInventory.add( inventory, serviceobj, name=self.setcommands[ 'server'], service_name=self.setcommands[ 'service'], prefix=self.get_prefix(args.prefix, i)) elif ((args.name) and (args.servicename)): myInventory.add(inventory, serviceobj, name=args.name, service_name=args.servicename, prefix=self.get_prefix(args.prefix, 1)) else: print "invalid argument only options are : server, service" return