Skip to content

chore: implement filters for the organizations query #14468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/server_createadminuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (r *RootCmd) newCreateAdminUserCommand() *serpent.Command {
// Create the user.
var newUser database.User
err = db.InTx(func(tx database.Store) error {
orgs, err := tx.GetOrganizations(ctx)
orgs, err := tx.GetOrganizations(ctx, database.GetOrganizationsParams{})
if err != nil {
return xerrors.Errorf("get organizations: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/server_createadminuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestServerCreateAdminUser(t *testing.T) {
require.EqualValues(t, []string{codersdk.RoleOwner}, user.RBACRoles, "user does not have owner role")

// Check that user is admin in every org.
orgs, err := db.GetOrganizations(ctx)
orgs, err := db.GetOrganizations(ctx, database.GetOrganizationsParams{})
require.NoError(t, err)
orgIDs := make(map[uuid.UUID]struct{}, len(orgs))
for _, org := range orgs {
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -1700,9 +1700,9 @@ func (q *querier) GetOrganizationIDsByMemberIDs(ctx context.Context, ids []uuid.
return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetOrganizationIDsByMemberIDs)(ctx, ids)
}

func (q *querier) GetOrganizations(ctx context.Context) ([]database.Organization, error) {
func (q *querier) GetOrganizations(ctx context.Context, args database.GetOrganizationsParams) ([]database.Organization, error) {
fetch := func(ctx context.Context, _ interface{}) ([]database.Organization, error) {
return q.db.GetOrganizations(ctx)
return q.db.GetOrganizations(ctx, args)
}
return fetchWithPostFilter(q.auth, policy.ActionRead, fetch)(ctx, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func (s *MethodTestSuite) TestOrganization() {
def, _ := db.GetDefaultOrganization(context.Background())
a := dbgen.Organization(s.T(), db, database.Organization{})
b := dbgen.Organization(s.T(), db, database.Organization{})
check.Args().Asserts(def, policy.ActionRead, a, policy.ActionRead, b, policy.ActionRead).Returns(slice.New(def, a, b))
check.Args(database.GetOrganizationsParams{}).Asserts(def, policy.ActionRead, a, policy.ActionRead, b, policy.ActionRead).Returns(slice.New(def, a, b))
}))
s.Run("GetOrganizationsByUserID", s.Subtest(func(db database.Store, check *expects) {
u := dbgen.User(s.T(), db, database.User{})
Expand Down
22 changes: 15 additions & 7 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3034,14 +3034,24 @@ func (q *FakeQuerier) GetOrganizationIDsByMemberIDs(_ context.Context, ids []uui
return getOrganizationIDsByMemberIDRows, nil
}

func (q *FakeQuerier) GetOrganizations(_ context.Context) ([]database.Organization, error) {
func (q *FakeQuerier) GetOrganizations(_ context.Context, args database.GetOrganizationsParams) ([]database.Organization, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

if len(q.organizations) == 0 {
return nil, sql.ErrNoRows
tmp := make([]database.Organization, 0)
for _, org := range q.organizations {
if len(args.IDs) > 0 {
if !slices.Contains(args.IDs, org.ID) {
continue
}
}
if args.Name != "" && !strings.EqualFold(org.Name, args.Name) {
continue
}
tmp = append(tmp, org)
}
return q.organizations, nil

return tmp, nil
}

func (q *FakeQuerier) GetOrganizationsByUserID(_ context.Context, userID uuid.UUID) ([]database.Organization, error) {
Expand All @@ -3060,9 +3070,7 @@ func (q *FakeQuerier) GetOrganizationsByUserID(_ context.Context, userID uuid.UU
organizations = append(organizations, organization)
}
}
if len(organizations) == 0 {
return nil, sql.ErrNoRows
}

return organizations, nil
}

Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dbmem/dbmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestInTx(t *testing.T) {
go func() {
<-inTx
for i := 0; i < 20; i++ {
orgs, err := uut.GetOrganizations(context.Background())
orgs, err := uut.GetOrganizations(context.Background(), database.GetOrganizationsParams{})
if err != nil {
assert.ErrorIs(t, err, sql.ErrNoRows)
}
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/dbmetrics/dbmetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/database/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func TestDefaultOrg(t *testing.T) {
ctx := context.Background()

// Should start with the default org
all, err := db.GetOrganizations(ctx)
all, err := db.GetOrganizations(ctx, database.GetOrganizationsParams{})
require.NoError(t, err)
require.Len(t, all, 1)
require.True(t, all[0].IsDefault, "first org should always be default")
Expand Down Expand Up @@ -1211,7 +1211,7 @@ func TestExpectOne(t *testing.T) {
dbgen.Organization(t, db, database.Organization{})

// Organizations is an easy table without foreign key dependencies
_, err = database.ExpectOne(db.GetOrganizations(ctx))
_, err = database.ExpectOne(db.GetOrganizations(ctx, database.GetOrganizationsParams{}))
require.ErrorContains(t, err, "too many rows returned")
})
}
Expand Down
22 changes: 20 additions & 2 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion coderd/database/queries/organizations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ LIMIT
SELECT
*
FROM
organizations;
organizations
WHERE
true
-- Filter by ids
AND CASE
WHEN array_length(@ids :: uuid[], 1) > 0 THEN
id = ANY(@ids)
ELSE true
END
AND CASE
WHEN @name::text != '' THEN
LOWER("name") = LOWER(@name)
ELSE true
END
;

-- name: GetOrganizationByID :one
SELECT
Expand Down
3 changes: 2 additions & 1 deletion coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coderd
import (
"net/http"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
Expand All @@ -18,7 +19,7 @@ import (
// @Router /organizations [get]
func (api *API) organizations(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
organizations, err := api.Database.GetOrganizations(ctx)
organizations, err := api.Database.GetOrganizations(ctx, database.GetOrganizationsParams{})
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
Expand Down
Loading