Skip to content

Commit 356dc3a

Browse files
committed
factor out patch primary
1 parent 881895b commit 356dc3a

File tree

1 file changed

+65
-49
lines changed

1 file changed

+65
-49
lines changed

enterprise/coderd/workspaceproxy.go

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -136,55 +136,9 @@ func (api *API) patchWorkspaceProxy(rw http.ResponseWriter, r *http.Request) {
136136
var updatedProxy database.WorkspaceProxy
137137
if proxy.ID.String() == deploymentIDStr {
138138
// User is editing the default primary proxy.
139-
if req.Name != "" && req.Name != proxy.Name {
140-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
141-
Message: "Cannot update name of default primary proxy, did you mean to update the 'display name'?",
142-
Validations: []codersdk.ValidationError{
143-
{Field: "name", Detail: "Cannot update name of default primary proxy"},
144-
},
145-
})
146-
return
147-
}
148-
if req.DisplayName == "" && req.Icon == "" {
149-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
150-
Message: "No update arguments provided. Nothing to do.",
151-
Validations: []codersdk.ValidationError{
152-
{Field: "display_name", Detail: "No value provided."},
153-
{Field: "icon", Detail: "No value provided."},
154-
},
155-
})
156-
return
157-
}
158-
159-
args := database.UpsertDefaultProxyParams{
160-
DisplayName: req.DisplayName,
161-
IconUrl: req.Icon,
162-
}
163-
if req.DisplayName == "" || req.Icon == "" {
164-
// If the user has not specified an update value, use the existing value.
165-
existing, err := api.Database.GetDefaultProxyConfig(ctx)
166-
if err != nil {
167-
httpapi.InternalServerError(rw, err)
168-
return
169-
}
170-
if req.DisplayName == "" {
171-
args.DisplayName = existing.DisplayName
172-
}
173-
if req.Icon == "" {
174-
args.IconUrl = existing.IconUrl
175-
}
176-
}
177-
178-
err = api.Database.UpsertDefaultProxy(ctx, args)
179-
if err != nil {
180-
httpapi.InternalServerError(rw, err)
181-
return
182-
}
183-
184-
// Use the primary region to fetch the default proxy values.
185-
updatedProxy, err = api.AGPL.PrimaryWorkspaceProxy(ctx)
186-
if err != nil {
187-
httpapi.InternalServerError(rw, err)
139+
var ok bool
140+
updatedProxy, ok = api.patchPrimaryWorkspaceProxy(req, rw, r)
141+
if !ok {
188142
return
189143
}
190144
} else {
@@ -221,6 +175,68 @@ func (api *API) patchWorkspaceProxy(rw http.ResponseWriter, r *http.Request) {
221175
go api.forceWorkspaceProxyHealthUpdate(api.ctx)
222176
}
223177

178+
// patchPrimaryWorkspaceProxy handles the special case of updating the default
179+
func (api *API) patchPrimaryWorkspaceProxy(req codersdk.PatchWorkspaceProxy, rw http.ResponseWriter, r *http.Request) (database.WorkspaceProxy, bool) {
180+
var (
181+
ctx = r.Context()
182+
proxy = httpmw.WorkspaceProxyParam(r)
183+
)
184+
185+
// User is editing the default primary proxy.
186+
if req.Name != "" && req.Name != proxy.Name {
187+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
188+
Message: "Cannot update name of default primary proxy, did you mean to update the 'display name'?",
189+
Validations: []codersdk.ValidationError{
190+
{Field: "name", Detail: "Cannot update name of default primary proxy"},
191+
},
192+
})
193+
return database.WorkspaceProxy{}, false
194+
}
195+
if req.DisplayName == "" && req.Icon == "" {
196+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
197+
Message: "No update arguments provided. Nothing to do.",
198+
Validations: []codersdk.ValidationError{
199+
{Field: "display_name", Detail: "No value provided."},
200+
{Field: "icon", Detail: "No value provided."},
201+
},
202+
})
203+
return database.WorkspaceProxy{}, false
204+
}
205+
206+
args := database.UpsertDefaultProxyParams{
207+
DisplayName: req.DisplayName,
208+
IconUrl: req.Icon,
209+
}
210+
if req.DisplayName == "" || req.Icon == "" {
211+
// If the user has not specified an update value, use the existing value.
212+
existing, err := api.Database.GetDefaultProxyConfig(ctx)
213+
if err != nil {
214+
httpapi.InternalServerError(rw, err)
215+
return database.WorkspaceProxy{}, false
216+
}
217+
if req.DisplayName == "" {
218+
args.DisplayName = existing.DisplayName
219+
}
220+
if req.Icon == "" {
221+
args.IconUrl = existing.IconUrl
222+
}
223+
}
224+
225+
err := api.Database.UpsertDefaultProxy(ctx, args)
226+
if err != nil {
227+
httpapi.InternalServerError(rw, err)
228+
return database.WorkspaceProxy{}, false
229+
}
230+
231+
// Use the primary region to fetch the default proxy values.
232+
updatedProxy, err := api.AGPL.PrimaryWorkspaceProxy(ctx)
233+
if err != nil {
234+
httpapi.InternalServerError(rw, err)
235+
return database.WorkspaceProxy{}, false
236+
}
237+
return updatedProxy, true
238+
}
239+
224240
// @Summary Delete workspace proxy
225241
// @ID delete-workspace-proxy
226242
// @Security CoderSessionToken

0 commit comments

Comments
 (0)