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