53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package target
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/pagination"
|
|
)
|
|
|
|
// List lists targets according to the page size,
|
|
// filtering out entries that do not
|
|
// pass the filter item function. It returns a new refresh token
|
|
// based on the grants hash and the returned targets.
|
|
func List(
|
|
ctx context.Context,
|
|
grantsHash []byte,
|
|
pageSize int,
|
|
filterItemFn pagination.ListFilterFunc[Target],
|
|
repo *Repository,
|
|
) (*pagination.ListResponse[Target], error) {
|
|
const op = "target.List"
|
|
|
|
if len(grantsHash) == 0 {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing grants hash")
|
|
}
|
|
if pageSize < 1 {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "page size must be at least 1")
|
|
}
|
|
if filterItemFn == nil {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing filter item callback")
|
|
}
|
|
if repo == nil {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing repo")
|
|
}
|
|
|
|
listItemsFn := func(ctx context.Context, lastPageItem Target, limit int) ([]Target, error) {
|
|
opts := []Option{
|
|
WithLimit(limit),
|
|
}
|
|
if lastPageItem != nil {
|
|
opts = append(opts,
|
|
WithStartPageAfterItem(lastPageItem),
|
|
)
|
|
}
|
|
return repo.listTargets(ctx, opts...)
|
|
}
|
|
|
|
return pagination.List(ctx, grantsHash, pageSize, filterItemFn, listItemsFn, repo.estimatedCount)
|
|
}
|