Skip to content

Commit 2cc8c5f

Browse files
committed
Minor fixes
1 parent 9675d0a commit 2cc8c5f

File tree

6 files changed

+110
-56
lines changed

6 files changed

+110
-56
lines changed

iib/common/pydantic_models.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@
3636
]
3737

3838

39-
class PydanticModel(BaseModel):
39+
class PydanticRequestBaseModel(BaseModel):
40+
"""Base model representing IIB request."""
41+
4042
@classmethod
4143
def _get_all_keys_to_check_in_db(cls):
4244
"""Class that returns request specific keys to check."""
4345
raise NotImplementedError("Not implemented")
4446

4547
def get_keys_to_check_in_db(self):
46-
"""Filter keys, which need to be checked in db. Return only a keys that are set to values."""
48+
"""
49+
Filter keys, which need to be checked in db.
50+
51+
Return only a keys that are set to values.
52+
"""
4753
return [k for k in self._get_all_keys_to_check_in_db() if getattr(self, k, None)]
4854

4955

50-
class AddPydanticModel(PydanticModel):
56+
class AddPydanticModel(PydanticRequestBaseModel):
5157
"""Datastructure of the request to /builds/add API point."""
5258

5359
add_arches: Optional[List[str]] = None
@@ -76,18 +82,22 @@ class AddPydanticModel(PydanticModel):
7682
BeforeValidator(distribution_scope_lower),
7783
] = None
7884
force_backport: Optional[bool] = False # deprecated
79-
from_index: Annotated[str, AfterValidator(image_format_check)]
85+
from_index: Annotated[Optional[str], AfterValidator(image_format_check)] = None
8086
graph_update_mode: Optional[GRAPH_MODE_LITERAL] = None
8187
organization: Optional[str] = None # deprecated
8288
overwrite_from_index: Optional[bool] = False
8389
overwrite_from_index_token: Optional[SecretStr] = None
8490

85-
_from_index_add_arches_check = model_validator(mode='after')(from_index_add_arches)
91+
@model_validator(mode='after')
92+
def verify_from_index_add_arches_combination(self) -> 'AddPydanticModel':
93+
"""Check the 'overwrite_from_index' parameter with 'overwrite_from_index_token' param."""
94+
from_index_add_arches(self.from_index, self.add_arches)
95+
return self
8696

8797
# TODO remove this comment -> Validator from RequestIndexImageMixin class
8898
@model_validator(mode='after')
8999
def verify_overwrite_from_index_token(self) -> 'AddPydanticModel':
90-
"""Check the 'overwrite_from_index' parameter in combination with 'overwrite_from_index_token' parameter."""
100+
"""Check the 'overwrite_from_index' parameter with 'overwrite_from_index_token' param."""
91101
validate_overwrite_params(self.overwrite_from_index, self.overwrite_from_index_token)
92102
return self
93103

@@ -138,7 +148,7 @@ def _get_all_keys_to_check_in_db(self):
138148
return ["binary_image", "bundles", "deprecation_list", "from_index"]
139149

140150

141-
class RmPydanticModel(PydanticModel):
151+
class RmPydanticModel(PydanticRequestBaseModel):
142152
"""Datastructure of the request to /builds/rm API point."""
143153

144154
add_arches: Optional[List[str]] = None
@@ -151,16 +161,24 @@ class RmPydanticModel(PydanticModel):
151161
Optional[DISTRIBUTION_SCOPE_LITERAL],
152162
BeforeValidator(distribution_scope_lower),
153163
] = None
154-
from_index: Annotated[str, AfterValidator(image_format_check)]
164+
from_index: Annotated[Optional[str], AfterValidator(image_format_check)] = None
155165
operators: Annotated[List[str], AfterValidator(length_validator)]
156166
overwrite_from_index: Optional[bool] = False
157167
overwrite_from_index_token: Optional[SecretStr] = None
158168

159-
_from_index_add_arches_check = model_validator(mode='after')(from_index_add_arches)
169+
@model_validator(mode='after')
170+
def verify_from_index_add_arches_combination(self) -> 'AddPydanticModel':
171+
"""Check the 'overwrite_from_index' parameter with 'overwrite_from_index_token' param."""
172+
from_index_add_arches(self.from_index, self.add_arches)
173+
return self
160174

161175
@model_validator(mode='after')
162176
def verify_overwrite_from_index_token(self) -> 'RmPydanticModel':
163-
validate_overwrite_params(self.overwrite_from_index, self.overwrite_from_index_token)
177+
"""Validate overwrite_from_index and overwrite_from_index_token param combination."""
178+
validate_overwrite_params(
179+
self.overwrite_from_index,
180+
self.overwrite_from_index_token,
181+
)
164182
return self
165183

166184
def get_json_for_request(self):
@@ -180,19 +198,30 @@ def _get_all_keys_to_check_in_db(self):
180198

181199

182200
class AddRmBatchPydanticModel(BaseModel):
201+
"""Datastructure of the request to /builds/add-rm-batch API point."""
202+
183203
annotations: Dict[str, Any]
184204
build_requests: List[Union[AddPydanticModel, RmPydanticModel]]
185205

186206

187207
class RegistryAuth(BaseModel):
208+
"""Datastructure representing private registry token."""
209+
188210
auth: SecretStr
189211

190212

191-
class RegistryAuths(BaseModel): # is {"auths":{}} allowed?
213+
class RegistryAuths(BaseModel):
214+
"""
215+
Datastructure used within recursive-related-bundles.
216+
217+
Provide the dockerconfig.json for authentication to private registries.
218+
Non-auth information in the dockerconfig.json is not allowed.
219+
"""
220+
192221
auths: Annotated[Dict[SecretStr, RegistryAuth], AfterValidator(length_validator)]
193222

194223

195-
class RegenerateBundlePydanticModel(PydanticModel):
224+
class RegenerateBundlePydanticModel(PydanticRequestBaseModel):
196225
"""Datastructure of the request to /builds/regenerate-bundle API point."""
197226

198227
# BUNDLE_IMAGE, from_bundle_image_resolved, build_tags?
@@ -213,12 +242,14 @@ def _get_all_keys_to_check_in_db(self):
213242

214243

215244
class RegenerateBundleBatchPydanticModel(BaseModel):
245+
"""Datastructure of the request to /builds/regenerate-bundle-batch API point."""
246+
216247
build_requests: List[RegenerateBundlePydanticModel]
217248
annotations: Dict[str, Any]
218249

219250

220-
class MergeIndexImagePydanticModel(PydanticModel):
221-
"""Datastructure of the request to /builds/regenerate-bundle API point."""
251+
class MergeIndexImagePydanticModel(PydanticRequestBaseModel):
252+
"""Datastructure of the request to /builds/merge-index-image API point."""
222253

223254
binary_image: Annotated[
224255
Optional[str],
@@ -245,11 +276,13 @@ class MergeIndexImagePydanticModel(PydanticModel):
245276

246277
@model_validator(mode='after')
247278
def verify_graph_update_mode_with_target_index(self) -> 'MergeIndexImagePydanticModel':
279+
"""Validate graph_update_mode with target_index param combination."""
248280
validate_graph_mode_index_image(self.graph_update_mode, self.target_index)
249281
return self
250282

251283
@model_validator(mode='after')
252284
def verify_overwrite_from_index_token(self) -> 'MergeIndexImagePydanticModel':
285+
"""Validate overwrite_target_index with overwrite_target_index_token param combination."""
253286
validate_overwrite_params(
254287
self.overwrite_target_index,
255288
self.overwrite_target_index_token,
@@ -274,8 +307,8 @@ def _get_all_keys_to_check_in_db(self):
274307
]
275308

276309

277-
class CreateEmptyIndexPydanticModel(PydanticModel):
278-
"""Datastructure of the request to /builds/regenerate-bundle API point."""
310+
class CreateEmptyIndexPydanticModel(PydanticRequestBaseModel):
311+
"""Datastructure of the request to /builds/create-empty-index API point."""
279312

280313
binary_image: Annotated[
281314
Optional[str],
@@ -302,7 +335,9 @@ def _get_all_keys_to_check_in_db(self):
302335
return ["binary_image", "from_index"]
303336

304337

305-
class RecursiveRelatedBundlesPydanticModel(PydanticModel):
338+
class RecursiveRelatedBundlesPydanticModel(PydanticRequestBaseModel):
339+
"""Datastructure of the request to /builds/recursive-related-bundles API point."""
340+
306341
organization: Optional[str] = None
307342
parent_bundle_image: Annotated[
308343
str,
@@ -322,7 +357,9 @@ def _get_all_keys_to_check_in_db(self):
322357
return ["parent_bundle_image"]
323358

324359

325-
class FbcOperationsPydanticModel(PydanticModel):
360+
class FbcOperationsPydanticModel(PydanticRequestBaseModel):
361+
"""Datastructure of the request to /builds/fbc-operations API point."""
362+
326363
add_arches: Optional[List[str]] = []
327364
binary_image: Annotated[
328365
Optional[str],
@@ -357,6 +394,7 @@ class FbcOperationsPydanticModel(PydanticModel):
357394

358395
@model_validator(mode='after')
359396
def verify_overwrite_from_index_token(self) -> 'FbcOperationsPydanticModel':
397+
"""Validate overwrite_from_index and overwrite_from_index_token param combination."""
360398
validate_overwrite_params(self.overwrite_from_index, self.overwrite_from_index_token)
361399
return self
362400

iib/common/pydantic_utils.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414

1515
# TODO add regex in future to not allow following values ":s", "s:", ":"?
1616
def image_format_check(image_name: str) -> str:
17+
"""Check format of the index image."""
1718
if '@' not in image_name and ':' not in image_name:
1819
raise ValidationError(f'Image {image_name} should have a tag or a digest specified.')
1920
return image_name
2021

2122

2223
def images_format_check(image_list: List[str]) -> List[str]:
24+
"""Check multiple image names."""
2325
for image_name in image_list:
2426
image_format_check(image_name)
2527
return image_list
2628

2729

2830
def get_unique_bundles(bundles: List[str]) -> List[str]:
31+
"""Check and possibly remove duplicates from a list of bundles."""
2932
if not bundles:
3033
return bundles
3134

@@ -42,14 +45,15 @@ def get_unique_bundles(bundles: List[str]) -> List[str]:
4245

4346

4447
# RequestIndexImageMixin
45-
def get_unique_deprecation_list_items(deprecation_list: Optional[List[str]]) -> Optional[List[str]]:
48+
def get_unique_deprecation_list_items(deprecation_list: List[str]) -> List[str]:
49+
"""Return a list of unique items."""
4650
return list(set(deprecation_list))
4751

4852

4953
def validate_graph_mode_index_image(
50-
graph_update_mode: str,
51-
index_image: str,
52-
) -> 'MergeIndexImageRequestPayload':
54+
graph_update_mode: Optional[GRAPH_MODE_LITERAL],
55+
index_image: Optional[str],
56+
) -> Optional[str]:
5357
"""
5458
Validate graph mode and check if index image is allowed to use different graph mode.
5559
@@ -59,8 +63,7 @@ def validate_graph_mode_index_image(
5963
:raises: Forbidden when graph_mode can't be used for given index image
6064
"""
6165
if graph_update_mode:
62-
# TODO remove this comment, replace value with current_app.config['IIB_GRAPH_MODE_INDEX_ALLOW_LIST']
63-
allowed_from_indexes: List[str] = ["REMOVE_#:r"]
66+
allowed_from_indexes: List[str] = current_app.config['IIB_GRAPH_MODE_INDEX_ALLOW_LIST']
6467
if index_image not in allowed_from_indexes:
6568
raise Forbidden(
6669
'"graph_update_mode" can only be used on the'
@@ -70,18 +73,15 @@ def validate_graph_mode_index_image(
7073

7174

7275
# RequestIndexImageMixin
73-
def from_index_add_arches(model: 'AddRequestPydanticModel') -> 'AddRequestPydanticModel':
76+
def from_index_add_arches(from_index: Optional[str], add_arches: Optional[List[str]]) -> None:
7477
"""Check if both `from_index` and `add_arches` are not specified."""
75-
if not model.from_index and not model.add_arches:
78+
if not from_index and not add_arches:
7679
raise ValidationError('One of "from_index" or "add_arches" must be specified')
77-
return model
7880

7981

8082
# RequestIndexImageMixin
8183
def binary_image_check(binary_image: str) -> str:
82-
"""
83-
# Validate binary_image is correctly provided.
84-
"""
84+
"""Validate binary_image is correctly provided."""
8585
if not binary_image and not current_app.config['IIB_BINARY_IMAGE_CONFIG']:
8686
raise ValidationError('The "binary_image" value must be a non-empty string')
8787
return binary_image
@@ -93,9 +93,7 @@ def validate_overwrite_params(
9393
overwrite_index_image_token: Optional[str],
9494
disable_auth_check: Optional[bool] = False,
9595
) -> None:
96-
"""
97-
Check if both `overwrite_index_image` and `overwrite_index_image_token` are specified.
98-
"""
96+
"""Check if both `overwrite_index_image` and `overwrite_index_image_token` are specified."""
9997
if overwrite_index_image_token and not overwrite_index_image:
10098
raise ValidationError(
10199
'The "overwrite_from_index" parameter is required when'
@@ -114,10 +112,12 @@ def validate_overwrite_params(
114112

115113
# RequestIndexImageMixin
116114
def distribution_scope_lower(distribution_scope: str) -> str:
115+
"""Transform distribution_scope parameter to lowercase."""
117116
return distribution_scope.lower()
118117

119118

120119
def length_validator(model_property: Any) -> Any:
120+
"""Validate length of the given model property."""
121121
if len(model_property) == 0:
122122
raise ValidationError(
123123
f"The {type(model_property)} {model_property} should have at least 1 item."

iib/web/iib_static_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: GPL-3.0-or-later
2-
from typing import Any, Dict, List, NamedTuple, Optional, Union, Sequence, Set
2+
from typing import Any, Dict, List, NamedTuple, Optional, Sequence, Set
33
from typing_extensions import NotRequired, TypedDict, Literal
44

55
from proton._message import Message

iib/workers/tasks/build.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,20 @@ def _update_index_image_pull_spec(
291291
else:
292292
index_image = output_pull_spec
293293

294-
payload: UpdateRequestPayload = {'arches': list(arches), 'index_image': index_image}
294+
update_payload: UpdateRequestPayload = {'arches': list(arches), 'index_image': index_image}
295295

296296
if add_or_rm:
297297
with set_registry_token(overwrite_from_index_token, from_index, append=True):
298298
index_image_resolved = get_resolved_image(index_image)
299-
payload['index_image_resolved'] = index_image_resolved
300-
payload['internal_index_image_copy'] = output_pull_spec
301-
payload['internal_index_image_copy_resolved'] = get_resolved_image(output_pull_spec)
299+
update_payload['index_image_resolved'] = index_image_resolved
300+
update_payload['internal_index_image_copy'] = output_pull_spec
301+
update_payload['internal_index_image_copy_resolved'] = get_resolved_image(output_pull_spec)
302302

303-
update_request(request_id, payload, exc_msg='Failed setting the index image on the request')
303+
update_request(
304+
request_id,
305+
update_payload,
306+
exc_msg='Failed setting the index image on the request',
307+
)
304308

305309

306310
def _get_external_arch_pull_spec(
@@ -670,7 +674,7 @@ def _update_index_image_build_state(
670674
image.
671675
"""
672676
arches_str = ', '.join(sorted(prebuild_info['arches']))
673-
payload: UpdateRequestPayload = {
677+
update_payload: UpdateRequestPayload = {
674678
'binary_image': prebuild_info['binary_image'],
675679
'binary_image_resolved': prebuild_info['binary_image_resolved'],
676680
'state': 'in_progress',
@@ -680,26 +684,26 @@ def _update_index_image_build_state(
680684

681685
bundle_mapping: Optional[Dict[str, List[str]]] = prebuild_info.get('bundle_mapping')
682686
if bundle_mapping:
683-
payload['bundle_mapping'] = bundle_mapping
687+
update_payload['bundle_mapping'] = bundle_mapping
684688

685689
from_index_resolved = prebuild_info.get('from_index_resolved')
686690
if from_index_resolved:
687-
payload['from_index_resolved'] = from_index_resolved
691+
update_payload['from_index_resolved'] = from_index_resolved
688692

689693
source_from_index_resolved = prebuild_info.get('source_from_index_resolved')
690694
if source_from_index_resolved:
691-
payload['source_from_index_resolved'] = source_from_index_resolved
695+
update_payload['source_from_index_resolved'] = source_from_index_resolved
692696

693697
target_index_resolved = prebuild_info.get('target_index_resolved')
694698
if target_index_resolved:
695-
payload['target_index_resolved'] = target_index_resolved
699+
update_payload['target_index_resolved'] = target_index_resolved
696700

697701
fbc_fragment_resolved = prebuild_info.get('fbc_fragment_resolved')
698702
if fbc_fragment_resolved:
699-
payload['fbc_fragment_resolved'] = fbc_fragment_resolved
703+
update_payload['fbc_fragment_resolved'] = fbc_fragment_resolved
700704

701705
exc_msg = 'Failed setting the resolved images on the request'
702-
update_request(request_id, payload, exc_msg)
706+
update_request(request_id, update_payload, exc_msg)
703707

704708

705709
@retry(

0 commit comments

Comments
 (0)