Skip to content

Commit 539d675

Browse files
SNOW-2114093 Probing script (#2335)
1 parent ba6e0f5 commit 539d675

File tree

10 files changed

+176
-1
lines changed

10 files changed

+176
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,7 @@ core.*
125125
# Compiled Cython
126126
src/snowflake/connector/arrow_iterator.cpp
127127
src/snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_arrow_iterator.cpp
128+
129+
# Prober files
130+
prober/parameters.json
131+
prober/snowflake_prober.egg-info/

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ exclude tox.ini
2020
exclude mypy.ini
2121
exclude .clang-format
2222
exclude .wiremock/*
23-
exclude cmd/prober/testing_matrix.json
2423

2524
prune ci
2625
prune benchmark
@@ -29,3 +28,4 @@ prune tested_requirements
2928
prune src/snowflake/connector/nanoarrow_cpp/scripts
3029
prune __pycache__
3130
prune samples
31+
prune prober

prober/__init__.py

Whitespace-only changes.

prober/probes/__init__.py

Whitespace-only changes.

prober/probes/logging_config.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import logging
2+
3+
4+
def initialize_logger(name=__name__, level=logging.INFO):
5+
"""
6+
Initializes and configures a logger.
7+
8+
Args:
9+
name (str): The name of the logger.
10+
level (int): The logging level (e.g., logging.INFO, logging.DEBUG).
11+
12+
Returns:
13+
logging.Logger: Configured logger instance.
14+
"""
15+
logger = logging.getLogger(name)
16+
logger.setLevel(level)
17+
18+
# Create a console handler
19+
handler = logging.StreamHandler()
20+
handler.setLevel(level)
21+
22+
# Create a formatter and set it for the handler
23+
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
24+
handler.setFormatter(formatter)
25+
26+
# Add the handler to the logger
27+
if not logger.handlers: # Avoid duplicate handlers
28+
logger.addHandler(handler)
29+
30+
return logger

prober/probes/login.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from probes.logging_config import initialize_logger
2+
from probes.registry import prober_function
3+
4+
import snowflake.connector
5+
6+
# Initialize logger
7+
logger = initialize_logger(__name__)
8+
9+
10+
def connect(connection_parameters: dict):
11+
"""
12+
Initializes the Python driver for login using the provided connection parameters.
13+
14+
Args:
15+
connection_parameters (dict): A dictionary containing connection details such as
16+
host, port, user, password, account, schema, etc.
17+
18+
Returns:
19+
snowflake.connector.SnowflakeConnection: A connection object if successful.
20+
"""
21+
try:
22+
# Initialize the Snowflake connection
23+
connection = snowflake.connector.connect(
24+
user=connection_parameters["user"],
25+
account=connection_parameters["account"],
26+
host=connection_parameters["host"],
27+
port=connection_parameters["port"],
28+
warehouse=connection_parameters["warehouse"],
29+
database=connection_parameters["database"],
30+
schema=connection_parameters["schema"],
31+
role=connection_parameters["role"],
32+
authenticator="KEY_PAIR_AUTHENTICATOR",
33+
private_key=connection_parameters["private_key"],
34+
)
35+
return connection
36+
except Exception as e:
37+
logger.info({f"success_login={False}"})
38+
logger.error(f"Error connecting to Snowflake: {e}")
39+
40+
41+
@prober_function
42+
def perform_login(connection_parameters: dict):
43+
"""
44+
Performs the login operation using the provided connection parameters.
45+
46+
Args:
47+
connection_parameters (dict): A dictionary containing connection details such as
48+
host, port, user, password, account, schema, etc.
49+
50+
Returns:
51+
bool: True if login is successful, False otherwise.
52+
"""
53+
try:
54+
# Connect to Snowflake
55+
connection = connect(connection_parameters)
56+
57+
# Perform a simple query to test the connection
58+
cursor = connection.cursor()
59+
cursor.execute("SELECT 1;")
60+
result = cursor.fetchone()
61+
logger.info(result)
62+
assert result == (1,)
63+
logger.info({f"success_login={True}"})
64+
except Exception as e:
65+
logger.info({f"success_login={False}"})
66+
logger.error(f"Error during login: {e}")

prober/probes/main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import argparse
2+
import logging
3+
4+
from probes.logging_config import initialize_logger
5+
from probes.registry import PROBES_FUNCTIONS
6+
7+
# Initialize logger
8+
logger = initialize_logger(__name__)
9+
10+
11+
def main():
12+
logger.info("Starting Python Driver Prober...")
13+
# Set up argument parser
14+
parser = argparse.ArgumentParser(description="Python Driver Prober")
15+
parser.add_argument("--scope", required=True, help="Scope of probing")
16+
parser.add_argument("--host", required=True, help="Host")
17+
parser.add_argument("--port", type=int, required=True, help="Port")
18+
parser.add_argument("--role", required=True, help="Protocol")
19+
parser.add_argument("--account", required=True, help="Account")
20+
parser.add_argument("--schema", required=True, help="Schema")
21+
parser.add_argument("--warehouse", required=True, help="Warehouse")
22+
parser.add_argument("--user", required=True, help="Username")
23+
parser.add_argument("--private_key", required=True, help="Private key")
24+
25+
# Parse arguments
26+
args = parser.parse_args()
27+
28+
connection_params = {
29+
"host": args.host,
30+
"port": args.port,
31+
"role": args.role,
32+
"account": args.account,
33+
"schema": args.schema,
34+
"warehouse": args.warehouse,
35+
"user": args.user,
36+
"private_key": args.private_key,
37+
}
38+
39+
for function_name, function in PROBES_FUNCTIONS.items():
40+
try:
41+
logging.info("BBB")
42+
logging.error(f"Running probe: {function_name}")
43+
function(connection_params)
44+
except Exception as e:
45+
logging.error(f"Error running probe {function_name}: {e}")
46+
47+
48+
if __name__ == "__main__":
49+
main()

prober/probes/registry.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
PROBES_FUNCTIONS = {}
2+
3+
4+
def prober_function(func):
5+
"""
6+
Register a function in the PROBES_FUNCTIONS dictionary.
7+
The key is the function name, and the value is the function itself.
8+
"""
9+
PROBES_FUNCTIONS[func.__name__] = func
10+
return func
File renamed without changes.

prober/setup.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from setuptools import find_packages, setup
2+
3+
setup(
4+
name="snowflake_prober",
5+
version="1.0.0",
6+
packages=find_packages(),
7+
install_requires=[
8+
"snowflake-connector-python",
9+
"requests",
10+
],
11+
entry_points={
12+
"console_scripts": [
13+
"prober=probes.main:main",
14+
],
15+
},
16+
)

0 commit comments

Comments
 (0)