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-1706990 Extract fields in compat module #1663

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 73 additions & 59 deletions src/snowflake/cli/_plugins/nativeapp/v2_conversions/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@
from snowflake.cli.api.project.schemas.v1.native_app.path_mapping import PathMapping
from snowflake.cli.api.utils.definition_rendering import render_definition_template

APP_AND_PACKAGE_OPTIONS = [
inspect.Parameter(
"package_entity_id",
inspect.Parameter.KEYWORD_ONLY,
annotation=Optional[str],
default=typer.Option(
default="",
help="The ID of the package entity on which to operate when definition_version is 2 or higher.",
),
),
inspect.Parameter(
"app_entity_id",
inspect.Parameter.KEYWORD_ONLY,
annotation=Optional[str],
default=typer.Option(
default="",
help="The ID of the application entity on which to operate when definition_version is 2 or higher.",
),
),
]


def _convert_v2_artifact_to_v1_dict(
v2_artifact: Union[PathMapping, str]
Expand All @@ -58,45 +79,12 @@ def _pdf_v2_to_v1(
) -> DefinitionV11:
pdfv1: Dict[str, Any] = {"definition_version": "1.1", "native_app": {}}

# Determine the application entity to convert, there can be zero or one
app_definition = find_entity(
v2_definition,
ApplicationEntityModel,
app_entity_id,
disambiguation_option="--app-entity-id",
required=app_required,
)

# Infer or verify the package if we have an app entity to convert
if app_definition:
target_package = app_definition.from_.target
if package_entity_id:
# If the user specified a package entity ID,
# check that the app entity targets the user-specified package entity
# if the app entity is used by the command being run
if target_package != package_entity_id and app_required:
raise ClickException(
f"The application entity {app_definition.entity_id} does not "
f"target the application package entity {package_entity_id}. Either"
f"use --package-entity-id {target_package} to target the correct package entity, "
f"or omit the --package-entity-id flag to automatically use the package entity "
f"that the application entity targets."
)
elif target_package in v2_definition.get_entities_by_type(
ApplicationPackageEntityModel.get_type()
):
# If the user didn't target a specific package entity, use the one the app entity targets
package_entity_id = target_package

# Determine the package entity to convert, there must be one
app_package_definition = find_entity(
v2_definition,
ApplicationPackageEntityModel,
package_entity_id,
disambiguation_option="--package-entity-id",
required=True,
app_definition, app_package_definition = _find_app_and_package_entities(
sfc-gh-bdufour marked this conversation as resolved.
Show resolved Hide resolved
v2_definition=v2_definition,
package_entity_id=package_entity_id,
app_entity_id=app_entity_id,
app_required=app_required,
)
assert app_package_definition is not None # satisfy mypy

# NativeApp
if app_definition and app_definition.fqn.identifier:
Expand Down Expand Up @@ -156,6 +144,52 @@ def _pdf_v2_to_v1(
return result.project_definition


def _find_app_and_package_entities(
v2_definition: DefinitionV20,
package_entity_id: str,
app_entity_id: str,
app_required: bool,
):
# Determine the application entity to convert, there can be zero or one
app_definition = find_entity(
v2_definition,
ApplicationEntityModel,
app_entity_id,
disambiguation_option="--app-entity-id",
required=app_required,
)
# Infer or verify the package if we have an app entity to convert
if app_definition:
target_package = app_definition.from_.target
if package_entity_id:
# If the user specified a package entity ID,
# check that the app entity targets the user-specified package entity
# if the app entity is used by the command being run
if target_package != package_entity_id and app_required:
raise ClickException(
f"The application entity {app_definition.entity_id} does not "
f"target the application package entity {package_entity_id}. Either"
f"use --package-entity-id {target_package} to target the correct package entity, "
f"or omit the --package-entity-id flag to automatically use the package entity "
f"that the application entity targets."
)
elif target_package in v2_definition.get_entities_by_type(
ApplicationPackageEntityModel.get_type()
):
# If the user didn't target a specific package entity, use the one the app entity targets
package_entity_id = target_package
# Determine the package entity to convert, there must be one
app_package_definition = find_entity(
v2_definition,
ApplicationPackageEntityModel,
package_entity_id,
disambiguation_option="--package-entity-id",
required=True,
)
assert app_package_definition is not None # satisfy mypy
return app_definition, app_package_definition


T = TypeVar("T", bound=EntityModelBase)


Expand Down Expand Up @@ -236,27 +270,7 @@ def wrapper(*args, **kwargs):
return func(*args, **kwargs)

return _options_decorator_factory(
wrapper,
additional_options=[
inspect.Parameter(
"package_entity_id",
inspect.Parameter.KEYWORD_ONLY,
annotation=Optional[str],
default=typer.Option(
default="",
help="The ID of the package entity on which to operate when definition_version is 2 or higher.",
),
),
inspect.Parameter(
"app_entity_id",
inspect.Parameter.KEYWORD_ONLY,
annotation=Optional[str],
default=typer.Option(
default="",
help="The ID of the application entity on which to operate when definition_version is 2 or higher.",
),
),
],
wrapper, additional_options=APP_AND_PACKAGE_OPTIONS
)

return decorator
Loading