-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/host: add host catalog pagination
- Loading branch information
1 parent
a973259
commit 6bdba65
Showing
6 changed files
with
765 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package host | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/boundary/internal/db" | ||
"github.com/hashicorp/boundary/internal/errors" | ||
"github.com/hashicorp/boundary/internal/plugin" | ||
"github.com/hashicorp/boundary/internal/util" | ||
) | ||
|
||
// PluginCatalogService defines the interface expected | ||
// to gather information about plugin host catalogs. | ||
type PluginCatalogService interface { | ||
EstimatedCatalogCount(context.Context) (int, error) | ||
ListDeletedCatalogIds(context.Context, time.Time, ...Option) ([]string, error) | ||
ListCatalogs(context.Context, []string, ...Option) ([]Catalog, []*plugin.Plugin, error) | ||
} | ||
|
||
// StaticCatalogService defines the interface expected | ||
// to gather information about static host catalogs. | ||
type StaticCatalogService interface { | ||
EstimatedCatalogCount(context.Context) (int, error) | ||
ListDeletedCatalogIds(context.Context, time.Time, ...Option) ([]string, error) | ||
ListCatalogs(context.Context, []string, ...Option) ([]Catalog, error) | ||
} | ||
|
||
// CatalogService coordinates calls across different subtype repositories | ||
// to gather information about all host catalogs. | ||
type CatalogService struct { | ||
pluginService PluginCatalogService | ||
staticService StaticCatalogService | ||
writer db.Writer | ||
} | ||
|
||
// NewCatalogService returns a new host catalog service. | ||
func NewCatalogService(ctx context.Context, writer db.Writer, pluginService PluginCatalogService, staticService StaticCatalogService) (*CatalogService, error) { | ||
const op = "host.NewCatalogService" | ||
switch { | ||
case util.IsNil(writer): | ||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing DB writer") | ||
case util.IsNil(pluginService): | ||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing plugin service") | ||
case util.IsNil(staticService): | ||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing static service") | ||
} | ||
return &CatalogService{ | ||
staticService: staticService, | ||
pluginService: pluginService, | ||
writer: writer, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package host | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
|
||
"github.com/hashicorp/boundary/internal/errors" | ||
"github.com/hashicorp/boundary/internal/pagination" | ||
"github.com/hashicorp/boundary/internal/plugin" | ||
) | ||
|
||
// List lists host catalogs according to the page size, | ||
// filtering out entries that do not pass the filter item fn. | ||
// It returns a new refresh token based on the grants hash and the returned catalogs. | ||
func (s *CatalogService) List( | ||
ctx context.Context, | ||
grantsHash []byte, | ||
pageSize int, | ||
filterItemFn pagination.ListPluginsFilterFunc[Catalog], | ||
projectIds []string, | ||
) (*pagination.ListResponse[Catalog], []*plugin.Plugin, error) { | ||
const op = "host.(*CatalogService).List" | ||
|
||
if len(grantsHash) == 0 { | ||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing grants hash") | ||
} | ||
if pageSize < 1 { | ||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "page size must be at least 1") | ||
} | ||
if filterItemFn == nil { | ||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing filter item callback") | ||
} | ||
if len(projectIds) == 0 { | ||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing project ids") | ||
} | ||
|
||
listItemsFn := func(ctx context.Context, lastPageItem Catalog, limit int) ([]Catalog, []*plugin.Plugin, error) { | ||
opts := []Option{ | ||
WithLimit(limit), | ||
} | ||
if lastPageItem != nil { | ||
opts = append(opts, WithStartPageAfterItem(lastPageItem)) | ||
} | ||
// Request another page from the DB until we fill the final items | ||
pluginPage, plgs, err := s.pluginService.ListCatalogs(ctx, projectIds, opts...) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
staticPage, err := s.staticService.ListCatalogs(ctx, projectIds, opts...) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
page := append(pluginPage, staticPage...) | ||
slices.SortFunc(page, func(i, j Catalog) int { | ||
return i.GetUpdateTime().AsTime().Compare(j.GetUpdateTime().AsTime()) | ||
}) | ||
// Truncate slice to at most limit number of elements | ||
if len(page) > limit { | ||
page = page[:limit] | ||
} | ||
return page, plgs, nil | ||
} | ||
estimatedCountFn := func(ctx context.Context) (int, error) { | ||
pluginCount, err := s.pluginService.EstimatedCatalogCount(ctx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
staticCount, err := s.staticService.EstimatedCatalogCount(ctx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return pluginCount + staticCount, nil | ||
} | ||
|
||
return pagination.ListPlugins(ctx, grantsHash, pageSize, filterItemFn, listItemsFn, estimatedCountFn) | ||
} |
Oops, something went wrong.