From 0637edc7b5d9b6cd89edaed8b8ebfb049068cbd3 Mon Sep 17 00:00:00 2001 From: Ivan Zubenko Date: Mon, 5 Jun 2023 09:59:54 +0300 Subject: [PATCH 1/3] support NO_ORG job filtering --- platform_api/handlers/jobs_handler.py | 9 ++++++++- tests/unit/test_models.py | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/platform_api/handlers/jobs_handler.py b/platform_api/handlers/jobs_handler.py index 7895656d0..aea67e528 100644 --- a/platform_api/handlers/jobs_handler.py +++ b/platform_api/handlers/jobs_handler.py @@ -1088,7 +1088,7 @@ def create_from_query(self, query: MultiDictProxy) -> JobFilter: # type: ignore for cluster_name in query.getall("cluster_name", []) } orgs = { - self._org_name_validator.check(org_name) + self._parse_org_name(org_name) for org_name in query.getall("org_name", []) } projects = { @@ -1134,6 +1134,13 @@ def create_from_query(self, query: MultiDictProxy) -> JobFilter: # type: ignore **bool_filters, # type: ignore ) + def _parse_org_name(self, org_name: str) -> Optional[str]: + return ( + None + if org_name.upper() == "NO_ORG" + else self._org_name_validator.check(org_name) + ) + @dataclass(frozen=True) class BulkJobFilter: diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index ecd07e402..87b1eaa04 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -1075,6 +1075,11 @@ def test_create_from_query(self) -> None: query = MultiDict([("logs_removed", "False")]) assert factory(query) == JobFilter(logs_removed=False) + query = MultiDict( + [("org_name", "NO_ORG"), ("org_name", "org1"), ("org_name", "org2")] + ) + assert factory(query) == JobFilter(orgs={None, "org1", "org2"}) + def test_create_from_query_with_status(self) -> None: factory = JobFilterFactory().create_from_query @@ -1185,6 +1190,7 @@ def test_create_from_query_by_hostname_with_status(self) -> None: [("hostname", "testjob--johndoe.example.org"), ("project_name", "johndoe")], [("hostname", "TESTJOB--johndoe.example.org")], [("hostname", "testjob--JOHNDOE.example.org")], + [("org_name", "invalid_org")], ], ) def test_create_from_query_fail(self, query: Any) -> None: From fa8262e94f094cebb8e5450bf6a1e843ce6791f3 Mon Sep 17 00:00:00 2001 From: Ivan Zubenko Date: Mon, 5 Jun 2023 17:38:33 +0300 Subject: [PATCH 2/3] address comments --- platform_api/config.py | 2 ++ platform_api/handlers/jobs_handler.py | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/platform_api/config.py b/platform_api/config.py index a48aedd66..6d8655354 100644 --- a/platform_api/config.py +++ b/platform_api/config.py @@ -14,6 +14,8 @@ STORAGE_URI_SCHEME = "storage" +NO_ORG = "NO_ORG" + class StorageType(str, Enum): HOST = "host" diff --git a/platform_api/handlers/jobs_handler.py b/platform_api/handlers/jobs_handler.py index aea67e528..98c96966c 100644 --- a/platform_api/handlers/jobs_handler.py +++ b/platform_api/handlers/jobs_handler.py @@ -9,7 +9,6 @@ import aiohttp.web import iso8601 import trafaret as t -import trafaret.keys from aiohttp_security import check_authorized from multidict import MultiDictProxy from neuro_auth_client import ( @@ -22,7 +21,7 @@ from yarl import URL from platform_api.cluster_config import ClusterConfig -from platform_api.config import STORAGE_URI_SCHEME, Config +from platform_api.config import NO_ORG, STORAGE_URI_SCHEME, Config from platform_api.log import log_debug_time from platform_api.orchestrator.job import ( JOB_USER_NAMES_SEPARATOR, @@ -1137,7 +1136,7 @@ def create_from_query(self, query: MultiDictProxy) -> JobFilter: # type: ignore def _parse_org_name(self, org_name: str) -> Optional[str]: return ( None - if org_name.upper() == "NO_ORG" + if org_name.upper() == NO_ORG else self._org_name_validator.check(org_name) ) From 984128e7f81337645e6b1e4bebeab15016869229 Mon Sep 17 00:00:00 2001 From: Ivan Zubenko Date: Mon, 5 Jun 2023 18:42:00 +0300 Subject: [PATCH 3/3] Update jobs_handler.py --- platform_api/handlers/jobs_handler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/platform_api/handlers/jobs_handler.py b/platform_api/handlers/jobs_handler.py index 98c96966c..6a6f5fd2e 100644 --- a/platform_api/handlers/jobs_handler.py +++ b/platform_api/handlers/jobs_handler.py @@ -9,6 +9,7 @@ import aiohttp.web import iso8601 import trafaret as t +import trafaret.keys from aiohttp_security import check_authorized from multidict import MultiDictProxy from neuro_auth_client import (