コード例 #1
0
 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)
コード例 #2
0
 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)
コード例 #3
0
 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)
コード例 #4
0
 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)
コード例 #5
0
 def on_base_delete(self, req, resp, id=None):
     req_data = req.media or {}
     qrs = Query_Request_Schema(method=HTTP_Method.DELETE)
     resp_data, valid = qrs.validate(data=req_data, id=id)
     if resp:
         try:
             qr = Query_Reader(index=self.doc.Index.name)
             s = qr.parse(query=req_data, id=id)
             hits = s.execute()
             if len(hits) > 0:
                 for hit in hits:
                     try:
                         obj = self.doc.get(id=hit.meta.id)
                         msg = f'{self.name.capitalize()} with the id={hit.meta.id} correctly deleted'
                         resp_data_lcp = []
                         resp_data = Reset_Content_Response(msg)
                         hndl = self.get_lcp_handler(HTTP_Method.DELETE)
                         hndl(instance=obj, req=hit, resp=resp_data_lcp)
                         if len(resp_data_lcp) > 0:
                             for rdl in resp_data_lcp:
                                 if rdl['error']:
                                     msg = f'Not possible to delete the {self.name} with the id={hit.meta.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.delete()
                             if force:
                                 msg = f'Some errors occur but the {self.name} with the id={hit.meta.id} forcedly deleted'
                                 resp_data = Unprocessable_Entity_Response(
                                     msg)
                         resp_data.add(resp)
                     except Exception as e:
                         msg = f'Not possible to delete the {self.name} with the id={hit.meta.id}'
                         Unprocessable_Entity_Response(
                             msg, exception=e).add(resp)
             else:
                 msg = f'{self.names.capitalize()} based on the request {{query}} not found'
                 Not_Found_Response(msg, query=req_data).apply(resp)
         except Exception as e:
             msg = f'Not possible to delete {self.names} with the request {{query}}'
             Unprocessable_Entity_Response(msg, exception=e,
                                           query=req_data).apply(resp)
     else:
         resp_data.apply(resp)
コード例 #6
0
ファイル: lcp.py プロジェクト: guard-project/cb-manager
 def __apply(self, instance, exec_env, caller, data):
     h, p = exec_env.hostname, exec_env.lcp.port
     schema = 'https' if exec_env.lcp.https else 'http'
     resp_caller = caller(f'{schema}://{h}:{p}/code',
                          headers={'Authorization': create_token()},
                          json=data(self.catalog))
     if resp_caller.content:
         try:
             self.resp.extend(wrap(resp_caller.json()))
         except Exception as e:
             msg = f'Response from LCP({exec_env.meta.id}@{exec_env.hostname}:{exec_env.lcp.port}) not valid'
             self.log.exception(msg, e)
             uer = Unprocessable_Entity_Response(msg, exception=e)
             uer.add(self.resp)
     else:
         msg = f'Request to LCP({exec_env.meta.id}@{exec_env.hostname}:{exec_env.lcp.port}) not executed'
         Unprocessable_Entity_Response(msg).add(self.resp)
コード例 #7
0
 def __apply(self, instance):
     if self.num > 0:
         try:
             save_parameters = self.__save(
                 instance=instance,
                 type='parameter',
                 catalogs=self.catalogs['parameters'],
                 handler=self.__save_parameter)
             if save_parameters:
                 instance.save()
             return True
         except Exception as e:
             msg = 'Request not valid'
             self.log.exception(msg, e)
             uer = Unprocessable_Entity_Response(msg, exception=e)
             uer.add(self.resp)
             return False
     return False
コード例 #8
0
ファイル: lcp.py プロジェクト: guard-project/cb-manager
 def __apply(self, instance, exec_env):
     if self.num > 0:
         schema = 'https' if exec_env.lcp.https else 'http'
         resp_lcp = post_req(
             f'{schema}://{exec_env.hostname}:{exec_env.lcp.port}/config',
             headers={'Authorization': create_token()},
             json=self.req_lcp)
         if resp_lcp.content:
             try:
                 resp_lcp_data = resp_lcp.json()
                 if resp_lcp.status_code >= 300 or (is_dict(resp_lcp)
                                                    and resp_lcp_data.get(
                                                        'error', False)):
                     Unprocessable_Entity_Response(resp_lcp_data) \
                         .add(self.resp)
                     return False
                 else:
                     save_actions = self.__save(
                         instance=instance,
                         data=resp_lcp_data,
                         type='action',
                         catalogs=self.catalogs['actions'],
                         handler=self.__save_action)
                     save_parameters = self.__save(
                         instance=instance,
                         data=resp_lcp_data,
                         type='parameter',
                         catalogs=self.catalogs['parameters'],
                         handler=self.__save_parameter)
                     save_resources = self.__save(
                         instance=instance,
                         data=resp_lcp_data,
                         type='resource',
                         catalogs=self.catalogs['resources'],
                         handler=self.__save_resource)
                     if save_actions or save_parameters or save_resources:
                         instance.save()
                     self.resp.extend(wrap(resp_lcp_data))
                     return True
             except Exception as e:
                 msg = f'Response from LCP({exec_env.meta.id}@{exec_env.hostname}:{exec_env.lcp.port}) not valid'
                 self.log.exception(msg, e)
                 uer = Unprocessable_Entity_Response(msg, exception=e)
                 uer.add(self.resp)
                 return False
         else:
             msg = f'Request to LCP({exec_env.meta.id}@{exec_env.hostname}:{exec_env.lcp.port}) not executed'
             Unprocessable_Entity_Response(msg).add(self.resp)
             return False
     return False
コード例 #9
0
 def on_base_get(self, req, resp, id=None):
     req_data = req.media or {}
     qrs = Query_Request_Schema(method=HTTP_Method.GET, unknown='INCLUDE')
     resp_data, valid = qrs.validate(data=req_data, id=id)
     if valid:
         try:
             qr = Query_Reader(index=self.doc.Index.name)
             s = qr.parse(query=req_data, id=id)
             resp_data = [
                 dict(hit.to_dict(), id=hit.meta.id) for hit in s.execute()
             ]
             if len(resp_data) > 0:
                 Content_Response(resp_data).apply(resp)
             else:
                 msg = f'{self.name.capitalize()} based on the request {{query}} not found'
                 Not_Found_Response(msg, query=req_data).apply(resp)
         except Exception as e:
             msg = f'Not possible to get {self.names} with the request {{query}}'
             Unprocessable_Entity_Response(msg, exception=e,
                                           query=req_data).apply(resp)
     else:
         resp_data.apply(resp)
コード例 #10
0
class Unprocessable_Entity_Response_Schema(Base_Response_Schema):
    status = Constant(Unprocessable_Entity_Response.status())
    error = Constant(Unprocessable_Entity_Response.error)
    code = Constant(Unprocessable_Entity_Response.code)
コード例 #11
0
    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,
    Unauthorized_Response.code, Unprocessable_Entity_Response.code,
    Unsupported_Media_Type_Response.code
]


class Exception_Response_Schema(Schema):
    reason = Raw(required=True,