From db4ac1298aa07498ea22c8262b49316ac0bf21fa Mon Sep 17 00:00:00 2001 From: Jim Lambert Date: Tue, 16 Jun 2020 19:31:34 -0400 Subject: [PATCH] add ListProjects and ListOrganizations --- internal/iam/repository_scope.go | 23 ++++ internal/iam/repository_scope_test.go | 154 ++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/internal/iam/repository_scope.go b/internal/iam/repository_scope.go index 357d0d9fab..dd66c6f32f 100644 --- a/internal/iam/repository_scope.go +++ b/internal/iam/repository_scope.go @@ -120,3 +120,26 @@ func (r *Repository) DeleteScope(ctx context.Context, withPublicId string, opt . } return rowsDeleted, nil } + +// ListProjects in an organization and supports the WithLimit option. +func (r *Repository) ListProjects(ctx context.Context, withOrganizationId string, opt ...Option) ([]*Scope, error) { + if withOrganizationId == "" { + return nil, fmt.Errorf("list projects: missing organization id %w", db.ErrInvalidParameter) + } + var projects []*Scope + err := r.list(ctx, &projects, "parent_id = ? and type = ?", []interface{}{withOrganizationId, ProjectScope.String()}, opt...) + if err != nil { + return nil, fmt.Errorf("list projects: %w", err) + } + return projects, nil +} + +// ListOrganizations organizations and supports the WithLimit option. +func (r *Repository) ListOrganizations(ctx context.Context, opt ...Option) ([]*Scope, error) { + var projects []*Scope + err := r.list(ctx, &projects, "type = ?", []interface{}{OrganizationScope.String()}, opt...) + if err != nil { + return nil, fmt.Errorf("list organizations: %w", err) + } + return projects, nil +} diff --git a/internal/iam/repository_scope_test.go b/internal/iam/repository_scope_test.go index 79702099b6..716b1bf2c6 100644 --- a/internal/iam/repository_scope_test.go +++ b/internal/iam/repository_scope_test.go @@ -493,3 +493,157 @@ func TestRepository_UpdateScope(t *testing.T) { assert.Nil(updatedScope, "scope should be nil") }) } + +func TestRepository_ListProjects(t *testing.T) { + t.Parallel() + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + err := cleanup() + assert.NoError(t, err) + err = conn.Close() + assert.NoError(t, err) + }() + const testLimit = 10 + rw := db.New(conn) + wrapper := db.TestWrapper(t) + repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit)) + require.NoError(t, err) + org := testOrg(t, conn, "", "") + + type args struct { + withOrganizationId string + opt []Option + } + tests := []struct { + name string + createCnt int + args args + wantCnt int + wantErr bool + }{ + { + name: "no-limit", + createCnt: repo.defaultLimit + 1, + args: args{ + withOrganizationId: org.PublicId, + opt: []Option{WithLimit(-1)}, + }, + wantCnt: repo.defaultLimit + 1, + wantErr: false, + }, + { + name: "default-limit", + createCnt: repo.defaultLimit + 1, + args: args{ + withOrganizationId: org.PublicId, + }, + wantCnt: repo.defaultLimit, + wantErr: false, + }, + { + name: "custom-limit", + createCnt: repo.defaultLimit + 1, + args: args{ + withOrganizationId: org.PublicId, + opt: []Option{WithLimit(3)}, + }, + wantCnt: 3, + wantErr: false, + }, + { + name: "bad-org", + createCnt: 1, + args: args{ + withOrganizationId: "bad-id", + }, + wantCnt: 0, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert, require := assert.New(t), require.New(t) + testProjects := []*Scope{} + for i := 0; i < tt.createCnt; i++ { + testProjects = append(testProjects, testProject(t, conn, org.PublicId)) + } + assert.Equal(tt.createCnt, len(testProjects)) + got, err := repo.ListProjects(context.Background(), tt.args.withOrganizationId, tt.args.opt...) + if tt.wantErr { + require.Error(err) + return + } + require.NoError(err) + assert.Equal(tt.wantCnt, len(got)) + }) + } +} + +func TestRepository_ListOrganizations(t *testing.T) { + t.Parallel() + cleanup, conn, _ := db.TestSetup(t, "postgres") + defer func() { + err := cleanup() + assert.NoError(t, err) + err = conn.Close() + assert.NoError(t, err) + }() + const testLimit = 10 + rw := db.New(conn) + wrapper := db.TestWrapper(t) + repo, err := NewRepository(rw, rw, wrapper, WithLimit(testLimit)) + require.NoError(t, err) + type args struct { + opt []Option + } + tests := []struct { + name string + createCnt int + args args + wantCnt int + wantErr bool + }{ + { + name: "no-limit", + createCnt: repo.defaultLimit + 1, + args: args{ + opt: []Option{WithLimit(-1)}, + }, + wantCnt: repo.defaultLimit + 1, + wantErr: false, + }, + { + name: "default-limit", + createCnt: repo.defaultLimit + 1, + args: args{}, + wantCnt: repo.defaultLimit, + wantErr: false, + }, + { + name: "custom-limit", + createCnt: repo.defaultLimit + 1, + args: args{ + opt: []Option{WithLimit(3)}, + }, + wantCnt: 3, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert, require := assert.New(t), require.New(t) + testOrgs := []*Scope{} + for i := 0; i < tt.createCnt; i++ { + testOrgs = append(testOrgs, testOrg(t, conn, "", "")) + } + assert.Equal(tt.createCnt, len(testOrgs)) + got, err := repo.ListOrganizations(context.Background(), tt.args.opt...) + if tt.wantErr { + require.Error(err) + return + } + require.NoError(err) + assert.Equal(tt.wantCnt, len(got)) + }) + } +}