Skip to content

Commit

Permalink
fix: properly serialize v1 jupyter servers
Browse files Browse the repository at this point in the history
  • Loading branch information
olevski committed Dec 11, 2024
1 parent 518ca49 commit 85aeb5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
16 changes: 4 additions & 12 deletions components/renku_data_services/notebooks/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
from renku_data_services.notebooks.api.schemas.cloud_storage import RCloneStorage
from renku_data_services.notebooks.api.schemas.config_server_options import ServerOptionsEndpointResponse
from renku_data_services.notebooks.api.schemas.logs import ServerLogs
from renku_data_services.notebooks.api.schemas.servers_get import (
NotebookResponse,
ServersGetResponse,
)
from renku_data_services.notebooks.config import NotebooksConfig
from renku_data_services.notebooks.crs import (
Affinity,
Expand Down Expand Up @@ -110,7 +106,7 @@ async def _user_servers(
) -> JSONResponse:
filter_attrs = list(filter(lambda x: x[1] is not None, request.get_query_args()))
filtered_servers = await core.user_servers(self.nb_config, user, filter_attrs)
return json(ServersGetResponse().dump({"servers": filtered_servers}))
return core.serialize_v1_servers(filtered_servers, self.nb_config)

return "/notebooks/servers", ["GET"], _user_servers

Expand All @@ -122,7 +118,7 @@ async def _user_server(
request: Request, user: AnonymousAPIUser | AuthenticatedAPIUser, server_name: str
) -> JSONResponse:
server = await core.user_server(self.nb_config, user, server_name)
return json(NotebookResponse().dump(server))
return core.serialize_v1_server(server, self.nb_config)

return "/notebooks/servers/<server_name>", ["GET"], _user_server

Expand All @@ -138,7 +134,7 @@ async def _launch_notebook(
body: apispec.LaunchNotebookRequestOld,
) -> JSONResponse:
server, status_code = await core.launch_notebook(self.nb_config, user, internal_gitlab_user, body)
return json(NotebookResponse().dump(server), status_code)
return core.serialize_v1_server(server, self.nb_config, status_code)

return "/notebooks/servers", ["POST"], _launch_notebook

Expand All @@ -158,11 +154,7 @@ async def _patch_server(
raise AnonymousUserPatchError()

manifest = await core.patch_server(self.nb_config, user, internal_gitlab_user, server_name, body)
notebook_response = apispec.NotebookResponse.parse_obj(manifest)
return json(
notebook_response.model_dump(),
200,
)
return core.serialize_v1_server(manifest, self.nb_config)

return "/notebooks/servers/<server_name>", ["PATCH"], _patch_server

Expand Down
22 changes: 21 additions & 1 deletion components/renku_data_services/notebooks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
from gitlab.const import Visibility as GitlabVisibility
from gitlab.v4.objects.projects import Project as GitlabProject
from sanic.log import logger
from sanic.response import JSONResponse

from renku_data_services.base_models import AnonymousAPIUser, APIUser, AuthenticatedAPIUser
from renku_data_services.base_models.validation import validated_json
from renku_data_services.errors import errors
from renku_data_services.notebooks import apispec
from renku_data_services.notebooks.api.classes.auth import GitlabToken, RenkuTokens
Expand All @@ -23,6 +25,7 @@
from renku_data_services.notebooks.api.schemas.cloud_storage import RCloneStorage
from renku_data_services.notebooks.api.schemas.secrets import K8sUserSecrets
from renku_data_services.notebooks.api.schemas.server_options import ServerOptions
from renku_data_services.notebooks.api.schemas.servers_get import NotebookResponse
from renku_data_services.notebooks.api.schemas.servers_patch import PatchServerStatusEnum
from renku_data_services.notebooks.config import NotebooksConfig
from renku_data_services.notebooks.errors import intermittent
Expand Down Expand Up @@ -64,7 +67,7 @@ def notebooks_info(config: NotebooksConfig) -> dict:

async def user_servers(
config: NotebooksConfig, user: AnonymousAPIUser | AuthenticatedAPIUser, filter_attrs: list[dict]
) -> dict:
) -> dict[str, UserServerManifest]:
"""Returns a filtered list of servers for the given user."""

servers = [
Expand Down Expand Up @@ -602,3 +605,20 @@ async def launch_notebook(
repositories=None,
internal_gitlab_user=internal_gitlab_user,
)


def serialize_v1_server(manifest: UserServerManifest, nb_config: NotebooksConfig, status: int = 200) -> JSONResponse:
"""Format and serialize a Renku v1 JupyterServer manifest."""
data = NotebookResponse().dump(NotebookResponse.format_user_pod_data(manifest, nb_config))
return validated_json(apispec.NotebookResponse, data, status=status)


def serialize_v1_servers(
manifests: dict[str, UserServerManifest], nb_config: NotebooksConfig, status: int = 200
) -> JSONResponse:
"""Format and serialize many Renku v1 JupyterServer manifests."""
data = {
manifest.server_name: NotebookResponse().dump(NotebookResponse.format_user_pod_data(manifest, nb_config))
for manifest in sorted(manifests.values(), key=lambda x: x.server_name)
}
return validated_json(apispec.ServersGetResponse, {"servers": data}, status=status)

0 comments on commit 85aeb5d

Please sign in to comment.