Skip to content

Commit

Permalink
PLAT-567: Ensure canary and sdk connections use project id
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 69bbca9f8691523fae77964a6e4df9dbe3c09c22
  • Loading branch information
drew committed Apr 10, 2023
1 parent 985fd31 commit 5d1a070
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
30 changes: 28 additions & 2 deletions src/gretel_client/cli/connections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import click

from gretel_client.cli.common import parse_file, pass_session, SessionContext
from gretel_client.cli.common import (
parse_file,
pass_session,
project_option,
SessionContext,
)
from gretel_client.config import get_session_config
from gretel_client.rest_v1.api.connections_api import ConnectionsApi
from gretel_client.rest_v1.model.connection import Connection
Expand All @@ -25,10 +30,31 @@ def get_connections_api() -> ConnectionsApi:
help="Path to the file containing Gretel connection.",
required=True,
)
@project_option
@pass_session
def create(sc: SessionContext, from_file: str):
def create(sc: SessionContext, from_file: str, project: str):
connection_api = get_connections_api()
conn = parse_file(from_file)

# we try and configure a project in the following order
#
# 1. the project passed via `--project`.
# 2. the project configured from the connection file.
# 3. the default configured project from the system environment.
#
project_id = conn.get("project_id")

# `project` is set if `--project` is passed.
if project is not None:
project_id = project

# `project_id` is unset at this point if no project flag is passed, and no
# project id is configured on the connection file.
if project_id is None:
project_id = sc.project.project_guid

conn["project_id"] = sc.project.project_guid

connection = connection_api.create_connection(connection=conn)

sc.log.info("Created connection:")
Expand Down
8 changes: 5 additions & 3 deletions tests/gretel_client/integration/test_cli_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
from click.testing import CliRunner

from gretel_client.cli.cli import cli
from gretel_client.projects.projects import Project


def test_connection_crud_from_cli(
get_fixture: Callable,
):
def test_connection_crud_from_cli(get_fixture: Callable, project: Project):
runner = CliRunner()

# Create a connection
Expand All @@ -18,11 +17,14 @@ def test_connection_crud_from_cli(
[
"connections",
"create",
"--project",
project.project_guid, # type: ignore
"--from-file",
get_fixture("connections/azure_connection.json"),
],
)
assert "Created connection:" in cmd.output
assert project.project_guid in cmd.output # type: ignore
assert cmd.exit_code == 0
connection_result = json.loads(cmd.output.rsplit("Created connection:\n")[1])
print(cmd.output)
Expand Down

0 comments on commit 5d1a070

Please sign in to comment.