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
79 changes: 79 additions & 0 deletions src/snowflake/cli/plugins/git/commands.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import logging
from pathlib import Path
from typing import Optional

import typer
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 +43,80 @@ def _repo_path_argument_callback(path):
)


@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.
"""

manager = GitManager()

def _assure_repository_does_not_exist() -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to move those functions outside the command, it will increase readability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

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

def _get_secret() -> Optional[str]:
secret_needed = typer.confirm("Use secret for authentication?")
if not secret_needed:
return None

use_existing_secret = typer.confirm("Use existing secret?")
if use_existing_secret:
existing_secret = typer.prompt("Secret identifier")
return existing_secret

cli_console.step("Creating new secret")
secret_name = f"{repository_name}_secret"
username = typer.prompt("username")
password = typer.prompt("password/token", hide_input=True)
manager.create_secret(username=username, password=password, name=secret_name)
cli_console.step(f"Secret '{secret_name}' successfully created")
return secret_name
Copy link
Contributor

Choose a reason for hiding this comment

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

Inline the prompts and confirm into command. It will be easier to follow the command flow. Currently I find it pretty hard to grasp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


def _get_api_integration(secret: Optional[str], url: str) -> str:
use_existing_api = typer.confirm("Use existing api integration?")
if use_existing_api:
api_name = typer.prompt("API integration identifier")
return api_name

api_name = f"{repository_name}_api_integration"
manager.create_api_integration(name=api_name, allowed_prefix=url, secret=secret)
cli_console.step(f"API integration '{api_name}' successfully created.")
return api_name

_assure_repository_does_not_exist()
url = typer.prompt("Origin url")
secret = _get_secret()
api_integration = _get_api_integration(secret=secret, url=url)
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
35 changes: 35 additions & 0 deletions src/snowflake/cli/plugins/git/manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from snowflake.cli.plugins.object.stage.manager import StageManager
from snowflake.connector.cursor import SnowflakeCursor

Expand All @@ -18,3 +20,36 @@ 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
Contributor

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
Contributor 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)

def create_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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Move to SQLMixin as those are not specific to git repo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I renamed create_secret -> create_password_secret as different secret types have different arguments

self, name: str, allowed_prefix: str, secret: Optional[str]
) -> SnowflakeCursor:
query = (
f"create api integration {name}"
f" api_provider = git_https_api"
f" api_allowed_prefixes = ('{allowed_prefix}')"
f" allowed_authentication_secrets = ({secret if secret else ''})"
f" enabled = true"
)
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