@Summary Create group for organization @ID create-group-for-organization @Security CoderSessionToken @Accept json @Produce json @Tags Enterprise @Param request body codersdk.CreateGroupRequest true "Create group request" @Param organization path string true "Organization ID" @Success 201 {object} co
(rw http.ResponseWriter, r *http.Request)
| 31 | // @Success 201 {object} codersdk.Group |
| 32 | // @Router /api/v2/organizations/{organization}/groups [post] |
| 33 | func (api *API) postGroupByOrganization(rw http.ResponseWriter, r *http.Request) { |
| 34 | var ( |
| 35 | ctx = r.Context() |
| 36 | org = httpmw.OrganizationParam(r) |
| 37 | auditor = api.AGPL.Auditor.Load() |
| 38 | aReq, commitAudit = audit.InitRequest[database.AuditableGroup](rw, &audit.RequestParams{ |
| 39 | Audit: *auditor, |
| 40 | Log: api.Logger, |
| 41 | Request: r, |
| 42 | Action: database.AuditActionCreate, |
| 43 | OrganizationID: org.ID, |
| 44 | }) |
| 45 | ) |
| 46 | defer commitAudit() |
| 47 | |
| 48 | var req codersdk.CreateGroupRequest |
| 49 | if !httpapi.Read(ctx, rw, r, &req) { |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | if req.Name == database.EveryoneGroup { |
| 54 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 55 | Message: "Invalid group name.", |
| 56 | Validations: []codersdk.ValidationError{{Field: "name", Detail: fmt.Sprintf("%q is a reserved group name", req.Name)}}, |
| 57 | }) |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | group, err := api.Database.InsertGroup(ctx, database.InsertGroupParams{ |
| 62 | ID: uuid.New(), |
| 63 | Name: req.Name, |
| 64 | DisplayName: req.DisplayName, |
| 65 | OrganizationID: org.ID, |
| 66 | AvatarURL: req.AvatarURL, |
| 67 | // #nosec G115 - Quota allowance is small and fits in int32 |
| 68 | QuotaAllowance: int32(req.QuotaAllowance), |
| 69 | }) |
| 70 | if database.IsUniqueViolation(err) { |
| 71 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 72 | Message: fmt.Sprintf("A group named %q already exists.", req.Name), |
| 73 | Validations: []codersdk.ValidationError{{Field: "name", Detail: "Group names must be unique"}}, |
| 74 | }) |
| 75 | return |
| 76 | } |
| 77 | if err != nil { |
| 78 | httpapi.InternalServerError(rw, err) |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | var emptyMembers []database.GroupMember |
| 83 | aReq.New = group.Auditable(emptyMembers) |
| 84 | |
| 85 | httpapi.Write(ctx, rw, http.StatusCreated, db2sdk.Group(database.GetGroupsRow{ |
| 86 | Group: group, |
| 87 | OrganizationName: org.Name, |
| 88 | OrganizationDisplayName: org.DisplayName, |
| 89 | }, nil, 0)) |
| 90 | } |
nothing calls this directly
no test coverage detected