def put(self): """ The put method is used to modify the Host's worker_num """ data = self.request.body h = json.loads(data) logging.info("In PUT method! Receive data: %s", str(data)) ## start check parameters hostname = h.get('hostname', None) host_id = h.get('host_id', None) hostname = tools.strip_string(hostname) host_id = tools.to_int(host_id) worker_num = tools.to_int(h.get('worker_num', 0)) if worker_num == None or worker_num == 0: logging.error("There is no parameter 'worker_num'! ") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='worker_num') self.write(resp) return ret = None err_str = "" host = None if hostname != None: ret = self.host_dao.update_worker_num_by_hostname(hostname, worker_num) err_str = "Error oucurred when Update host by hostname '%s', worker_num: %s" % (hostname, str(worker_num)) host = self.host_dao.get_by_hostname(hostname) elif host_id != None: ret = self.host_dao.update_worker_num_by_id(host_id, worker_num) err_str = "Error oucurred when Update host by host_id '%s', worker_num: %s" % (str(host_id), str(worker_num)) host = self.host_dao.get_by_id(host_id) else: logging.error("There is no parameter 'hostname' or 'host_id'! ") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='hostname or host_id') self.write(resp) return if ret == None: logging.error(err_str) resp = self.Resp.make_response(code=RespCode.DB_ERROR, err_str=err_str) self.write(resp) return if host == None: logging.info("No record!") resp = self.Resp.make_response(code=RespCode.NO_RECORD) self.write(resp) return logging.info("Update host object successed!") resp = self.Resp.make_response(code=RespCode.SUCCESS, content=host) self.write(resp)
def get(self): """ The GET method is used to get one Host by hostname or host list """ hostname = self.get_argument("hostname", None) host_id = self.get_argument("host_id", None) hostname = tools.strip_string(hostname) host_id = tools.to_int(host_id) resp = None if hostname is not None: logging.info("In GET method! Get host by hostname: '%s'", str(hostname)) host = self.host_dao.get_by_hostname(hostname) resp = host elif host_id is not None: logging.info("In GET method! Get host by host_id: '%s'", str(host_id)) host = self.host_dao.get_by_id(host_id) resp = host else: logging.info("In GET method! Get all hosts.") hosts = self.host_dao.get_all() resp = hosts logging.info("Query result: %s", str(resp)) if resp is None or len(resp) == 0: logging.error("There is no record! ") resp = self.Resp.make_response(code=RespCode.NO_RECORD) self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp)
def delete(self): """ The DELETE method is used to delete Host object """ hostname = self.get_argument("hostname", None) host_id = self.get_argument("host_id", None) hostname = tools.strip_string(hostname) host_id = tools.to_int(host_id) if hostname is not None: logging.info("In DELETE method! Delete host by hostname: '%s'.", str(hostname)) ret = self.host_dao.del_by_hostname(hostname) err_str = "Error oucurred when Delete host by hostname: '%s'" % hostname elif host_id is not None: logging.info("In DELETE method! Delete host by host_id: '%s'.", str(host_id)) ret = self.host_dao.del_by_id(host_id) err_str = "Error oucurred when Delete host by host_id: '%s'" % str(host_id) else: logging.error("There is no parameter 'hostname' or 'host_id'! ") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="hostname or host_id") self.write(resp) return if ret is None: logging.error(err_str) resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER, err_str=err_str) self.write(resp) return logging.info("Delete host object successed!") resp = self.Resp.make_response(code=RespCode.SUCCESS) self.write(resp)
def insert(self): data = self.request.body h = json.loads(data) logging.error("in insert method. receive data:%s", str(data)) reader_name = h.get('reader_name', None) reader_name = tools.strip_string(reader_name) if reader_name == None: logging.error("there is no parameter 'reader_name'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para='reader_name') self.write(resp) return sex = tools.to_int(h.get('sex', None)) age = tools.to_int(h.get('age', None)) email = tools.strip_string(h.get('email', '')) logging.debug("check parameters complete, ready to save in db") reader = {} reader['reader_name'] = reader_name reader['sex'] = sex reader['age'] = age reader['email'] = email ret = self.readerdao.insert_by_dict(reader) #########insert fail return None?############### if ret == None: err_str = "error oucurred when insert into table 'reader'" logging.error(err_str) resp = self.Resp.make_response(code=RespCode.DB_ERROR, err_str=err_str) self.write(resp) return logging.info('save reader object successed! the reader: %s', str(reader)) reader['reader_id'] = ret resp = self.Resp.make_response(code=RespCode.SUCCESS, content=reader) self.write(resp)
def insert(self): data = self.request.body h = json.loads(data) logging.error("in insert method. receive data:%s", str(data)) reader_name = h.get('reader_name',None) reader_name = tools.strip_string(reader_name) if reader_name == None: logging.error("there is no parameter 'reader_name'!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER,para='reader_name') self.write(resp) return sex=tools.to_int(h.get('sex',None)) age=tools.to_int(h.get('age',None)) email=tools.strip_string(h.get('email','')) logging.debug("check parameters complete, ready to save in db") reader={} reader['reader_name']=reader_name reader['sex']=sex reader['age']=age reader['email']=email ret = self.readerdao.insert_by_dict(reader) #########insert fail return None?############### if ret == None: err_str="error oucurred when insert into table 'reader'" logging.error(err_str) resp = self.Resp.make_response(code = RespCode.DB_ERROR,err_str=err_str) self.write(resp) return logging.info('save reader object successed! the reader: %s', str(reader)) reader['reader_id']=ret resp = self.Resp.make_response(code=RespCode.SUCCESS,content=reader) self.write(resp)
def get(self): book_id = self.get_argument('book_id', None) book_id = tools.to_int(book_id) book_title = self.get_argument('book_title', None) book_title = tools.strip_string(book_title) author = self.get_argument('author', None) author = tools.strip_string(author) publisher = self.get_argument('publisher', None) publisher = tools.strip_string(publisher) resp = None if book_id != None: logging.info("in GET method! Get book by bookid: '%s'", str(book_id)) book = self.bookdao.get_by_bid(book_id) resp = book elif book_title != None: logging.info("in GET method! Get book by title: '%s'", str(book_title)) book = self.bookdao.get_by_title(book_title) resp = book elif author != None: logging.info("in GET method! Get book by author: '%s'", str(author)) book = self.bookdao.get_by_author(author) resp = book elif publisher != None: logging.info("in GET method! Get book by publisher: '%s'", str(publisher)) book = self.bookdao.get_by_publisher(publisher) resp = book else: logging.info("in GET method! Get All book") books = self.bookdao.get_allbook resp = books logging.info("Query result: %s", str(resp)) if resp == None: logging.error("There is no record!") resp = self.Resp.make_response(code=RespCode.NO_RECORD) self.write(resp) return resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp)
def get(self): rid = self.get_argument('reader_id', None) rid = tools.to_int(rid) if rid == None: logging.error("there is no reader_id!") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER) self.write(resp) return logging.info("reader detail! readerid:'%s'", str(rid)) ret = self.readerdao.get_by_rid(rid) resp = ret logging.info('query result: %s', str(resp)) if resp == None or len(resp) == 0: logging.error('there is no record!') resp = self.Resp.make_response(code=RespCode.NO_RECORD) self.write(resp) resp = self.Resp.make_response(code=RespCode.SUCCESS, content=resp) self.write(resp)
def post(self): """ The POST method is used to inert one host into 'Host' Table """ data = self.request.body h = json.loads(data) logging.info("In POST method. Receive data: %s", str(data)) # start check parameters hostname = h.get("hostname", None) hostname = tools.strip_string(hostname) if hostname is None: logging.error("There is no parameter 'hostname'! ") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="hostname") self.write(resp) return ip = h.get("ip", None) ip = tools.strip_string(ip) if ip is None: logging.error("There is no parameter 'ip'! ") resp = self.Resp.make_response(code=RespCode.NO_PARAMETER, para="ip") self.write(resp) return # check if exist if_exist = self.host_dao.if_exist(hostname, ip) if if_exist is True: logging.error("The host has existed!") resp = self.Resp.make_response(code=RespCode.HAS_EXISTED, para="Host") self.write(resp) return host_type = tools.to_int(h.get("host_type", 0)) if host_type is None or self.HostType.check(host_type) != True: logging.error("The value of parameter 'host_type' is invalid!") resp = self.Resp.make_response(code=RespCode.INVALID_PARAMETER, para="host_type") self.write(resp) return cpu_count = tools.to_int(h.get("cpu_count", 8)) memory = tools.to_int(h.get("memory", 8)) os = h.get("os", "") comment = h.get("comment", "") worker_num = tools.to_int(h.get("worker_num", 0)) if worker_num is None or worker_num == 0: worker_num = Config.default_worker_num logging.debug("Check parameters complete, ready to save in db") # save in db host = dict() host["hostname"] = hostname host["host_type"] = host_type host["ip"] = ip host["cpu_count"] = cpu_count host["memory"] = memory host["os"] = os host["comment"] = comment host["worker_num"] = worker_num create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) host["create_time"] = create_time ret = self.host_dao.insert_by_dict(host) if ret is None: err_str = "Error oucurred when insert into table 'Host'" logging.error(err_str) resp = self.Resp.make_response(code=RespCode.DB_ERROR, err_str=err_str) self.write(resp) return logging.info("Save host object successed! The host: %s", str(host)) host["host_id"] = ret resp = self.Resp.make_response(code=RespCode.SUCCESS, content=host) self.write(resp)