-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update the list of supported entrypoint parameter types
This update adds support for the "boolean", "integer", "list", and "mapping" entrypoint parameter types that are already supported by the task execution engine. In addition, the /workflows/jobFilesDownload endpoint service has been updated to handle all supported types when creating the parameters.json and task engine YAML files. The "path" and "uri" types have been removed since they were treated the same as strings.
- Loading branch information
1 parent
d85d677
commit ae3ab68
Showing
9 changed files
with
289 additions
and
29 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
...optra/restapi/db/alembic/versions/6a75ede23821_update_entry_point_parameter_types_list.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
"""Update entry point parameter types list | ||
Revision ID: 6a75ede23821 | ||
Revises: 4b2d781f8bb4 | ||
Create Date: 2024-09-10 16:32:40.707231 | ||
""" | ||
|
||
from typing import Annotated, Optional | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.orm import ( | ||
DeclarativeBase, | ||
Mapped, | ||
MappedAsDataclass, | ||
mapped_column, | ||
sessionmaker, | ||
) | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6a75ede23821" | ||
down_revision = "4b2d781f8bb4" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
# Upgrade and downgrade inserts and deletes | ||
UPGRADE_INSERTS = ["boolean", "integer", "list", "mapping"] | ||
UPGRADE_DELETES = ["path", "uri"] | ||
DOWNGRADE_INSERTS = ["path", "uri"] | ||
|
||
|
||
# Migration data models | ||
intpk = Annotated[ | ||
int, | ||
mapped_column(sa.BigInteger().with_variant(sa.Integer, "sqlite"), primary_key=True), | ||
] | ||
text_ = Annotated[str, mapped_column(sa.Text())] | ||
bool_ = Annotated[bool, mapped_column(sa.Boolean())] | ||
optionalstr = Annotated[Optional[str], mapped_column(sa.Text(), nullable=True)] | ||
|
||
|
||
class UpgradeBase(DeclarativeBase, MappedAsDataclass): | ||
pass | ||
|
||
|
||
class DowngradeBase(DeclarativeBase, MappedAsDataclass): | ||
pass | ||
|
||
|
||
class EntryPointParameterTypeUpgrade(UpgradeBase): | ||
__tablename__ = "entry_point_parameter_types" | ||
|
||
parameter_type: Mapped[text_] = mapped_column(primary_key=True) | ||
|
||
|
||
class EntryPointParameterUpgrade(UpgradeBase): | ||
__tablename__ = "entry_point_parameters" | ||
|
||
entry_point_resource_snapshot_id: Mapped[intpk] = mapped_column(init=False) | ||
parameter_number: Mapped[intpk] | ||
parameter_type: Mapped[text_] = mapped_column(nullable=False) | ||
name: Mapped[text_] = mapped_column(nullable=False) | ||
default_value: Mapped[optionalstr] | ||
|
||
|
||
class EntryPointParameterTypeDowngrade(DowngradeBase): | ||
__tablename__ = "entry_point_parameter_types" | ||
|
||
parameter_type: Mapped[text_] = mapped_column(primary_key=True) | ||
|
||
|
||
def upgrade(): | ||
bind = op.get_bind() | ||
Session = sessionmaker(bind=bind) | ||
|
||
with Session() as session: | ||
for parameter_type in UPGRADE_INSERTS: | ||
stmt = sa.select(EntryPointParameterTypeUpgrade).where( | ||
EntryPointParameterTypeUpgrade.parameter_type == parameter_type | ||
) | ||
|
||
if session.scalar(stmt) is None: | ||
session.add( | ||
EntryPointParameterTypeUpgrade(parameter_type=parameter_type) | ||
) | ||
|
||
# Search for any parameters that are of type "path" or "uri" and convert them to | ||
# "string" | ||
to_string_params_stmt = sa.select(EntryPointParameterUpgrade).where( | ||
EntryPointParameterUpgrade.parameter_type.in_(["path", "uri"]) | ||
) | ||
|
||
for entry_point_parameter in session.execute(to_string_params_stmt): | ||
entry_point_parameter.parameter_type = "string" | ||
|
||
for parameter_type in UPGRADE_DELETES: | ||
stmt = sa.select(EntryPointParameterTypeUpgrade).where( | ||
EntryPointParameterTypeUpgrade.parameter_type == parameter_type | ||
) | ||
entry_point_parameter_type = session.scalar(stmt) | ||
|
||
if entry_point_parameter_type is not None: | ||
session.delete(entry_point_parameter_type) | ||
|
||
session.commit() | ||
|
||
|
||
def downgrade(): | ||
bind = op.get_bind() | ||
Session = sessionmaker(bind=bind) | ||
|
||
with Session() as session: | ||
for parameter_type in DOWNGRADE_INSERTS: | ||
stmt = sa.select(EntryPointParameterTypeDowngrade).where( | ||
EntryPointParameterTypeDowngrade.parameter_type == parameter_type | ||
) | ||
|
||
if session.scalar(stmt) is None: | ||
session.add( | ||
EntryPointParameterTypeDowngrade(parameter_type=parameter_type) | ||
) | ||
|
||
session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# This Software (Dioptra) is being made available as a public service by the | ||
# National Institute of Standards and Technology (NIST), an Agency of the United | ||
# States Department of Commerce. This software was developed in part by employees of | ||
# NIST and in part by NIST contractors. Copyright in portions of this software that | ||
# were developed by NIST contractors has been licensed or assigned to NIST. Pursuant | ||
# to Title 17 United States Code Section 105, works of NIST employees are not | ||
# subject to copyright protection in the United States. However, NIST may hold | ||
# international copyright in software created by its employees and domestic | ||
# copyright (or licensing rights) in portions of software that were assigned or | ||
# licensed to NIST. To the extent that NIST holds copyright in this software, it is | ||
# being made available under the Creative Commons Attribution 4.0 International | ||
# license (CC BY 4.0). The disclaimers of the CC BY 4.0 license apply to all parts | ||
# of the software developed or licensed by NIST. | ||
# | ||
# ACCESS THE FULL CC BY 4.0 LICENSE HERE: | ||
# https://creativecommons.org/licenses/by/4.0/legalcode | ||
import json | ||
from typing import Any, Final, cast | ||
|
||
JsonType = dict[str, Any] | list[Any] | ||
GlobalParameterType = str | float | int | bool | dict[str, Any] | list[Any] | None | ||
|
||
|
||
BOOLEAN_PARAM_TYPE: Final[str] = "boolean" | ||
FLOAT_PARAM_TYPE: Final[str] = "float" | ||
INTEGER_PARAM_TYPE: Final[str] = "integer" | ||
LIST_PARAM_TYPE: Final[str] = "list" | ||
MAPPING_PARAM_TYPE: Final[str] = "mapping" | ||
STRING_PARAM_TYPE: Final[str] = "string" | ||
|
||
|
||
def coerce_to_type(x: str | None, type_name: str) -> GlobalParameterType: | ||
coerce_fn_registry = { | ||
BOOLEAN_PARAM_TYPE: to_boolean_type, | ||
FLOAT_PARAM_TYPE: to_float_type, | ||
INTEGER_PARAM_TYPE: to_integer_type, | ||
LIST_PARAM_TYPE: to_list_type, | ||
MAPPING_PARAM_TYPE: to_mapping_type, | ||
STRING_PARAM_TYPE: to_string_type, | ||
} | ||
|
||
if type_name not in coerce_fn_registry: | ||
raise ValueError(f"Invalid parameter type: {type_name}.") | ||
|
||
if x is None: | ||
return None | ||
|
||
coerce_fn = coerce_fn_registry[type_name] | ||
return cast(GlobalParameterType, coerce_fn(x)) | ||
|
||
|
||
def to_string_type(x: str) -> str: | ||
return x | ||
|
||
|
||
def to_boolean_type(x: str) -> bool: | ||
if x.lower() not in {"true", "false"}: | ||
raise ValueError(f"Not a boolean: {x}") | ||
|
||
return x.lower() == "true" | ||
|
||
|
||
def to_float_type(x: str) -> float: | ||
# TODO: Handle coercion failures | ||
return float(x) | ||
|
||
|
||
def to_integer_type(x: str) -> int: | ||
# TODO: Handle coercion failures | ||
return int(x) | ||
|
||
|
||
def to_list_type(x: str) -> list[Any]: | ||
# TODO: Handle coercion failures | ||
x_coerced = cast(JsonType, json.loads(x)) | ||
|
||
if not isinstance(x_coerced, list): | ||
raise ValueError(f"Not a list: {x}") | ||
|
||
return x_coerced | ||
|
||
|
||
def to_mapping_type(x: str) -> dict[str, Any]: | ||
# TODO: Handle coercion failures | ||
x_coerced = cast(JsonType, json.loads(x)) | ||
|
||
if not isinstance(x_coerced, dict): | ||
raise ValueError(f"Not a mapping: {x}") | ||
|
||
return x_coerced |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters