Skip to content

Commit

Permalink
Support project definition v2 in "snow app" commands: bundle, validat…
Browse files Browse the repository at this point in the history
…e, deploy (#1332)

* support v2 in "snow app bundle"

* refactor deploy_root

* update unit test

* Support project definition v2 in "app validate" and "app deploy" (#1339)

support v2 in validate and deploy

* remove pushd
  • Loading branch information
sfc-gh-gbloom committed Jul 18, 2024
1 parent 4744696 commit 59905dc
Show file tree
Hide file tree
Showing 24 changed files with 486 additions and 400 deletions.
3 changes: 3 additions & 0 deletions src/snowflake/cli/plugins/nativeapp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def app_list_templates(**options) -> CommandResult:

@app.command("bundle")
@with_project_definition()
@nativeapp_definition_v2_to_v1
def app_bundle(
**options,
) -> CommandResult:
Expand Down Expand Up @@ -284,6 +285,7 @@ def app_teardown(

@app.command("deploy", requires_connection=True)
@with_project_definition()
@nativeapp_definition_v2_to_v1
def app_deploy(
prune: Optional[bool] = typer.Option(
default=None,
Expand Down Expand Up @@ -350,6 +352,7 @@ def app_deploy(

@app.command("validate", requires_connection=True)
@with_project_definition()
@nativeapp_definition_v2_to_v1
def app_validate(**options):
"""
Validates a deployed Snowflake Native App's setup script.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from __future__ import annotations

from functools import wraps
from typing import Any, Dict, Optional
from pathlib import Path
from typing import Any, Dict, Optional, Union

from click import ClickException
from snowflake.cli.api.cli_global_context import cli_context, cli_context_manager
Expand All @@ -25,12 +26,25 @@
from snowflake.cli.api.project.schemas.entities.application_package_entity import (
ApplicationPackageEntity,
)
from snowflake.cli.api.project.schemas.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.schemas.project_definition import (
DefinitionV11,
DefinitionV20,
)


def _convert_v2_artifact_to_v1_dict(
v2_artifact: Union[PathMapping, Path]
) -> Union[Dict, str]:
if isinstance(v2_artifact, PathMapping):
return {
"src": v2_artifact.src,
"dest": v2_artifact.dest,
"processors": v2_artifact.processors,
}
return str(v2_artifact)


def _pdf_v2_to_v1(v2_definition: DefinitionV20) -> DefinitionV11:
pdfv1: Dict[str, Any] = {"definition_version": "1.1", "native_app": {}}

Expand All @@ -57,8 +71,13 @@ def _pdf_v2_to_v1(v2_definition: DefinitionV20) -> DefinitionV11:

# NativeApp
pdfv1["native_app"]["name"] = "Auto converted NativeApp project from V2"
pdfv1["native_app"]["artifacts"] = app_package_definition.artifacts
pdfv1["native_app"]["artifacts"] = [
_convert_v2_artifact_to_v1_dict(a) for a in app_package_definition.artifacts
]
pdfv1["native_app"]["source_stage"] = app_package_definition.stage
pdfv1["native_app"]["bundle_root"] = str(app_package_definition.bundle_root)
pdfv1["native_app"]["generated_root"] = str(app_package_definition.generated_root)
pdfv1["native_app"]["deploy_root"] = str(app_package_definition.deploy_root)

# Package
pdfv1["native_app"]["package"] = {}
Expand Down
6 changes: 6 additions & 0 deletions tests/nativeapp/test_v2_to_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
"artifacts": [{"src": "app/*", "dest": "./"}],
"manifest": "",
"stage": "app.stage",
"bundle_root": "bundle_root",
"generated_root": "generated_root",
"deploy_root": "deploy_root",
},
"app": {
"type": "application",
Expand All @@ -103,6 +106,9 @@
"name": "Auto converted NativeApp project from V2",
"artifacts": [{"src": "app/*", "dest": "./"}],
"source_stage": "app.stage",
"bundle_root": "bundle_root",
"generated_root": "generated_root",
"deploy_root": "deploy_root",
"package": {
"name": "pkg_name",
},
Expand Down
122 changes: 114 additions & 8 deletions tests_integration/nativeapp/__snapshots__/test_deploy.ambr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# serializer version: 1
# name: test_nativeapp_deploy
# name: test_nativeapp_deploy[v1]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Expand All @@ -14,7 +14,7 @@

'''
# ---
# name: test_nativeapp_deploy_dot
# name: test_nativeapp_deploy[v2]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Expand All @@ -29,20 +29,78 @@

'''
# ---
# name: test_nativeapp_deploy_files
# name: test_nativeapp_deploy_dot[v1]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Local changes to be deployed:
added: app/README.md -> README.md
added: app/manifest.yml -> manifest.yml
added: app/setup_script.sql -> setup_script.sql
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Validating Snowflake Native App setup script.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_dot[v2]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Local changes to be deployed:
added: app/README.md -> README.md
added: app/manifest.yml -> manifest.yml
added: app/setup_script.sql -> setup_script.sql
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Validating Snowflake Native App setup script.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_files[v1]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Local changes to be deployed:
added: app/manifest.yml -> manifest.yml
added: app/setup_script.sql -> setup_script.sql
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_files[v2]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Local changes to be deployed:
added: app/manifest.yml -> manifest.yml
added: app/setup_script.sql -> setup_script.sql
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_looks_for_prefix_matches[v1]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Local changes to be deployed:
added: app/README.md -> README.md
added: app/manifest.yml -> manifest.yml
added: app/setup_script.sql -> setup_script.sql
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Validating Snowflake Native App setup script.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_looks_for_prefix_matches
# name: test_nativeapp_deploy_looks_for_prefix_matches[v2]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Expand All @@ -57,7 +115,7 @@

'''
# ---
# name: test_nativeapp_deploy_nested_directories
# name: test_nativeapp_deploy_nested_directories[v1]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Expand All @@ -69,7 +127,55 @@

'''
# ---
# name: test_nativeapp_deploy_prune[app deploy --no-prune-contains2-not_contains2]
# name: test_nativeapp_deploy_nested_directories[v2]
'''
Creating new application package myapp_pkg_@@USER@@ in account.
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Local changes to be deployed:
added: app/nested/dir/file.txt -> nested/dir/file.txt
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_prune[v1-app deploy --no-prune-contains2-not_contains2]
'''
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
The following files exist only on the stage:
README.md

Use the --prune flag to delete them from the stage.
Your stage is up-to-date with your local deploy root.
Validating Snowflake Native App setup script.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_prune[v1-app deploy --no-validate-contains1-not_contains1]
'''
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Deleted paths to be removed from your stage:
deleted: README.md
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_prune[v1-app deploy --prune --no-validate-contains0-not_contains0]
'''
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Deleted paths to be removed from your stage:
deleted: README.md
Updating the Snowflake stage from your local @@DEPLOY_ROOT@@ directory.
Deployed successfully. Application package and stage are up-to-date.

'''
# ---
# name: test_nativeapp_deploy_prune[v2-app deploy --no-prune-contains2-not_contains2]
'''
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Expand All @@ -83,7 +189,7 @@

'''
# ---
# name: test_nativeapp_deploy_prune[app deploy --no-validate-contains1-not_contains1]
# name: test_nativeapp_deploy_prune[v2-app deploy --no-validate-contains1-not_contains1]
'''
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Expand All @@ -94,7 +200,7 @@

'''
# ---
# name: test_nativeapp_deploy_prune[app deploy --prune --no-validate-contains0-not_contains0]
# name: test_nativeapp_deploy_prune[v2-app deploy --prune --no-validate-contains0-not_contains0]
'''
Checking if stage myapp_pkg_@@USER@@.app_src.stage exists, or creating a new one if none exists.
Performing a diff between the Snowflake stage and your local deploy_root ('@@DEPLOY_ROOT@@') directory.
Expand Down
Loading

0 comments on commit 59905dc

Please sign in to comment.