def get_by_regtoken(self, token, args): # FIXME: this code is currently non-operational in VF 0.0.3 and later # this code can be pruned if regtoken functional is needed and # reinstated. """ Get a machines by registration token. @param token: A security token. @type token: string @param args: A dictionary of machine attributes. - registration_token - offset (optional) - limit (optional) @type args: dict @return A list of machines. @rtype: [dict,] @raise SQLException: On database error """ required = ("registration_token",) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] regtoken = args["registration_token"] offset, limit = self.offset_and_limit(args) query = session.query(db.Machine).offset(offset).limit(limit) for machine in query.select_by(registration_token=regtoken): result.append(self.expand(machine)) return codes.success(result) finally: session.close()
def get_by_hostname(self, token, args): """ Get a machines by hostname. @param token: A security token. @type token: string @param args: A dictionary of machine attributes. - hostname - offset (optional) - limit (optional) @type args: dict @return A list of all machines. @rtype: [dict,] @raise SQLException: On database error """ required = ("hostname",) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] hostname = args["hostname"] offset, limit = self.offset_and_limit(args) query = session.query(db.Machine).offset(offset).limit(limit) for machine in query.select_by(hostname=hostname): result.append(self.expand(machine)) return codes.success(result) finally: session.close()
def get_profile_choices(self, token, args={}): """ Returns the list of profiles that can be installed on this given host """ this_object = self.get(token, {"id": args["id"]}) this_object = this_object.data try: # the WUI should filter this out but lets be safe anyway if this_object["profile"]["is_container"] != codes.MACHINE_IS_CONTAINER: # cannot install any virt types on this machine return codes.success([]) need_virt = this_object["profile"]["virt_type"] need_arch = this_object["profile"]["distribution"]["architecture"] except KeyError, e: # missing distribution or profile, for now return empty list rather than error print "missing distribution or profile, for now return empty list rather than error", e print "machine: ", this_object return codes.success([])
def get_by_mac_address(self, token, args): """ """ required = ("mac_address",) FieldValidator(args).verify_required(required) session = db.open_session() try: result = [] mac_address = args["mac_address"] offset, limit = self.offset_and_limit(args) query = session.query(db.Machine).limit(limit).offset(offset) for machine in query.select_by(mac_address=mac_address): result.append(self.expand(machine)) return codes.success(result) finally: session.close()
def add(self, token, args): """ Create a machine. @param token: A security token. @type token: string @param args: A dictionary of machine attributes. @type args: dict @raise SQLException: On database error """ optional = ( "hostname", "ip_address", "registration_token", "architecture", "processor_speed", "processor_count", "memory", "kernel_options", "kickstart_metadata", "list_group", "mac_address", "is_container", "puppet_node_diff", "netboot_enabled", "is_locked", "status", "last_heartbeat", "tag_ids", ) required = ("profile_id",) self.validate(args, required) session = db.open_session() try: machine = db.Machine() machine.update(args) machine.registration_token = regtoken.RegToken().generate(token) machine.netboot_enabled = 1 # initially, allow PXE, until it registers session.save(machine) if args.has_key("tag_ids"): setattr(machine, "tags", tag.Tag().tags_from_ids(session, args["tag_ids"])) session.flush() if machine.profile_id >= 0: self.cobbler_sync(machine.get_hash()) return codes.success(machine.id) finally: session.close()
def edit(self, token, args): """ Edit a machine. @param token: A security token. @type token: string @param args: A dictionary of machine attributes. @type args: dict @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ("id",) optional = ( "hostname", "ip_address", "registration_token", "architecture", "processor_speed", "processor_count", "memory", "kernel_options", "kickstart_metadata", "profile_id", "list_group", "mac_address", "is_container", "puppet_node_diff", "netboot_enabled", "is_locked", "state", "last_heartbeat", "tag_ids", ) self.validate(args, required) session = db.open_session() try: machine = db.Machine.get(session, args["id"]) machine.update(args) session.save(machine) if args.has_key("tag_ids"): setattr(machine, "tags", tag.Tag().tags_from_ids(session, args["tag_ids"])) session.flush() if machine.profile_id >= 0: self.cobbler_sync(machine.get_hash()) return codes.success() finally: session.close()
def delete(self, token, args): """ Deletes a tag. @param token: A security token. @type token: string @param args: A dictionary of user attributes. - id @type args: dict @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ('id',) FieldValidator(args).verify_required(required) session = db.open_session() try: db.Tag.delete(session, args['id']) return codes.success() finally: session.close()
def get_by_tag(self, token, args): """ Return a list of all machines tagged with the given tag """ required = ("tag_id",) FieldValidator(args).verify_required(required) tag_id = args["tag_id"] session = db.open_session() try: name = args["name"] machine_tags = db.Database.table["machine_tags"] machines = session.query(db.Machine).select( (machine_tags.c.tag_id == tag_id & machine_tags.c.machine_id == db.Machine.c.id) ) if machines: machines = self.expand(machines) return codes.success(machines) finally: session.close()
def remove_tag(self, token, args): """ Given machine id and tag string, remove the tag from the machine @param token: A security token. @type token: string @param args: A dictionary of machine attributes. - id - tag_id @type args: dict @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ("id", "tag_id") FieldValidator(args).verify_required(required) machine = self.get(token, {"id": args["id"]}).data tag_id = args["tag_id"] tag_ids = machine["tag_ids"] if int(tag_id) in tag_ids: tag_ids.remove(int(tag_id)) self.edit(token, {"id": args["id"], "tag_ids": tag_ids}) return codes.success()
def list(self, token, args): """ Get a list of all tags. @param token: A security token. @type token: string @param args: A dictionary of tag attributes. @type args: dict - offset (optional) - limit (optional) @return A list of all tags. @rtype: [dict,] @raise SQLException: On database error """ session = db.open_session() try: result = [] offset, limit = self.offset_and_limit(args) for tag in db.Tag.list(session, offset, limit): result.append(self.expand(tag)) return codes.success(result) finally: session.close()
def get(self, token, args): """ Get a machine by id. @param token: A security token. @type token: string @param args: A dictionary of machine attributes. - id @type args: dict @return A machine. @rtype: dict @raise SQLException: On database error @raise NoSuchObjectException: On object not found. """ required = ("id",) FieldValidator(args).verify_required(required) session = db.open_session() try: self.logger.info(args) machine = db.Machine.get(session, args["id"]) return codes.success(self.expand(machine)) finally: session.close()