Repository navigation
Expand file tree
/
Copy pathworkspace.go
More file actions
143 lines (131 loc) · 4.4 KB
/
Copy pathworkspace.go
File metadata and controls
143 lines (131 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2026 The k8shell Authors.
// SPDX-License-Identifier: AGPL-3.0-only
package k8shell
import (
"context"
"io"
"net/http"
"net/url"
"strings"
"github.com/k8shell-io/common/pkg/models"
)
// WorkspaceCreateRequest is the payload for creating a workspace.
// Either Blueprint or (RepoOwner + RepoName) must be set.
type WorkspaceCreateRequest struct {
Username string `json:"username"`
Blueprint string `json:"blueprint,omitempty"`
RepoOwner string `json:"repoOwner,omitempty"`
RepoName string `json:"repoName,omitempty"`
RepoRef string `json:"repoRef,omitempty"`
}
// WorkspaceCreateResponse is the 202 Accepted body returned by CreateWorkspace.
type WorkspaceCreateResponse struct {
Workspace string `json:"workspace"`
JobID string `json:"jobId"`
MonitorURL string `json:"monitorUrl"`
}
// ListWorkspaces returns workspaces visible to the authenticated token.
// When username is non-empty the results are filtered by owner.
// When all is true, workspaces in all states are included.
func (c *Client) ListWorkspaces(ctx context.Context, username string, all bool) ([]models.WorkspaceDetails, error) {
q := url.Values{}
if username != "" {
q.Set("username", username)
}
if all {
q.Set("all", "true")
}
path := "/api/v1/workspaces"
if len(q) > 0 {
path += "?" + q.Encode()
}
var workspaces []models.WorkspaceDetails
if err := c.get(ctx, path, &workspaces); err != nil {
return nil, err
}
return workspaces, nil
}
// CreateWorkspace submits a workspace creation request and returns the 202 response
// containing the workspace name, job ID, and SSE monitor URL.
func (c *Client) CreateWorkspace(ctx context.Context, req WorkspaceCreateRequest) (*WorkspaceCreateResponse, error) {
var resp WorkspaceCreateResponse
if err := c.post(ctx, "/api/v1/workspaces", req, &resp); err != nil {
return nil, err
}
cleanMonitorURL(&resp)
return &resp, nil
}
// cleanMonitorURL strips a duplicated jobId segment that some server versions
// emit in a WorkspaceCreateResponse's MonitorURL.
func cleanMonitorURL(resp *WorkspaceCreateResponse) {
if resp.JobID != "" && strings.Count(resp.MonitorURL, resp.JobID) > 1 {
if idx := strings.LastIndex(resp.MonitorURL, "/"+resp.JobID); idx >= 0 {
resp.MonitorURL = resp.MonitorURL[:idx]
}
}
}
// GetWorkspace returns the details of the named workspace.
func (c *Client) GetWorkspace(ctx context.Context, name string) (*models.WorkspaceDetails, error) {
var ws models.WorkspaceDetails
if err := c.get(ctx, "/api/v1/workspaces/"+name, &ws); err != nil {
return nil, err
}
return &ws, nil
}
// StartWorkspace starts a previously stopped workspace and returns the 202
// response containing the workspace name, job ID, and SSE monitor URL —
// starting is a streamed provisioning operation on the server, the same as
// CreateWorkspace.
func (c *Client) StartWorkspace(ctx context.Context, name string) (*WorkspaceCreateResponse, error) {
var resp WorkspaceCreateResponse
if err := c.post(ctx, "/api/v1/workspaces/"+name+"/start", struct{}{}, &resp); err != nil {
return nil, err
}
cleanMonitorURL(&resp)
return &resp, nil
}
// DeleteWorkspace shuts down the named workspace.
// When deleteData is true, workspace storage is permanently deleted.
func (c *Client) DeleteWorkspace(ctx context.Context, name string, deleteData bool) error {
path := "/api/v1/workspaces/" + name
if deleteData {
path += "?delete=true"
}
return c.delete(ctx, path)
}
// MonitorWorkspace opens an SSE stream at monitorURL and returns the response
// body for the caller to consume. monitorURL may be a full URL or a path
// relative to the client's server. The caller is responsible for closing the
// returned ReadCloser.
func (c *Client) MonitorWorkspace(ctx context.Context, monitorURL string) (io.ReadCloser, error) {
if u, err := url.Parse(monitorURL); err == nil && u.Scheme == "" {
monitorURL = c.server + monitorURL
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, monitorURL, nil)
if err != nil {
return nil, err
}
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
req.Header.Set("Accept", "text/event-stream")
if c.debug {
c.debugRequest(req)
}
if c.curl {
c.printCurl(req, nil)
return nil, ErrDryRun
}
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
if c.debug {
c.debugResponse(resp)
}
if resp.StatusCode >= 400 {
resp.Body.Close()
return nil, &APIError{StatusCode: resp.StatusCode}
}
return resp.Body, nil
}