Skip to content

Commit 1bfa766

Browse files
committed
Delete types replaced by pydantic
1 parent d8e52df commit 1bfa766

File tree

3 files changed

+64
-301
lines changed

3 files changed

+64
-301
lines changed

iib/common/pydantic_models.py

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,20 @@
3636
]
3737

3838

39-
class AddPydanticModel(BaseModel):
39+
class PydanticModel(BaseModel):
40+
41+
@classmethod
42+
def _get_all_keys_to_check_in_db(cls):
43+
raise NotImplementedError("Not implemented")
44+
45+
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."""
47+
return [
48+
k for k in self._get_all_keys_to_check_in_db() if getattr(self, k, None)
49+
]
50+
51+
52+
class AddPydanticModel(PydanticModel):
4053
"""Datastructure of the request to /builds/add API point."""
4154

4255
add_arches: Optional[List[str]] = None
@@ -109,24 +122,24 @@ def bundles_needed_with_check_related_images(self) -> 'AddPydanticModel':
109122

110123
def get_json_for_request(self):
111124
"""Return json with the parameters we store in the db."""
112-
return {
113-
# "add_arches": self.add_arches, # not in db?
114-
"binary_image": self.binary_image,
115-
# "build_tags": self.build_tags, # not in db
116-
"bundles": self.bundles,
117-
"check_related_images": self.check_related_images,
118-
"deprecation_list": self.deprecation_list,
119-
"distribution_scope": self.distribution_scope,
120-
"from_index": self.from_index,
121-
"graph_update_mode": self.graph_update_mode,
122-
"organization": self.organization,
123-
}
125+
return self.model_dump(
126+
exclude=[
127+
"add_arches",
128+
"build_tags",
129+
"cnr_token",
130+
"force_backport",
131+
"overwrite_from_index",
132+
"overwrite_from_index_token",
133+
],
134+
exclude_defaults=True,
135+
)
124136

125-
def get_keys_to_check_in_db(self):
137+
138+
def _get_all_keys_to_check_in_db(self):
126139
return ["binary_image", "bundles", "deprecation_list", "from_index"]
127140

128141

129-
class RmPydanticModel(BaseModel):
142+
class RmPydanticModel(PydanticModel):
130143
"""Datastructure of the request to /builds/rm API point."""
131144

132145
add_arches: Optional[List[str]] = None
@@ -152,17 +165,12 @@ def verify_overwrite_from_index_token(self) -> 'RmPydanticModel':
152165

153166
def get_json_for_request(self):
154167
"""Return json with the parameters we store in the db."""
155-
return {
156-
# "add_arches": self.add_arches, # not in db?
157-
"binary_image": self.binary_image,
158-
# "build_tags": self.build_tags, # not in db
159-
"distribution_scope": self.distribution_scope,
160-
"from_index": self.from_index,
161-
"operators": self.operators,
162-
"organization": self.organization,
163-
}
168+
return self.model_dump(
169+
exclude=["add_arches", "build_tags", "overwrite_from_index", "overwrite_from_index_token"],
170+
exclude_defaults=True,
171+
)
164172

165-
def get_keys_to_check_in_db(self):
173+
def _get_all_keys_to_check_in_db(self):
166174
return ["binary_image", "from_index", "operators"]
167175

168176

@@ -179,7 +187,7 @@ class RegistryAuths(BaseModel): # is {"auths":{}} allowed?
179187
auths: Annotated[Dict[SecretStr, RegistryAuth], AfterValidator(length_validator)]
180188

181189

182-
class RegenerateBundlePydanticModel(BaseModel):
190+
class RegenerateBundlePydanticModel(PydanticModel):
183191
"""Datastructure of the request to /builds/regenerate-bundle API point."""
184192

185193
# BUNDLE_IMAGE, from_bundle_image_resolved, build_tags?
@@ -190,13 +198,12 @@ class RegenerateBundlePydanticModel(BaseModel):
190198

191199
def get_json_for_request(self):
192200
"""Return json with the parameters we store in the db."""
193-
return {
194-
"bundle_replacements": self.bundle_replacements,
195-
"from_bundle_image": self.from_bundle_image,
196-
"organization": self.organization,
197-
}
201+
return self.model_dump(
202+
exclude=["registry_auths"],
203+
exclude_defaults=True,
204+
)
198205

199-
def get_keys_to_check_in_db(self):
206+
def _get_all_keys_to_check_in_db(self):
200207
return ["from_bundle_image"]
201208

202209

@@ -205,7 +212,7 @@ class RegenerateBundleBatchPydanticModel(BaseModel):
205212
annotations: Dict[str, Any]
206213

207214

208-
class MergeIndexImagePydanticModel(BaseModel):
215+
class MergeIndexImagePydanticModel(PydanticModel):
209216
"""Datastructure of the request to /builds/regenerate-bundle API point."""
210217

211218
binary_image: Annotated[
@@ -246,23 +253,16 @@ def verify_overwrite_from_index_token(self) -> 'MergeIndexImagePydanticModel':
246253

247254
def get_json_for_request(self):
248255
"""Return json with the parameters we store in the db."""
249-
return {
250-
"binary_image": self.binary_image,
251-
# "build_tags": self.build_tags, # not in db
252-
"deprecation_list": self.deprecation_list,
253-
"distribution_scope": self.distribution_scope,
254-
"graph_update_mode": self.graph_update_mode,
255-
"source_from_index": self.source_from_index,
256-
"target_index": self.target_index,
257-
"batch": self.batch,
258-
"user": self.user,
259-
}
256+
return self.model_dump(
257+
exclude=["build_tags", "overwrite_target_index", "overwrite_target_index_token"],
258+
exclude_defaults=True,
259+
)
260260

261-
def get_keys_to_check_in_db(self):
261+
def _get_all_keys_to_check_in_db(self):
262262
return ["binary_image", "deprecation_list", "source_from_index", "target_index", "target_index"]
263263

264264

265-
class CreateEmptyIndexPydanticModel(BaseModel):
265+
class CreateEmptyIndexPydanticModel(PydanticModel):
266266
"""Datastructure of the request to /builds/regenerate-bundle API point."""
267267

268268
binary_image: Annotated[
@@ -280,18 +280,15 @@ class CreateEmptyIndexPydanticModel(BaseModel):
280280

281281
def get_json_for_request(self):
282282
"""Return json with the parameters we store in the db."""
283-
return {
284-
"binary_image": self.binary_image,
285-
"from_index": self.from_index,
286-
"labels": self.labels,
287-
"output_fbc": self.output_fbc,
288-
}
283+
return self.model_dump(
284+
exclude_defaults=True,
285+
)
289286

290-
def get_keys_to_check_in_db(self):
287+
def _get_all_keys_to_check_in_db(self):
291288
return ["binary_image", "from_index"]
292289

293290

294-
class RecursiveRelatedBundlesPydanticModel(BaseModel):
291+
class RecursiveRelatedBundlesPydanticModel(PydanticModel):
295292
organization: Optional[str] = None
296293
parent_bundle_image: Annotated[
297294
str,
@@ -302,17 +299,18 @@ class RecursiveRelatedBundlesPydanticModel(BaseModel):
302299

303300
def get_json_for_request(self):
304301
"""Return json with the parameters we store in the db."""
305-
return {
306-
"organization": self.organization,
307-
"parent_bundle_image": self.parent_bundle_image,
308-
}
302+
return self.model_dump(
303+
exclude=["registry_auths"],
304+
exclude_defaults=True,
305+
)
309306

310-
def get_keys_to_check_in_db(self):
307+
308+
def _get_all_keys_to_check_in_db(self):
311309
return ["parent_bundle_image"]
312310

313311

314-
class FbcOperationsPydanticModel(BaseModel):
315-
add_arches: Optional[List[str]]
312+
class FbcOperationsPydanticModel(PydanticModel):
313+
add_arches: Optional[List[str]] = []
316314
binary_image: Annotated[
317315
Optional[str],
318316
AfterValidator(image_format_check),
@@ -349,16 +347,10 @@ def verify_overwrite_from_index_token(self) -> 'FbcOperationsPydanticModel':
349347

350348
def get_json_for_request(self):
351349
"""Return json with the parameters we store in the db."""
352-
return {
353-
# "add_arches": self.add_arches, # not in db?
354-
"binary_image": self.binary_image,
355-
"bundles": self.bundles,
356-
# "build_tags": self.build_tags, # not in db
357-
"distribution_scope": self.distribution_scope,
358-
"fbc_fragment": self.fbc_fragment,
359-
"from_index": self.from_index,
360-
"organization": self.organization,
361-
}
350+
return self.model_dump(
351+
exclude=["add_arches", "build_tags", "overwrite_from_index", "overwrite_from_index_token"],
352+
exclude_defaults=True,
353+
)
362354

363-
def get_keys_to_check_in_db(self):
355+
def _get_all_keys_to_check_in_db(self):
364356
return ["binary_image", "bundles", "fbc_fragment", "from_index"]

0 commit comments

Comments
 (0)