diff --git a/cli/src/etos_client/etos/v0/schema/request.py b/cli/src/etos_client/etos/v0/schema/request.py index 2fe77902..9e1974fd 100644 --- a/cli/src/etos_client/etos/v0/schema/request.py +++ b/cli/src/etos_client/etos/v0/schema/request.py @@ -13,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""ETOS v0 schema.""" +"""ETOS v0 request schema.""" import json from uuid import UUID diff --git a/cli/src/etos_client/etos/v0/schema/response.py b/cli/src/etos_client/etos/v0/schema/response.py index 31c3d924..ae301b6a 100644 --- a/cli/src/etos_client/etos/v0/schema/response.py +++ b/cli/src/etos_client/etos/v0/schema/response.py @@ -13,7 +13,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""ETOS v0 response.""" +"""ETOS v0 response schema.""" from uuid import UUID from pydantic import BaseModel diff --git a/cli/src/etos_client/etos/v1alpha/etos.py b/cli/src/etos_client/etos/v1alpha/etos.py index 6daabfa8..bba6a9c6 100644 --- a/cli/src/etos_client/etos/v1alpha/etos.py +++ b/cli/src/etos_client/etos/v1alpha/etos.py @@ -35,8 +35,8 @@ from etos_client.etos.v0.test_results import TestResults from etos_client.etos.v0.event_repository import graphql from etos_client.etos.v0.test_run import TestRun -from etos_client.etos.v0.schema.response import ResponseSchema -from etos_client.etos.v0.schema.request import RequestSchema +from etos_client.etos.v1alpha.schema.response import ResponseSchema +from etos_client.etos.v1alpha.schema.request import RequestSchema # Max total time for a ping request including delays with backoff factor 0.5 will be: diff --git a/cli/src/etos_client/etos/v1alpha/schema/__init__.py b/cli/src/etos_client/etos/v1alpha/schema/__init__.py new file mode 100644 index 00000000..68455975 --- /dev/null +++ b/cli/src/etos_client/etos/v1alpha/schema/__init__.py @@ -0,0 +1,16 @@ +# Copyright Axis Communications AB. +# +# For a full list of individual contributors, please see the commit history. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""ETOSv0 schemas.""" diff --git a/cli/src/etos_client/etos/v1alpha/schema/request.py b/cli/src/etos_client/etos/v1alpha/schema/request.py new file mode 100644 index 00000000..e3533e33 --- /dev/null +++ b/cli/src/etos_client/etos/v1alpha/schema/request.py @@ -0,0 +1,73 @@ +# Copyright Axis Communications AB. +# +# For a full list of individual contributors, please see the commit history. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""ETOS v1 request schema.""" +import json +from uuid import UUID + +from typing import Optional, Union +from pydantic import BaseModel, ValidationInfo, field_validator + + +class RequestSchema(BaseModel): + """Schema for ETOSv1 API requests.""" + + artifact_id: Optional[str] + artifact_identity: Optional[str] + test_suite_url: str + dataset: Optional[Union[dict, list]] = {} + execution_space_provider: Optional[str] = "default" + iut_provider: Optional[str] = "default" + log_area_provider: Optional[str] = "default" + + @classmethod + def from_args(cls, args: dict) -> "RequestSchema": + """Create a RequestSchema from an argument list from argparse.""" + return RequestSchema( + artifact_id=args["--identity"], + artifact_identity=args["--identity"], + test_suite_url=args["--test-suite"], + dataset=args["--dataset"], + execution_space_provider=args["--execution-space-provider"] or "default", + iut_provider=args["--iut-provider"] or "default", + log_area_provider=args["--log-area-provider"] or "default", + ) + + @field_validator("artifact_identity") + def trim_identity_if_necessary( + cls, artifact_identity: Optional[str], info: ValidationInfo + ) -> Optional[str]: + """Trim identity if id is set.""" + if info.data.get("artifact_id") is not None: + return None + return artifact_identity + + @field_validator("artifact_id") + def is_uuid(cls, artifact_id: Optional[str]) -> Optional[str]: + """Test if string is a valid UUID v4.""" + try: + UUID(artifact_id, version=4) + except ValueError: + return None + return artifact_id + + @field_validator("dataset") + def dataset_list_trimming(cls, dataset: Optional[Union[dict, list]]) -> list[dict]: + """Trim away list completely should the dataset only contain a single index.""" + if dataset is None: + return [{}] + if len(dataset) == 1: + return json.loads(dataset[0]) + return [json.loads(data) for data in dataset] diff --git a/cli/src/etos_client/etos/v1alpha/schema/response.py b/cli/src/etos_client/etos/v1alpha/schema/response.py new file mode 100644 index 00000000..c69aeae7 --- /dev/null +++ b/cli/src/etos_client/etos/v1alpha/schema/response.py @@ -0,0 +1,37 @@ +# Copyright Axis Communications AB. +# +# For a full list of individual contributors, please see the commit history. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""ETOS v1 response schema.""" +from uuid import UUID +from pydantic import BaseModel + + +class ResponseSchema(BaseModel): + """Response model for the ETOS start API.""" + + event_repository: str + tercc: UUID + artifact_id: UUID + artifact_identity: str + + @classmethod + def from_response(cls, response: dict) -> "ResponseSchema": + """Create a ResponseSchema from a dictionary. Typically used for ETOS API http response.""" + return ResponseSchema( + event_repository=response.get("event_repository"), # type: ignore + tercc=response.get("tercc"), # type: ignore + artifact_id=response.get("artifact_id"), # type: ignore + artifact_identity=response.get("artifact_identity"), # type: ignore + )