def resolv_name(self, name=None, location=None, options=['all']): """Resolv a name, location to lease(s) :param extras: The extra inforamtions to add :type extras: [] :returns: A dict :rtype: dict() return [add_ctrl, add_node, options] """ query = self.dbsession.query(jntmodel.Lease) conditions = [] if location is None and name is not None: #Look for name in all locations query = query.filter(jntmodel.Lease.name == name) elif location is not None and name is None: #Look for location query = query.filter(jntmodel.Lease.location.like("%"+location)) elif location is None and name is None: #Return all locations pass else: #Look for name in one location query = query.filter(jntmodel.Lease.name == name, jntmodel.Lease.location == location) data = query.all() res = {} for line in data: #print line.cmd_classes res[HADD%(line.add_ctrl, line.add_node)] = saobject_to_dict(line) return res
def lock_lease(self, add_ctrl, add_node): """Lock a lease already in database. We should manage the case of a db crash. Also update last_seen to now and state to boot :param add_ctrl: the controller part of the address :type add_ctrl: Integer :param add_node: the node part of the address. 0 for controller. :type add_node: Integer :returns: A dict with all informations :rtype: dict() """ query = self.dbsession.query(jntmodel.Lease).filter(jntmodel.Lease.add_ctrl==add_ctrl, jntmodel.Lease.add_node==add_node) try: node = query.one() node.state = "BOOT" node.last_seen = datetime.datetime.now() self._cachemgr.update(add_ctrl, add_node, state=node.state, last_seen=node.last_seen) ddict = saobject_to_dict(node) self.dbsession.commit() return ddict except sa.orm.exc.NoResultFound: return None
def resolv_hadd(self, add_ctrl, add_node): """Resolv an address to lease :param add_ctrl: the controller part of the address :type add_ctrl: Integer :param add_node: the node part of the address. 0 for controller :type add_node: Integer :returns: A dict with all informations :rtype: dict() """ query = self.dbsession.query(jntmodel.Lease).filter(jntmodel.Lease.add_ctrl==add_ctrl, jntmodel.Lease.add_node==add_node) try: ddict = saobject_to_dict(query.one()) try: ddict['state'] = self._cachemgr.entries[add_ctrl][add_node]['state'] except KeyError: pass try: ddict['last_seen'] = self._cachemgr.entries[add_ctrl][add_node]['last_seen'] except KeyError: pass return ddict except sa.orm.exc.NoResultFound: return None
def resolv_cmd_classes(self, cmd_classes=[]): """Resolv a cmd_classes to lease(s) :param extras: The extra inforamtions to add :type extras: [] :returns: A dict :rtype: dict() return [add_ctrl, add_node, options] """ query = self.dbsession.query(jntmodel.Lease) conditions = [] if type(cmd_classes) == type(""): cmd_classes = [cmd_classes] for cmdc in cmd_classes: #print cmdc conditions.append(jntmodel.Lease.cmd_classes.like("%"+cmdc+"%")) query = query.filter(or_(*conditions)) #query = query.filter(jntmodel.Lease.cmd_classes.like(""+cmd_classes[0]+"%")) data = query.all() res = {} for line in data: #print line.cmd_classes res[HADD%(line.add_ctrl, line.add_node)] = saobject_to_dict(line) return res