Skip to content

Commit 5094954

Browse files
authored
SNOW-1980772: command for checking variables (#2127)
1 parent 3e2319f commit 5094954

File tree

3 files changed

+195
-1
lines changed

3 files changed

+195
-1
lines changed

src/snowflake/cli/_plugins/helpers/commands.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
from __future__ import annotations
1616

1717
import logging
18+
import os
1819
from pathlib import Path
1920
from typing import Any, List, Optional
2021

2122
import typer
2223
import yaml
2324
from click import ClickException
25+
from snowflake.cli._plugins.helpers.snowsl_vars_reader import check_env_vars
2426
from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
2527
from snowflake.cli.api.config import (
2628
ConnectionConfig,
@@ -29,7 +31,12 @@
2931
set_config_value,
3032
)
3133
from snowflake.cli.api.console import cli_console
32-
from snowflake.cli.api.output.types import CommandResult, MessageResult
34+
from snowflake.cli.api.output.types import (
35+
CollectionResult,
36+
CommandResult,
37+
MessageResult,
38+
MultipleResults,
39+
)
3340
from snowflake.cli.api.project.definition_conversion import (
3441
convert_project_definition_to_v2,
3542
)
@@ -293,3 +300,20 @@ def _validate_and_save_connections_imported_from_snowsql(
293300
path=["default_connection_name"],
294301
value=default_cli_connection_name,
295302
)
303+
304+
305+
@app.command(name="check-snowsql-env-vars", requires_connection=False)
306+
def check_snowsql_env_vars(**options):
307+
"""Check if there are any SnowSQL environment variables set."""
308+
309+
env_vars = os.environ.copy()
310+
discovered, unused, summary = check_env_vars(env_vars)
311+
312+
results = []
313+
if discovered:
314+
results.append(CollectionResult(discovered))
315+
if unused:
316+
results.append(CollectionResult(unused))
317+
318+
results.append(MessageResult(summary))
319+
return MultipleResults(results)
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
from __future__ import annotations
2+
3+
EnvVars = dict[str, str]
4+
5+
DicoveredVars = list[dict[str, str]]
6+
UnusedVars = list[dict[str, str]]
7+
Summary = str
8+
9+
CheckResult = tuple[DicoveredVars, UnusedVars, Summary]
10+
11+
KNOWN_SNOWSQL_ENV_VARS = {
12+
"SNOWSQL_ACCOUNT": {
13+
"Found": "SNOWSQL_ACCOUNT",
14+
"Suggested": "SNOWFLAKE_ACCOUNT",
15+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
16+
},
17+
"SNOWSQL_PWD": {
18+
"Found": "SNOWSQL_PASSWORD",
19+
"Suggested": "SNOWFLAKE_PASSWORD",
20+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
21+
},
22+
"SNOWSQL_USER": {
23+
"Found": "SNOWSQL_USER",
24+
"Suggested": "SNOWFLAKE_USER",
25+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
26+
},
27+
"SNOWSQL_REGION": {
28+
"Found": "SNOWSQL_REGION",
29+
"Suggested": "SNOWFLAKE_REGION",
30+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
31+
},
32+
"SNOWSQL_ROLE": {
33+
"Found": "SNOWSQL_ROLE",
34+
"Suggested": "SNOWFLAKE_ROLE",
35+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
36+
},
37+
"SNOWSQL_WAREHOUSE": {
38+
"Found": "SNOWSQL_WAREHOUSE",
39+
"Suggested": "SNOWFLAKE_WAREHOUSE",
40+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
41+
},
42+
"SNOWSQL_DATABASE": {
43+
"Found": "SNOWSQL_DATABASE",
44+
"Suggested": "SNOWFLAKE_DATABASE",
45+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
46+
},
47+
"SNOWSQL_SCHEMA": {
48+
"found": "SNOWSQL_SCHEMA",
49+
"Suggested": "SNOWFLAKE_SCHEMA",
50+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
51+
},
52+
"SNOWSQL_HOST": {
53+
"Found": "SNOWSQL_HOST",
54+
"Suggested": "SNOWFLAKE_HOST",
55+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
56+
},
57+
"SNOWSQL_PORT": {
58+
"Found": "SNOWSQL_PORT",
59+
"Suggested": "SNOWFLAKE_PORT",
60+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
61+
},
62+
"SNOWSQL_PROTOCOL": {
63+
"Found": "SNOWSQL_PROTOCOL",
64+
"Suggested": "SNOWFLAKE_PROTOCOL",
65+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#use-environment-variables-for-snowflake-credentials",
66+
},
67+
"SNOWSQL_PROXY_HOST": {
68+
"Found": "SNOWSQL_PROXY_HOST",
69+
"Suggested": "PROXY_HOST",
70+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-cli#use-a-proxy-server",
71+
},
72+
"SNOWSQL_PROXY_PORT": {
73+
"Found": "SNOWSQL_PROXY_HOST",
74+
"Suggested": "PROXY_HOST",
75+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-cli#use-a-proxy-server",
76+
},
77+
"SNOWSQL_PROXY_USER": {
78+
"Found": "SNOWSQL_PROXY_PORT",
79+
"Suggested": "PROXY_PORT",
80+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-cli#use-a-proxy-server",
81+
},
82+
"SNOWSQL_PROXY_PWD": {
83+
"Found": "SNOWSQL_PROXY_PWD",
84+
"Suggested": "PROXY_PWD",
85+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-cli#use-a-proxy-server",
86+
},
87+
"SNOWSQL_PRIVATE_KEY_PASSPHRASE": {
88+
"Found": "SNOWSQL_PRIVATE_KEY_PASSPHRASE",
89+
"Suggested": "PRIVATE_KEY_PASSPHRASE",
90+
"Additional info": "https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections",
91+
},
92+
}
93+
94+
95+
def check_env_vars(variables: EnvVars) -> CheckResult:
96+
"""Checks passed dict objects for possible SnowSQL variables.
97+
98+
Returns tuple of
99+
- sequence of variables that can be adjusted
100+
- sequence of variables that have no corresponding variables
101+
- a summary messages
102+
"""
103+
discovered: DicoveredVars = []
104+
unused: UnusedVars = []
105+
106+
prefix_matched = (e for e in variables if e.lower().startswith("snowsql"))
107+
108+
for var in prefix_matched:
109+
if suggestion := KNOWN_SNOWSQL_ENV_VARS.get(var, None):
110+
discovered.append(suggestion)
111+
else:
112+
unused.append(
113+
{
114+
"Found": var,
115+
"Suggested": "n/a",
116+
"Additional info": "Unused variable",
117+
}
118+
)
119+
120+
discovered_cnt = len(discovered)
121+
unused_cnt = len(unused)
122+
123+
summary: Summary = (
124+
f"Found {discovered_cnt + unused_cnt} SnowSQL environment variables,"
125+
f" {discovered_cnt} with replacements, {unused_cnt} unused."
126+
)
127+
128+
return discovered, unused, summary
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
from unittest import mock
3+
4+
COMMAND = "check-snowsql-env-vars"
5+
6+
VALID_VARS = {
7+
"SNOWSQL_ACCOUNT": "test",
8+
"SNOWSQL_WAREHOUSE": "whwhw",
9+
}
10+
INVALID_VARS = {
11+
"SNOWSQL_ABC": "test",
12+
"SNOWSQL_CBD": "whwhw",
13+
}
14+
15+
16+
@mock.patch.dict(os.environ, VALID_VARS, clear=True)
17+
def test_replecaple_env_found(runner):
18+
result = runner.invoke(("helpers", COMMAND))
19+
assert result.exit_code == 0
20+
assert (
21+
"Found 2 SnowSQL environment variables, 2 with replacements, 0 unused."
22+
in result.output
23+
)
24+
25+
26+
@mock.patch.dict(os.environ, INVALID_VARS, clear=True)
27+
def test_non_replacable_env_found(runner):
28+
result = runner.invoke(("helpers", COMMAND))
29+
assert result.exit_code == 0
30+
assert (
31+
"Found 2 SnowSQL environment variables, 0 with replacements, 2 unused."
32+
in result.output
33+
)
34+
35+
36+
def test_no_env_found(runner):
37+
result = runner.invoke(("helpers", COMMAND))
38+
assert result.exit_code == 0
39+
assert (
40+
"Found 0 SnowSQL environment variables, 0 with replacements, 0 unused."
41+
in result.output
42+
)

0 commit comments

Comments
 (0)