示例#1
0
 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
示例#2
0
 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)
示例#3
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