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 8, 2023
1 parent 8254277 commit d69a673
Show file tree
Hide file tree
Showing 5 changed files with 807 additions and 1 deletion.
312 changes: 311 additions & 1 deletion iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@

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

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

from iib.web.iib_static_types import (
AddRequestPayload,
Expand Down Expand Up @@ -390,6 +399,64 @@ class Request(db.Model):
'polymorphic_on': 'type',
}

def from_json_replacement_even_shorter(
self,
payload: UnionPydanticRequestType,
batch: Optional[Batch] = None,
batch_allowed: Optional[bool] = False,
build_tags_allowed: Optional[bool] = False,
):
"""
Handle JSON requests for the builds/* API endpoint.
:param UnionPydanticRequestType payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

keys_to_check = payload.get_keys_to_check_in_db()
for key in keys_to_check:
if key in [
'binary_image',
'fbc_fragment',
'from_index',
'from_bundle_image',
'source_from_index',
'target_index',
'parent_bundle_image',
]:
request_kwargs[key] = Image.get_or_create(pull_specification=request_kwargs[key])

elif key in ["bundles", "deprecation_list"]:
images = request_kwargs.get(key, [])
request_kwargs[key] = [
Image.get_or_create(pull_specification=image) for image in images
]

elif key == ["operators"]:
request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in request_kwargs["operators"]]
else:
raise ValidationError(f"Unexpected key: {key} during from_json() method.")

# 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
if batch_allowed:
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = self(**request_kwargs)

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

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

@validates('type')
def validate_type(self, key: Optional[str], type_num: int) -> int:
"""
Expand Down Expand Up @@ -1002,6 +1069,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 +1258,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: 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()

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_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)

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 +1396,39 @@ 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=payload.from_index)
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)

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 +1575,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 +1807,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 +2118,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 +2272,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 +2409,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 d69a673

Please sign in to comment.