mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: add group_ids filter to /groups endpoint (#14688)
Allow filtering groups by IDs.
This commit is contained in:
@@ -430,6 +430,7 @@ func (api *API) groupsByOrganization(rw http.ResponseWriter, r *http.Request) {
|
||||
// @Tags Enterprise
|
||||
// @Param organization query string true "Organization ID or name"
|
||||
// @Param has_member query string true "User ID or name"
|
||||
// @Param group_ids query string true "Comma separated list of group IDs"
|
||||
// @Success 200 {array} codersdk.Group
|
||||
// @Router /groups [get]
|
||||
func (api *API) groups(rw http.ResponseWriter, r *http.Request) {
|
||||
@@ -457,6 +458,9 @@ func (api *API) groups(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
return user.ID, nil
|
||||
})
|
||||
|
||||
filter.GroupIds = parser.UUIDs(r.URL.Query(), []uuid.UUID{}, "group_ids")
|
||||
|
||||
parser.ErrorExcessParams(r.URL.Query())
|
||||
if len(parser.Errors) > 0 {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
|
||||
@@ -779,6 +779,45 @@ func TestGroup(t *testing.T) {
|
||||
require.Contains(t, group.Members, user2.ReducedUser)
|
||||
})
|
||||
|
||||
t.Run("ByIDs", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client, user := coderdenttest.New(t, &coderdenttest.Options{LicenseOptions: &coderdenttest.LicenseOptions{
|
||||
Features: license.Features{
|
||||
codersdk.FeatureTemplateRBAC: 1,
|
||||
},
|
||||
}})
|
||||
userAdminClient, _ := coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleUserAdmin())
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
groupA, err := userAdminClient.CreateGroup(ctx, user.OrganizationID, codersdk.CreateGroupRequest{
|
||||
Name: "group-a",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
groupB, err := userAdminClient.CreateGroup(ctx, user.OrganizationID, codersdk.CreateGroupRequest{
|
||||
Name: "group-b",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// group-c should be omitted from the filter
|
||||
_, err = userAdminClient.CreateGroup(ctx, user.OrganizationID, codersdk.CreateGroupRequest{
|
||||
Name: "group-c",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
found, err := userAdminClient.Groups(ctx, codersdk.GroupArguments{
|
||||
GroupIDs: []uuid.UUID{groupA.ID, groupB.ID},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
foundIDs := db2sdk.List(found, func(g codersdk.Group) uuid.UUID {
|
||||
return g.ID
|
||||
})
|
||||
|
||||
require.ElementsMatch(t, []uuid.UUID{groupA.ID, groupB.ID}, foundIDs)
|
||||
})
|
||||
|
||||
t.Run("everyoneGroupReturnsEmpty", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user