From 2c4c3ee585121756822528069008c9c02855156f Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:53:42 +0100 Subject: [PATCH 1/4] fix datetime --- .../models/comp_runs.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/services/director-v2/src/simcore_service_director_v2/models/comp_runs.py b/services/director-v2/src/simcore_service_director_v2/models/comp_runs.py index 2c671d2c146..aba02269415 100644 --- a/services/director-v2/src/simcore_service_director_v2/models/comp_runs.py +++ b/services/director-v2/src/simcore_service_director_v2/models/comp_runs.py @@ -95,8 +95,10 @@ def convert_null_to_empty_metadata(cls, v): "cluster_id": 0, "iteration": 42, "result": "UNKNOWN", - "created": "2021-03-01 13:07:34.19161", - "modified": "2021-03-01 13:07:34.19161", + "started": None, + "ended": None, + "created": "2021-03-01T13:07:34.191610", + "modified": "2021-03-01T13:07:34.191610", "cancelled": None, "use_on_demand_clusters": False, }, @@ -107,8 +109,10 @@ def convert_null_to_empty_metadata(cls, v): "cluster_id": None, # this default to DEFAULT_CLUSTER_ID "iteration": 42, "result": "NOT_STARTED", - "created": "2021-03-01 13:07:34.19161", - "modified": "2021-03-01 13:07:34.19161", + "started": None, + "ended": None, + "created": "2021-03-01T13:07:34.191610", + "modified": "2021-03-01T13:07:34.191610", "cancelled": None, "use_on_demand_clusters": False, }, @@ -119,10 +123,10 @@ def convert_null_to_empty_metadata(cls, v): "cluster_id": 123, "iteration": 12, "result": "SUCCESS", - "created": "2021-03-01 13:07:34.19161", - "modified": "2021-03-01 13:07:34.19161", - "started": "2021-03-01 08:07:34.19161", - "ended": "2021-03-01 13:07:34.10", + "created": "2021-03-01T13:07:34.191610", + "modified": "2021-03-01T13:07:34.191610", + "started": "2021-03-01T08:07:34.191610", + "ended": "2021-03-01T13:07:34.10", "cancelled": None, "metadata": { "node_id_names_map": {}, @@ -140,10 +144,10 @@ def convert_null_to_empty_metadata(cls, v): "cluster_id": 123, "iteration": 12, "result": "SUCCESS", - "created": "2021-03-01 13:07:34.19161", - "modified": "2021-03-01 13:07:34.19161", - "started": "2021-03-01 8:07:34.19161", - "ended": "2021-03-01 13:07:34.10", + "created": "2021-03-01T13:07:34.191610", + "modified": "2021-03-01T13:07:34.191610", + "started": "2021-03-01T08:07:34.191610", + "ended": "2021-03-01T13:07:34.10", "cancelled": None, "metadata": None, "use_on_demand_clusters": False, From c71c2c22110e2e0537f8c389478e45cf070c1ecf Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:06:35 +0100 Subject: [PATCH 2/4] use after validator --- .../src/models_library/clusters.py | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/packages/models-library/src/models_library/clusters.py b/packages/models-library/src/models_library/clusters.py index 8b38913c246..fa0e79e5ba5 100644 --- a/packages/models-library/src/models_library/clusters.py +++ b/packages/models-library/src/models_library/clusters.py @@ -1,8 +1,7 @@ from enum import auto from pathlib import Path -from typing import Final, Literal, TypeAlias +from typing import Final, Literal, Self, TypeAlias -from models_library.utils._original_fastapi_encoders import jsonable_encoder from pydantic import ( AnyUrl, BaseModel, @@ -222,29 +221,22 @@ class Cluster(BaseCluster): }, ) - @model_validator(mode="before") - @classmethod - def check_owner_has_access_rights(cls, values): - values = jsonable_encoder(values) - - is_default_cluster = bool(values["id"] == DEFAULT_CLUSTER_ID) - owner_gid = values["owner"] + @model_validator(mode="after") + def check_owner_has_access_rights(self) -> Self: + is_default_cluster = bool(self.id == DEFAULT_CLUSTER_ID) + owner_gid = self.owner # check owner is in the access rights, if not add it - access_rights = values.get("access_rights", values.get("accessRights", {})) + access_rights = self.access_rights.copy() if owner_gid not in access_rights: access_rights[owner_gid] = ( - CLUSTER_USER_RIGHTS.model_dump() - if is_default_cluster - else CLUSTER_ADMIN_RIGHTS.model_dump() + CLUSTER_USER_RIGHTS if is_default_cluster else CLUSTER_ADMIN_RIGHTS ) # check owner has the expected access if access_rights[owner_gid] != ( - CLUSTER_USER_RIGHTS.model_dump() - if is_default_cluster - else CLUSTER_ADMIN_RIGHTS.model_dump() + CLUSTER_USER_RIGHTS if is_default_cluster else CLUSTER_ADMIN_RIGHTS ): msg = f"the cluster owner access rights are incorrectly set: {access_rights[owner_gid]}" raise ValueError(msg) - values["access_rights"] = access_rights - return values + self.access_rights = access_rights + return self From 676b32031bff5044a9d9b36f86ea353cd2a78de2 Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:17:15 +0100 Subject: [PATCH 3/4] fix test --- packages/models-library/src/models_library/clusters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/models-library/src/models_library/clusters.py b/packages/models-library/src/models_library/clusters.py index fa0e79e5ba5..7df17412ec4 100644 --- a/packages/models-library/src/models_library/clusters.py +++ b/packages/models-library/src/models_library/clusters.py @@ -222,7 +222,7 @@ class Cluster(BaseCluster): ) @model_validator(mode="after") - def check_owner_has_access_rights(self) -> Self: + def check_owner_has_access_rights(self: Self) -> Self: is_default_cluster = bool(self.id == DEFAULT_CLUSTER_ID) owner_gid = self.owner @@ -238,5 +238,5 @@ def check_owner_has_access_rights(self) -> Self: ): msg = f"the cluster owner access rights are incorrectly set: {access_rights[owner_gid]}" raise ValueError(msg) - self.access_rights = access_rights + object.__setattr__(self, "access_rights", access_rights) return self From b4565b3743d2841a1758ad10e8f8b854b59b54db Mon Sep 17 00:00:00 2001 From: sanderegg <35365065+sanderegg@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:32:38 +0100 Subject: [PATCH 4/4] @pcrespov review: add doc --- packages/models-library/src/models_library/clusters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/models-library/src/models_library/clusters.py b/packages/models-library/src/models_library/clusters.py index 7df17412ec4..c857073dead 100644 --- a/packages/models-library/src/models_library/clusters.py +++ b/packages/models-library/src/models_library/clusters.py @@ -238,5 +238,6 @@ def check_owner_has_access_rights(self: Self) -> Self: ): msg = f"the cluster owner access rights are incorrectly set: {access_rights[owner_gid]}" raise ValueError(msg) + # NOTE: overcomes frozen configuration (far fetched in ClusterGet model of webserver) object.__setattr__(self, "access_rights", access_rights) return self