コード例 #1
0
async def data_manager(websocket, path):
    logging.info(f"[INFO] Current path: {str(path)}")
    try:
        if path == "/":
            raise Exception(f"No operation defined on the current path."
                            f"\nReceived path: {str(path)}")
        data = await websocket.recv()
        data = json.loads(data)

        identifier = data["id"]
        request_type = data["type"]
        payload = data["payload"]
        time = data["time"]
        logging.info(f"[INFO] Data received: {data}")

        if request_type != RDM_CALL:
            raise Exception(f"Type of service not as expected. Probably a call"
                            f"for other service. Verify logs for further infor"
                            f"mation!")

        if save_audit(identifier, request_type, payload, time):
            pass
        else:
            logging.error(f"[ERROR] Error while trying to save data to audit"
                          f" database. Verify Rethink Data Manager logs for "
                          f"furhter information!")

        try:
            connection = configure_database(ReDB_HOST, ReDB_PORT,
                                            ReDB_DEFAULT_DB, ReDB_USER,
                                            ReDB_PASS)

            if str(path) == op[1]:
                database = payload["database"]
                table = payload["table"]
                payload = payload["data"]
                logging.info(f"[INFO] Inserting data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tDATA: "
                             f"{payload}")
                result = insert(database, table, payload, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to insert data. "
                                    f"Verify the logs for the full traceback. "
                                    f"The data was not inserted!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[2]:
                database = payload["database"]
                table = payload["table"]
                logging.info(f"[INFO] Getting all data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}")
                result = get_all(database, table, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to getting data. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the table is empty or no table"
                                    f" was found!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[3]:
                database = payload["database"]
                table = payload["table"]
                identifier = payload["identifier"]
                logging.info(f"[INFO] Getting objetct data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: "
                             f"{identifier}")
                result = get(database, table, identifier, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to getting data. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the object doesn't exists or no"
                                    f" table was found!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[4]:
                database = payload["database"]
                table = payload["table"]
                identifier = payload["identifier"]
                statment = payload["data"]
                logging.info(f"[INFO] Updating object:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: "
                             f"{identifier}\n\tUPDATE STATEMENT: {statment}")
                result = update(database, table, identifier, statment,
                                connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to update object. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the object doesn't exists or no"
                                    f" table was found!")
                elif result["objects_updated"] == 0:
                    raise Exception(f"[ERROR] Error trying to update object. "
                                    f"Verify the logs for the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[5]:
                database = payload["database"]
                table = payload["table"]
                identifier = payload["identifier"]
                logging.info(f"[INFO] Deleting object:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: "
                             f"{identifier}")
                result = delete(database, table, identifier, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to delete object. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither the object doesn't exists or no"
                                    f" table was found!")
                elif result["objects_deleted"] == 0:
                    raise Exception(f"[ERROR] Error trying to delete object. "
                                    f"Verify the logs for the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[6]:
                database = payload["database"]
                table = payload["table"]
                logging.info(f"[INFO] Deleting all objects:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}")
                result = delete_all(database, table, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Error trying to delete objects. "
                                    f"Verify the logs for the full traceback. "
                                    f"Neither some error was found or no table"
                                    f" was found!")
                elif result["objects_deleted"] == 0:
                    raise Exception(f"[ERROR] Error trying to delete object. "
                                    f"Verify the logs for the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[7]:
                database = payload["database"]
                table = payload["table"]
                logging.info(f"[INFO] Creating new table:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}")
                result = create_table(database, table, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Unknown error while trying to "
                                    f"create a new table. Verify the logs"
                                    f" for the the full traceback.")
                elif result["tables_created"] == 0:
                    raise Exception(f"[ERROR] This table ({table})"
                                    f" already exists!")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[8]:
                database = payload["database"]
                table = payload["table"]
                statment = payload["filter"]
                statment = json.loads(statment)
                logging.info(f"[INFO] Getting objetct data:\n\tDATABASE: "
                             f"{database}\n\tTABLE: {table}\n\tFILTER: "
                             f"{statment}")
                result = get_where(database, table, statment, connection)
                if result == {}:
                    raise Exception(f"[ERROR] Unknown error while trying to "
                                    f"create a new table. Verify the logs"
                                    f" for the the full traceback.")
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            else:
                raise Exception(f"Unknown operation on the current path."
                                f"\nReceived path: {str(path)}")
        except Exception as err:
            if connection:
                disconnect_database(connection)
                raise err
        except KeyboardInterrupt as err:
            disconnect_database(connection)
            raise err
        except ConnectionClosed as err:
            disconnect_database(connection)
            raise err
        except CancelledError as err:
            disconnect_database(connection)
            raise err
    except Exception as err:
        logging.error(f"[ERROR] Error at services/data_manager."
                      f"\nTraceback: {err}")
        await websocket.send(json.dumps(error_msg(path)))
    else:
        pass
コード例 #2
0
def test_successful_get_all(rethink_connect):
    result = get_all(database, table, rethink_connect)
    assert result is not None
    assert len(result) == 3
コード例 #3
0
def test_unsuccessful_get_all(rethink_connect):
    result = get_all(database, empty_table, rethink_connect)
    assert result is not None
    assert len(result) == 0
    assert result == []
コード例 #4
0
async def data_manager(websocket, path):
    logging.info(f'[INFO] Current path: {str(path)}')
    try:
        if path is '/':
            raise Exception(f'No operation defined on the current path.'
                            f'\nReceived path: {str(path)}')
        data = await websocket.recv()
        data = json.loads(data)

        identifier = data['id']
        request_type = data['type']
        payload = data['payload']
        time = data['time']
        logging.info(f'[INFO] Data received: {data}')

        if request_type != RDM_CALL:
            raise Exception(f'Type of service not as expected. Probably a call'
                            f'for other service. Verify logs for further infor'
                            f'mation!')

        if save_audit(identifier, request_type, payload, time):
            pass
        else:
            logging.error(f'[ERROR] Error while trying to save data to audit'
                          f' database. Verify Rethink Data Manager logs for '
                          f'furhter information!')

        try:
            connection = configure_database(
                    ReDB_HOST,
                    ReDB_PORT,
                    ReDB_DEFAULT_DB,
                    ReDB_USER,
                    ReDB_PASS
                )

            if str(path) == op[1]:
                database = payload['database']
                table = payload['table']
                payload = payload['data']
                logging.info(f'[INFO] Inserting data:\n\tDATABASE: '
                             f'{database}\n\tTABLE: {table}\n\tDATA: '
                             f'{payload}')
                result = insert(database, table, payload, connection)
                if result == {}:
                    raise Exception(f'[ERROR] Error trying to insert data. '
                                    f'Verify the logs for the full traceback. '
                                    f'The data was not inserted!')
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[2]:
                database = payload['database']
                table = payload['table']
                logging.info(f'[INFO] Getting all data:\n\tDATABASE: '
                             f'{database}\n\tTABLE: {table}')
                result = get_all(database, table, connection)
                if result == {}:
                    raise Exception(f'[ERROR] Error trying to getting data. '
                                    f'Verify the logs for the full traceback. '
                                    f'Neither the table is empty or no table'
                                    f' was found!')
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[3]:
                database = payload['database']
                table = payload['table']
                identifier = payload['identifier']
                logging.info(f'[INFO] Getting objetct data:\n\tDATABASE: '
                             f'{database}\n\tTABLE: {table}\n\tIDENTIFIER: '
                             f'{identifier}')
                result = get(database, table, identifier, connection)
                if result == {}:
                    raise Exception(f'[ERROR] Error trying to getting data. '
                                    f'Verify the logs for the full traceback. '
                                    f'Neither the object doesn\'t exists or no'
                                    f' table was found!')
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[4]:
                database = payload['database']
                table = payload['table']
                identifier = payload['identifier']
                statment = payload['data']
                logging.info(f'[INFO] Updating object:\n\tDATABASE: '
                             f'{database}\n\tTABLE: {table}\n\tIDENTIFIER: '
                             f'{identifier}\n\tUPDATE STATEMENT: {statment}')
                result = update(
                    database,
                    table,
                    identifier,
                    statment,
                    connection
                    )
                if result == {}:
                    raise Exception(f'[ERROR] Error trying to update object. '
                                    f'Verify the logs for the full traceback. '
                                    f'Neither the object doesn\'t exists or no'
                                    f' table was found!')
                elif result['objects_updated'] is 0:
                    raise Exception(f'[ERROR] Error trying to update object. '
                                    f'Verify the logs for the full traceback.')
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[5]:
                database = payload['database']
                table = payload['table']
                identifier = payload['identifier']
                logging.info(f'[INFO] Deleting object:\n\tDATABASE: '
                             f'{database}\n\tTABLE: {table}\n\tIDENTIFIER: '
                             f'{identifier}')
                result = delete(database, table, identifier, connection)
                if result == {}:
                    raise Exception(f'[ERROR] Error trying to delete object. '
                                    f'Verify the logs for the full traceback. '
                                    f'Neither the object doesn\'t exists or no'
                                    f' table was found!')
                elif result['objects_deleted'] is 0:
                    raise Exception(f'[ERROR] Error trying to delete object. '
                                    f'Verify the logs for the full traceback.')
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[6]:
                pass
            # database = payload['database']
            # table = payload['table']
            # logging.info(f'[INFO] Deleting all objects:\n\tDATABASE: '
            #              f'{database}\n\tTABLE: {table}')
            # result = delete_all(database, table, connection)
            # if result == {}:
            #     raise Exception(f'[ERROR] Error trying to delete objects. '
            #                     f'Verify the logs for the full traceback. '
            #                     f'Neither some error was found or no table'
            #                     f' was found!')
            # elif result['objects_deleted'] is 0:
            #     raise Exception(f'[ERROR] Error trying to delete object. '
            #                     f'Verify the logs for the full traceback.')
            # else:
            #     await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[7]:
                pass
                # database = payload['database']
                # table = payload['table']
                # logging.info(f'[INFO] Creating new table:\n\tDATABASE: '
                #              f'{database}\n\tTABLE: {table}')
                # result = create_table(database, table, connection)
                # if result == {}:
                #     raise Exception(f'[ERROR] Unknown error while trying to '
                #                     f'create a new table. Verify the logs'
                #                     f' for the the full traceback.')
                # elif result['tables_created'] is 0:
                #     raise Exception(f'[ERROR] This table ({table})'
                #                     f' already exists!')
                # else:
                #     await websocket.send(json.dumps(success_msg(result)))
            elif str(path) == op[8]:
                database = payload['database']
                table = payload['table']
                statment = payload['filter']
                statment = json.loads(statment)
                logging.info(f'[INFO] Getting objetct data:\n\tDATABASE: '
                             f'{database}\n\tTABLE: {table}\n\tFILTER: '
                             f'{statment}')
                result = get_where(database, table, statment, connection)
                if result == {}:
                    raise Exception(f'[ERROR] Unknown error while trying to '
                                    f'create a new table. Verify the logs'
                                    f' for the the full traceback.')
                else:
                    await websocket.send(json.dumps(success_msg(result)))
            else:
                raise Exception(f'Unknown operation on the current path.'
                                f'\nReceived path: {str(path)}')
        except Exception as err:
            if connection:
                disconnect_database(connection)
                raise err
        except KeyboardInterrupt as err:
            disconnect_database(connection)
            raise err
        except ConnectionClosed as err:
            disconnect_database(connection)
            raise err
        except CancelledError as err:
            disconnect_database(connection)
            raise err
    except Exception as err:
        logging.error(f'[ERROR] Error at services/data_manager.'
                      f'\nTraceback: {err}')
        await websocket.send(json.dumps(
            error_msg(path)
        ))
    else:
        pass