Skip to content

Commit

Permalink
feat(ws): This PR builds on top of: kubeflow#61 and kubeflow#65
Browse files Browse the repository at this point in the history
In this PR:
- Created handlers and repositories for get workspacekinds

This PR closes kubeflow#51

Signed-off-by: Eder Ignatowicz <ignatowicz@gmail.com>
  • Loading branch information
ederign committed Oct 10, 2024
1 parent 26f8b82 commit ac16aa5
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 20 deletions.
40 changes: 24 additions & 16 deletions workspaces/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ make run PORT=8000
```
### Endpoints

| URL Pattern | Handler | Action |
|----------------------------------------------|----------------------|-----------------------------------------|
| GET /api/v1/healthcheck | HealthcheckHandler | Show application information. |
| GET /api/v1/workspaces | GetWorkspacesHandler | Get all Workspaces |
| GET /api/v1/workspaces/{namespace} | GetWorkspacesHandler | Get all Workspaces from a namespace |
| POST /api/v1/workspaces/{namespace} | GetWorkspacesHandler | Create a Workspace in a given namespace |
| GET /api/v1/workspaces/{namespace}/{name} | GetWorkspacesHandler | Get a Workspace entity |
| PATCH /api/v1/workspaces/{namespace}/{name} | TBD | Patch a Workspace entity |
| PUT /api/v1/workspaces/{namespace}/{name} | TBD | Update a Workspace entity |
| DELETE /api/v1/workspaces/{namespace}/{name} | GetWorkspacesHandler | Delete a Workspace entity |
| GET /api/v1/workspacekinds | TBD | Get all WorkspaceKind |
| POST /api/v1/workspacekinds | TBD | Create a WorkspaceKind |
| GET /api/v1/workspacekinds/{name} | TBD | Get a WorkspaceKind entity |
| PATCH /api/v1/workspacekinds/{name} | TBD | Patch a WorkspaceKind entity |
| PUT /api/v1/workspacekinds/{name} | TBD | Update a WorkspaceKind entity |
| DELETE /api/v1/workspacekinds/{name} | TBD | Delete a WorkspaceKind entity |
| URL Pattern | Handler | Action |
|----------------------------------------------|------------------------|-----------------------------------------|
| GET /api/v1/healthcheck | healthcheck_handler | Show application information. |
| GET /api/v1/workspaces | workspaces_handler | Get all Workspaces |
| GET /api/v1/workspaces/{namespace} | workspaces_handler | Get all Workspaces from a namespace |
| POST /api/v1/workspaces/{namespace} | workspaces_handler | Create a Workspace in a given namespace |
| GET /api/v1/workspaces/{namespace}/{name} | workspaces_handler | Get a Workspace entity |
| PATCH /api/v1/workspaces/{namespace}/{name} | TBD | Patch a Workspace entity |
| PUT /api/v1/workspaces/{namespace}/{name} | TBD | Update a Workspace entity |
| DELETE /api/v1/workspaces/{namespace}/{name} | workspaces_handler | Delete a Workspace entity |
| GET /api/v1/workspacekinds | workspacekinds_handler | Get all WorkspaceKind |
| POST /api/v1/workspacekinds | TBD | Create a WorkspaceKind |
| GET /api/v1/workspacekinds/{name} | workspacekinds_handler | Get a WorkspaceKind entity |
| PATCH /api/v1/workspacekinds/{name} | TBD | Patch a WorkspaceKind entity |
| PUT /api/v1/workspacekinds/{name} | TBD | Update a WorkspaceKind entity |
| DELETE /api/v1/workspacekinds/{name} | TBD | Delete a WorkspaceKind entity |

### Sample local calls
```
Expand Down Expand Up @@ -83,3 +83,11 @@ curl -i localhost:4000/api/v1/workspaces/default/dora
# DELETE /api/v1/workspaces/{namespace}/{name}
curl -X DELETE localhost:4000/api/v1/workspaces/workspace-test/dora
```
```
# GET /api/v1/workspacekinds
curl -i localhost:4000/api/v1/workspacekinds
```
```
# GET /api/v1/workspacekinds/{name}
curl -i localhost:4000/api/v1/workspacekinds/jupyterlab
```
8 changes: 8 additions & 0 deletions workspaces/backend/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const (

WorkspaceNamePathParam = "name"
WorkspacesByNamePath = AllWorkspacesPath + "/:" + NamespacePathParam + "/:" + WorkspaceNamePathParam

//workspacekinds
AllWorkspaceKindsPath = PathPrefix + "/workspacekinds"
WorkspaceKindNamePathParam = "name"
WorkspaceKindsByNamePath = AllWorkspaceKindsPath + "/:" + WorkspaceNamePathParam
)

type App struct {
Expand Down Expand Up @@ -77,5 +82,8 @@ func (a *App) Routes() http.Handler {
router.POST(WorkspacesByNamespacePath, a.CreateWorkspaceHandler)
router.DELETE(WorkspacesByNamePath, a.DeleteWorkspaceHandler)

router.GET(AllWorkspaceKindsPath, a.GetWorkspaceKindsHandler)
router.GET(WorkspaceKindsByNamePath, a.GetWorkspaceKindHandler)

return a.RecoverPanic(a.enableCORS(router))
}
76 changes: 76 additions & 0 deletions workspaces/backend/api/workspacekinds_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* Copyright 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/

package api

import (
"errors"
"fmt"
"github.com/julienschmidt/httprouter"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
"net/http"
)

type WorkspaceKindsEnvelope Envelope[[]models.WorkspaceKindModel]
type WorkspaceKindEnvelope Envelope[models.WorkspaceKindModel]

func (a *App) GetWorkspaceKindHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
name := ps.ByName("name")

if name == "" {
a.serverErrorResponse(w, r, fmt.Errorf("workspace kind name is missing"))
return
}

workspaceKind, err := a.repositories.WorkspaceKind.GetWorkspaceKind(r.Context(), name)
if err != nil {
if errors.Is(err, repositories.ErrWorkspaceKindNotFound) {
a.notFoundResponse(w, r)
return
}
a.serverErrorResponse(w, r, err)
return
}

workspaceKindEnvelope := WorkspaceKindEnvelope{
Data: workspaceKind,
}

err = a.WriteJSON(w, http.StatusOK, workspaceKindEnvelope, nil)
if err != nil {
a.serverErrorResponse(w, r, err)
}
}

func (a *App) GetWorkspaceKindsHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
workspaceKinds, err := a.repositories.WorkspaceKind.GetWorkspaceKinds(r.Context())
if err != nil {
a.serverErrorResponse(w, r, err)
return
}

workspaceKindsEnvelope := WorkspaceKindsEnvelope{
Data: workspaceKinds,
}

err = a.WriteJSON(w, http.StatusOK, workspaceKindsEnvelope, nil)
if err != nil {
a.serverErrorResponse(w, r, err)
}
}
Loading

0 comments on commit ac16aa5

Please sign in to comment.