Skip to content

Commit bbd3201

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

File tree

5 files changed

+801
-1
lines changed

5 files changed

+801
-1
lines changed

iib/web/models.py

Lines changed: 311 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@
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+
UnionPydanticRequestType,
30+
)
2231

2332
from iib.web.iib_static_types import (
2433
AddRequestPayload,
@@ -390,6 +399,64 @@ class Request(db.Model):
390399
'polymorphic_on': 'type',
391400
}
392401

402+
def from_json_replacement_even_shorter(
403+
self,
404+
payload: UnionPydanticRequestType,
405+
batch: Optional[Batch] = None,
406+
batch_allowed: Optional[bool] = False,
407+
build_tags_allowed: Optional[bool] = False,
408+
):
409+
"""
410+
Handle JSON requests for the builds/* API endpoint.
411+
412+
:param UnionPydanticRequestType payload: the Pydantic model representing the request.
413+
:param Batch batch: the batch to specify with the request.
414+
"""
415+
request_kwargs = payload.get_json_for_request()
416+
417+
keys_to_check = payload.get_keys_to_check_in_db()
418+
for key in keys_to_check:
419+
if key in [
420+
'binary_image',
421+
'fbc_fragment',
422+
'from_index',
423+
'from_bundle_image',
424+
'source_from_index',
425+
'target_index',
426+
'parent_bundle_image',
427+
]:
428+
request_kwargs[key] = Image.get_or_create(pull_specification=request_kwargs[key])
429+
430+
elif key in ["bundles", "deprecation_list"]:
431+
images = request_kwargs.get(key, [])
432+
request_kwargs[key] = [
433+
Image.get_or_create(pull_specification=image) for image in images
434+
]
435+
436+
elif key == ["operators"]:
437+
request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in request_kwargs["operators"]]
438+
else:
439+
raise ValidationError(f"Unexpected key: {key} during from_json() method.")
440+
441+
# current_user.is_authenticated is only ever False when auth is disabled
442+
if current_user.is_authenticated:
443+
request_kwargs['user'] = current_user
444+
445+
# Add the request to a new batch
446+
if batch_allowed:
447+
batch = batch or Batch()
448+
db.session.add(batch)
449+
request_kwargs['batch'] = batch
450+
451+
request = self(**request_kwargs)
452+
453+
if build_tags_allowed:
454+
for bt in payload.build_tags:
455+
request.add_build_tag(bt)
456+
457+
request.add_state('in_progress', 'The request was initiated')
458+
return request
459+
393460
@validates('type')
394461
def validate_type(self, key: Optional[str], type_num: int) -> int:
395462
"""
@@ -1002,6 +1069,20 @@ def _from_json(
10021069
db.session.add(batch)
10031070
request_kwargs['batch'] = batch
10041071

1072+
@staticmethod
1073+
def from_json_replacement(
1074+
request_kwargs: RequestPayload,
1075+
batch: Optional[Batch] = None,
1076+
):
1077+
# current_user.is_authenticated is only ever False when auth is disabled
1078+
if current_user.is_authenticated:
1079+
request_kwargs['user'] = current_user
1080+
1081+
# Add the request to a new batch
1082+
batch = batch or Batch()
1083+
db.session.add(batch)
1084+
request_kwargs['batch'] = batch
1085+
10051086
def get_common_index_image_json(self) -> CommonIndexImageResponseBase:
10061087
"""
10071088
Return the common set of attributes for an index image request.
@@ -1177,6 +1258,45 @@ def from_json( # type: ignore[override] # noqa: F821
11771258
request.add_state('in_progress', 'The request was initiated')
11781259
return request
11791260

1261+
def from_json_replacement(
1262+
cls,
1263+
payload: AddPydanticModel,
1264+
batch: Optional[Batch] = None,
1265+
):
1266+
"""
1267+
Handle JSON requests for the builds/add API endpoint.
1268+
1269+
:param AddPydanticModel payload: the Pydantic model representing the request.
1270+
:param Batch batch: the batch to specify with the request.
1271+
"""
1272+
request_kwargs = payload.get_json_for_request()
1273+
1274+
request_kwargs["bundles"] = [
1275+
Image.get_or_create(pull_specification=item) for item in payload.bundles
1276+
]
1277+
request_kwargs["deprecation_list"] = [
1278+
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
1279+
]
1280+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
1281+
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
1282+
1283+
# current_user.is_authenticated is only ever False when auth is disabled
1284+
if current_user.is_authenticated:
1285+
request_kwargs['user'] = current_user
1286+
1287+
# Add the request to a new batch
1288+
batch = batch or Batch()
1289+
db.session.add(batch)
1290+
request_kwargs['batch'] = batch
1291+
1292+
request = cls(**request_kwargs)
1293+
1294+
for bt in payload.build_tags:
1295+
request.add_build_tag(bt)
1296+
1297+
request.add_state('in_progress', 'The request was initiated')
1298+
return request
1299+
11801300
def to_json(self, verbose: Optional[bool] = True) -> AddRequestResponse:
11811301
"""
11821302
Provide the JSON representation of an "add" build request.
@@ -1276,6 +1396,39 @@ def from_json( # type: ignore[override] # noqa: F821
12761396

12771397
return request
12781398

1399+
def from_json_replacement(
1400+
cls,
1401+
payload: RmPydanticModel,
1402+
batch: Optional[Batch] = None,
1403+
):
1404+
"""
1405+
Handle JSON requests for the builds/rm API endpoint.
1406+
1407+
:param RmPydanticModel payload: the Pydantic model representing the request.
1408+
:param Batch batch: the batch to specify with the request.
1409+
"""
1410+
request_kwargs = payload.get_json_for_request()
1411+
1412+
request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in payload.operators]
1413+
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
1414+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
1415+
1416+
if current_user.is_authenticated:
1417+
request_kwargs['user'] = current_user
1418+
1419+
# Add the request to a new batch
1420+
batch = batch or Batch()
1421+
db.session.add(batch)
1422+
request_kwargs['batch'] = batch
1423+
1424+
request = cls(**request_kwargs)
1425+
request.add_state('in_progress', 'The request was initiated')
1426+
1427+
for bt in payload.build_tags:
1428+
request.add_build_tag(bt)
1429+
1430+
return request
1431+
12791432
def to_json(self, verbose: Optional[bool] = True) -> AddRmRequestResponseBase:
12801433
"""
12811434
Provide the JSON representation of an "rm" build request.
@@ -1422,6 +1575,36 @@ def from_json( # type: ignore[override] # noqa: F821
14221575
request.add_state('in_progress', 'The request was initiated')
14231576
return request
14241577

1578+
def from_json_replacement(
1579+
cls,
1580+
payload: RegenerateBundlePydanticModel,
1581+
batch: Optional[Batch] = None,
1582+
):
1583+
"""
1584+
Handle JSON requests for the builds/egenerate-bundle API endpoint.
1585+
1586+
:param RegenerateBundlePydanticModel payload: the Pydantic model representing the request.
1587+
:param Batch batch: the batch to specify with the request.
1588+
"""
1589+
request_kwargs = payload.get_json_for_request()
1590+
1591+
request_kwargs['from_bundle_image'] = Image.get_or_create(
1592+
pull_specification=payload.from_bundle_image
1593+
)
1594+
1595+
# current_user.is_authenticated is only ever False when auth is disabled
1596+
if current_user.is_authenticated:
1597+
request_kwargs['user'] = current_user
1598+
1599+
# Add the request to a new batch
1600+
batch = batch or Batch()
1601+
db.session.add(batch)
1602+
request_kwargs['batch'] = batch
1603+
1604+
request = cls(**request_kwargs)
1605+
request.add_state('in_progress', 'The request was initiated')
1606+
return request
1607+
14251608
def to_json(self, verbose: Optional[bool] = True) -> RegenerateBundleRequestResponse:
14261609
"""
14271610
Provide the JSON representation of a "regenerate-bundle" build request.
@@ -1624,6 +1807,45 @@ def from_json( # type: ignore[override] # noqa: F821
16241807
request.add_state('in_progress', 'The request was initiated')
16251808
return request
16261809

1810+
def from_json_replacement(
1811+
cls,
1812+
payload: MergeIndexImagePydanticModel,
1813+
batch: Optional[Batch] = None,
1814+
):
1815+
"""
1816+
Handle JSON requests for the builds/merge-index-image API endpoint.
1817+
1818+
:param MergeIndexImagePydanticModel payload: the Pydantic model representing the request.
1819+
:param Batch batch: the batch to specify with the request.
1820+
"""
1821+
request_kwargs = payload.get_json_for_request()
1822+
1823+
request_kwargs['deprecation_list'] = [
1824+
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
1825+
]
1826+
request_kwargs['source_from_index'] = Image.get_or_create(
1827+
pull_specification=payload.source_from_index
1828+
)
1829+
request_kwargs['target_index'] = Image.get_or_create(pull_specification=payload.target_index)
1830+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
1831+
1832+
# current_user.is_authenticated is only ever False when auth is disabled
1833+
if current_user.is_authenticated:
1834+
request_kwargs['user'] = current_user
1835+
1836+
# Add the request to a new batch
1837+
batch = batch or Batch()
1838+
db.session.add(batch)
1839+
request_kwargs['batch'] = batch
1840+
1841+
request = cls(**request_kwargs)
1842+
1843+
for bt in payload.build_tags:
1844+
request.add_build_tag(bt)
1845+
1846+
request.add_state('in_progress', 'The request was initiated')
1847+
return request
1848+
16271849
def to_json(self, verbose: Optional[bool] = True) -> MergeIndexImageRequestResponse:
16281850
"""
16291851
Provide the JSON representation of an "merge-index-image" build request.
@@ -1896,6 +2118,36 @@ def from_json( # type: ignore[override] # noqa: F821
18962118

18972119
return request
18982120

2121+
def from_json_replacement(
2122+
cls,
2123+
payload: CreateEmptyIndexPydanticModel,
2124+
batch: Optional[Batch] = None,
2125+
):
2126+
"""
2127+
Handle JSON requests for the builds/create-empty-index API endpoint.
2128+
2129+
:param CreateEmptyIndexPydanticModel payload: the Pydantic model representing the request.
2130+
:param Batch batch: the batch to specify with the request.
2131+
"""
2132+
request_kwargs = payload.get_json_for_request()
2133+
2134+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
2135+
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
2136+
2137+
# current_user.is_authenticated is only ever False when auth is disabled
2138+
if current_user.is_authenticated:
2139+
request_kwargs['user'] = current_user
2140+
2141+
# Add the request to a new batch
2142+
batch = batch or Batch()
2143+
db.session.add(batch)
2144+
request_kwargs['batch'] = batch
2145+
2146+
request = cls(**request_kwargs)
2147+
request.add_state('in_progress', 'The request was initiated')
2148+
2149+
return request
2150+
18992151
def to_json(self, verbose: Optional[bool] = True) -> CreateEmptyIndexRequestResponse:
19002152
"""
19012153
Provide the JSON representation of an "create-empty-index" build request.
@@ -2020,6 +2272,35 @@ def from_json( # type: ignore[override] # noqa: F821
20202272
request.add_state('in_progress', 'The request was initiated')
20212273
return request
20222274

2275+
def from_json_replacement(
2276+
cls,
2277+
payload: RecursiveRelatedBundlesPydanticModel,
2278+
batch: Optional[Batch] = None,
2279+
):
2280+
"""
2281+
Handle JSON requests for the builds/recursive-related-bundles API endpoint.
2282+
2283+
:param RecursiveRelatedBundlesPydanticModel payload: the Pydantic model representing the request.
2284+
:param Batch batch: the batch to specify with the request.
2285+
"""
2286+
2287+
request_kwargs = payload.get_json_for_request()
2288+
2289+
request_kwargs['parent_bundle_image'] = Image.get_or_create(pull_specification=payload.parent_bundle_image)
2290+
2291+
# current_user.is_authenticated is only ever False when auth is disabled
2292+
if current_user.is_authenticated:
2293+
request_kwargs['user'] = current_user
2294+
2295+
# Add the request to a new batch
2296+
batch = batch or Batch()
2297+
db.session.add(batch)
2298+
request_kwargs['batch'] = batch
2299+
2300+
request = cls(**request_kwargs)
2301+
request.add_state('in_progress', 'The request was initiated')
2302+
return request
2303+
20232304
def to_json(self, verbose: Optional[bool] = True) -> RecursiveRelatedBundlesRequestResponse:
20242305
"""
20252306
Provide the JSON representation of a "recursive-related-bundles" build request.
@@ -2128,6 +2409,35 @@ def from_json( # type: ignore[override] # noqa: F821
21282409
request.add_state('in_progress', 'The request was initiated')
21292410
return request
21302411

2412+
def from_json_replacement(
2413+
cls,
2414+
payload: FbcOperationsPydanticModel,
2415+
):
2416+
"""
2417+
Handle JSON requests for the builds/fbc-operations API endpoint.
2418+
2419+
:param FbcOperationsPydanticModel payload: the Pydantic model representing the request.
2420+
:param Batch batch: the batch to specify with the request.
2421+
"""
2422+
request_kwargs = payload.get_json_for_request()
2423+
2424+
request_kwargs['fbc_fragment'] = Image.get_or_create(pull_specification=payload.fbc_fragment)
2425+
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
2426+
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
2427+
2428+
# current_user.is_authenticated is only ever False when auth is disabled
2429+
if current_user.is_authenticated:
2430+
request_kwargs['user'] = current_user
2431+
2432+
request = cls(**request_kwargs)
2433+
2434+
for bt in payload.build_tags:
2435+
request.add_build_tag(bt)
2436+
2437+
request.add_state('in_progress', 'The request was initiated')
2438+
return request
2439+
2440+
21312441
def to_json(self, verbose: Optional[bool] = True) -> FbcOperationRequestResponse:
21322442
"""
21332443
Provide the JSON representation of a "fbc-operation" build request.

0 commit comments

Comments
 (0)