MCPcopy Index your code
hub / github.com/coder/coder / postWorkspaceAgentPortShare

Method postWorkspaceAgentPortShare

coderd/workspaceagentportshare.go:25–113  ·  view source on GitHub ↗

@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

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

23// @Success 200 {object} codersdk.WorkspaceAgentPortShare
24// @Router /api/v2/workspaces/{workspace}/port-share [post]
25func (api *API) postWorkspaceAgentPortShare(rw http.ResponseWriter, r *http.Request) {
26 ctx := r.Context()
27 workspace := httpmw.WorkspaceParam(r)
28 portSharer := *api.PortSharer.Load()
29 var req codersdk.UpsertWorkspaceAgentPortShareRequest
30 if !httpapi.Read(ctx, rw, r, &req) {
31 return
32 }
33
34 if !req.ShareLevel.ValidPortShareLevel() {
35 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
36 Message: "Port sharing level not allowed.",
37 Validations: []codersdk.ValidationError{
38 {
39 Field: "share_level",
40 Detail: "Port sharing level not allowed.",
41 },
42 },
43 })
44 return
45 }
46
47 if req.Port < 9 || req.Port > 65535 {
48 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
49 Message: "Port must be between 9 and 65535.",
50 Validations: []codersdk.ValidationError{
51 {
52 Field: "port",
53 Detail: "Port must be between 9 and 65535.",
54 },
55 },
56 })
57 return
58 }
59 if !req.Protocol.ValidPortProtocol() {
60 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
61 Message: "Port protocol not allowed.",
62 })
63 return
64 }
65
66 template, err := api.Database.GetTemplateByID(ctx, workspace.TemplateID)
67 if err != nil {
68 httpapi.InternalServerError(rw, err)
69 return
70 }
71
72 err = portSharer.AuthorizedLevel(template, req.ShareLevel)
73 if err != nil {
74 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
75 Message: err.Error(),
76 })
77 return
78 }
79
80 agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
81 if err != nil {
82 httpapi.InternalServerError(rw, err)

Callers

nothing calls this directly

Calls 15

WorkspaceParamFunction · 0.92
ReadFunction · 0.92
WriteFunction · 0.92
InternalServerErrorFunction · 0.92
AppSharingLevelTypeAlias · 0.92
PortShareProtocolTypeAlias · 0.92
convertPortShareFunction · 0.85
ValidPortShareLevelMethod · 0.80
ValidPortProtocolMethod · 0.80
ContextMethod · 0.65
GetTemplateByIDMethod · 0.65
AuthorizedLevelMethod · 0.65

Tested by

no test coverage detected