-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial CRUD API for workspaces (#620)
This adds a simple and unimplemented REST API for workspaces. Workspaces will be the base for all other resources in terms of REST resource mapping, so these go first. These are initially left entirely unimplemented as #600 needs to merge Signed-off-by: Juan Antonio Osorio <ozz@stacklok.com>
- Loading branch information
Showing
7 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from fastapi import APIRouter, Response | ||
from fastapi.exceptions import HTTPException | ||
from fastapi.routing import APIRoute | ||
|
||
from codegate.api import v1_models | ||
from codegate.pipeline.workspace import commands as wscmd | ||
|
||
v1 = APIRouter() | ||
wscrud = wscmd.WorkspaceCrud() | ||
|
||
|
||
def uniq_name(route: APIRoute): | ||
return f"v1_{route.name}" | ||
|
||
|
||
@v1.get("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name) | ||
async def list_workspaces() -> v1_models.ListWorkspacesResponse: | ||
"""List all workspaces.""" | ||
wslist = await wscrud.get_workspaces() | ||
|
||
resp = v1_models.ListWorkspacesResponse.from_db_workspaces(wslist) | ||
|
||
return resp | ||
|
||
|
||
@v1.get("/workspaces/active", tags=["Workspaces"], generate_unique_id_function=uniq_name) | ||
async def list_active_workspaces() -> v1_models.ListActiveWorkspacesResponse: | ||
"""List all active workspaces. | ||
In it's current form, this function will only return one workspace. That is, | ||
the globally active workspace.""" | ||
activews = await wscrud.get_active_workspace() | ||
|
||
resp = v1_models.ListActiveWorkspacesResponse.from_db_workspaces(activews) | ||
|
||
return resp | ||
|
||
|
||
@v1.post("/workspaces/active", tags=["Workspaces"], generate_unique_id_function=uniq_name) | ||
async def activate_workspace(request: v1_models.ActivateWorkspaceRequest, status_code=204): | ||
"""Activate a workspace by name.""" | ||
activated = await wscrud.activate_workspace(request.name) | ||
|
||
# TODO: Refactor | ||
if not activated: | ||
return HTTPException(status_code=409, detail="Workspace already active") | ||
|
||
return Response(status_code=204) | ||
|
||
|
||
@v1.post("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name, status_code=201) | ||
async def create_workspace(request: v1_models.CreateWorkspaceRequest): | ||
"""Create a new workspace.""" | ||
# Input validation is done in the model | ||
created = await wscrud.add_workspace(request.name) | ||
|
||
# TODO: refactor to use a more specific exception | ||
if not created: | ||
raise HTTPException(status_code=400, detail="Failed to create workspace") | ||
|
||
return v1_models.Workspace(name=request.name) | ||
|
||
|
||
|
||
@v1.delete("/workspaces/{workspace_name}", tags=["Workspaces"], | ||
generate_unique_id_function=uniq_name, status_code=204) | ||
async def delete_workspace(workspace_name: str): | ||
"""Delete a workspace by name.""" | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Any, List, Optional | ||
|
||
import pydantic | ||
|
||
from codegate.db import models as db_models | ||
|
||
|
||
class Workspace(pydantic.BaseModel): | ||
name: str | ||
is_active: bool | ||
|
||
class ActiveWorkspace(Workspace): | ||
# TODO: use a more specific type for last_updated | ||
last_updated: Any | ||
|
||
class ListWorkspacesResponse(pydantic.BaseModel): | ||
workspaces: list[Workspace] | ||
|
||
@classmethod | ||
def from_db_workspaces( | ||
cls, db_workspaces: List[db_models.WorkspaceActive])-> "ListWorkspacesResponse": | ||
return cls(workspaces=[ | ||
Workspace(name=ws.name, is_active=ws.active_workspace_id is not None) | ||
for ws in db_workspaces]) | ||
|
||
class ListActiveWorkspacesResponse(pydantic.BaseModel): | ||
workspaces: list[ActiveWorkspace] | ||
|
||
@classmethod | ||
def from_db_workspaces( | ||
cls, ws: Optional[db_models.ActiveWorkspace]) -> "ListActiveWorkspacesResponse": | ||
if ws is None: | ||
return cls(workspaces=[]) | ||
return cls(workspaces=[ | ||
ActiveWorkspace(name=ws.name, | ||
is_active=True, | ||
last_updated=ws.last_update) | ||
]) | ||
|
||
class CreateWorkspaceRequest(pydantic.BaseModel): | ||
name: str | ||
|
||
class ActivateWorkspaceRequest(pydantic.BaseModel): | ||
name: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters