Esempio n. 1
0
def add_campaign(connection: Connection, campaign: str):
	exception = HTTPException(
		status_code=HTTP_409_CONFLICT,
		detail="A campaign with that name already exists",
	)
	test_connection(connection)
	if campaign in connection.campaigns:
		raise exception
	if len(campaign) > 30:
		exception.status_code=HTTP_422_UNPROCESSABLE_ENTITY
		exception.detail="A campaign identifier must be no longer than 30 characters"
		raise exception
	if not "".join(campaign.split("_")).isalnum():
		exception.status_code=HTTP_422_UNPROCESSABLE_ENTITY
		exception.detail="A campaign identifier must not contain special characters (only '_' are allowed)"
		raise exception
	db_name = "daqbroker_" + campaign
	connection.create_database(db_name)
	connection.get_databases()
	return connection.campaigns
Esempio n. 2
0
async def change_connection(conn_id: int,
                            new_data: ConnectionInput,
                            session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    connection_dict = connection.dict()
    exception = HTTPException(
        status_code=HTTP_409_CONFLICT,
        detail="Attempting to change to already existing connection")
    if not connection:
        raise exception
    for attr, val in new_data.dict().items():
        if val:
            connection_dict[attr] = val
    new_connection = Connection(**connection_dict)
    key_vals = {
        attr: getattr(new_connection, attr)
        for attr in ["hostname", "port", "type"]
    }
    new_connection_query = get_local_resources(db=session,
                                               Resource=Connection,
                                               key_vals=key_vals).first()
    if new_connection_query:
        raise exception
    new_connection_test = new_connection.test()
    if not new_connection_test[0]:
        exception.status_code = HTTP_503_SERVICE_UNAVAILABLE
        exception.detail = new_connection_test[1]
        raise exception
    return add_local_resource(db=session,
                              Resource=Connection,
                              user_input=new_data,
                              r_id=connection.id)