Skip to content

Commit 7b44f99

Browse files
authored
refactor: use typed patch instance for platform config updates (#499)
See #485.
1 parent 829e5d8 commit 7b44f99

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

components/renku_data_services/platform/blueprints.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from renku_data_services.base_api.etag import extract_if_none_match, if_match_required
1313
from renku_data_services.base_models.validation import validated_json
1414
from renku_data_services.platform import apispec
15+
from renku_data_services.platform.core import validate_platform_config_patch
1516
from renku_data_services.platform.db import PlatformRepository
1617

1718

@@ -54,8 +55,8 @@ def patch_singleton_configuration(self) -> BlueprintFactoryResponse:
5455
async def _patch_singleton_configuration(
5556
_: Request, user: base_models.APIUser, body: apispec.PlatformConfigPatch, etag: str
5657
) -> JSONResponse:
57-
body_dict = body.model_dump(exclude_none=True)
58-
config = await self.platform_repo.update_config(user=user, etag=etag, **body_dict)
58+
platform_config_patch = validate_platform_config_patch(body)
59+
config = await self.platform_repo.update_config(user=user, etag=etag, patch=platform_config_patch)
5960
headers = {"ETag": config.etag}
6061
return validated_json(
6162
apispec.PlatformConfig,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Business logic for the platform configuration."""
2+
3+
from renku_data_services.platform import apispec, models
4+
5+
6+
def validate_platform_config_patch(patch: apispec.PlatformConfigPatch) -> models.PlatformConfigPatch:
7+
"""Validate the update to the platform configuration."""
8+
return models.PlatformConfigPatch(incident_banner=patch.incident_banner)

components/renku_data_services/platform/db.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ async def get_or_create_config(self) -> models.PlatformConfig:
3030
await session.refresh(config)
3131
return config.dump()
3232

33-
async def update_config(self, user: base_models.APIUser, etag: str, **kwargs: dict) -> models.PlatformConfig:
33+
async def update_config(
34+
self, user: base_models.APIUser, etag: str, patch: models.PlatformConfigPatch
35+
) -> models.PlatformConfig:
3436
"""Update the platform configuration."""
3537
if user.id is None:
3638
raise errors.UnauthorizedError(message="You do not have the required permissions for this operation.")
@@ -47,9 +49,8 @@ async def update_config(self, user: base_models.APIUser, etag: str, **kwargs: di
4749
if current_etag != etag:
4850
raise errors.ConflictError(message=f"Current ETag is {current_etag}, not {etag}.")
4951

50-
for key, value in kwargs.items():
51-
if key in ["incident_banner"]:
52-
setattr(config, key, value)
52+
if patch.incident_banner is not None:
53+
config.incident_banner = patch.incident_banner
5354

5455
await session.flush()
5556
await session.refresh(config)

components/renku_data_services/platform/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Models for Sessions."""
1+
"""Models for the platform configuration."""
22

33
from dataclasses import dataclass, field
44
from datetime import UTC, datetime
@@ -26,3 +26,10 @@ class PlatformConfig:
2626
def etag(self) -> str:
2727
"""Entity tag value for this project object."""
2828
return compute_etag_from_timestamp(self.updated_at, include_quotes=True)
29+
30+
31+
@dataclass(frozen=True, eq=True, kw_only=True)
32+
class PlatformConfigPatch:
33+
"""Model for changes requested on the platform configuration."""
34+
35+
incident_banner: str | None = None

0 commit comments

Comments
 (0)