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

Allow blocks in deployment params #14741

Merged
merged 17 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
37 changes: 37 additions & 0 deletions src/prefect/blocks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import html
import inspect
import sys
import uuid
import warnings
from abc import ABC
from functools import partial
Expand Down Expand Up @@ -272,6 +273,15 @@ class Block(BaseModel, ABC):
)

def __init__(self, *args, **kwargs):
block_document_id = kwargs.pop("$ref", None)
if block_document_id:
block_document, block_document_name = self._get_block_document_by_id(
GalLadislav marked this conversation as resolved.
Show resolved Hide resolved
block_document_id
)
kwargs = {
**block_document.data,
**kwargs,
}
super().__init__(*args, **kwargs)
self.block_initialization()

Expand Down Expand Up @@ -790,6 +800,33 @@ async def _get_block_document(

return block_document, block_document_name

@classmethod
@sync_compatible
@inject_client
async def _get_block_document_by_id(
cls,
block_document_id: Union[str, uuid.UUID],
client: Optional["PrefectClient"] = None,
):
if isinstance(block_document_id, str):
try:
block_document_id = UUID(block_document_id)
except ValueError as e:
raise ValueError(
f"Block document ID {block_document_id!r} is not a valid UUID"
) from e
GalLadislav marked this conversation as resolved.
Show resolved Hide resolved

try:
block_document = await client.read_block_document(
block_document_id=block_document_id
)
except prefect.exceptions.ObjectNotFound as e:
raise ValueError(
f"Unable to find block document with ID {block_document_id!r}"
) from e
GalLadislav marked this conversation as resolved.
Show resolved Hide resolved

return block_document, block_document.name

@classmethod
@sync_compatible
@inject_client
Expand Down
19 changes: 19 additions & 0 deletions src/prefect/utilities/schema_tools/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,24 @@ def preprocess_schema(
process_properties(
definition["properties"], required_fields, allow_none_with_default
)
# Allow block types to be referenced by their id
if "block_type_slug" in definition:
schema["definitions"][definition["title"]] = {
"oneOf": [
definition,
{
"type": "object",
"properties": {
"$ref": {
"type": "string",
"format": "uuid",
},
},
"required": [
"$ref",
],
},
]
}
desertaxle marked this conversation as resolved.
Show resolved Hide resolved

return schema
Loading