Skip to content

Commit

Permalink
Evaluate templates when converting v1 to v2 in-memory, fix snow app run
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fcampbell committed Oct 1, 2024
1 parent 4c209a1 commit 5654c29
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
28 changes: 12 additions & 16 deletions src/snowflake/cli/_plugins/nativeapp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
)
from snowflake.cli.api.project.project_verification import assert_project_type
from snowflake.cli.api.project.schemas.project_definition import ProjectDefinitionV1
from snowflake.cli.api.project.util import to_identifier
from typing_extensions import Annotated

app = SnowTyperFactory(
Expand Down Expand Up @@ -181,17 +182,6 @@ def app_run(
Creates an application package in your Snowflake account, uploads code files to its stage,
then creates or upgrades an application object from the application package.
"""
assert_project_type("native_app")

is_interactive = False
if force:
policy = AllowAlwaysPolicy()
elif interactive:
is_interactive = True
policy = AskAlwaysPolicy()
else:
policy = DenyAlwaysPolicy()

cli_context = get_cli_context()
ws = WorkspaceManager(
project_definition=cli_context.project_definition,
Expand All @@ -204,15 +194,21 @@ def app_run(
EntityActions.DEPLOY,
validate=validate,
from_release_directive=from_release_directive,
prune=True,
recursive=True,
paths=[],
interactive=interactive,
force=force,
)
snowsight_url = ApplicationEntity.get_snowsight_url(
app.identifier.name,
app.meta.warehouse, # TODO This is broken if None
)

warehouse = None
if app.meta and app.meta.warehouse:
warehouse = to_identifier(app.meta.warehouse)
elif cli_context.connection and cli_context.connection.warehouse:
warehouse = to_identifier(cli_context.connection.warehouse)
snowsight_url = ApplicationEntity.get_snowsight_url(app.fqn.name, warehouse)
return MessageResult(
f"Your application object ({app.identifier.name}) is now available:\n{snowsight_url}"
f"Your application object ({app.fqn.name}) is now available:\n{snowsight_url}"
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ def wrapper(*args, **kwargs):
cli_context.project_root,
original_pdf,
accept_templates=False, # Templates should all be rendered by now
template_context=None, # Force inclusion of all fields
template_context=cli_context.template_context,
evaluate_replacement_templates=True, # This is being used in-memory, so we need to eval any templates generated by the conversion
)
for entity_id, entity in pdfv2.entities.items():
# Backfill kwargs for the command to use,
Expand Down
24 changes: 21 additions & 3 deletions src/snowflake/cli/api/project/definition_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
)
from snowflake.cli.api.project.schemas.v1.snowpark.snowpark import Snowpark
from snowflake.cli.api.project.schemas.v1.streamlit.streamlit import Streamlit
from snowflake.cli.api.rendering.jinja import get_basic_jinja_env
from snowflake.cli.api.rendering.jinja import FUNCTION_KEY, get_basic_jinja_env
from snowflake.cli.api.rendering.sql_templates import (
choose_sql_jinja_env_based_on_template_syntax,
)
from snowflake.cli.api.utils.templating_functions import get_templating_functions

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -69,13 +73,19 @@ def convert_project_definition_to_v2(
pd: ProjectDefinition,
accept_templates: bool = False,
template_context: Optional[Dict[str, Any]] = None,
evaluate_replacement_templates: bool = False,
) -> ProjectDefinitionV2:
_check_if_project_definition_meets_requirements(pd, accept_templates)

snowpark_data = convert_snowpark_to_v2_data(pd.snowpark) if pd.snowpark else {}
streamlit_data = convert_streamlit_to_v2_data(pd.streamlit) if pd.streamlit else {}
native_app_data = (
convert_native_app_to_v2_data(project_root, pd.native_app, template_context)
convert_native_app_to_v2_data(
project_root,
pd.native_app,
template_context,
evaluate_replacement_templates,
)
if pd.native_app
else {}
)
Expand Down Expand Up @@ -199,6 +209,7 @@ def convert_native_app_to_v2_data(
project_root,
native_app: NativeApp,
template_context: Optional[Dict[str, Any]] = None,
evaluate_replacement_templates: bool = False,
) -> Dict[str, Any]:
def _make_meta(obj: Application | Package):
meta = {}
Expand Down Expand Up @@ -244,7 +255,14 @@ def _find_manifest():
return manifest_path.relative_to(project_root).as_posix()

def _make_template(template: str) -> str:
return f"{PROJECT_TEMPLATE_VARIABLE_OPENING} {template} {PROJECT_TEMPLATE_VARIABLE_CLOSING}"
template = f"{PROJECT_TEMPLATE_VARIABLE_OPENING} {template} {PROJECT_TEMPLATE_VARIABLE_CLOSING}"
if evaluate_replacement_templates:
env = choose_sql_jinja_env_based_on_template_syntax(template)
jinja_context = (template_context or {}) | {
FUNCTION_KEY: get_templating_functions()
}
return env.from_string(template).render(jinja_context)
return template

def _convert_package_script_files(package_scripts: list[str]):
# PDFv2 doesn't support package scripts, only post-deploy scripts, so we
Expand Down

0 comments on commit 5654c29

Please sign in to comment.