def on_delete(self, req, resp, id=None): req_data = req.media or {} resp_data, valid = Code_Request_Schema( many=is_list(req_data), partial=True, method=HTTP_Method.DELETE).validate(data=req.media, id=id) if valid: req_data_wrap = wrap(req_data) if len(req_data_wrap) > 0: for data in req_data_wrap: id = data.get('id', None) if id is not None: pc = self.polycube.delete(cube=id) if not pc.get('error', False): msg = f'Code with the id={id} correctly deleted' resp_data = Reset_Content_Response(msg) else: msg = f'Not possible to delete code with the id={id}' resp_data = Unprocessable_Entity_Response(msg) resp_data.update(**pc) else: msg = f'Not possible to update code with the id={id}' resp_data = Unprocessable_Entity_Response(msg) resp_data.apply(resp) else: msg = 'No content to delete code with the {{request}}' No_Content_Response(msg, request=req_data).apply(resp) else: resp_data.apply(resp)
def on_put(self, req, resp, id=None): req_data = req.media or {} resp_data, valid = Code_Request_Schema( many=is_list(req_data), partial=True, method=HTTP_Method.PUT).validate(data=req.media, id=id) if valid: req_data_wrap = wrap(req_data) if len(req_data_wrap) > 0: for data in req_data_wrap: id, code, interface, metrics = item_getter( 'id', 'code', 'interface', 'metrics')(data) if all([id, code, interface]): if is_list(code): code = '\n'.join(code) pc = self.polycube.update(cube=id, code=code, interface=interface, metrics=metrics) if not pc.get('error', False): msg = f'Code with the id={id} correctly updated' resp_data = Ok_Response(msg) else: msg = f'Not possible to update code with the id={id}' resp_data = Unprocessable_Entity_Response(msg) resp_data.update(**pc) else: msg = f'Not possible to update code with the id={id}' resp_data = Unprocessable_Entity_Response(msg) resp_data.apply(resp) else: msg = 'No content to update code with the {{request}}' No_Content_Response(msg, request=req_data).apply(resp) else: resp_data.apply(resp)
def on_base_put(self, req, resp, id=None): so = self.doc.Status_Operation req_data = req.media or {} resp_data, valid = self.schema(many=is_list(req_data), unknown='INCLUDE', partial=True, method=HTTP_Method.PUT).validate( data=req_data, id=id) if valid: req_data_wrap = wrap(req_data) if len(req_data_wrap) > 0: for req_data in req_data_wrap: req_data_lcp = deepcopy(req_data) req_data_id = req_data.pop('id', id) try: if len(req_data) == 0: msg = f'Update for {self.name} with id={req_data_id} not necessary' Not_Modified_Response(msg).add(resp) else: self.rm_ignore_fields(req_data) obj = self.doc.get(id=req_data_id) resp_data_lcp = [] hndl = self.get_lcp_handler(HTTP_Method.PUT) modified = hndl(instance=obj, req=req_data_lcp, resp=resp_data_lcp) resp_data = Ok_Response( f'{self.name.capitalize()} with the id={req_data_id} correctly updated' ) if len(resp_data_lcp) > 0: for rdl in resp_data_lcp: if rdl['error']: msg = f'Not possible to update a {self.name} with the id={req_data_id}' resp_data = Unprocessable_Entity_Response( msg) break resp_data.update(lcp_response=resp_data_lcp) force = req_data.get('force', False) if (not resp_data.error or force) and len(req_data) > 0: res = obj.update(**req_data) if res == so.UPDATED: modified = True if force: msg = f'Some errors occur but the {self.name} with the id={req_data_id} forcedly updated' resp_data = Unprocessable_Entity_Response( msg) if not resp_data.error and not modified: msg = f'{self.name.capitalize()} with the id={req_data_id} no need to update' resp_data = Not_Modified_Response(msg) resp_data.add(resp) except Exception as e: msg = f'Not possible to update a {self.name} with the id={req_data_id}' Unprocessable_Entity_Response(msg, exception=e).add(resp) else: msg = f'No content to update {self.name} based on the {{request}}' No_Content_Response(msg, request=req_data).apply(resp) else: resp_data.apply(resp)
def on_base_post(self, req, resp, id=None): req_data = req.media or {} resp_data, valid = self.schema(many=is_list(req_data), unknown='INCLUDE', method=HTTP_Method.POST).validate( data=req_data, id=id) if valid: req_data_wrap = wrap(req_data) if len(req_data_wrap) > 0: for req_data in req_data_wrap: req_data_lcp = deepcopy(req_data) req_data_id = req_data.pop('id', id) try: self.rm_ignore_fields(req_data) obj = self.doc(meta={'id': req_data_id}, **req_data) resp_data_lcp = [] resp_data = Created_Response( f'{self.name.capitalize()} with the id={req_data_id} correctly created' ) hndl = self.get_lcp_handler(HTTP_Method.POST) hndl(instance=obj, req=req_data_lcp, resp=resp_data_lcp) if len(resp_data_lcp) > 0: for rdl in resp_data_lcp: if rdl['error']: msg = f'Not possible to create a {self.name} with the id={req_data_id}' resp_data = Unprocessable_Entity_Response( msg) break resp_data.update(lcp_response=resp_data_lcp) force = req_data.get('force', False) if not resp_data.error or force: obj.save() if force: msg = f'Some errors occur but the {self.name} with the id={req_data_id} forcedly created' resp_data = Unprocessable_Entity_Response(msg) resp_data.add(resp) except Exception as e: msg = f'Not possible to create a {self.name} with the id={req_data_id}' Unprocessable_Entity_Response(msg, exception=e).add(resp) else: msg = f'No content to create {self.names} based the {{request}}' No_Content_Response(msg, request=req_data).apply(resp) else: resp_data.apply(resp)
def on_post(self, req, resp): req_data = req.media or {} resp_data, valid = Config_Request_Schema( many=is_list(req_data), method=HTTP_Method.POST).validate(data=req_data) if valid: req_data_wrap = wrap(req_data) if len(req_data_wrap) > 0: for config in req_data_wrap: for cfg, cfg_list in config.items(): for data in wrap(cfg_list): output = {} if cfg == 'actions': output = self.__actions(data) schema = Config_Action_Response_Schema elif cfg == 'parameters': output = self.__parameters(data) schema = Config_Parameter_Response_Schema elif cfg == 'resources': output = self.__resources(data) schema = Config_Resource_Response_Schema if isinstance(output, Base_Response): output.add(resp) else: output_data = data.copy() id = output_data.pop('id', None) output.update(id=id, data=output_data, timestamp=datetime_to_str()) resp_data, valid = schema( many=False, method=HTTP_Method.POST, unknown='INCLUDE').validate(data=output) if valid: Content_Response(output).add(resp) else: resp_data.add(resp) else: msg = 'No content to apply configurations with the {{request}}' No_Content_Response(msg, request=req_data).apply(resp) else: resp_data.apply(resp)
class No_Content_Response_Schema(Base_Response_Schema): status = Constant(No_Content_Response.status()) error = Constant(No_Content_Response.error) code = Constant(No_Content_Response.code)
from marshmallow import Schema, validate from marshmallow.fields import Bool, Constant, Integer, Nested, Raw, Str from lib.response import ( Bad_Request_Response, Conflict_Response, Content_Response, Created_Response, Internal_Server_Error_Response, No_Content_Response, Not_Acceptable_Response, Not_Found_Response, Not_Modified_Response, Ok_Response, Reset_Content_Response, Unauthorized_Response, Unprocessable_Entity_Response, Unsupported_Media_Type_Response) RESPONSE_STATUS = [ Bad_Request_Response.status(), Conflict_Response.status(), Content_Response.status(), Created_Response.status(), No_Content_Response.status(), Not_Acceptable_Response.status(), Not_Found_Response.status(), Not_Modified_Response.status(), Ok_Response.status(), Reset_Content_Response.status(), Unauthorized_Response.status(), Unprocessable_Entity_Response.status(), Unsupported_Media_Type_Response.status() ] RESPONSE_CODES = [ Bad_Request_Response.code, Conflict_Response.code, Content_Response.code, Created_Response.code, No_Content_Response.code, Not_Acceptable_Response.code, Not_Found_Response.code, Not_Modified_Response.code, Ok_Response.code, Reset_Content_Response.code,