Skip to content

Commit ff9d968

Browse files
committed
add delete group endpoint
1 parent d70911b commit ff9d968

File tree

7 files changed

+97
-5
lines changed

7 files changed

+97
-5
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,7 @@ func (q *fakeQuerier) GetGroupMembers(_ context.Context, groupID uuid.UUID) ([]d
27902790
return users, nil
27912791
}
27922792

2793-
func (q *fakeQuerier) GetGroupsByOrganizationID(ctx context.Context, organizationID uuid.UUID) ([]database.Group, error) {
2793+
func (q *fakeQuerier) GetGroupsByOrganizationID(_ context.Context, organizationID uuid.UUID) ([]database.Group, error) {
27942794
q.mutex.RLock()
27952795
defer q.mutex.RUnlock()
27962796

@@ -2803,3 +2803,17 @@ func (q *fakeQuerier) GetGroupsByOrganizationID(ctx context.Context, organizatio
28032803

28042804
return groups, nil
28052805
}
2806+
2807+
func (q *fakeQuerier) DeleteGroupByID(_ context.Context, id uuid.UUID) error {
2808+
q.mutex.Lock()
2809+
defer q.mutex.Unlock()
2810+
2811+
for i, group := range q.groups {
2812+
if group.ID == id {
2813+
q.groups = append(q.groups[:i], q.groups[i+1:]...)
2814+
return nil
2815+
}
2816+
}
2817+
2818+
return sql.ErrNoRows
2819+
}

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/groups.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,10 @@ DELETE FROM
8484
WHERE
8585
user_id = $1;
8686

87+
-- name: DeleteGroupByID :exec
88+
DELETE FROM
89+
groups
90+
WHERE
91+
id = $1;
92+
93+

coderd/groups.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,25 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
157157
}
158158

159159
func (api *API) deleteGroup(rw http.ResponseWriter, r *http.Request) {
160+
var (
161+
ctx = r.Context()
162+
group = httpmw.GroupParam(r)
163+
)
164+
165+
if !api.Authorize(r, rbac.ActionDelete, rbac.ResourceGroup) {
166+
httpapi.ResourceNotFound(rw)
167+
return
168+
}
160169

170+
err := api.Database.DeleteGroupByID(ctx, group.ID)
171+
if err != nil {
172+
httpapi.InternalServerError(rw, err)
173+
return
174+
}
175+
176+
httpapi.Write(rw, http.StatusOK, codersdk.Response{
177+
Message: "Successfully deleted group!",
178+
})
161179
}
162180

163181
func (api *API) group(rw http.ResponseWriter, r *http.Request) {

coderd/groups_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,29 @@ func TestGroups(t *testing.T) {
274274
require.Len(t, groups, 0)
275275
})
276276
}
277+
278+
func TestDeleteGroup(t *testing.T) {
279+
t.Parallel()
280+
281+
t.Run("OK", func(t *testing.T) {
282+
t.Parallel()
283+
284+
client := coderdtest.New(t, nil)
285+
user := coderdtest.CreateFirstUser(t, client)
286+
287+
ctx, _ := testutil.Context(t)
288+
group1, err := client.CreateGroup(ctx, user.OrganizationID, codersdk.CreateGroupRequest{
289+
Name: "hi",
290+
})
291+
require.NoError(t, err)
292+
293+
err = client.DeleteGroup(ctx, group1.ID)
294+
require.NoError(t, err)
295+
296+
_, err = client.Group(ctx, group1.ID)
297+
require.Error(t, err)
298+
cerr, ok := codersdk.AsError(err)
299+
require.True(t, ok)
300+
require.Equal(t, http.StatusNotFound, cerr.StatusCode())
301+
})
302+
}

codersdk/groups.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,19 @@ func (c *Client) PatchGroup(ctx context.Context, group uuid.UUID, req PatchGroup
9595
var resp Group
9696
return resp, json.NewDecoder(res.Body).Decode(&resp)
9797
}
98+
99+
func (c *Client) DeleteGroup(ctx context.Context, group uuid.UUID) error {
100+
res, err := c.Request(ctx, http.MethodDelete,
101+
fmt.Sprintf("/api/v2/groups/%s", group.String()),
102+
nil,
103+
)
104+
if err != nil {
105+
return xerrors.Errorf("make request: %w", err)
106+
}
107+
defer res.Body.Close()
108+
109+
if res.StatusCode != http.StatusOK {
110+
return readBodyAsError(res)
111+
}
112+
return nil
113+
}

0 commit comments

Comments
 (0)