Skip to content

Commit

Permalink
Move PathMapping and ProcessorMapping out of native app logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-bdufour committed Nov 25, 2024
1 parent 4f72231 commit 22427a5
Show file tree
Hide file tree
Showing 26 changed files with 103 additions and 131 deletions.
2 changes: 1 addition & 1 deletion src/snowflake/cli/_plugins/nativeapp/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from snowflake.cli.api.artifacts.utils import symlink_or_copy
from snowflake.cli.api.cli_global_context import span
from snowflake.cli.api.constants import DEFAULT_SIZE_LIMIT_MB
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.schemas.entities.common import PathMapping
from snowflake.cli.api.project.util import to_identifier
from snowflake.cli.api.secure_path import SecurePath
from snowflake.cli.api.utils.path_utils import delete
Expand Down
2 changes: 1 addition & 1 deletion src/snowflake/cli/_plugins/nativeapp/bundle_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
List,
)

from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.schemas.entities.common import PathMapping


@dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from click import ClickException
from snowflake.cli._plugins.nativeapp.bundle_context import BundleContext
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import (
from snowflake.cli.api.project.schemas.entities.common import (
PathMapping,
ProcessorMapping,
)
Expand Down
4 changes: 1 addition & 3 deletions src/snowflake/cli/_plugins/nativeapp/codegen/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
from snowflake.cli.api.cli_global_context import get_cli_context
from snowflake.cli.api.console import cli_console as cc
from snowflake.cli.api.metrics import CLICounterField
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import (
ProcessorMapping,
)
from snowflake.cli.api.project.schemas.entities.common import ProcessorMapping

SNOWPARK_PROCESSOR = "snowpark"
NA_SETUP_PROCESSOR = "native app setup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from snowflake.cli._plugins.stage.diff import to_stage_path
from snowflake.cli.api.artifacts.bundle_map import BundleMap
from snowflake.cli.api.console import cli_console as cc
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import (
from snowflake.cli.api.project.schemas.entities.common import (
PathMapping,
ProcessorMapping,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from snowflake.cli.api.cli_global_context import get_cli_context, span
from snowflake.cli.api.console import cli_console as cc
from snowflake.cli.api.metrics import CLICounterField
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import (
from snowflake.cli.api.project.schemas.entities.common import (
PathMapping,
ProcessorMapping,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from snowflake.cli.api.cli_global_context import get_cli_context, span
from snowflake.cli.api.console import cli_console as cc
from snowflake.cli.api.metrics import CLICounterField
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import (
from snowflake.cli.api.project.schemas.entities.common import (
PathMapping,
ProcessorMapping,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@
)
from snowflake.cli.api.errno import DOES_NOT_EXIST_OR_NOT_AUTHORIZED
from snowflake.cli.api.exceptions import SnowflakeSQLExecutionError
from snowflake.cli.api.project.schemas.commons import Artifacts
from snowflake.cli.api.project.schemas.entities.common import (
EntityModelBase,
ArtifactsBaseModel,
Identifier,
PostDeployHook,
)
Expand All @@ -75,7 +74,6 @@
IdentifierField,
)
from snowflake.cli.api.project.schemas.v1.native_app.package import DistributionOptions
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.util import (
SCHEMA_AND_NAME,
append_test_resource_suffix,
Expand All @@ -89,19 +87,12 @@
from snowflake.connector.cursor import SnowflakeCursor


class ApplicationPackageEntityModel(EntityModelBase):
class ApplicationPackageEntityModel(ArtifactsBaseModel):
type: Literal["application package"] = DiscriminatorField() # noqa: A003
artifacts: Artifacts = Field(
title="List of paths or file source/destination pairs to add to the deploy root",
)
bundle_root: Optional[str] = Field(
title="Folder at the root of your project where artifacts necessary to perform the bundle step are stored.",
default="output/bundle/",
)
deploy_root: Optional[str] = Field(
title="Folder at the root of your project where the build step copies the artifacts",
default="output/deploy/",
)
generated_root: Optional[str] = Field(
title="Subdirectory of the deploy root where files generated by the Snowflake CLI will be written.",
default="__generated/",
Expand Down Expand Up @@ -136,21 +127,6 @@ def append_test_resource_suffix_to_identifier(
return input_value.model_copy(update=dict(name=with_suffix))
return with_suffix

@field_validator("artifacts")
@classmethod
def transform_artifacts(cls, orig_artifacts: Artifacts) -> List[PathMapping]:
transformed_artifacts: List[PathMapping] = []
if orig_artifacts is None:
return transformed_artifacts

for artifact in orig_artifacts:
if isinstance(artifact, PathMapping):
transformed_artifacts.append(artifact)
else:
transformed_artifacts.append(PathMapping(src=artifact))

return transformed_artifacts

@field_validator("stage")
@classmethod
def validate_source_stage(cls, input_value: str):
Expand Down
2 changes: 1 addition & 1 deletion src/snowflake/cli/_plugins/snowpark/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@
from snowflake.cli.api.project.definition_conversion import (
convert_project_definition_to_v2,
)
from snowflake.cli.api.project.schemas.entities.common import PathMapping
from snowflake.cli.api.project.schemas.project_definition import (
ProjectDefinition,
ProjectDefinitionV2,
)
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.secure_path import SecurePath
from snowflake.connector import DictCursor, ProgrammingError
from snowflake.connector.cursor import SnowflakeCursor
Expand Down
4 changes: 2 additions & 2 deletions src/snowflake/cli/_plugins/snowpark/snowpark_entity_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
from pydantic import Field, field_validator
from snowflake.cli.api.feature_flags import FeatureFlag
from snowflake.cli.api.identifiers import FQN
from snowflake.cli.api.project.schemas.commons import Artifacts
from snowflake.cli.api.project.schemas.entities.common import (
Artifacts,
EntityModelBase,
ExternalAccessBaseModel,
ImportsBaseModel,
PathMapping,
)
from snowflake.cli.api.project.schemas.updatable_model import (
DiscriminatorField,
)
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.schemas.v1.snowpark.argument import Argument


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
from pathlib import Path, PurePosixPath
from typing import Optional

from snowflake.cli._plugins.snowpark.snowpark_entity_model import PathMapping
from snowflake.cli._plugins.snowpark.zipper import zip_dir
from snowflake.cli.api.console import cli_console
from snowflake.cli.api.constants import DEPLOYMENT_STAGE
from snowflake.cli.api.feature_flags import FeatureFlag
from snowflake.cli.api.identifiers import FQN
from snowflake.cli.api.project.project_paths import ProjectPaths
from snowflake.cli.api.project.schemas.entities.common import PathMapping
from snowflake.cli.api.secure_path import SecurePath


Expand Down
2 changes: 1 addition & 1 deletion src/snowflake/cli/_plugins/streamlit/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from snowflake.cli.api.console import cli_console
from snowflake.cli.api.feature_flags import FeatureFlag
from snowflake.cli.api.identifiers import FQN
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.schemas.entities.common import PathMapping
from snowflake.cli.api.sql_execution import SqlExecutionMixin
from snowflake.connector.cursor import SnowflakeCursor
from snowflake.connector.errors import ProgrammingError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
from typing import Literal, Optional

from pydantic import Field, field_validator
from snowflake.cli.api.project.schemas.commons import Artifacts
from snowflake.cli.api.project.schemas.entities.common import (
Artifacts,
EntityModelBase,
ExternalAccessBaseModel,
ImportsBaseModel,
PathMapping,
)
from snowflake.cli.api.project.schemas.updatable_model import (
DiscriminatorField,
)
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping


class StreamlitEntityModel(EntityModelBase, ExternalAccessBaseModel, ImportsBaseModel):
Expand Down
2 changes: 1 addition & 1 deletion src/snowflake/cli/api/artifacts/bundle_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
SourceNotFoundError,
TooManyFilesError,
)
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.schemas.entities.common import PathMapping
from snowflake.cli.api.utils.path_utils import resolve_without_follow

ArtifactPredicate = Callable[[Path, Path], bool]
Expand Down
5 changes: 0 additions & 5 deletions src/snowflake/cli/api/project/schemas/commons.py

This file was deleted.

75 changes: 74 additions & 1 deletion src/snowflake/cli/api/project/schemas/entities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from __future__ import annotations

from abc import ABC
from typing import Dict, Generic, List, Optional, TypeVar, Union
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union

from pydantic import Field, PrivateAttr, field_validator
from snowflake.cli.api.identifiers import FQN
Expand Down Expand Up @@ -162,3 +162,76 @@ def get_secrets_sql(self) -> str | None:
return None
secrets = ", ".join(f"'{key}'={value}" for key, value in self.secrets.items())
return f"secrets=({secrets})"


class ProcessorMapping(UpdatableModel):
name: str = Field(
title="Name of a processor to invoke on a collection of artifacts."
)
properties: Optional[Dict[str, Any]] = Field(
title="A set of key-value pairs used to configure the output of the processor. Consult a specific processor's documentation for more details on the supported properties.",
default=None,
)


class PathMapping(UpdatableModel):
src: str = Field(
title="Source path or glob pattern (relative to project root)", default=None
)

dest: Optional[str] = Field(
title="Destination path on stage",
description="Paths are relative to stage root; paths ending with a slash indicate that the destination is a directory which source files should be copied into.",
default=None,
)

processors: Optional[List[Union[str, ProcessorMapping]]] = Field(
title="List of processors to apply to matching source files during bundling.",
default=[],
)

@field_validator("processors")
@classmethod
def transform_processors(
cls, input_values: Optional[List[Union[str, Dict, ProcessorMapping]]]
) -> List[ProcessorMapping]:
if input_values is None:
return []

transformed_processors: List[ProcessorMapping] = []
for input_processor in input_values:
if isinstance(input_processor, str):
transformed_processors.append(ProcessorMapping(name=input_processor))
elif isinstance(input_processor, Dict):
transformed_processors.append(ProcessorMapping(**input_processor))
else:
transformed_processors.append(input_processor)
return transformed_processors


Artifacts = List[Union[PathMapping, str]]


class ArtifactsBaseModel(EntityModelBase):
artifacts: Artifacts = Field(
title="List of paths or file source/destination pairs to add to the deploy root",
)
deploy_root: Optional[str] = Field(
title="Folder at the root of your project where the build step copies the artifacts",
default="output/deploy/",
)

@field_validator("artifacts")
@classmethod
def transform_artifacts(cls, orig_artifacts: Artifacts) -> List[PathMapping]:
transformed_artifacts: List[PathMapping] = []
if orig_artifacts is None:
return transformed_artifacts

for artifact in orig_artifacts:
if isinstance(artifact, PathMapping):
transformed_artifacts.append(artifact)
else:
transformed_artifacts.append(PathMapping(src=artifact))

return transformed_artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
from typing import List, Optional

from pydantic import Field, field_validator
from snowflake.cli.api.project.schemas.commons import Artifacts
from snowflake.cli.api.project.schemas.entities.common import Artifacts, PathMapping
from snowflake.cli.api.project.schemas.updatable_model import UpdatableModel
from snowflake.cli.api.project.schemas.v1.native_app.application import (
Application,
ApplicationV11,
)
from snowflake.cli.api.project.schemas.v1.native_app.package import Package, PackageV11
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.util import (
SCHEMA_AND_NAME,
)
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion tests/api/artifacts/test_bundle_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
SourceNotFoundError,
TooManyFilesError,
)
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.project.schemas.entities.common import PathMapping
from snowflake.cli.api.utils.path_utils import resolve_without_follow

from tests.nativeapp.utils import touch
Expand Down
Loading

0 comments on commit 22427a5

Please sign in to comment.