Skip to content

Commit

Permalink
Merge pull request #36 from nebari-dev/start-stop-server
Browse files Browse the repository at this point in the history
Add start/stop server endpoints
  • Loading branch information
aktech authored Jan 5, 2024
2 parents 3300108 + ecb1f60 commit e486efd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
17 changes: 14 additions & 3 deletions jhub_apps/hub_client/hub_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@ def normalize_server_name(self, servername):
text = text.replace(" ", "-")
return text

def start_server(self, username, servername):
server = self.get_server(username, servername)
if not server:
return None
url = f"/users/{username}/servers/{servername}"
data = {"name": servername}
r = requests.post(API_URL + url, headers=self._headers(), json=data)
r.raise_for_status()
return r.status_code, servername

def create_server(self, username, servername, edit=False, user_options=None):
server = self.get_server(username, servername)
if not edit:
servername = self.normalize_server_name(servername)
servername = f"{servername}-{uuid.uuid4().hex[:7]}"
if server:
if edit:
self.delete_server(username, server["name"])
self.delete_server(username, server["name"], remove=True)
else:
raise ValueError(f"Server: {servername} already exists")
url = f"/users/{username}/servers/{servername}"
Expand All @@ -63,9 +73,10 @@ def create_server(self, username, servername, edit=False, user_options=None):
r.raise_for_status()
return r.status_code, servername

def delete_server(self, username, server_name):
def delete_server(self, username, server_name, remove=False):
url = f"/users/{username}/servers/{server_name}"
params = {"remove": True}
# This will remove it from the database, otherwise it will just stop the server
params = {"remove": remove}
r = requests.delete(API_URL + url, headers=self._headers(), json=params)
r.raise_for_status()
return r.status_code
32 changes: 30 additions & 2 deletions jhub_apps/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ async def get_server(user: User = Depends(get_current_user), server_name=None):
for s_name, server_details in user_servers.items():
if s_name == server_name:
return server_details
return status.HTTP_404_NOT_FOUND
raise HTTPException(
detail=f"server '{server_name}' not found",
status_code=status.HTTP_404_NOT_FOUND,
)
else:
# Get all servers
return user_servers
Expand Down Expand Up @@ -121,6 +124,25 @@ async def create_server(
)


@router.post("/server/{server_name}")
async def start_server(
server_name=None,
user: User = Depends(get_current_user),
):
"""Start an already existing server."""
hub_client = HubClient()
response = hub_client.start_server(
username=user.name,
servername=server_name,
)
if response is None:
raise HTTPException(
detail=f"server '{server_name}' not found",
status_code=status.HTTP_404_NOT_FOUND,
)
return response


@router.put("/server/{server_name}")
async def update_server(
server: ServerCreation = Depends(Checker(ServerCreation)),
Expand All @@ -142,11 +164,17 @@ async def update_server(


@router.delete("/server/{server_name}")
async def delete_server(user: User = Depends(get_current_user), server_name=None):
async def delete_server(
user: User = Depends(get_current_user),
server_name=None,
remove: bool = False,
):
"""Delete or stop server. Delete if remove is True otherwise stop the server"""
hub_client = HubClient()
return hub_client.delete_server(
user.name,
server_name=server_name,
remove=remove
)


Expand Down
48 changes: 46 additions & 2 deletions jhub_apps/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
from unittest.mock import patch

import pytest

from jhub_apps.hub_client.hub_client import HubClient
from jhub_apps.spawner.types import FRAMEWORKS
from jhub_apps.tests.constants import MOCK_USER
Expand Down Expand Up @@ -31,6 +33,7 @@ def test_api_get_server(get_user, client):
get_user.return_value = create_server_response
response = client.get("/server/panel-app")
get_user.assert_called_once_with(MOCK_USER.name)
assert response.status_code == 200
assert response.json() == server_data["panel-app"]


Expand All @@ -54,18 +57,57 @@ def test_api_create_server(create_server, client):
servername="panel-app",
user_options=final_user_options,
)
assert response.status_code == 200
assert response.json() == create_server_response


@patch.object(HubClient, "start_server")
def test_api_start_server(create_server, client):
start_server_response = {"user": "aktech"}
create_server.return_value = start_server_response
server_name = "server-name"
response = client.post(
f"/server/{server_name}",
)
create_server.assert_called_once_with(
username=MOCK_USER.name,
servername=server_name,
)
assert response.status_code == 200
assert response.json() == start_server_response


@patch.object(HubClient, "start_server")
def test_api_start_server_404(start_server, client):
start_server_response = None
start_server.return_value = start_server_response
server_name = "server-name"
response = client.post(
f"/server/{server_name}",
)
start_server.assert_called_once_with(
username=MOCK_USER.name,
servername=server_name,
)
assert response.status_code == 404
assert response.json() == {"detail": "server 'server-name' not found"}


@pytest.mark.parametrize("name,remove", [
('delete', True,),
('stop', False,),
])
@patch.object(HubClient, "delete_server")
def test_api_delete_server(delete_server, client):
def test_api_delete_server(delete_server, name, remove, client):
create_server_response = {"user": "aktech"}
delete_server.return_value = create_server_response
response = client.delete("/server/panel-app")
response = client.delete("/server/panel-app", params={"remove": remove})
delete_server.assert_called_once_with(
MOCK_USER.name,
server_name="panel-app",
remove=remove
)
assert response.status_code == 200
assert response.json() == create_server_response


Expand All @@ -91,6 +133,7 @@ def test_api_update_server(create_server, client):
edit=True,
user_options=final_user_options,
)
assert response.status_code == 200
assert response.json() == create_server_response


Expand All @@ -108,4 +151,5 @@ def test_api_status(client):
response = client.get(
"/status",
)
assert response.status_code == 200
assert response.json() == {"status": "ok"}

0 comments on commit e486efd

Please sign in to comment.