From 1bfa76626947b020e8c50b41807564d4554bcad2 Mon Sep 17 00:00:00 2001 From: xdaile Date: Thu, 4 Jan 2024 20:06:18 +0100 Subject: [PATCH] Delete types replaced by pydantic --- iib/common/pydantic_models.py | 136 +++++++++++------------ iib/web/iib_static_types.py | 201 ---------------------------------- iib/web/models.py | 28 ----- 3 files changed, 64 insertions(+), 301 deletions(-) diff --git a/iib/common/pydantic_models.py b/iib/common/pydantic_models.py index 50fdcec81..7a819bc45 100644 --- a/iib/common/pydantic_models.py +++ b/iib/common/pydantic_models.py @@ -36,7 +36,20 @@ ] -class AddPydanticModel(BaseModel): +class PydanticModel(BaseModel): + + @classmethod + def _get_all_keys_to_check_in_db(cls): + raise NotImplementedError("Not implemented") + + def get_keys_to_check_in_db(self): + """Filter keys, which need to be checked in db. Return only a keys that are set to values.""" + return [ + k for k in self._get_all_keys_to_check_in_db() if getattr(self, k, None) + ] + + +class AddPydanticModel(PydanticModel): """Datastructure of the request to /builds/add API point.""" add_arches: Optional[List[str]] = None @@ -109,24 +122,24 @@ def bundles_needed_with_check_related_images(self) -> 'AddPydanticModel': def get_json_for_request(self): """Return json with the parameters we store in the db.""" - return { - # "add_arches": self.add_arches, # not in db? - "binary_image": self.binary_image, - # "build_tags": self.build_tags, # not in db - "bundles": self.bundles, - "check_related_images": self.check_related_images, - "deprecation_list": self.deprecation_list, - "distribution_scope": self.distribution_scope, - "from_index": self.from_index, - "graph_update_mode": self.graph_update_mode, - "organization": self.organization, - } + return self.model_dump( + exclude=[ + "add_arches", + "build_tags", + "cnr_token", + "force_backport", + "overwrite_from_index", + "overwrite_from_index_token", + ], + exclude_defaults=True, + ) - def get_keys_to_check_in_db(self): + + def _get_all_keys_to_check_in_db(self): return ["binary_image", "bundles", "deprecation_list", "from_index"] -class RmPydanticModel(BaseModel): +class RmPydanticModel(PydanticModel): """Datastructure of the request to /builds/rm API point.""" add_arches: Optional[List[str]] = None @@ -152,17 +165,12 @@ def verify_overwrite_from_index_token(self) -> 'RmPydanticModel': def get_json_for_request(self): """Return json with the parameters we store in the db.""" - return { - # "add_arches": self.add_arches, # not in db? - "binary_image": self.binary_image, - # "build_tags": self.build_tags, # not in db - "distribution_scope": self.distribution_scope, - "from_index": self.from_index, - "operators": self.operators, - "organization": self.organization, - } + return self.model_dump( + exclude=["add_arches", "build_tags", "overwrite_from_index", "overwrite_from_index_token"], + exclude_defaults=True, + ) - def get_keys_to_check_in_db(self): + def _get_all_keys_to_check_in_db(self): return ["binary_image", "from_index", "operators"] @@ -179,7 +187,7 @@ class RegistryAuths(BaseModel): # is {"auths":{}} allowed? auths: Annotated[Dict[SecretStr, RegistryAuth], AfterValidator(length_validator)] -class RegenerateBundlePydanticModel(BaseModel): +class RegenerateBundlePydanticModel(PydanticModel): """Datastructure of the request to /builds/regenerate-bundle API point.""" # BUNDLE_IMAGE, from_bundle_image_resolved, build_tags? @@ -190,13 +198,12 @@ class RegenerateBundlePydanticModel(BaseModel): def get_json_for_request(self): """Return json with the parameters we store in the db.""" - return { - "bundle_replacements": self.bundle_replacements, - "from_bundle_image": self.from_bundle_image, - "organization": self.organization, - } + return self.model_dump( + exclude=["registry_auths"], + exclude_defaults=True, + ) - def get_keys_to_check_in_db(self): + def _get_all_keys_to_check_in_db(self): return ["from_bundle_image"] @@ -205,7 +212,7 @@ class RegenerateBundleBatchPydanticModel(BaseModel): annotations: Dict[str, Any] -class MergeIndexImagePydanticModel(BaseModel): +class MergeIndexImagePydanticModel(PydanticModel): """Datastructure of the request to /builds/regenerate-bundle API point.""" binary_image: Annotated[ @@ -246,23 +253,16 @@ def verify_overwrite_from_index_token(self) -> 'MergeIndexImagePydanticModel': def get_json_for_request(self): """Return json with the parameters we store in the db.""" - return { - "binary_image": self.binary_image, - # "build_tags": self.build_tags, # not in db - "deprecation_list": self.deprecation_list, - "distribution_scope": self.distribution_scope, - "graph_update_mode": self.graph_update_mode, - "source_from_index": self.source_from_index, - "target_index": self.target_index, - "batch": self.batch, - "user": self.user, - } + return self.model_dump( + exclude=["build_tags", "overwrite_target_index", "overwrite_target_index_token"], + exclude_defaults=True, + ) - def get_keys_to_check_in_db(self): + def _get_all_keys_to_check_in_db(self): return ["binary_image", "deprecation_list", "source_from_index", "target_index", "target_index"] -class CreateEmptyIndexPydanticModel(BaseModel): +class CreateEmptyIndexPydanticModel(PydanticModel): """Datastructure of the request to /builds/regenerate-bundle API point.""" binary_image: Annotated[ @@ -280,18 +280,15 @@ class CreateEmptyIndexPydanticModel(BaseModel): def get_json_for_request(self): """Return json with the parameters we store in the db.""" - return { - "binary_image": self.binary_image, - "from_index": self.from_index, - "labels": self.labels, - "output_fbc": self.output_fbc, - } + return self.model_dump( + exclude_defaults=True, + ) - def get_keys_to_check_in_db(self): + def _get_all_keys_to_check_in_db(self): return ["binary_image", "from_index"] -class RecursiveRelatedBundlesPydanticModel(BaseModel): +class RecursiveRelatedBundlesPydanticModel(PydanticModel): organization: Optional[str] = None parent_bundle_image: Annotated[ str, @@ -302,17 +299,18 @@ class RecursiveRelatedBundlesPydanticModel(BaseModel): def get_json_for_request(self): """Return json with the parameters we store in the db.""" - return { - "organization": self.organization, - "parent_bundle_image": self.parent_bundle_image, - } + return self.model_dump( + exclude=["registry_auths"], + exclude_defaults=True, + ) - def get_keys_to_check_in_db(self): + + def _get_all_keys_to_check_in_db(self): return ["parent_bundle_image"] -class FbcOperationsPydanticModel(BaseModel): - add_arches: Optional[List[str]] +class FbcOperationsPydanticModel(PydanticModel): + add_arches: Optional[List[str]] = [] binary_image: Annotated[ Optional[str], AfterValidator(image_format_check), @@ -349,16 +347,10 @@ def verify_overwrite_from_index_token(self) -> 'FbcOperationsPydanticModel': def get_json_for_request(self): """Return json with the parameters we store in the db.""" - return { - # "add_arches": self.add_arches, # not in db? - "binary_image": self.binary_image, - "bundles": self.bundles, - # "build_tags": self.build_tags, # not in db - "distribution_scope": self.distribution_scope, - "fbc_fragment": self.fbc_fragment, - "from_index": self.from_index, - "organization": self.organization, - } + return self.model_dump( + exclude=["add_arches", "build_tags", "overwrite_from_index", "overwrite_from_index_token"], + exclude_defaults=True, + ) - def get_keys_to_check_in_db(self): + def _get_all_keys_to_check_in_db(self): return ["binary_image", "bundles", "fbc_fragment", "from_index"] diff --git a/iib/web/iib_static_types.py b/iib/web/iib_static_types.py index b72520d88..f4682781b 100644 --- a/iib/web/iib_static_types.py +++ b/iib/web/iib_static_types.py @@ -35,207 +35,6 @@ class RelatedBundlesMetadata(TypedDict): url: str -# Start of the Payloads Part - -# try inheritance from other payloads - -PayloadTags = Literal[ - 'AddRequestPayload', - 'RmRequestPayload', - 'RegenerateBundlePayload', - 'RegenerateBundleBatchPayload', - 'AddRmBatchPayload', - 'MergeIndexImagesPayload', - 'CreateEmptyIndexPayload', - 'FbcOperationRequestPayload', -] - - -PossiblePayloadParameters = Sequence[ - Literal[ - 'add_arches', - 'annotations', - 'batch', - 'binary_image', - 'build_requests', - 'build_tags', - 'bundles', - 'cnr_token', - 'check_related_images', - 'deprecation_list', - 'distribution_scope', - 'force_backport', - 'from_bundle_image', - 'from_index', - 'graph_update_mode', - 'labels', - 'operators', - 'organization', - 'output_fbc', - 'overwrite_from_index', - 'overwrite_from_index_token', - 'registry_auths', - 'related_bundles', - 'source_from_index', - 'target_index', - 'user', - ] -] - - -class AddRequestPayload(TypedDict): - """Datastructure of the request to /builds/add API point.""" - - add_arches: NotRequired[List[str]] - binary_image: NotRequired[str] - build_tags: NotRequired[List[str]] - bundles: List[str] - cnr_token: NotRequired[str] - check_related_images: NotRequired[bool] - deprecation_list: NotRequired[List[str]] - distribution_scope: NotRequired[str] - force_backport: NotRequired[bool] - from_index: NotRequired[str] - graph_update_mode: NotRequired[GRAPH_MODE_LITERAL] - organization: NotRequired[str] - overwrite_from_index: NotRequired[bool] - overwrite_from_index_token: NotRequired[str] - - -class RmRequestPayload(TypedDict): - """Datastructure of the request to /builds/rm API point.""" - - add_arches: NotRequired[List[str]] - binary_image: NotRequired[str] - build_tags: NotRequired[List[str]] - distribution_scope: NotRequired[str] - from_index: str - operators: List[str] - overwrite_from_index: NotRequired[bool] - overwrite_from_index_token: Optional[str] - - -class FbcOperationRequestPayload(TypedDict): - """Datastructure of the request to /builds/fbc-operation API point.""" - - fbc_fragment: str - from_index: str - binary_image: NotRequired[str] - build_tags: NotRequired[List[str]] - add_arches: NotRequired[List[str]] - overwrite_from_index: NotRequired[bool] - overwrite_from_index_token: NotRequired[str] - batch: NotRequired[str] - distribution_scope: NotRequired[str] - user: NotRequired[str] - - -class RegenerateBundlePayload(TypedDict): - """Datastructure of the request to /builds/regenerate-bundle API point.""" - - from_bundle_image: str - organization: NotRequired[str] - registry_auths: NotRequired[Dict[str, Any]] - related_bundles: NotRequired[RelatedBundlesMetadata] - user: NotRequired[str] - batch: NotRequired[str] - - -class RegenerateBundleBatchPayload(TypedDict): - """Datastructure of the request to /builds/regenerate-bundle-batch API point.""" - - annotations: NotRequired[Dict[str, Any]] - build_requests: List[RegenerateBundlePayload] - - -class AddRmBatchPayload(TypedDict): - """Datastructure of the request to /builds/add-rm-batch API point.""" - - annotations: NotRequired[Dict[str, Any]] - build_requests: List[Union[AddRequestPayload, RmRequestPayload]] - - -class MergeIndexImagesPayload(TypedDict): - """Datastructure of the request to /builds/merge-index-image API point.""" - - binary_image: NotRequired[str] - build_tags: NotRequired[List[str]] - deprecation_list: NotRequired[List[str]] - distribution_scope: NotRequired[str] - graph_update_mode: NotRequired[GRAPH_MODE_LITERAL] - overwrite_target_index: NotRequired[bool] - overwrite_target_index_token: NotRequired[str] - source_from_index: str - target_index: NotRequired[str] - batch: NotRequired[str] - user: NotRequired[str] - - -class CreateEmptyIndexPayload(TypedDict): - """Datastructure of the request to /builds/create-empty-index API point.""" - - binary_image: NotRequired[str] - from_index: str - labels: NotRequired[Dict[str, str]] - output_fbc: NotRequired[bool] - - -class RecursiveRelatedBundlesRequestPayload(TypedDict): - """Datastructure of the request to /builds/recursive-related-bundles API point.""" - - batch: NotRequired[int] - organization: NotRequired[str] - parent_bundle_image: str - registry_auths: NotRequired[Dict[str, Any]] - user: NotRequired[str] - - -class RequestPayload(TypedDict): - """Datastructure with all the possible keys that can API points receive.""" - - add_arches: NotRequired[List[str]] - annotations: NotRequired[Dict[str, Any]] - batch: NotRequired[int] - binary_image: NotRequired[str] - build_requests: NotRequired[ - List[Union[AddRequestPayload, RmRequestPayload, RegenerateBundlePayload]] - ] - build_tags: NotRequired[List[str]] - bundles: NotRequired[Optional[List[str]]] - cnr_token: NotRequired[str] - check_related_images: NotRequired[bool] - deprecation_list: NotRequired[List[str]] - distribution_scope: NotRequired[str] - fbc_fragment: NotRequired[bool] - force_backport: NotRequired[bool] - from_bundle_image: NotRequired[str] - from_index: NotRequired[str] - labels: NotRequired[Dict[str, str]] - operators: NotRequired[List[str]] - organization: NotRequired[str] - output_fbc: NotRequired[bool] - overwrite_from_index: NotRequired[bool] - overwrite_from_index_token: NotRequired[str] - overwrite_target_index: NotRequired[bool] - overwrite_target_index_token: NotRequired[str] - registry_auths: NotRequired[Dict[str, Any]] - related_bundles: NotRequired[RelatedBundlesMetadata] - source_from_index: NotRequired[str] - target_index: NotRequired[str] - user: NotRequired[str] - - -PayloadTypesUnion = Union[ - AddRequestPayload, - CreateEmptyIndexPayload, - FbcOperationRequestPayload, - MergeIndexImagesPayload, - RecursiveRelatedBundlesRequestPayload, - RegenerateBundlePayload, - RmRequestPayload, -] - -# End of the Payloads Part # Start of the RequestResponses Part diff --git a/iib/web/models.py b/iib/web/models.py index 5d6227db7..47c8c8fe4 100644 --- a/iib/web/models.py +++ b/iib/web/models.py @@ -24,7 +24,6 @@ from iib.web.iib_static_types import ( AddRequestResponse, - AddRmBatchPayload, AddRmRequestResponseBase, BaseClassRequestResponse, BuildRequestState, @@ -33,7 +32,6 @@ FbcOperationRequestResponse, MergeIndexImageRequestResponse, RecursiveRelatedBundlesRequestResponse, - RegenerateBundleBatchPayload, RegenerateBundleRequestResponse, ) @@ -666,32 +664,6 @@ def annotations(self, annotations: Optional[Dict[str, Any]]) -> None: json.dumps(annotations, sort_keys=True) if annotations is not None else None ) - # TODO DELETE - @staticmethod - def validate_batch_request_params( - payload: Union[AddRmBatchPayload, RegenerateBundleBatchPayload] - ) -> None: - """ - Validate batch specific parameters from the input JSON payload. - - The requests in the "build_requests" key's value are not validated. Those should be - validated separately. - - :raises ValidationError: if the payload is invalid - """ - if ( - not isinstance(payload, dict) - or not isinstance(payload.get('build_requests'), list) - or not payload['build_requests'] - ): - raise ValidationError( - 'The input data must be a JSON object and the "build_requests" value must be a ' - 'non-empty array' - ) - - if not isinstance(payload.get('annotations', {}), dict): - raise ValidationError('The value of "annotations" must be a JSON object') - @property def state(self) -> str: """