@@ -17,6 +17,7 @@ limitations under the License.
17
17
package api
18
18
19
19
import (
20
+ "context"
20
21
"errors"
21
22
"net/http"
22
23
@@ -30,24 +31,11 @@ import (
30
31
repository "github.com/kubeflow/notebooks/workspaces/backend/internal/repositories/workspaces"
31
32
)
32
33
33
- // PauseWorkspaceHandler handles the pause workspace action
34
- //
35
- // @Summary Pause workspace
36
- // @Description Pauses a workspace, stopping all associated pods.
37
- // @Tags workspaces
38
- // @Accept json
39
- // @Produce json
40
- // @Param namespace path string true "Namespace of the workspace" example(default)
41
- // @Param workspaceName path string true "Name of the workspace" example(my-workspace)
42
- // @Success 200 {object} EmptyResponse "Successful action. Returns an empty JSON object."
43
- // @Failure 400 {object} ErrorEnvelope "Bad Request. Invalid workspace kind name format."
44
- // @Failure 401 {object} ErrorEnvelope "Unauthorized. Authentication is required."
45
- // @Failure 403 {object} ErrorEnvelope "Forbidden. User does not have permission to access the workspace."
46
- // @Failure 404 {object} ErrorEnvelope "Not Found. Workspace does not exist."
47
- // @Failure 500 {object} ErrorEnvelope "Internal server error. An unexpected error occurred on the server."
48
- // @Router /workspaces/{namespace}/{workspaceName}/actions/pause [post]
49
- // @Security ApiKeyAuth
50
- func (a * App ) PauseWorkspaceHandler (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
34
+ // workspaceActionFunc represents a function that performs an action on a workspace
35
+ type workspaceActionFunc func (ctx context.Context , namespace , name string ) error
36
+
37
+ // handleWorkspaceAction is a helper function that handles common logic for workspace actions
38
+ func (a * App ) handleWorkspaceAction (w http.ResponseWriter , r * http.Request , ps httprouter.Params , action workspaceActionFunc ) {
51
39
namespace := ps .ByName (NamespacePathParam )
52
40
workspaceName := ps .ByName (ResourceNamePathParam )
53
41
@@ -60,7 +48,7 @@ func (a *App) PauseWorkspaceHandler(w http.ResponseWriter, r *http.Request, ps h
60
48
return
61
49
}
62
50
63
- // =========================== AUTH ===========================
51
+ // Authorization check
64
52
authPolicies := []* auth.ResourcePolicy {
65
53
auth .NewResourcePolicy (
66
54
auth .ResourceVerbUpdate ,
@@ -75,9 +63,9 @@ func (a *App) PauseWorkspaceHandler(w http.ResponseWriter, r *http.Request, ps h
75
63
if success := a .requireAuth (w , r , authPolicies ); ! success {
76
64
return
77
65
}
78
- // ============================================================
79
66
80
- err := a .repositories .Workspace .PauseWorkspace (r .Context (), namespace , workspaceName )
67
+ // Execute the workspace action
68
+ err := action (r .Context (), namespace , workspaceName )
81
69
if err != nil {
82
70
if errors .Is (err , repository .ErrWorkspaceNotFound ) {
83
71
a .notFoundResponse (w , r )
@@ -94,3 +82,45 @@ func (a *App) PauseWorkspaceHandler(w http.ResponseWriter, r *http.Request, ps h
94
82
return
95
83
}
96
84
}
85
+
86
+ // PauseWorkspaceHandler handles the pause workspace action
87
+ //
88
+ // @Summary Pause workspace
89
+ // @Description Pauses a workspace, stopping all associated pods.
90
+ // @Tags workspaces
91
+ // @Accept json
92
+ // @Produce json
93
+ // @Param namespace path string true "Namespace of the workspace" example(default)
94
+ // @Param workspaceName path string true "Name of the workspace" example(my-workspace)
95
+ // @Success 200 {object} EmptyResponse "Successful action. Returns an empty JSON object."
96
+ // @Failure 400 {object} ErrorEnvelope "Bad Request. Invalid workspace kind name format."
97
+ // @Failure 401 {object} ErrorEnvelope "Unauthorized. Authentication is required."
98
+ // @Failure 403 {object} ErrorEnvelope "Forbidden. User does not have permission to access the workspace."
99
+ // @Failure 404 {object} ErrorEnvelope "Not Found. Workspace does not exist."
100
+ // @Failure 500 {object} ErrorEnvelope "Internal server error. An unexpected error occurred on the server."
101
+ // @Router /workspaces/{namespace}/{workspaceName}/actions/pause [post]
102
+ // @Security ApiKeyAuth
103
+ func (a * App ) PauseWorkspaceHandler (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
104
+ a .handleWorkspaceAction (w , r , ps , a .repositories .Workspace .PauseWorkspace )
105
+ }
106
+
107
+ // StartWorkspaceHandler handles the start workspace action
108
+ //
109
+ // @Summary Start workspace
110
+ // @Description Starts a workspace, resuming all associated pods.
111
+ // @Tags workspaces
112
+ // @Accept json
113
+ // @Produce json
114
+ // @Param namespace path string true "Namespace of the workspace" example(default)
115
+ // @Param workspaceName path string true "Name of the workspace" example(my-workspace)
116
+ // @Success 200 {object} EmptyResponse "Successful action. Returns an empty JSON object."
117
+ // @Failure 400 {object} ErrorEnvelope "Bad Request. Invalid workspace kind name format."
118
+ // @Failure 401 {object} ErrorEnvelope "Unauthorized. Authentication is required."
119
+ // @Failure 403 {object} ErrorEnvelope "Forbidden. User does not have permission to access the workspace."
120
+ // @Failure 404 {object} ErrorEnvelope "Not Found. Workspace does not exist."
121
+ // @Failure 500 {object} ErrorEnvelope "Internal server error. An unexpected error occurred on the server."
122
+ // @Router /workspaces/{namespace}/{workspaceName}/actions/start [post]
123
+ // @Security ApiKeyAuth
124
+ func (a * App ) StartWorkspaceHandler (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
125
+ a .handleWorkspaceAction (w , r , ps , a .repositories .Workspace .StartWorkspace )
126
+ }
0 commit comments