Source code for snowflake_utilities.utils

r"""Submodule utils.py includes the following functions:

- **get_snowflake_connection** - returns a connection to Snowflake

- **check_resource_viable** - checks if a resource type is viable (i.e. is the string a resource type in Snowflake)

- **print_resources** - prints the resources in Snowflake

"""

import os
import snowflake.connector
from dotenv import load_dotenv, find_dotenv

_ = load_dotenv(find_dotenv())


[docs]def get_snowflake_connection(): """ Returns a connection to Snowflake. Assuming the following environment variables are set: SNOWFLAKE_USERNAME - the username SNOWFLAKE_PASSWORD - the password SNOWFLAKE_ACCOUNT - the account name (the beginning of the URL when you log in to Snowflake) Parameters ---------- None : None Returns ------- conn : snowflake.connector.connection.SnowflakeConnection A connection to Snowflake """ conn = snowflake.connector.connect( user=os.getenv("SNOWFLAKE_USERNAME"), password=os.getenv("SNOWFLAKE_PASSWORD"), account=os.getenv("SNOWFLAKE_ACCOUNT"), ) return conn
[docs]def check_resource_viable(resource_type: str = "WAREHOUSES"): """ Checks if a resource type is viable (i.e. it is not a table). Parameters ---------- resource_type : str The type of resource to check Returns ------- is_viable : bool True if the resource type is viable, False otherwise """ viable_resource_types = [ "WAREHOUSE", "DATABASE", "SCHEMA", "TABLE", "WAREHOUSES", "DATABASES", "SCHEMAS", "TABLES", ] is_viable = resource_type in viable_resource_types if not is_viable: raise ValueError( f"Resource type {resource_type} is not viable. Please choose from {viable_resource_types}" )