Skip to content

Commit

Permalink
Add Pydantic models to IIB
Browse files Browse the repository at this point in the history
  • Loading branch information
xDaile committed Dec 7, 2023
1 parent 8254277 commit 7a6a734
Show file tree
Hide file tree
Showing 5 changed files with 699 additions and 1 deletion.
257 changes: 256 additions & 1 deletion iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@

from iib.exceptions import ValidationError
from iib.web import db

from iib.web.pydantic_models import (
AddPydanticModel,
CreateEmptyIndexPydanticModel,
FbcOperationsPydanticModel,
MergeIndexImagePydanticModel,
RecursiveRelatedBundlesPydanticModel,
RegenerateBundlePydanticModel,
RmPydanticModel,
)

from iib.web.iib_static_types import (
AddRequestPayload,
Expand Down Expand Up @@ -1002,6 +1010,20 @@ def _from_json(
db.session.add(batch)
request_kwargs['batch'] = batch

@staticmethod
def from_json_replacement(
request_kwargs: RequestPayload,
batch: Optional[Batch] = None,
):
# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

def get_common_index_image_json(self) -> CommonIndexImageResponseBase:
"""
Return the common set of attributes for an index image request.
Expand Down Expand Up @@ -1177,6 +1199,50 @@ def from_json( # type: ignore[override] # noqa: F821
request.add_state('in_progress', 'The request was initiated')
return request

def from_json_replacement(
cls,
payload: AddPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/add API endpoint.
:param AddPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

# setup user and batch in request
# cls.from_json_replacement(
# cast(RequestPayload, request_kwargs),
# batch=batch,
# )

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch


request_kwargs["bundles"] = [
Image.get_or_create(pull_specification=item) for item in payload.bundles
]
request_kwargs["deprecation_list"] = [
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
]

request = cls(**request_kwargs)

for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request

def to_json(self, verbose: Optional[bool] = True) -> AddRequestResponse:
"""
Provide the JSON representation of an "add" build request.
Expand Down Expand Up @@ -1276,6 +1342,38 @@ def from_json( # type: ignore[override] # noqa: F821

return request

def from_json_replacement(
cls,
payload: RmPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/rm API endpoint.
:param RmPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in payload.operators]
request_kwargs['from_index'] = Image.get_or_create(pull_specification=request_kwargs['from_index'])

if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')

for bt in payload.build_tags:
request.add_build_tag(bt)

return request

def to_json(self, verbose: Optional[bool] = True) -> AddRmRequestResponseBase:
"""
Provide the JSON representation of an "rm" build request.
Expand Down Expand Up @@ -1422,6 +1520,36 @@ def from_json( # type: ignore[override] # noqa: F821
request.add_state('in_progress', 'The request was initiated')
return request

def from_json_replacement(
cls,
payload: RegenerateBundlePydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/egenerate-bundle API endpoint.
:param RegenerateBundlePydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['from_bundle_image'] = Image.get_or_create(
pull_specification=payload.from_bundle_image
)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')
return request

def to_json(self, verbose: Optional[bool] = True) -> RegenerateBundleRequestResponse:
"""
Provide the JSON representation of a "regenerate-bundle" build request.
Expand Down Expand Up @@ -1624,6 +1752,45 @@ def from_json( # type: ignore[override] # noqa: F821
request.add_state('in_progress', 'The request was initiated')
return request

def from_json_replacement(
cls,
payload: MergeIndexImagePydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/merge-index-image API endpoint.
:param MergeIndexImagePydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['deprecation_list'] = [
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
]
request_kwargs['source_from_index'] = Image.get_or_create(
pull_specification=payload.source_from_index
)
request_kwargs['target_index'] = Image.get_or_create(pull_specification=payload.target_index)
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)

for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request

def to_json(self, verbose: Optional[bool] = True) -> MergeIndexImageRequestResponse:
"""
Provide the JSON representation of an "merge-index-image" build request.
Expand Down Expand Up @@ -1896,6 +2063,36 @@ def from_json( # type: ignore[override] # noqa: F821

return request

def from_json_replacement(
cls,
payload: CreateEmptyIndexPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/create-empty-index API endpoint.
:param CreateEmptyIndexPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')

return request

def to_json(self, verbose: Optional[bool] = True) -> CreateEmptyIndexRequestResponse:
"""
Provide the JSON representation of an "create-empty-index" build request.
Expand Down Expand Up @@ -2020,6 +2217,35 @@ def from_json( # type: ignore[override] # noqa: F821
request.add_state('in_progress', 'The request was initiated')
return request

def from_json_replacement(
cls,
payload: RecursiveRelatedBundlesPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/recursive-related-bundles API endpoint.
:param RecursiveRelatedBundlesPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""

request_kwargs = payload.get_json_for_request()

request_kwargs['parent_bundle_image'] = Image.get_or_create(pull_specification=payload.parent_bundle_image)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')
return request

def to_json(self, verbose: Optional[bool] = True) -> RecursiveRelatedBundlesRequestResponse:
"""
Provide the JSON representation of a "recursive-related-bundles" build request.
Expand Down Expand Up @@ -2128,6 +2354,35 @@ def from_json( # type: ignore[override] # noqa: F821
request.add_state('in_progress', 'The request was initiated')
return request

def from_json_replacement(
cls,
payload: FbcOperationsPydanticModel,
):
"""
Handle JSON requests for the builds/fbc-operations API endpoint.
:param FbcOperationsPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['fbc_fragment'] = Image.get_or_create(pull_specification=payload.fbc_fragment)
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

request = cls(**request_kwargs)

for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request


def to_json(self, verbose: Optional[bool] = True) -> FbcOperationRequestResponse:
"""
Provide the JSON representation of a "fbc-operation" build request.
Expand Down
Loading

0 comments on commit 7a6a734

Please sign in to comment.