Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snow 1181759 git setup #880

Merged
merged 14 commits into from
Mar 11, 2024
23 changes: 23 additions & 0 deletions src/snowflake/cli/api/sql_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ def use_role(self, new_role: str):
if is_different_role:
self._execute_query(f"use role {prev_role}")

def create_password_secret(
self, name: str, username: str, password: str
) -> SnowflakeCursor:
query = (
f"create secret {name}"
f" type = password"
f" username = '{username}'"
f" password = '{password}'"
)
return self._execute_query(query)

def create_api_integration(
self, name: str, api_provider: str, allowed_prefix: str, secret: Optional[str]
) -> SnowflakeCursor:
query = (
f"create api integration {name}"
f" api_provider = {api_provider}"
f" api_allowed_prefixes = ('{allowed_prefix}')"
f" allowed_authentication_secrets = ({secret if secret else ''})"
f" enabled = true"
)
return self._execute_query(query)

def _execute_schema_query(self, query: str, name: Optional[str] = None, **kwargs):
"""
Check that a database and schema are provided before executing the query. Useful for operating on schema level objects.
Expand Down
76 changes: 76 additions & 0 deletions src/snowflake/cli/plugins/git/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
from click import ClickException
from snowflake.cli.api.commands.flags import identifier_argument
from snowflake.cli.api.commands.snow_typer import SnowTyper
from snowflake.cli.api.console.console import cli_console
from snowflake.cli.api.constants import ObjectType
from snowflake.cli.api.output.types import CommandResult, QueryResult
from snowflake.cli.api.utils.path_utils import is_stage_path
from snowflake.cli.plugins.git.manager import GitManager
from snowflake.cli.plugins.object.manager import ObjectManager
from snowflake.connector import ProgrammingError

app = SnowTyper(
name="git",
Expand Down Expand Up @@ -38,6 +42,78 @@ def _repo_path_argument_callback(path):
)


def _assure_repository_does_not_exist(repository_name: str) -> None:
try:
ObjectManager().describe(
object_type=ObjectType.GIT_REPOSITORY.value.cli_name,
name=repository_name,
)
raise ClickException(f"Repository '{repository_name}' already exists")
except ProgrammingError:
pass


@app.command("setup", requires_connection=True)
def setup(
repository_name: str = RepoNameArgument,
**options,
) -> CommandResult:
"""
Sets up a git repository object.

You will be prompted for:

* url - address of repository to be used for git clone operation

* secret - Snowflake secret containing authentication credentials. Not needed if origin repository does not require
authentication for RO operations (clone, fetch)

* API integration - object allowing Snowflake to interact with git repository.
"""
_assure_repository_does_not_exist(repository_name)
manager = GitManager()

url = typer.prompt("Origin url")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we validate if not empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case typer asks again:

▶ snow git setup repo
Origin url: 
Origin url:  
Origin url: 
Origin url: 

I was considering validating whether it starts with https://, wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for at least https validation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


secret = None
secret_needed = typer.confirm("Use secret for authentication?")
if secret_needed:
use_existing_secret = typer.confirm("Use existing secret?")
if use_existing_secret:
secret = typer.prompt("Secret identifier")
else:
cli_console.step("Creating new secret")
secret = f"{repository_name}_secret"
username = typer.prompt("username")
password = typer.prompt("password/token", hide_input=True)
manager.create_password_secret(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move object creation to the end, in this way there's a clear separation of "prompts" and "actions" WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

username=username, password=password, name=secret
)
cli_console.step(f"Secret '{secret}' successfully created")

use_existing_api = typer.confirm("Use existing api integration?")
if use_existing_api:
api_integration = typer.prompt("API integration identifier")
else:
api_integration = f"{repository_name}_api_integration"
manager.create_api_integration(
name=api_integration,
api_provider="git_https_api",
allowed_prefix=url,
secret=secret,
)
cli_console.step(f"API integration '{api_integration}' successfully created.")

return QueryResult(
manager.create(
repo_name=repository_name,
url=url,
api_integration=api_integration,
secret=secret,
)
)


@app.command(
"list-branches",
requires_connection=True,
Expand Down
12 changes: 12 additions & 0 deletions src/snowflake/cli/plugins/git/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ def show_files(self, repo_path: str) -> SnowflakeCursor:
def fetch(self, repo_name: str) -> SnowflakeCursor:
query = f"alter git repository {repo_name} fetch"
return self._execute_query(query)

def create(
self, repo_name: str, api_integration: str, url: str, secret: str
) -> SnowflakeCursor:
query = (
f"create git repository {repo_name}"
f" api_integration = {api_integration}"
f" origin = '{url}'"
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use multiline string, it makes things a bit more readable

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if secret is not None:
query += f" git_credentials = {secret}"
return self._execute_query(query)
73 changes: 73 additions & 0 deletions tests/__snapshots__/test_help_messages.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,78 @@
╰──────────────────────────────────────────────────────────────────────────────╯


'''
# ---
# name: test_help_messages[git.setup]
'''

Usage: default git setup [OPTIONS] REPOSITORY_NAME

Sets up a git repository object.
You will be prompted for:
* url - address of repository to be used for git clone operation
* secret - Snowflake secret containing authentication credentials. Not needed
if origin repository does not require authentication for RO operations (clone,
fetch)
* API integration - object allowing Snowflake to interact with git repository.

╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * repository_name TEXT Identifier of the git repository. For │
│ example: my_repo │
│ [default: None] │
│ [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help -h Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Connection configuration ───────────────────────────────────────────────────╮
│ --connection,--environment -c TEXT Name of the connection, as defined │
│ in your `config.toml`. Default: │
│ `default`. │
│ --account,--accountname TEXT Name assigned to your Snowflake │
│ account. Overrides the value │
│ specified for the connection. │
│ --user,--username TEXT Username to connect to Snowflake. │
│ Overrides the value specified for │
│ the connection. │
│ --password TEXT Snowflake password. Overrides the │
│ value specified for the │
│ connection. │
│ --authenticator TEXT Snowflake authenticator. Overrides │
│ the value specified for the │
│ connection. │
│ --private-key-path TEXT Snowflake private key path. │
│ Overrides the value specified for │
│ the connection. │
│ --database,--dbname TEXT Database to use. Overrides the │
│ value specified for the │
│ connection. │
│ --schema,--schemaname TEXT Database schema to use. Overrides │
│ the value specified for the │
│ connection. │
│ --role,--rolename TEXT Role to use. Overrides the value │
│ specified for the connection. │
│ --warehouse TEXT Warehouse to use. Overrides the │
│ value specified for the │
│ connection. │
│ --temporary-connection -x Uses connection defined with │
│ command line parameters, instead │
│ of one defined in config │
│ --mfa-passcode TEXT Token to use for multi-factor │
│ authentication (MFA) │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Global configuration ───────────────────────────────────────────────────────╮
│ --format [TABLE|JSON] Specifies the output format. │
│ [default: TABLE] │
│ --verbose -v Displays log entries for log levels `info` │
│ and higher. │
│ --debug Displays log entries for log levels `debug` │
│ and higher; debug logs contains additional │
│ information. │
│ --silent Turns off intermediate output to console. │
╰──────────────────────────────────────────────────────────────────────────────╯


'''
# ---
# name: test_help_messages[git]
Expand All @@ -1146,6 +1218,7 @@
│ list-branches List all branches in the repository. │
│ list-files List files from given state of git repository. │
│ list-tags List all tags in the repository. │
│ setup Sets up a git repository object. │
╰──────────────────────────────────────────────────────────────────────────────╯


Expand Down
Loading
Loading