-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathworkspaceagentportshare.go
More file actions
204 lines (183 loc) · 5.69 KB
/
workspaceagentportshare.go
File metadata and controls
204 lines (183 loc) · 5.69 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package coderd
import (
"database/sql"
"errors"
"net/http"
"slices"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/codersdk"
)
// @Summary Upsert workspace agent port share
// @ID upsert-workspace-agent-port-share
// @Security CoderSessionToken
// @Accept json
// @Produce json
// @Tags PortSharing
// @Param workspace path string true "Workspace ID" format(uuid)
// @Param request body codersdk.UpsertWorkspaceAgentPortShareRequest true "Upsert port sharing level request"
// @Success 200 {object} codersdk.WorkspaceAgentPortShare
// @Router /workspaces/{workspace}/port-share [post]
func (api *API) postWorkspaceAgentPortShare(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
workspace := httpmw.WorkspaceParam(r)
portSharer := *api.PortSharer.Load()
var req codersdk.UpsertWorkspaceAgentPortShareRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}
if !req.ShareLevel.ValidPortShareLevel() {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Port sharing level not allowed.",
Validations: []codersdk.ValidationError{
{
Field: "share_level",
Detail: "Port sharing level not allowed.",
},
},
})
return
}
if req.Port < 9 || req.Port > 65535 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Port must be between 9 and 65535.",
Validations: []codersdk.ValidationError{
{
Field: "port",
Detail: "Port must be between 9 and 65535.",
},
},
})
return
}
if !req.Protocol.ValidPortProtocol() {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Port protocol not allowed.",
})
return
}
template, err := api.Database.GetTemplateByID(ctx, workspace.TemplateID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
}
err = portSharer.AuthorizedLevel(template, req.ShareLevel)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: err.Error(),
})
return
}
agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
}
found := false
for _, agent := range agents {
if agent.Name == req.AgentName {
found = true
break
}
}
if !found {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Agent not found.",
})
return
}
psl, err := api.Database.UpsertWorkspaceAgentPortShare(ctx, database.UpsertWorkspaceAgentPortShareParams{
WorkspaceID: workspace.ID,
AgentName: req.AgentName,
Port: req.Port,
ShareLevel: database.AppSharingLevel(req.ShareLevel),
Protocol: database.PortShareProtocol(req.Protocol),
})
if err != nil {
httpapi.InternalServerError(rw, err)
return
}
httpapi.Write(ctx, rw, http.StatusOK, convertPortShare(psl))
}
// @Summary Get workspace agent port shares
// @ID get-workspace-agent-port-shares
// @Security CoderSessionToken
// @Produce json
// @Tags PortSharing
// @Param workspace path string true "Workspace ID" format(uuid)
// @Success 200 {object} codersdk.WorkspaceAgentPortShares
// @Router /workspaces/{workspace}/port-share [get]
func (api *API) workspaceAgentPortShares(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
workspace := httpmw.WorkspaceParam(r)
shares, err := api.Database.ListWorkspaceAgentPortShares(ctx, workspace.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
}
httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceAgentPortShares{
Shares: convertPortShares(shares),
})
}
// @Summary Delete workspace agent port share
// @ID delete-workspace-agent-port-share
// @Security CoderSessionToken
// @Accept json
// @Tags PortSharing
// @Param workspace path string true "Workspace ID" format(uuid)
// @Param request body codersdk.DeleteWorkspaceAgentPortShareRequest true "Delete port sharing level request"
// @Success 200
// @Router /workspaces/{workspace}/port-share [delete]
func (api *API) deleteWorkspaceAgentPortShare(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
workspace := httpmw.WorkspaceParam(r)
var req codersdk.DeleteWorkspaceAgentPortShareRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}
_, err := api.Database.GetWorkspaceAgentPortShare(ctx, database.GetWorkspaceAgentPortShareParams{
WorkspaceID: workspace.ID,
AgentName: req.AgentName,
Port: req.Port,
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
Message: "Port share not found.",
})
return
}
httpapi.InternalServerError(rw, err)
return
}
err = api.Database.DeleteWorkspaceAgentPortShare(ctx, database.DeleteWorkspaceAgentPortShareParams{
WorkspaceID: workspace.ID,
AgentName: req.AgentName,
Port: req.Port,
})
if err != nil {
httpapi.InternalServerError(rw, err)
return
}
rw.WriteHeader(http.StatusOK)
}
func convertPortShares(shares []database.WorkspaceAgentPortShare) []codersdk.WorkspaceAgentPortShare {
converted := []codersdk.WorkspaceAgentPortShare{}
for _, share := range shares {
converted = append(converted, convertPortShare(share))
}
slices.SortFunc(converted, func(i, j codersdk.WorkspaceAgentPortShare) int {
return (int)(i.Port - j.Port)
})
return converted
}
func convertPortShare(share database.WorkspaceAgentPortShare) codersdk.WorkspaceAgentPortShare {
return codersdk.WorkspaceAgentPortShare{
WorkspaceID: share.WorkspaceID,
AgentName: share.AgentName,
Port: share.Port,
ShareLevel: codersdk.WorkspaceAgentPortShareLevel(share.ShareLevel),
Protocol: codersdk.WorkspaceAgentPortShareProtocol(share.Protocol),
}
}