Skip to content

Commit

Permalink
Fix filtering for shared servers (nebari-dev#285)
Browse files Browse the repository at this point in the history
* Fix filtering for shared servers

* remove unused params

* add tests and minor refactoring
  • Loading branch information
aktech authored May 20, 2024
1 parent 197ac4d commit 759363b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
16 changes: 1 addition & 15 deletions jhub_apps/service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
get_conda_envs,
get_jupyterhub_config,
get_spawner_profiles,
get_thumbnail_data_url,
get_thumbnail_data_url, get_shared_servers,
)
from jhub_apps.spawner.types import FRAMEWORKS
from jhub_apps.version import get_version
Expand Down Expand Up @@ -94,20 +94,6 @@ async def login(request: Request):
return RedirectResponse(authorization_url, status_code=302)


def get_shared_servers(user_servers, current_hub_user):
user_servers_without_default_jlab = list(filter(lambda server: server["name"] != "", user_servers.values()))
# Filter servers shared with the user
hub_client = HubClient(username=current_hub_user["name"])
shared_servers = hub_client.get_shared_servers()
shared_server_names = {shared_server["server"]["name"] for shared_server in shared_servers}
shared_servers_rich = [
server for server in user_servers_without_default_jlab
if server["name"] in shared_server_names
]
return shared_servers_rich



@router.get("/server/", description="Get all servers")
@router.get("/server/{server_name}", description="Get a server by server name")
async def get_server(user: User = Depends(get_current_user), server_name=None):
Expand Down
19 changes: 19 additions & 0 deletions jhub_apps/service/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import itertools

import structlog
import os
Expand All @@ -9,6 +10,7 @@
from jupyterhub.app import JupyterHub
from traitlets.config import LazyConfigValue

from jhub_apps.hub_client.hub_client import HubClient
from jhub_apps.spawner.types import FrameworkConf, FRAMEWORKS_MAPPING
from slugify import slugify

Expand Down Expand Up @@ -145,3 +147,20 @@ def get_theme(config):
return config.JupyterHub.template_vars
else:
return None


def get_shared_servers(current_hub_user):
# Filter servers shared with the user
hub_client_service = HubClient()
all_users_servers = list(itertools.chain.from_iterable([
list(user['servers'].values()) for user in hub_client_service.get_users()
]))
user_servers_without_default_jlab = list(filter(lambda server: server["name"] != "", all_users_servers))
hub_client_user = HubClient(username=current_hub_user['name'])
shared_servers = hub_client_user.get_shared_servers()
shared_server_names = {shared_server["server"]["name"] for shared_server in shared_servers}
shared_servers_rich = [
server for server in user_servers_without_default_jlab
if server["name"] in shared_server_names
]
return shared_servers_rich
38 changes: 38 additions & 0 deletions jhub_apps/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from jhub_apps.hub_client.hub_client import HubClient
from jhub_apps.service.utils import get_shared_servers
from jhub_apps.spawner.types import FRAMEWORKS
from jhub_apps.tests.constants import MOCK_USER

Expand Down Expand Up @@ -144,6 +145,43 @@ def test_api_update_server(edit_server, client):
assert response.json() == create_server_response


@patch.object(HubClient, "get_users")
@patch.object(HubClient, "get_shared_servers")
def test_shared_server_filtering(hub_get_shared_servers, get_users):
current_hub_user = {"name": "fakeuser"}
get_users.return_value = [
{
"servers": {
'': {'name': ''},
"panel-12": {"name": "panel-12"}
}
},
{
"servers": {
'': {'name': ''},
"panel-34": {"name": "panel-34", "fullname": "panel shared 34"}
}
},
{
"servers": {
'': {'name': ''},
"panel-56": {"name": "panel-56", "fullname": "panel shared server"}
}
}
]
hub_get_shared_servers.return_value = [
{"server": {"name": "panel-56"}},
{"server": {"name": "panel-34"}},
]
shared_servers = get_shared_servers(current_hub_user)
assert shared_servers == [
{"name": "panel-34", "fullname": "panel shared 34"},
{"name": "panel-56", "fullname": "panel shared server"}
]
hub_get_shared_servers.assert_called_once_with()
get_users.assert_called_once_with()


def test_api_frameworks(client):
response = client.get(
"/frameworks",
Expand Down

0 comments on commit 759363b

Please sign in to comment.