Skip to content

Commit

Permalink
Add --default to add command to mark connection as default (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-turbaszek committed Jun 11, 2024
1 parent 80791cb commit d69c305
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
* `snow notebook execute` enabling head-less execution of a notebook.
* `snow notebook create` proving an option to create a Snowflake Notebook from a file on stage.
* Added templating support for project definition file.
* Template variables can now be used anywhere in the the project definition file.
* Template variables can now be used anywhere in the project definition file.
* Added `--default` flag to `snow connection add` commands allowing users to mark the new connection as default.

## Fixes and improvements
* Fixed error handling for malformatted `config.toml`
Expand Down
11 changes: 11 additions & 0 deletions src/snowflake/cli/plugins/connection/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ def add(
prompt="Path to private key file",
help="Path to file containing private key",
),
set_as_default: bool = typer.Option(
False,
"--default",
is_flag=True,
help="If provided the connection will be configured as default connection.",
),
**options,
) -> CommandResult:
"""Adds a connection to configuration file."""
Expand All @@ -241,6 +247,11 @@ def add(
private_key_path=private_key_path,
),
)
if set_as_default:
set_config_value(
section=None, key="default_connection_name", value=connection_name
)

return MessageResult(
f"Wrote new connection {connection_name} to {CONFIG_MANAGER.file_path}"
)
Expand Down
10 changes: 10 additions & 0 deletions tests/__snapshots__/test_connection.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@

'''
# ---
# name: test_new_connection_can_be_added_as_default
'''
default_connection_name = "default-conn"
[connections.default-conn]
account = "account1"
user = "user1"
password = "password1"

'''
# ---
# name: test_new_connection_with_jwt_auth
'''
[connections.conn2]
Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/test_help_messages.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@
│ [default: optional] │
│ --private-key -k TEXT Path to file containing private key │
│ [default: optional] │
│ --default If provided the connection will be │
│ configured as default connection. │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Global configuration ───────────────────────────────────────────────────────╮
Expand Down
23 changes: 23 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ def test_new_connection_can_be_added(runner, snapshot):
assert content == snapshot


def test_new_connection_can_be_added_as_default(runner, snapshot):
with NamedTemporaryFile("w+", suffix=".toml") as tmp_file:
result = runner.invoke_with_config_file(
tmp_file.name,
[
"connection",
"add",
"--connection-name",
"default-conn",
"--username",
"user1",
"--password",
"password1",
"--account",
"account1",
"--default",
],
)
content = tmp_file.read()
assert result.exit_code == 0, result.output
assert content == snapshot


def test_new_connection_with_jwt_auth(runner, snapshot):
with NamedTemporaryFile("w+", suffix=".toml") as tmp_file:
result = runner.invoke_with_config_file(
Expand Down

0 comments on commit d69c305

Please sign in to comment.