Files
threshold/internal/host/static/repository.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.5 KiB
Go
Raw Normal View History

2020-08-10 17:15:02 -04:00
// Copyright (c) HashiCorp, Inc.
2020-05-27 18:16:43 -04:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-05-27 18:16:43 -04:00
package static
import (
"context"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/db"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-19 16:47:18 -04:00
"github.com/hashicorp/boundary/internal/kms"
2020-05-27 18:16:43 -04:00
)
// A Repository stores and retrieves the persistent types in the static
// package. It is not safe to use a repository concurrently.
type Repository struct {
2020-08-19 16:47:18 -04:00
reader db.Reader
writer db.Writer
kms *kms.Kms
2020-08-19 14:14:14 -04:00
// defaultLimit provides a default for limiting the number of results
// returned from the repo
defaultLimit int
2020-05-27 18:16:43 -04:00
}
// NewRepository creates a new Repository. The returned repository should
// only be used for one transaction and it is not safe for concurrent go
2020-08-19 14:14:14 -04:00
// routines to access it. WithLimit option is used as a repo wide default
// limit applied to all ListX methods.
func NewRepository(ctx context.Context, r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error) {
2020-12-09 10:11:38 -08:00
const op = "static.NewRepository"
2020-05-27 18:16:43 -04:00
switch {
case r == nil:
return nil, errors.New(ctx, errors.InvalidParameter, op, "db.Reader")
2020-05-27 18:16:43 -04:00
case w == nil:
return nil, errors.New(ctx, errors.InvalidParameter, op, "db.Writer")
2020-08-19 16:47:18 -04:00
case kms == nil:
return nil, errors.New(ctx, errors.InvalidParameter, op, "kms")
2020-05-27 18:16:43 -04:00
}
2020-08-19 14:14:14 -04:00
opts := getOpts(opt...)
if opts.withLimit == 0 {
// zero signals the boundary defaults should be used.
opts.withLimit = db.DefaultLimit
}
2020-05-27 18:16:43 -04:00
return &Repository{
2020-08-19 14:14:14 -04:00
reader: r,
writer: w,
2020-08-19 16:47:18 -04:00
kms: kms,
2020-08-19 14:14:14 -04:00
defaultLimit: opts.withLimit,
2020-05-27 18:16:43 -04:00
}, nil
}