Skip to content

Commit

Permalink
SNOW-1706990 Convert snow app version commands to v2-native (#1708)
Browse files Browse the repository at this point in the history
Converts `snow app version` commands to operate on v2 entities by default. Also removes `@nativeapp_definition_v2_to_v1` decorator and `test_v2_to_v1.py` since they're now unused.
  • Loading branch information
sfc-gh-fcampbell authored Oct 11, 2024
1 parent 0dbb551 commit 9232ed6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 494 deletions.
4 changes: 2 additions & 2 deletions src/snowflake/cli/_plugins/nativeapp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def app_open(

@app.command("teardown", requires_connection=True)
@with_project_definition()
# This command doesn't use @nativeapp_definition_v2_to_v1 because it needs to
# This command doesn't use @single_app_and_package because it needs to
# be aware of PDFv2 definitions that have multiple apps created from the same package,
# which all need to be torn down.
def app_teardown(
Expand All @@ -233,7 +233,7 @@ def app_teardown(
show_default=False,
),
interactive: bool = InteractiveOption,
# Same as the param auto-added by @nativeapp_definition_v2_to_v1
# Same as the param auto-added by @single_app_and_package
package_entity_id: Optional[str] = typer.Option(
default="",
help="The ID of the package entity on which to operate when definition_version is 2 or higher.",
Expand Down
33 changes: 0 additions & 33 deletions src/snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,39 +248,6 @@ def find_entity(
return entity


def nativeapp_definition_v2_to_v1(*, app_required: bool = False):
"""
A command decorator that attempts to automatically convert a native app project from
definition v2 to v1.1. Assumes with_project_definition() has already been called.
The definition object in CliGlobalContext will be replaced with the converted object.
Exactly one application package entity type is expected, and up to one application
entity type is expected.
"""

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
original_pdf: Optional[DefinitionV20] = get_cli_context().project_definition
if not original_pdf:
raise ValueError(
"Project definition could not be found. The nativeapp_definition_v2_to_v1 command decorator assumes with_project_definition() was called before it."
)
if original_pdf.definition_version == "2":
package_entity_id = kwargs.get("package_entity_id", "")
app_entity_id = kwargs.get("app_entity_id", "")
pdfv1 = _pdf_v2_to_v1(
original_pdf, package_entity_id, app_entity_id, app_required
)
get_cli_context_manager().override_project_definition = pdfv1
return func(*args, **kwargs)

return _options_decorator_factory(
wrapper, additional_options=APP_AND_PACKAGE_OPTIONS
)

return decorator


def single_app_and_package(*, app_required: bool = False):
"""
A command decorator that attempts to extract a single application package and up to one
Expand Down
57 changes: 29 additions & 28 deletions src/snowflake/cli/_plugins/nativeapp/version/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@
import typer
from click import MissingParameter
from snowflake.cli._plugins.nativeapp.common_flags import ForceOption, InteractiveOption
from snowflake.cli._plugins.nativeapp.run_processor import NativeAppRunProcessor
from snowflake.cli._plugins.nativeapp.v2_conversions.compat import (
nativeapp_definition_v2_to_v1,
)
from snowflake.cli._plugins.nativeapp.version.version_processor import (
NativeAppVersionCreateProcessor,
NativeAppVersionDropProcessor,
single_app_and_package,
)
from snowflake.cli._plugins.workspace.manager import WorkspaceManager
from snowflake.cli.api.cli_global_context import get_cli_context
from snowflake.cli.api.commands.decorators import (
with_project_definition,
)
from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
from snowflake.cli.api.entities.common import EntityActions
from snowflake.cli.api.output.types import CommandResult, MessageResult, QueryResult
from snowflake.cli.api.project.project_verification import assert_project_type

app = SnowTyperFactory(
name="version",
Expand All @@ -46,7 +42,7 @@

@app.command(requires_connection=True)
@with_project_definition()
@nativeapp_definition_v2_to_v1()
@single_app_and_package()
def create(
version: Optional[str] = typer.Argument(
None,
Expand All @@ -71,18 +67,18 @@ def create(
"""
Adds a new patch to the provided version defined in your application package. If the version does not exist, creates a version with patch 0.
"""

assert_project_type("native_app")

if version is None and patch is not None:
raise MissingParameter("Cannot provide a patch without version!")

cli_context = get_cli_context()
processor = NativeAppVersionCreateProcessor(
project_definition=cli_context.project_definition.native_app,
ws = WorkspaceManager(
project_definition=cli_context.project_definition,
project_root=cli_context.project_root,
)
processor.process(
package_id = options["package_entity_id"]
ws.perform_action(
package_id,
EntityActions.VERSION_CREATE,
version=version,
patch=patch,
force=force,
Expand All @@ -94,28 +90,29 @@ def create(

@app.command("list", requires_connection=True)
@with_project_definition()
@nativeapp_definition_v2_to_v1()
@single_app_and_package()
def version_list(
**options,
) -> CommandResult:
"""
Lists all versions defined in an application package.
"""

assert_project_type("native_app")

cli_context = get_cli_context()
processor = NativeAppRunProcessor(
project_definition=cli_context.project_definition.native_app,
ws = WorkspaceManager(
project_definition=cli_context.project_definition,
project_root=cli_context.project_root,
)
cursor = processor.get_all_existing_versions()
package_id = options["package_entity_id"]
cursor = ws.perform_action(
package_id,
EntityActions.VERSION_LIST,
)
return QueryResult(cursor)


@app.command(requires_connection=True)
@with_project_definition()
@nativeapp_definition_v2_to_v1()
@single_app_and_package()
def drop(
version: Optional[str] = typer.Argument(
None,
Expand All @@ -129,13 +126,17 @@ def drop(
Drops a version defined in your application package. Versions can either be passed in as an argument to the command or read from the `manifest.yml` file.
Dropping patches is not allowed.
"""

assert_project_type("native_app")

cli_context = get_cli_context()
processor = NativeAppVersionDropProcessor(
project_definition=cli_context.project_definition.native_app,
ws = WorkspaceManager(
project_definition=cli_context.project_definition,
project_root=cli_context.project_root,
)
processor.process(version, force, interactive)
package_id = options["package_entity_id"]
ws.perform_action(
package_id,
EntityActions.VERSION_DROP,
version=version,
interactive=interactive,
force=force,
)
return MessageResult(f"Version drop is now complete.")
Loading

0 comments on commit 9232ed6

Please sign in to comment.