import snowflake.connector
import json
# Get credentials from Kubernetes. The credentials were setup as a dictionary, so will use JSON to load.
credentials = None
with open('/var/run/secrets/user_credentials/snowflake_credentials') as f:
    credentials = json.load(f)
# Check and make sure the credentials were pulled correctly
if credentials:
    # Connect to snowflake
    ctx = snowflake.connector.connect(
        user=credentials.get('username'),
        password=credentials.get('password'),
        account=credentials.get('account')
    )
    # Establish a cursor and execute a query
    cs = ctx.cursor()
    ret = []
    try:
        test = cs.execute("USE database SNOWFLAKE_SAMPLE_DATA")
        cs.execute("SHOW TABLES")
        ret = cs.fetchmany(30)
    finally:
        # Always close connections if there is an error
        cs.close()
    # Close the connection
    ctx.close()
    # Print out the return values for the data
    for data in ret:
        print(data)