Skip to content

Commit 7f34604

Browse files
committed
Add Pydantic models to IIB
1 parent 8254277 commit 7f34604

File tree

5 files changed

+700
-1
lines changed

5 files changed

+700
-1
lines changed

iib/web/models.py

Lines changed: 256 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818

1919
from iib.exceptions import ValidationError
2020
from iib.web import db
21-
21+
from iib.web.pydantic_models import (
22+
AddPydanticModel,
23+
CreateEmptyIndexPydanticModel,
24+
FbcOperationsPydanticModel,
25+
MergeIndexImagePydanticModel,
26+
RecursiveRelatedBundlesPydanticModel,
27+
RegenerateBundlePydanticModel,
28+
RmPydanticModel,
29+
)
2230

2331
from iib.web.iib_static_types import (
2432
AddRequestPayload,
@@ -1002,6 +1010,20 @@ def _from_json(
10021010
db.session.add(batch)
10031011
request_kwargs['batch'] = batch
10041012

1013+
@staticmethod
1014+
def from_json_replacement(
1015+
request_kwargs: RequestPayload,
1016+
batch: Optional[Batch] = None,
1017+
):
1018+
# current_user.is_authenticated is only ever False when auth is disabled
1019+
if current_user.is_authenticated:
1020+
request_kwargs['user'] = current_user
1021+
1022+
# Add the request to a new batch
1023+
batch = batch or Batch()
1024+
db.session.add(batch)
1025+
request_kwargs['batch'] = batch
1026+
10051027
def get_common_index_image_json(self) -> CommonIndexImageResponseBase:
10061028
"""
10071029
Return the common set of attributes for an index image request.
@@ -1177,6 +1199,50 @@ def from_json( # type: ignore[override] # noqa: F821
11771199
request.add_state('in_progress', 'The request was initiated')
11781200
return request
11791201

1202+
def from_json_replacement(
1203+
cls,
1204+
payload: AddPydanticModel,
1205+
batch: Optional[Batch] = None,
1206+
):
1207+
"""
1208+
Handle JSON requests for the builds/add API endpoint.
1209+
1210+
:param AddPydanticModel payload: the Pydantic model representing the request.
1211+
:param Batch batch: the batch to specify with the request.
1212+
"""
1213+
request_kwargs = payload.get_json_for_request()
1214+
1215+
# setup user and batch in request
1216+
# cls.from_json_replacement(
1217+
# cast(RequestPayload, request_kwargs),
1218+
# batch=batch,
1219+
# )
1220+
1221+
# current_user.is_authenticated is only ever False when auth is disabled
1222+
if current_user.is_authenticated:
1223+
request_kwargs['user'] = current_user
1224+
1225+
# Add the request to a new batch
1226+
batch = batch or Batch()
1227+
db.session.add(batch)
1228+
request_kwargs['batch'] = batch
1229+
1230+
1231+
request_kwargs["bundles"] = [
1232+
Image.get_or_create(pull_specification=item) for item in payload.bundles
1233+
]
1234+
request_kwargs["deprecation_list"] = [
1235+
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
1236+
]
1237+
1238+
request = cls(**request_kwargs)
1239+
1240+
for bt in payload.build_tags:
1241+
request.add_build_tag(bt)
1242+
1243+
request.add_state('in_progress', 'The request was initiated')
1244+
return request
1245+
11801246
def to_json(self, verbose: Optional[bool] = True) -> AddRequestResponse:
11811247
"""
11821248
Provide the JSON representation of an "add" build request.
@@ -1276,6 +1342,38 @@ def from_json( # type: ignore[override] # noqa: F821
12761342

12771343
return request
12781344

1345+
def from_json_replacement(
1346+
cls,
1347+
payload: RmPydanticModel,
1348+
batch: Optional[Batch] = None,
1349+
):
1350+
"""
1351+
Handle JSON requests for the builds/rm API endpoint.
1352+
1353+
:param RmPydanticModel payload: the Pydantic model representing the request.
1354+
:param Batch batch: the batch to specify with the request.
1355+
"""
1356+
request_kwargs = payload.get_json_for_request()
1357+
1358+
request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in payload.operators]
1359+
request_kwargs['from_index'] = Image.get_or_create(pull_specification=request_kwargs['from_index'])
1360+
1361+
if current_user.is_authenticated:
1362+
request_kwargs['user'] = current_user
1363+
1364+
# Add the request to a new batch
1365+
batch = batch or Batch()
1366+
db.session.add(batch)
1367+
request_kwargs['batch'] = batch
1368+
1369+
request = cls(**request_kwargs)
1370+
request.add_state('in_progress', 'The request was initiated')
1371+
1372+
for bt in payload.build_tags:
1373+
request.add_build_tag(bt)
1374+
1375+
return request
1376+
12791377
def to_json(self, verbose: Optional[bool] = True) -> AddRmRequestResponseBase:
12801378
"""
12811379
Provide the JSON representation of an "rm" build request.
@@ -1422,6 +1520,36 @@ def from_json( # type: ignore[override] # noqa: F821
14221520
request.add_state('in_progress', 'The request was initiated')
14231521
return request
14241522

1523+
def from_json_replacement(
1524+
cls,
1525+
payload: RegenerateBundlePydanticModel,
1526+
batch: Optional[Batch] = None,
1527+
):
1528+
"""
1529+
Handle JSON requests for the builds/egenerate-bundle API endpoint.
1530+
1531+
:param RegenerateBundlePydanticModel payload: the Pydantic model representing the request.
1532+
:param Batch batch: the batch to specify with the request.
1533+
"""
1534+
request_kwargs = payload.get_json_for_request()
1535+
1536+
request_kwargs['from_bundle_image'] = Image.get_or_create(
1537+
pull_specification=payload.from_bundle_image
1538+
)
1539+
1540+
# current_user.is_authenticated is only ever False when auth is disabled
1541+
if current_user.is_authenticated:
1542+
request_kwargs['user'] = current_user
1543+
1544+
# Add the request to a new batch
1545+
batch = batch or Batch()
1546+
db.session.add(batch)
1547+
request_kwargs['batch'] = batch
1548+
1549+
request = cls(**request_kwargs)
1550+
request.add_state('in_progress', 'The request was initiated')
1551+
return request
1552+
14251553
def to_json(self, verbose: Optional[bool] = True) -> RegenerateBundleRequestResponse:
14261554
"""
14271555
Provide the JSON representation of a "regenerate-bundle" build request.
@@ -1624,6 +1752,45 @@ def from_json( # type: ignore[override] # noqa: F821
16241752
request.add_state('in_progress', 'The request was initiated')
16251753
return request
16261754

1755+
def from_json_replacement(
1756+
cls,
1757+
payload: MergeIndexImagePydanticModel,
1758+
batch: Optional[Batch] = None,
1759+
):
1760+
"""
1761+
Handle JSON requests for the builds/merge-index-image API endpoint.
1762+
1763+
:param MergeIndexImagePydanticModel payload: the Pydantic model representing the request.
1764+
:param Batch batch: the batch to specify with the request.
1765+
"""
1766+
request_kwargs = payload.get_json_for_request()
1767+
1768+
request_kwargs['deprecation_list'] = [
1769+
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
1770+
]
1771+
request_kwargs['source_from_index'] = Image.get_or_create(
1772+
pull_specification=payload.source_from_index
1773+
)
1774+
request_kwargs['target_index'] = Image.get_or_create(pull_specification=payload.target_index)
1775+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
1776+
1777+
# current_user.is_authenticated is only ever False when auth is disabled
1778+
if current_user.is_authenticated:
1779+
request_kwargs['user'] = current_user
1780+
1781+
# Add the request to a new batch
1782+
batch = batch or Batch()
1783+
db.session.add(batch)
1784+
request_kwargs['batch'] = batch
1785+
1786+
request = cls(**request_kwargs)
1787+
1788+
for bt in payload.build_tags:
1789+
request.add_build_tag(bt)
1790+
1791+
request.add_state('in_progress', 'The request was initiated')
1792+
return request
1793+
16271794
def to_json(self, verbose: Optional[bool] = True) -> MergeIndexImageRequestResponse:
16281795
"""
16291796
Provide the JSON representation of an "merge-index-image" build request.
@@ -1896,6 +2063,36 @@ def from_json( # type: ignore[override] # noqa: F821
18962063

18972064
return request
18982065

2066+
def from_json_replacement(
2067+
cls,
2068+
payload: CreateEmptyIndexPydanticModel,
2069+
batch: Optional[Batch] = None,
2070+
):
2071+
"""
2072+
Handle JSON requests for the builds/create-empty-index API endpoint.
2073+
2074+
:param CreateEmptyIndexPydanticModel payload: the Pydantic model representing the request.
2075+
:param Batch batch: the batch to specify with the request.
2076+
"""
2077+
request_kwargs = payload.get_json_for_request()
2078+
2079+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
2080+
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
2081+
2082+
# current_user.is_authenticated is only ever False when auth is disabled
2083+
if current_user.is_authenticated:
2084+
request_kwargs['user'] = current_user
2085+
2086+
# Add the request to a new batch
2087+
batch = batch or Batch()
2088+
db.session.add(batch)
2089+
request_kwargs['batch'] = batch
2090+
2091+
request = cls(**request_kwargs)
2092+
request.add_state('in_progress', 'The request was initiated')
2093+
2094+
return request
2095+
18992096
def to_json(self, verbose: Optional[bool] = True) -> CreateEmptyIndexRequestResponse:
19002097
"""
19012098
Provide the JSON representation of an "create-empty-index" build request.
@@ -2020,6 +2217,35 @@ def from_json( # type: ignore[override] # noqa: F821
20202217
request.add_state('in_progress', 'The request was initiated')
20212218
return request
20222219

2220+
def from_json_replacement(
2221+
cls,
2222+
payload: RecursiveRelatedBundlesPydanticModel,
2223+
batch: Optional[Batch] = None,
2224+
):
2225+
"""
2226+
Handle JSON requests for the builds/recursive-related-bundles API endpoint.
2227+
2228+
:param RecursiveRelatedBundlesPydanticModel payload: the Pydantic model representing the request.
2229+
:param Batch batch: the batch to specify with the request.
2230+
"""
2231+
2232+
request_kwargs = payload.get_json_for_request()
2233+
2234+
request_kwargs['parent_bundle_image'] = Image.get_or_create(pull_specification=payload.parent_bundle_image)
2235+
2236+
# current_user.is_authenticated is only ever False when auth is disabled
2237+
if current_user.is_authenticated:
2238+
request_kwargs['user'] = current_user
2239+
2240+
# Add the request to a new batch
2241+
batch = batch or Batch()
2242+
db.session.add(batch)
2243+
request_kwargs['batch'] = batch
2244+
2245+
request = cls(**request_kwargs)
2246+
request.add_state('in_progress', 'The request was initiated')
2247+
return request
2248+
20232249
def to_json(self, verbose: Optional[bool] = True) -> RecursiveRelatedBundlesRequestResponse:
20242250
"""
20252251
Provide the JSON representation of a "recursive-related-bundles" build request.
@@ -2128,6 +2354,35 @@ def from_json( # type: ignore[override] # noqa: F821
21282354
request.add_state('in_progress', 'The request was initiated')
21292355
return request
21302356

2357+
def from_json_replacement(
2358+
cls,
2359+
payload: FbcOperationsPydanticModel,
2360+
):
2361+
"""
2362+
Handle JSON requests for the builds/fbc-operations API endpoint.
2363+
2364+
:param FbcOperationsPydanticModel payload: the Pydantic model representing the request.
2365+
:param Batch batch: the batch to specify with the request.
2366+
"""
2367+
request_kwargs = payload.get_json_for_request()
2368+
2369+
request_kwargs['fbc_fragment'] = Image.get_or_create(pull_specification=payload.fbc_fragment)
2370+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
2371+
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
2372+
2373+
# current_user.is_authenticated is only ever False when auth is disabled
2374+
if current_user.is_authenticated:
2375+
request_kwargs['user'] = current_user
2376+
2377+
request = cls(**request_kwargs)
2378+
2379+
for bt in payload.build_tags:
2380+
request.add_build_tag(bt)
2381+
2382+
request.add_state('in_progress', 'The request was initiated')
2383+
return request
2384+
2385+
21312386
def to_json(self, verbose: Optional[bool] = True) -> FbcOperationRequestResponse:
21322387
"""
21332388
Provide the JSON representation of a "fbc-operation" build request.

0 commit comments

Comments
 (0)