@Summary Create workspace proxy @ID create-workspace-proxy @Security CoderSessionToken @Accept json @Produce json @Tags Enterprise @Param request body codersdk.CreateWorkspaceProxyRequest true "Create workspace proxy request" @Success 201 {object} codersdk.WorkspaceProxy @Router /api/v2/workspacepro
(rw http.ResponseWriter, r *http.Request)
| 315 | // @Success 201 {object} codersdk.WorkspaceProxy |
| 316 | // @Router /api/v2/workspaceproxies [post] |
| 317 | func (api *API) postWorkspaceProxy(rw http.ResponseWriter, r *http.Request) { |
| 318 | var ( |
| 319 | ctx = r.Context() |
| 320 | auditor = api.AGPL.Auditor.Load() |
| 321 | aReq, commitAudit = audit.InitRequest[database.WorkspaceProxy](rw, &audit.RequestParams{ |
| 322 | Audit: *auditor, |
| 323 | Log: api.Logger, |
| 324 | Request: r, |
| 325 | Action: database.AuditActionCreate, |
| 326 | }) |
| 327 | ) |
| 328 | defer commitAudit() |
| 329 | |
| 330 | var req codersdk.CreateWorkspaceProxyRequest |
| 331 | if !httpapi.Read(ctx, rw, r, &req) { |
| 332 | return |
| 333 | } |
| 334 | |
| 335 | if strings.ToLower(req.Name) == "primary" { |
| 336 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 337 | Message: `The name "primary" is reserved for the primary region.`, |
| 338 | Detail: "Cannot name a workspace proxy 'primary'.", |
| 339 | Validations: []codersdk.ValidationError{ |
| 340 | { |
| 341 | Field: "name", |
| 342 | Detail: "Reserved name", |
| 343 | }, |
| 344 | }, |
| 345 | }) |
| 346 | return |
| 347 | } |
| 348 | |
| 349 | id := uuid.New() |
| 350 | fullToken, hashedSecret, err := generateWorkspaceProxyToken(id) |
| 351 | if err != nil { |
| 352 | httpapi.InternalServerError(rw, err) |
| 353 | return |
| 354 | } |
| 355 | |
| 356 | proxy, err := api.Database.InsertWorkspaceProxy(ctx, database.InsertWorkspaceProxyParams{ |
| 357 | ID: id, |
| 358 | Name: req.Name, |
| 359 | DisplayName: req.DisplayName, |
| 360 | Icon: req.Icon, |
| 361 | TokenHashedSecret: hashedSecret, |
| 362 | // Enabled by default, but will be disabled on register if the proxy has |
| 363 | // it disabled. |
| 364 | DerpEnabled: true, |
| 365 | // Disabled by default, but blah blah blah. |
| 366 | DerpOnly: false, |
| 367 | CreatedAt: dbtime.Now(), |
| 368 | UpdatedAt: dbtime.Now(), |
| 369 | }) |
| 370 | if database.IsUniqueViolation(err, database.UniqueWorkspaceProxiesLowerNameIndex) { |
| 371 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 372 | Message: fmt.Sprintf("Workspace proxy with name %q already exists.", req.Name), |
| 373 | }) |
| 374 | return |
nothing calls this directly
no test coverage detected