def send_client_id(self): with create_session() as session: client = session.query(Client).filter( Client.id == self.client_id).first() self.send_action( SetIdClientAction(client_id=client.id, client_token=client.token))
async def get_example(request): """ --- tags: - Example module summary: Example API description: Example Skywall module API endpoint produces: - application/json responses: 200: description: Example response schema: type: object title: GetExample required: - message properties: message: type: string """ with create_session() as session: example = Example(value='Example value') before_example_create.emit(session=session, example=example) session.add(example) session.flush() after_example_create.emit(session=session, example=example) return json_response({'message': 'Example response'})
def execute(self, connection): with create_session() as session: client = session.query(Client).filter( Client.id == connection.client_id).first() client.label = self.payload['label'] or '' before_client_update.emit(session=session, client=client) session.flush() after_client_update.emit(session=session, client=client)
def _save_connection_closed(self): with create_session() as session: connection = session.query(Connection).filter( Connection.id == self.connection_id).first() connection.closed = current_timestamp() before_connection_update.emit(session=session, connection=connection) session.flush() after_connection_update.emit(session=session, connection=connection)
def _get_request_connection_id(self, client_id): with create_session() as session: connection = Connection(client_id=client_id) before_connection_create.emit(session=session, connection=connection) session.add(connection) session.flush() after_connection_create.emit(session=session, connection=connection) return connection.id
def execute(self, connection): with create_session() as session: client = session.query(Client).filter( Client.id == connection.client_id).first() report = Report(client=client) before_report_create.emit(session=session, report=report) session.add(report) session.flush() after_report_create.emit(session=session, report=report) values = self.payload['report'] for name in reports_registry: value = values.get(name, None) before_report_value_create.emit(session=session, report_value=value) session.add(ReportValue(report=report, name=name, value=value)) session.flush() after_report_value_create.emit(session=session, report_value=value)
def _get_request_client_id(self, request): try: client_id = request.headers[CLIENT_ID_HEADER] client_token = request.headers[CLIENT_TOKEN_HEADER] except KeyError: raise HTTPBadRequest(reason='Missing Client ID or Token Header') with create_session() as session: if client_id == 'None': client = Client(token=randomstring(32)) before_client_create.emit(session=session, client=client) session.add(client) session.flush() after_client_create.emit(session=session, client=client) else: client = session.query(Client).filter( Client.id == client_id).first() if not client or client.token != client_token: raise HTTPForbidden(reason='Invalid Client ID or Token') return client.id
async def delete_group(request): """ --- tags: - Skywall Core summary: Delete group description: Deletes an existing group produces: - application/json parameters: - name: groupId in: path description: ID of group to delete required: true type: integer responses: 200: description: Group deleted schema: type: object title: DeleteGroupResponse required: - ok properties: ok: type: boolean 404: description: Group not found """ with create_session() as session: group = parse_obj_path_param(request, 'groupId', session, Group) before_group_delete.emit(session=session, group=group) session.delete(group) session.flush() after_group_delete.emit(session=session, group=group) return json_response({'ok': True})
async def get_clients(request): """ --- tags: - Skywall Core summary: List of clients description: Returns list of clients with their most recent connections and reports produces: - application/json responses: 200: description: List of clients with their most recent connections and reports schema: type: object title: GetClientsResponse required: - clients - connections - reports - values - fields properties: clients: type: array items: type: object title: Client required: - id - created - label - connected properties: id: type: integer created: type: number format: float label: type: string groupId: type: integer connected: type: boolean groups: type: array items: type: object title: Group required: - id - name - description properties: id: type: integer name: type: string description: type: string connections: type: array items: type: object title: Connection required: - id - created - clientId - lastActivity - closed properties: id: type: integer created: type: number format: float clientId: type: integer lastActivity: type: number format: float closed: type: number format: float reports: type: array items: type: object title: Report required: - id - created - clientId properties: id: type: integer created: type: number format: float clientId: type: integer values: type: array items: type: object title: Value required: - id - created - reportId - name - value properties: id: type: integer created: type: number format: float reportId: type: integer name: type: string value: type: any fields: type: array items: type: object title: Field required: - name - label properties: name: type: string label: type: string """ with create_session() as session: clients = session.query(Client).order_by(Client.id).all() groups = session.query(Group).order_by(Group.id).all() connections = list(filter(None, ( session.query(Connection) .filter(Connection.client_id == client.id) .order_by(desc(Connection.created)).first() for client in clients ))) reports = list(filter(None, ( session.query(Report) .filter(Report.client_id == client.id) .order_by(desc(Report.created)).first() for client in clients ))) values = (session.query(ReportValue) .filter(ReportValue.report_id.in_(report.id for report in reports)).all()) return json_response({ 'clients': _clients_response(clients), 'groups': _group_responses(groups), 'connections': _connections_response(connections), 'reports': _reports_response(reports), 'values': _values_response(values), 'fields': _fields_response(reports_registry.values()), })
async def add_group(request): """ --- tags: - Skywall Core summary: Add group description: Creates a new group produces: - application/json parameters: - name: body in: body description: Group properties to be saved required: true schema: type: object title: PostGroupBody required: - name properties: name: type: string description: type: string responses: 200: description: Group added schema: type: object title: PostGroupResponse required: - ok - groupId properties: ok: type: boolean groupId: type: integer """ body = await parse_json_body(request) try: with create_session() as session: name = assert_request_param_is_string('name', body) if not re.match(r'^\w+$', name): raise HTTPBadRequest( reason='Name may contain only numbers and letters') if name.lower() == 'default': raise HTTPBadRequest(reason='Name "default" is reserverd') if 'description' in body: description = assert_request_param_is_string( 'description', body) else: description = None group = Group( name=name, description=description, ) before_group_create.emit(session=session, group=group) session.add(group) session.flush() after_group_create.emit(session=session, group=group) return json_response({'ok': True, 'groupId': group.id}) except IntegrityError as e: if 'group_name_key' in str(e): raise HTTPBadRequest(reason='Name already used by another group') raise e
async def update_group(request): """ --- tags: - Skywall Core summary: Update group description: Updates an existing group produces: - application/json parameters: - name: groupId in: path description: ID of group to update required: true type: integer - name: body in: body description: Group properties to be updated required: true schema: type: object title: PutGroupBody required: [] properties: name: type: string description: type: string responses: 200: description: Group updated schema: type: object title: PutGroupResponse required: - ok properties: ok: type: boolean 404: description: Group not found """ body = await parse_json_body(request) try: with create_session() as session: group = parse_obj_path_param(request, 'groupId', session, Group) if 'name' in body: name = assert_request_param_is_string('name', body) if not re.match(r'^\w+$', name): raise HTTPBadRequest( reason='Name may contain only numbers and letters') if name.lower() == 'default': raise HTTPBadRequest(reason='Name "default" is reserverd') group.name = name if 'description' in body: description = assert_request_param_is_string( 'description', body) group.description = description before_group_update.emit(session=session, group=group) session.flush() after_group_update.emit(session=session, group=group) return json_response({'ok': True}) except IntegrityError as e: if 'group_name_key' in str(e): raise HTTPBadRequest(reason='Name already used by another group') raise e
async def update_client(request): """ --- tags: - Skywall Core summary: Update client description: Updates an existing client produces: - application/json parameters: - name: clientId in: path description: ID of client to update required: true type: integer - name: body in: body description: Client properties to be updated required: true schema: type: object title: PutClientBody required: [] properties: label: type: string groupId: type: integer responses: 200: description: Client updated schema: type: object title: PutClientResponse required: - ok properties: ok: type: boolean 404: description: Client not found """ body = await parse_json_body(request) with create_session() as session: client = parse_obj_path_param(request, 'clientId', session, Client) if 'label' in body: label = assert_request_param_is_string('label', body) if label != client.label: await _send_label_to_client(client.id, label) client.label = label if 'groupId' in body: if body['groupId'] is None: group = None else: group = assert_request_param_is_entity('groupId', body, session, Group) client.group = group before_client_update.emit(session=session, client=client) session.flush() after_client_update.emit(session=session, client=client) return json_response({'ok': True})