internal/credential/static: add store pagination logic
This commit is contained in:
1 parent
7efb43ac03
commit
3197ab763f
3 files changed
+31
-90
No files matched your search
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package static
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/credential"
|
||||
)
|
||||
|
||||
func init() {
|
||||
credential.RegisterStoreSubtype("static", &credentialHooks{})
|
||||
}
|
||||
|
||||
type credentialHooks struct{}
|
||||
|
||||
// NewStore creates a new static credential store from the result
|
||||
func (credentialHooks) NewStore(ctx context.Context, result *credential.StoreListQueryResult) (credential.Store, error) {
|
||||
s := allocCredentialStore()
|
||||
s.PublicId = result.PublicId
|
||||
s.ProjectId = result.ProjectId
|
||||
s.CreateTime = result.CreateTime
|
||||
s.UpdateTime = result.UpdateTime
|
||||
s.Name = result.Name
|
||||
s.Description = result.Description
|
||||
s.ProjectId = result.ProjectId
|
||||
s.Version = result.Version
|
||||
|
||||
return s, nil
|
||||
}
|
||||
@@ -171,27 +171,6 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
return returnedCredentialStore, rowsUpdated, nil
|
||||
}
|
||||
|
||||
// ListCredentialStores returns a slice of CredentialStores for the
|
||||
// projectIds. WithLimit is the only option supported.
|
||||
func (r *Repository) ListCredentialStores(ctx context.Context, projectIds []string, opt ...Option) ([]*CredentialStore, error) {
|
||||
const op = "static.(Repository).ListCredentialStores"
|
||||
if len(projectIds) == 0 {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no projectIds")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
if opts.withLimit != 0 {
|
||||
// non-zero signals an override of the default limit for the repo.
|
||||
limit = opts.withLimit
|
||||
}
|
||||
var credentialStores []*CredentialStore
|
||||
err := r.reader.SearchWhere(ctx, &credentialStores, "project_id in (?)", []any{projectIds}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return credentialStores, nil
|
||||
}
|
||||
|
||||
// DeleteCredentialStore deletes publicId from the repository and returns
|
||||
// the number of records deleted. All options are ignored.
|
||||
func (r *Repository) DeleteCredentialStore(ctx context.Context, publicId string, _ ...Option) (int, error) {
|
||||
|
||||
@@ -630,75 +630,6 @@ func TestRepository_UpdateCredentialStore(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_ListCredentialStores(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(context.Background(), rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
const num = 10
|
||||
var prjs []string
|
||||
var total int
|
||||
for i := 0; i < num; i++ {
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
prjs = append(prjs, prj.GetPublicId())
|
||||
TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), num)
|
||||
total += num
|
||||
}
|
||||
|
||||
type args struct {
|
||||
projectIds []string
|
||||
opt []Option
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantCnt int
|
||||
}{
|
||||
{
|
||||
name: "no-limit",
|
||||
args: args{
|
||||
projectIds: prjs,
|
||||
opt: []Option{WithLimit(-1)},
|
||||
},
|
||||
wantCnt: total,
|
||||
},
|
||||
{
|
||||
name: "default-limit",
|
||||
args: args{
|
||||
projectIds: prjs,
|
||||
},
|
||||
wantCnt: total,
|
||||
},
|
||||
{
|
||||
name: "custom-limit",
|
||||
args: args{
|
||||
projectIds: prjs,
|
||||
opt: []Option{WithLimit(3)},
|
||||
},
|
||||
wantCnt: 3,
|
||||
},
|
||||
{
|
||||
name: "bad-project",
|
||||
args: args{
|
||||
projectIds: []string{"bad-id"},
|
||||
},
|
||||
wantCnt: 0,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, err := repo.ListCredentialStores(context.Background(), tt.args.projectIds, tt.args.opt...)
|
||||
require.NoError(err)
|
||||
assert.Equal(tt.wantCnt, len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_DeleteCredentialStore(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
|
||||
Reference in new issue
Block a user