def on_put_cf_file(self, req, resp, name): cf_bytes = req.bounded_stream.read() if not cf_bytes: # TODO: Add special error! self.log_req(req, 'Empty!') raise errors_.UnknownError('No CF-file in body') self.log_req(req, f'File size {len(cf_bytes)} bytes') agent = get_agent(req) infobase = com1c.InfoBase(agent, name) infobase.close_designer() cf_file = NamedTemporaryFile(suffix=f'{name}_update.cf', delete=False) cf_file.write(cf_bytes) cf_file.close() designer = designer1c.Designer(infobase) code, message = designer.load_cf(cf_file.name) remove(cf_file.name) resp.body = jdumps({'code': code, 'message': message}) self.log_resp(resp, resp.body)
def on_get(self, req, resp): self.log_req(req) self.log.info('Test API') resp.status = falcon.HTTP_200 resp.body = jdumps({'status': 'Your boat is ready captain!'}) self.log_resp(resp, resp.body)
def on_post(self, req, resp, name): try: str_props = req.bounded_stream.read().decode() new_props = loads(str_props) except Exception as err: # TODO: Add special error! raise errors_.UnknownError(str(err)) self.log_req(req, jdumps(new_props)) agent = get_agent(req) infobase = com1c.InfoBase(agent, name) new_props = loads(req.bounded_stream.read().decode()) result_set = infobase.set_props(new_props) resp.body = jdumps(result_set) self.log_resp(resp, resp.body)
def on_get(self, req, resp, name): self.log_req(req) agent = get_agent(req) infobase = com1c.InfoBase(agent, name) resp.body = jdumps(infobase.get_props()) resp.status = falcon.HTTP_200 self.log_resp(resp, resp.body)
def on_get(self, req, resp): self.log_req(req) agent_service = get_agent_service(req) info = agent_service.service.as_dict() info.update({'parameters': agent_service.parameters.__dict__}) resp.body = jdumps(info) self.log_resp(resp, resp.body)
def on_put_cfdb_update(self, req, resp, name): self.log_req(req) agent = get_agent(req) infobase = com1c.InfoBase(agent, name) infobase.close_designer() designer = designer1c.Designer(infobase) code, message = designer.update_dbcf() resp.body = jdumps({'code': code, 'message': message})
def on_get_sessions(self, req, resp, name): self.log_req(req) agent = get_agent(req) sessions = [] for session in com1c.InfoBase(agent, name).sessions(): sessions.append({ 'UserName': session.userName, 'SessionID': session.SessionID, 'AppID': session.AppID, 'Host': session.Host, 'StartedAt': session.StartedAt.isoformat(), }) resp.body = jdumps(sessions) self.log_resp(resp, f'Found {len(sessions)} sessions')
def on_get_connections(self, req, resp, name): self.log_req(req) agent = get_agent(req) connections = [] for connect in com1c.InfoBase(agent, name).connections(short=False): connections.append({ 'UserName': connect.userName, 'ConnID': connect.ConnID, 'AppID': connect.AppID, 'HostName': connect.HostName, 'ConnectedAt': connect.ConnectedAt.isoformat(), }) resp.body = jdumps(connections) self.log_resp(resp, f'Found {len(connections)} connections')
def prepare(self, description=None): super(type(self), self).__init__() super().__init__() error_title = getattr(self.__class__, 'title', '') if error_title: self.title = error_title if description: self.description = description self.headers = {'onec-sentry-error': self.__class__.__name__} error_body = { 'title': error_title, 'description': description, } error_body = jdumps(error_body) self.log_resp(self, error_body, 'onec-sentry-error')
def on_post(self, req, resp, action): self.log_req(req) action_mapping = { 'start': svc1c.AgentService.start, 'stop': svc1c.AgentService.stop, 'restart': svc1c.AgentService.restart, 'clear_cache': svc1c.AgentService.clear_cache, 'kill_processes': svc1c.AgentService.kill_processes, } if action not in action_mapping: # TODO: Add special error! raise errors_.UnknownError('Unknown command: {action}') agent_service = get_agent_service(req) action_method = action_mapping.get(action) getattr(agent_service, action_method.__name__)() resp.body = jdumps({'result': 'in progress'}) self.log_resp(resp, resp.body)
def log_req(self, req, body='', headers=''): message = f'\nbody:\n{body}' if body else '' query = {name: value for name, value in req.params.items() if not name.endswith('pwd')} if query: query = jdumps(query) query = f'\nquery:\n{query}' else: query = '' headers = ResourceLogger.extract_headers(req, headers) extra_data = { 'url': req.path, 'template': req.uri_template, 'resource': self.__class__.__name__, 'method': stack()[1][3], 'query': query, 'headers': headers, } self._logger_req.info(message, extra=extra_data)