from neo4j import GraphDatabase
import json
"""
Get credentials from Kubernetes. The credentials were set up as a dictionary. For example:
{
    "username": "USERNAME",
    "password": "PASSWORD"
}
"""
credentials = None
with open('/var/run/secrets/user_credentials/neo4j_credentials') as f:
    credentials = json.load(f)
# Verify that the credentials were pulled correctly
if credentials:
    # Setup the connection
    uri = f"bolt://{credentials.get('host_name')}:7687"
    driver = GraphDatabase.driver(
        uri,
        auth=(
            credentials.get('username'),
            credentials.get('password')
        )
    )
    # Define function that queries the database
    def print_info(tx):
        for record in tx.run(
            'MATCH (tom:Person {name: "Tom Hanks"})-'
            '[:ACTED_IN]->(tomHanksMovies) RETURN tom,tomHanksMovies'
        ):
            # Print out the records that it finds
            print(record)
    # Scope the session and call the query
    with driver.session() as session:
        session.read_transaction(print_info)