Refactor host_repo -> domain errors (#786)
* Refactor domain errors: New -> E, create helper New and Wrap * Update host repo to use domain errors
This commit is contained in:
1 parent
aa4157639c
commit
ec0020de90
33 files changed
+996
-374
No files matched your search
@@ -1,9 +0,0 @@
|
||||
package static
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrInvalidAddress results from attempting to perform an operation
|
||||
// that sets an address on a host to an invalid value.
|
||||
ErrInvalidAddress = errors.New("invalid address")
|
||||
)
|
||||
@@ -1,8 +1,6 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
@@ -25,7 +23,7 @@ type Host struct {
|
||||
// ignored.
|
||||
func NewHost(catalogId string, opt ...Option) (*Host, error) {
|
||||
if catalogId == "" {
|
||||
return nil, fmt.Errorf("new: static host: no catalog id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, "static.NewHost", "no catalog id")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
@@ -21,7 +19,7 @@ type HostCatalog struct {
|
||||
// ignored.
|
||||
func NewHostCatalog(scopeId string, opt ...Option) (*HostCatalog, error) {
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("new: static host catalog: no scope id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, "static.NewHostCatalog", "no scope id")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
@@ -20,7 +18,7 @@ type HostSet struct {
|
||||
// ignored.
|
||||
func NewHostSet(catalogId string, opt ...Option) (*HostSet, error) {
|
||||
if catalogId == "" {
|
||||
return nil, fmt.Errorf("new: static host set: no catalog id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, "static.NewHostSet", "no catalog id")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
)
|
||||
@@ -16,11 +14,12 @@ type HostSetMember struct {
|
||||
// NewHostSetMember creates a new in memory HostSetMember representing the
|
||||
// membership of hostId in hostSetId.
|
||||
func NewHostSetMember(hostSetId, hostId string, opt ...Option) (*HostSetMember, error) {
|
||||
const op = "static.NewHostSetMember"
|
||||
if hostSetId == "" {
|
||||
return nil, fmt.Errorf("new: static host set member: no host set id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no host set id")
|
||||
}
|
||||
if hostId == "" {
|
||||
return nil, fmt.Errorf("new: static host set member: no host id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no host id")
|
||||
}
|
||||
member := &HostSetMember{
|
||||
HostSetMember: &store.HostSetMember{
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
)
|
||||
|
||||
// PublicId prefixes for the resources in the static package.
|
||||
@@ -16,7 +15,7 @@ const (
|
||||
func newHostCatalogId() (string, error) {
|
||||
id, err := db.NewPublicId(HostCatalogPrefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("new host catalog id: %w", err)
|
||||
return "", errors.Wrap(err, "static.newHostCatalogId")
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
@@ -24,7 +23,7 @@ func newHostCatalogId() (string, error) {
|
||||
func newHostId() (string, error) {
|
||||
id, err := db.NewPublicId(HostPrefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("new host id: %w", err)
|
||||
return "", errors.Wrap(err, "static.newHostId")
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
@@ -32,7 +31,7 @@ func newHostId() (string, error) {
|
||||
func newHostSetId() (string, error) {
|
||||
id, err := db.NewPublicId(HostSetPrefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("new host set id: %w", err)
|
||||
return "", errors.Wrap(err, "static.newHostSetId")
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
@@ -25,13 +23,14 @@ type Repository struct {
|
||||
// limit applied to all ListX methods.
|
||||
// routines to access it.
|
||||
func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error) {
|
||||
const op = "static.NewRepository"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, fmt.Errorf("db.Reader: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "db.Reader")
|
||||
case w == nil:
|
||||
return nil, fmt.Errorf("db.Writer: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "db.Writer")
|
||||
case kms == nil:
|
||||
return nil, fmt.Errorf("kms: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "kms")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -22,24 +22,25 @@ import (
|
||||
// Both h.Name and h.Description are optional. If h.Name is set, it must be
|
||||
// unique within h.CatalogId.
|
||||
func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, opt ...Option) (*Host, error) {
|
||||
const op = "static.CreateHost"
|
||||
if h == nil {
|
||||
return nil, fmt.Errorf("create: static host: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil Host")
|
||||
}
|
||||
if h.Host == nil {
|
||||
return nil, fmt.Errorf("create: static host: embedded Host: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded Host")
|
||||
}
|
||||
if h.CatalogId == "" {
|
||||
return nil, fmt.Errorf("create: static host: no catalog id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
if h.PublicId != "" {
|
||||
return nil, fmt.Errorf("create: static host: public id not empty: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("create: static host: no scopeId: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
h.Address = strings.TrimSpace(h.Address)
|
||||
if len(h.Address) < MinHostAddressLength || len(h.Address) > MaxHostAddressLength {
|
||||
return nil, fmt.Errorf("create: static host: bad address: %w", ErrInvalidAddress)
|
||||
return nil, errors.New(errors.InvalidAddress, op, "invalid address")
|
||||
}
|
||||
h = h.clone()
|
||||
|
||||
@@ -47,20 +48,24 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
|
||||
if opts.withPublicId != "" {
|
||||
if !strings.HasPrefix(opts.withPublicId, HostPrefix+"_") {
|
||||
return nil, fmt.Errorf("create: static host: passed-in public ID %q has wrong prefix, should be %q: %w", opts.withPublicId, HostPrefix, errors.ErrInvalidPublicId)
|
||||
return nil, errors.New(
|
||||
errors.InvalidPublicId,
|
||||
op,
|
||||
fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, HostPrefix),
|
||||
)
|
||||
}
|
||||
h.PublicId = opts.withPublicId
|
||||
} else {
|
||||
id, err := newHostId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
h.PublicId = id
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host: unable to get oplog wrapper: %w", err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var newHost *Host
|
||||
@@ -73,14 +78,17 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, fmt.Errorf("create: static host: in catalog: %s: name %s already exists: %w",
|
||||
h.CatalogId, h.Name, errors.ErrNotUnique)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", h.CatalogId, h.Name)))
|
||||
}
|
||||
if errors.IsCheckConstraintError(err) || errors.IsNotNullError(err) {
|
||||
return nil, fmt.Errorf("create: static host: in catalog: %s: %q: %w",
|
||||
h.CatalogId, h.Address, ErrInvalidAddress)
|
||||
return nil, errors.New(
|
||||
errors.InvalidAddress,
|
||||
op,
|
||||
fmt.Sprintf("in catalog: %s: %q", h.CatalogId, h.Address),
|
||||
errors.WithWrap(err),
|
||||
)
|
||||
}
|
||||
return nil, fmt.Errorf("create: static host: in catalog: %s: %w", h.CatalogId, err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", h.CatalogId)))
|
||||
}
|
||||
return newHost, nil
|
||||
}
|
||||
@@ -98,20 +106,21 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
// An attribute of h will be set to NULL in the database if the attribute
|
||||
// in h is the zero value and it is included in fieldMaskPaths.
|
||||
func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, version uint32, fieldMaskPaths []string, opt ...Option) (*Host, int, error) {
|
||||
const op = "static.UpdateHost"
|
||||
if h == nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil Host")
|
||||
}
|
||||
if h.Host == nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: embedded Host: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil embedded Host")
|
||||
}
|
||||
if h.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: missing public id: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: no version supplied: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: no scopeId: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
|
||||
for _, f := range fieldMaskPaths {
|
||||
@@ -121,10 +130,10 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
case strings.EqualFold("Address", f):
|
||||
h.Address = strings.TrimSpace(h.Address)
|
||||
if len(h.Address) < MinHostAddressLength || len(h.Address) > MaxHostAddressLength {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: bad address: %w", ErrInvalidAddress)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidAddress, op, "invalid address")
|
||||
}
|
||||
default:
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: field: %s: %w", f, errors.ErrInvalidFieldMask)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, fmt.Sprintf("field: %s", f))
|
||||
}
|
||||
}
|
||||
var dbMask, nullFields []string
|
||||
@@ -138,12 +147,12 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
nil,
|
||||
)
|
||||
if len(dbMask) == 0 && len(nullFields) == 0 {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: %w", errors.ErrEmptyFieldMask)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "empty field mask")
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: unable to get oplog wrapper: %w", err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsUpdated int
|
||||
@@ -156,7 +165,7 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_UPDATE)),
|
||||
db.WithVersion(&version))
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return errors.ErrMultipleRecords
|
||||
return errors.E(errors.WithCode(errors.MultipleRecords))
|
||||
}
|
||||
return err
|
||||
},
|
||||
@@ -164,13 +173,17 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: %s: name %s already exists: %w",
|
||||
h.PublicId, h.Name, errors.ErrNotUnique)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s: name %s already exists", h.PublicId, h.Name)))
|
||||
}
|
||||
if errors.IsCheckConstraintError(err) || errors.IsNotNullError(err) {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: %s: %q: %w", h.PublicId, h.Address, ErrInvalidAddress)
|
||||
return nil, db.NoRowsAffected, errors.New(
|
||||
errors.InvalidAddress,
|
||||
op,
|
||||
fmt.Sprintf("in %s: %q", h.PublicId, h.Address),
|
||||
errors.WithWrap(err),
|
||||
)
|
||||
}
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host: %s: %w", h.PublicId, err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", h.PublicId)))
|
||||
}
|
||||
|
||||
return returnedHost, rowsUpdated, nil
|
||||
@@ -179,16 +192,17 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
// LookupHost will look up a host in the repository. If the host is not
|
||||
// found, it will return nil, nil. All options are ignored.
|
||||
func (r *Repository) LookupHost(ctx context.Context, publicId string, opt ...Option) (*Host, error) {
|
||||
const op = "static.LookupHost"
|
||||
if publicId == "" {
|
||||
return nil, fmt.Errorf("lookup: static host: missing public id %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
h := allocHost()
|
||||
h.PublicId = publicId
|
||||
if err := r.reader.LookupByPublicId(ctx, h); err != nil {
|
||||
if errors.Is(err, errors.ErrRecordNotFound) {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("lookup: static host: failed %w for %s", err, publicId)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for %s", publicId)))
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
@@ -196,8 +210,9 @@ func (r *Repository) LookupHost(ctx context.Context, publicId string, opt ...Opt
|
||||
// ListHosts returns a slice of Hosts for the catalogId.
|
||||
// WithLimit is the only option supported.
|
||||
func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Option) ([]*Host, error) {
|
||||
const op = "static.ListHosts"
|
||||
if catalogId == "" {
|
||||
return nil, fmt.Errorf("list: static host: missing catalog id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -208,7 +223,7 @@ func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Opt
|
||||
var hosts []*Host
|
||||
err := r.reader.SearchWhere(ctx, &hosts, "catalog_id = ?", []interface{}{catalogId}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list: static host: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
return hosts, nil
|
||||
}
|
||||
@@ -217,15 +232,16 @@ func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Opt
|
||||
// returning a count of the number of records deleted. All options are
|
||||
// ignored.
|
||||
func (r *Repository) DeleteHost(ctx context.Context, scopeId string, publicId string, opt ...Option) (int, error) {
|
||||
const op = "static.DeleteHost"
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host: missing public id: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
h := allocHost()
|
||||
h.PublicId = publicId
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
@@ -235,14 +251,14 @@ func (r *Repository) DeleteHost(ctx context.Context, scopeId string, publicId st
|
||||
dh := h.clone()
|
||||
rowsDeleted, err = w.Delete(ctx, dh, db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return errors.ErrMultipleRecords
|
||||
return errors.E(errors.WithCode(errors.MultipleRecords))
|
||||
}
|
||||
return err
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host: %s: %w", publicId, err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", publicId)))
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
|
||||
@@ -21,17 +21,18 @@ import (
|
||||
//
|
||||
// Both c.CreateTime and c.UpdateTime are ignored.
|
||||
func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...Option) (*HostCatalog, error) {
|
||||
const op = "static.CreateCatalog"
|
||||
if c == nil {
|
||||
return nil, fmt.Errorf("create: static host catalog: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil HostCatalog")
|
||||
}
|
||||
if c.HostCatalog == nil {
|
||||
return nil, fmt.Errorf("create: static host catalog: embedded HostCatalog: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded HostCatalog")
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return nil, fmt.Errorf("create: static host catalog: no scope id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if c.PublicId != "" {
|
||||
return nil, fmt.Errorf("create: static host catalog: public id not empty: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
c = c.clone()
|
||||
|
||||
@@ -39,20 +40,24 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
|
||||
if opts.withPublicId != "" {
|
||||
if !strings.HasPrefix(opts.withPublicId, HostCatalogPrefix+"_") {
|
||||
return nil, fmt.Errorf("create: static host catalog: passed-in public ID %q has wrong prefix, should be %q: %w", opts.withPublicId, HostCatalogPrefix, errors.ErrInvalidPublicId)
|
||||
return nil, errors.New(
|
||||
errors.InvalidPublicId,
|
||||
op,
|
||||
fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, HostCatalogPrefix),
|
||||
)
|
||||
}
|
||||
c.PublicId = opts.withPublicId
|
||||
} else {
|
||||
id, err := newHostCatalogId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host catalog: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
c.PublicId = id
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_CREATE)
|
||||
@@ -74,10 +79,9 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, fmt.Errorf("create: static host catalog: in scope: %s: name %s already exists: %w",
|
||||
c.ScopeId, c.Name, errors.ErrNotUnique)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in scope: %s: name %s already exists", c.ScopeId, c.Name)))
|
||||
}
|
||||
return nil, fmt.Errorf("create: static host catalog: in scope: %s: %w", c.ScopeId, err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in scope: %s", c.ScopeId)))
|
||||
}
|
||||
return newHostCatalog, nil
|
||||
}
|
||||
@@ -94,20 +98,21 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
// An attribute of c will be set to NULL in the database if the attribute
|
||||
// in c is the zero value and it is included in fieldMask.
|
||||
func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version uint32, fieldMask []string, opt ...Option) (*HostCatalog, int, error) {
|
||||
const op = "static.UpdateCatalog"
|
||||
if c == nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil HostCatalog")
|
||||
}
|
||||
if c.HostCatalog == nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: embedded HostCatalog: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil embedded HostCatalog")
|
||||
}
|
||||
if c.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: missing public id: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: missing scope id: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if len(fieldMask) == 0 {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %w", errors.ErrEmptyFieldMask)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "empty field mask")
|
||||
}
|
||||
|
||||
var dbMask, nullFields []string
|
||||
@@ -123,13 +128,13 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
dbMask = append(dbMask, "description")
|
||||
|
||||
default:
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: field: %s: %w", f, errors.ErrInvalidFieldMask)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, fmt.Sprintf("field: %s", f))
|
||||
}
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
c = c.clone()
|
||||
@@ -154,7 +159,7 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
db.WithVersion(&version),
|
||||
)
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return errors.ErrMultipleRecords
|
||||
return errors.E(errors.WithCode(errors.MultipleRecords))
|
||||
}
|
||||
return err
|
||||
},
|
||||
@@ -162,10 +167,9 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %s: name %s already exists: %w",
|
||||
c.PublicId, c.Name, errors.ErrNotUnique)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s: name %s already exists", c.PublicId, c.Name)))
|
||||
}
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("update: static host catalog: %s: %w", c.PublicId, err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", c.PublicId)))
|
||||
}
|
||||
|
||||
return returnedCatalog, rowsUpdated, nil
|
||||
@@ -174,24 +178,26 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
// LookupCatalog returns the HostCatalog for id. Returns nil, nil if no
|
||||
// HostCatalog is found for id.
|
||||
func (r *Repository) LookupCatalog(ctx context.Context, id string, opt ...Option) (*HostCatalog, error) {
|
||||
const op = "static.LookupCatalog"
|
||||
if id == "" {
|
||||
return nil, fmt.Errorf("lookup: static host catalog: missing public id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
c := allocCatalog()
|
||||
c.PublicId = id
|
||||
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
|
||||
if err == errors.ErrRecordNotFound {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("lookup: static host catalog: %s: %w", id, err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// ListCatalogs returns a slice of HostCatalogs for the scopeId. WithLimit is the only option supported.
|
||||
func (r *Repository) ListCatalogs(ctx context.Context, scopeId string, opt ...Option) ([]*HostCatalog, error) {
|
||||
const op = "static.ListCatalogs"
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("list: static host catalog: missing scope id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -202,7 +208,7 @@ func (r *Repository) ListCatalogs(ctx context.Context, scopeId string, opt ...Op
|
||||
var hostCatalogs []*HostCatalog
|
||||
err := r.reader.SearchWhere(ctx, &hostCatalogs, "scope_id = ?", []interface{}{scopeId}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list: static host catalog: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
return hostCatalogs, nil
|
||||
}
|
||||
@@ -210,24 +216,25 @@ func (r *Repository) ListCatalogs(ctx context.Context, scopeId string, opt ...Op
|
||||
// DeleteCatalog deletes id from the repository returning a count of the
|
||||
// number of records deleted.
|
||||
func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option) (int, error) {
|
||||
const op = "static.DeleteCatalog"
|
||||
if id == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: missing public id: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
|
||||
c := allocCatalog()
|
||||
c.PublicId = id
|
||||
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
|
||||
if errors.Is(err, errors.ErrRecordNotFound) {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return db.NoRowsAffected, nil
|
||||
}
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: failed %w for %s", err, id)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for %s", id)))
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: missing scope id: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: unable to get oplog wrapper: %w", err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_DELETE)
|
||||
@@ -247,14 +254,14 @@ func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return errors.ErrMultipleRecords
|
||||
return errors.E(errors.WithCode(errors.MultipleRecords))
|
||||
}
|
||||
return err
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host catalog: %s: %w", c.PublicId, err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", c.PublicId)))
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
|
||||
@@ -25,16 +25,16 @@ func TestRepository_CreateCatalog(t *testing.T) {
|
||||
in *HostCatalog
|
||||
opts []Option
|
||||
want *HostCatalog
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "nil-catalog",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-embedded-catalog",
|
||||
in: &HostCatalog{},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "valid-no-options",
|
||||
@@ -87,8 +87,8 @@ func TestRepository_CreateCatalog(t *testing.T) {
|
||||
assert.Empty(tt.in.PublicId)
|
||||
}
|
||||
got, err := repo.CreateCatalog(context.Background(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
@@ -236,7 +236,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
masks []string
|
||||
want *HostCatalog
|
||||
wantCount int
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "nil-catalog",
|
||||
@@ -245,7 +245,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
},
|
||||
chgFn: makeNil(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-embedded-catalog",
|
||||
@@ -254,7 +254,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
},
|
||||
chgFn: makeEmbeddedNil(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "no-public-id",
|
||||
@@ -263,7 +263,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
},
|
||||
chgFn: deletePublicId(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "updating-non-existent-catalog",
|
||||
@@ -274,7 +274,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
},
|
||||
chgFn: combine(nonExistentPublicId(), changeName("test-update-name-repo")),
|
||||
masks: []string{"Name"},
|
||||
wantIsErr: errors.ErrRecordNotFound,
|
||||
wantIsErr: errors.RecordNotFound,
|
||||
},
|
||||
{
|
||||
name: "empty-field-mask",
|
||||
@@ -284,7 +284,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
},
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
wantIsErr: errors.ErrEmptyFieldMask,
|
||||
wantIsErr: errors.EmptyFieldMask,
|
||||
},
|
||||
{
|
||||
name: "read-only-fields-in-field-mask",
|
||||
@@ -295,7 +295,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
masks: []string{"PublicId", "CreateTime", "UpdateTime", "ScopeId"},
|
||||
wantIsErr: errors.ErrInvalidFieldMask,
|
||||
wantIsErr: errors.InvalidFieldMask,
|
||||
},
|
||||
{
|
||||
name: "unknown-field-in-field-mask",
|
||||
@@ -306,7 +306,7 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
masks: []string{"Bilbo"},
|
||||
wantIsErr: errors.ErrInvalidFieldMask,
|
||||
wantIsErr: errors.InvalidFieldMask,
|
||||
},
|
||||
{
|
||||
name: "change-name",
|
||||
@@ -448,8 +448,8 @@ func TestRepository_UpdateCatalog(t *testing.T) {
|
||||
orig = tt.chgFn(orig)
|
||||
}
|
||||
got, gotCount, err := repo.UpdateCatalog(context.Background(), orig, 1, tt.masks)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Equal(tt.wantCount, gotCount, "row count")
|
||||
assert.Nil(got)
|
||||
return
|
||||
@@ -579,7 +579,7 @@ func TestRepository_LookupCatalog(t *testing.T) {
|
||||
name string
|
||||
id string
|
||||
want *HostCatalog
|
||||
wantErr error
|
||||
wantErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "found",
|
||||
@@ -595,7 +595,7 @@ func TestRepository_LookupCatalog(t *testing.T) {
|
||||
name: "bad-public-id",
|
||||
id: "",
|
||||
want: nil,
|
||||
wantErr: errors.ErrInvalidParameter,
|
||||
wantErr: errors.InvalidParameter,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -609,8 +609,8 @@ func TestRepository_LookupCatalog(t *testing.T) {
|
||||
assert.NotNil(repo)
|
||||
|
||||
got, err := repo.LookupCatalog(context.Background(), tt.id)
|
||||
if tt.wantErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantErr), "want err: %q got: %q", tt.wantErr, err)
|
||||
if tt.wantErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantErr), err), "want err: %q got: %q", tt.wantErr, err)
|
||||
return
|
||||
}
|
||||
assert.NoError(err)
|
||||
@@ -641,7 +641,7 @@ func TestRepository_DeleteCatalog(t *testing.T) {
|
||||
name string
|
||||
id string
|
||||
want int
|
||||
wantErr error
|
||||
wantErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "found",
|
||||
@@ -657,7 +657,7 @@ func TestRepository_DeleteCatalog(t *testing.T) {
|
||||
name: "bad-public-id",
|
||||
id: "",
|
||||
want: 0,
|
||||
wantErr: errors.ErrInvalidParameter,
|
||||
wantErr: errors.InvalidParameter,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -671,8 +671,8 @@ func TestRepository_DeleteCatalog(t *testing.T) {
|
||||
assert.NotNil(repo)
|
||||
|
||||
got, err := repo.DeleteCatalog(context.Background(), tt.id)
|
||||
if tt.wantErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantErr), "want err: %q got: %q", tt.wantErr, err)
|
||||
if tt.wantErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantErr), err), "want err: %q got: %q", tt.wantErr, err)
|
||||
return
|
||||
}
|
||||
assert.NoError(err)
|
||||
|
||||
@@ -20,20 +20,21 @@ import (
|
||||
// Both s.Name and s.Description are optional. If s.Name is set, it must be
|
||||
// unique within s.CatalogId.
|
||||
func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet, opt ...Option) (*HostSet, error) {
|
||||
const op = "static.CreateSet"
|
||||
if s == nil {
|
||||
return nil, fmt.Errorf("create: static host set: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil HostSet")
|
||||
}
|
||||
if s.HostSet == nil {
|
||||
return nil, fmt.Errorf("create: static host set: embedded HostSet: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded HostSet")
|
||||
}
|
||||
if s.CatalogId == "" {
|
||||
return nil, fmt.Errorf("create: static host set: no catalog id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
if s.PublicId != "" {
|
||||
return nil, fmt.Errorf("create: static host set: public id not empty: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("create: static host set: no scopeId: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
s = s.clone()
|
||||
|
||||
@@ -41,20 +42,24 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
|
||||
if opts.withPublicId != "" {
|
||||
if !strings.HasPrefix(opts.withPublicId, HostSetPrefix+"_") {
|
||||
return nil, fmt.Errorf("create: static host set: passed-in public ID %q has wrong prefix, should be %q: %w", opts.withPublicId, HostSetPrefix, errors.ErrInvalidPublicId)
|
||||
return nil, errors.New(
|
||||
errors.InvalidPublicId,
|
||||
op,
|
||||
fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, HostSetPrefix),
|
||||
)
|
||||
}
|
||||
s.PublicId = opts.withPublicId
|
||||
} else {
|
||||
id, err := newHostSetId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host set: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
s.PublicId = id
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create: static host set: unable to get oplog wrapper: %w", err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var newHostSet *HostSet
|
||||
@@ -67,10 +72,9 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, fmt.Errorf("create: static host set: in catalog: %s: name %s already exists: %w",
|
||||
s.CatalogId, s.Name, errors.ErrNotUnique)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", s.CatalogId, s.Name)))
|
||||
}
|
||||
return nil, fmt.Errorf("create: static host set: in catalog: %s: %w", s.CatalogId, err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", s.CatalogId)))
|
||||
}
|
||||
return newHostSet, nil
|
||||
}
|
||||
@@ -90,20 +94,21 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
// The WithLimit option can be used to limit the number of hosts returned.
|
||||
// All other options are ignored.
|
||||
func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet, version uint32, fieldMaskPaths []string, opt ...Option) (*HostSet, []*Host, int, error) {
|
||||
const op = "static.UpdateSet"
|
||||
if s == nil {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: %w", errors.ErrInvalidParameter)
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil HostSet")
|
||||
}
|
||||
if s.HostSet == nil {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: embedded HostSet: %w", errors.ErrInvalidParameter)
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil embedded HostSet")
|
||||
}
|
||||
if s.PublicId == "" {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: missing public id: %w", errors.ErrInvalidParameter)
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: no version supplied: %w", errors.ErrInvalidParameter)
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: no scopeId: %w", errors.ErrInvalidParameter)
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
|
||||
for _, f := range fieldMaskPaths {
|
||||
@@ -111,7 +116,7 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
case strings.EqualFold("Name", f):
|
||||
case strings.EqualFold("Description", f):
|
||||
default:
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: field: %s: %w", f, errors.ErrInvalidFieldMask)
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, fmt.Sprintf("field: %s", f))
|
||||
}
|
||||
}
|
||||
var dbMask, nullFields []string
|
||||
@@ -124,7 +129,7 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
nil,
|
||||
)
|
||||
if len(dbMask) == 0 && len(nullFields) == 0 {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: %w", errors.ErrEmptyFieldMask)
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "empty field mask")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -136,7 +141,7 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: unable to get oplog wrapper: %w", err)
|
||||
return nil, nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsUpdated int
|
||||
@@ -150,7 +155,7 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
db.WithOplog(oplogWrapper, s.oplog(oplog.OpType_OP_TYPE_UPDATE)),
|
||||
db.WithVersion(&version))
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return errors.ErrMultipleRecords
|
||||
return errors.E(errors.WithCode(errors.MultipleRecords))
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -162,10 +167,9 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: %s: name %s already exists: %w",
|
||||
s.PublicId, s.Name, errors.ErrNotUnique)
|
||||
return nil, nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s: name %s already exists", s.PublicId, s.Name)))
|
||||
}
|
||||
return nil, nil, db.NoRowsAffected, fmt.Errorf("update: static host set: %s: %w", s.PublicId, err)
|
||||
return nil, nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", s.PublicId)))
|
||||
}
|
||||
|
||||
return returnedHostSet, hosts, rowsUpdated, nil
|
||||
@@ -176,8 +180,9 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
// found, it will return nil, nil, nil. The WithLimit option can be used to
|
||||
// limit the number of hosts returned. All other options are ignored.
|
||||
func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Option) (*HostSet, []*Host, error) {
|
||||
const op = "static.LookupSet"
|
||||
if publicId == "" {
|
||||
return nil, nil, fmt.Errorf("lookup: static host set: missing public id %w", errors.ErrInvalidParameter)
|
||||
return nil, nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -192,7 +197,7 @@ func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Opti
|
||||
var hosts []*Host
|
||||
_, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(reader db.Reader, _ db.Writer) error {
|
||||
if err := reader.LookupByPublicId(ctx, s); err != nil {
|
||||
if errors.Is(err, errors.ErrRecordNotFound) {
|
||||
if errors.IsNotFoundError(err) {
|
||||
s = nil
|
||||
return nil
|
||||
}
|
||||
@@ -204,7 +209,7 @@ func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Opti
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("lookup: static host set: failed %w for %s", err, publicId)
|
||||
return nil, nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", s.PublicId)))
|
||||
}
|
||||
|
||||
return s, hosts, nil
|
||||
@@ -213,8 +218,9 @@ func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Opti
|
||||
// ListSets returns a slice of HostSets for the catalogId. WithLimit is the
|
||||
// only option supported.
|
||||
func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...Option) ([]*HostSet, error) {
|
||||
const op = "static.ListSets"
|
||||
if catalogId == "" {
|
||||
return nil, fmt.Errorf("list: static host set: missing catalog id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -225,7 +231,7 @@ func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...Opti
|
||||
var sets []*HostSet
|
||||
err := r.reader.SearchWhere(ctx, &sets, "catalog_id = ?", []interface{}{catalogId}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list: static host set: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
return sets, nil
|
||||
}
|
||||
@@ -234,18 +240,19 @@ func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...Opti
|
||||
// returning a count of the number of records deleted. All options are
|
||||
// ignored.
|
||||
func (r *Repository) DeleteSet(ctx context.Context, scopeId string, publicId string, opt ...Option) (int, error) {
|
||||
const op = "static.DeleteSet"
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set: missing public id: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set: no scopeId: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
s := allocHostSet()
|
||||
s.PublicId = publicId
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set: unable to get oplog wrapper: %w", err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
@@ -255,14 +262,14 @@ func (r *Repository) DeleteSet(ctx context.Context, scopeId string, publicId str
|
||||
ds := s.clone()
|
||||
rowsDeleted, err = w.Delete(ctx, ds, db.WithOplog(oplogWrapper, s.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return errors.ErrMultipleRecords
|
||||
return errors.E(errors.WithCode(errors.MultipleRecords))
|
||||
}
|
||||
return err
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set: %s: %w", publicId, err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", s.PublicId)))
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
|
||||
@@ -18,28 +18,29 @@ import (
|
||||
// the set to be added. The version must match the current version of the
|
||||
// setId in the repository.
|
||||
func (r *Repository) AddSetMembers(ctx context.Context, scopeId string, setId string, version uint32, hostIds []string, opt ...Option) ([]*Host, error) {
|
||||
const op = "static.AddSetMembers"
|
||||
if scopeId == "" {
|
||||
return nil, fmt.Errorf("add: static host set members: missing scope id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if setId == "" {
|
||||
return nil, fmt.Errorf("add: static host set members: missing set id: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no set id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, fmt.Errorf("add: static host set members: version is zero: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if len(hostIds) == 0 {
|
||||
return nil, fmt.Errorf("add: static host set members: empty hostIds: %w", errors.ErrInvalidParameter)
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no host ids")
|
||||
}
|
||||
|
||||
// Create in-memory host set members
|
||||
members, err := r.newMembers(setId, hostIds)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add: static host set members: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
|
||||
wrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add: static host set members: unable to get oplog wrapper: %w", err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var hosts []*Host
|
||||
@@ -61,7 +62,7 @@ func (r *Repository) AddSetMembers(ctx context.Context, scopeId string, setId st
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("add: static host set members: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
|
||||
return hosts, nil
|
||||
@@ -73,7 +74,7 @@ func (r *Repository) newMembers(setId string, hostIds []string) ([]interface{},
|
||||
var m *HostSetMember
|
||||
m, err := NewHostSetMember(setId, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new members: %w", err)
|
||||
return nil, errors.Wrap(err, "static.newMembers")
|
||||
}
|
||||
members = append(members, m)
|
||||
}
|
||||
@@ -83,29 +84,30 @@ func (r *Repository) newMembers(setId string, hostIds []string) ([]interface{},
|
||||
func createMembers(ctx context.Context, w db.Writer, members []interface{}) ([]*oplog.Message, error) {
|
||||
var msgs []*oplog.Message
|
||||
if err := w.CreateItems(ctx, members, db.NewOplogMsgs(&msgs)); err != nil {
|
||||
return nil, fmt.Errorf("unable to create host set members: %w", err)
|
||||
return nil, errors.Wrap(err, "static.createMembers")
|
||||
}
|
||||
return msgs, nil
|
||||
}
|
||||
|
||||
func updateVersion(ctx context.Context, w db.Writer, wrapper wrapping.Wrapper, metadata oplog.Metadata, msgs []*oplog.Message, set *HostSet, version uint32) error {
|
||||
const op = "static.updateVersion"
|
||||
setMsg := new(oplog.Message)
|
||||
rowsUpdated, err := w.Update(ctx, set, []string{"Version"}, nil, db.NewOplogMsg(setMsg), db.WithVersion(&version))
|
||||
switch {
|
||||
case err != nil:
|
||||
return fmt.Errorf("unable to update host set version: %w", err)
|
||||
return errors.Wrap(err, op)
|
||||
case rowsUpdated > 1:
|
||||
return fmt.Errorf("unable to update host set version: %w", errors.ErrMultipleRecords)
|
||||
return errors.E(errors.WithCode(errors.MultipleRecords))
|
||||
}
|
||||
msgs = append(msgs, setMsg)
|
||||
|
||||
// Write oplog
|
||||
ticket, err := w.GetTicket(set)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get ticket: %w", err)
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get ticket"))
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, wrapper, ticket, metadata, msgs); err != nil {
|
||||
return fmt.Errorf("unable to write oplog: %w", err)
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to write oplog"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -142,7 +144,7 @@ func getHosts(ctx context.Context, reader db.Reader, setId string, limit int) ([
|
||||
params,
|
||||
db.WithLimit(limit),
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("get hosts: %w", err)
|
||||
return nil, errors.Wrap(err, "static.getHosts")
|
||||
}
|
||||
if len(hosts) == 0 {
|
||||
return nil, nil
|
||||
@@ -154,28 +156,29 @@ func getHosts(ctx context.Context, reader db.Reader, setId string, limit int) ([
|
||||
// returns the number of hosts deleted from the set. The version must match
|
||||
// the current version of the setId in the repository.
|
||||
func (r *Repository) DeleteSetMembers(ctx context.Context, scopeId string, setId string, version uint32, hostIds []string, opt ...Option) (int, error) {
|
||||
const op = "static.DeleteSetMembers"
|
||||
if scopeId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set members: missing scope id: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if setId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set members: missing set id: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no set id")
|
||||
}
|
||||
if version == 0 {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set members: version is zero: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if len(hostIds) == 0 {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set members: empty hostIds: %w", errors.ErrInvalidParameter)
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no host ids")
|
||||
}
|
||||
|
||||
// Create in-memory host set members
|
||||
members, err := r.newMembers(setId, hostIds)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set members: %w", err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op)
|
||||
}
|
||||
|
||||
wrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set members: unable to get oplog wrapper: %w", err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(_ db.Reader, w db.Writer) error {
|
||||
@@ -193,19 +196,20 @@ func (r *Repository) DeleteSetMembers(ctx context.Context, scopeId string, setId
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: static host set members: %w", err)
|
||||
return db.NoRowsAffected, errors.Wrap(err, op)
|
||||
}
|
||||
return len(hostIds), nil
|
||||
}
|
||||
|
||||
func deleteMembers(ctx context.Context, w db.Writer, members []interface{}) ([]*oplog.Message, error) {
|
||||
const op = "static.deleteMembers"
|
||||
var msgs []*oplog.Message
|
||||
rowsDeleted, err := w.DeleteItems(ctx, members, db.NewOplogMsgs(&msgs))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to delete host set members: %w", err)
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
if rowsDeleted != len(members) {
|
||||
return nil, fmt.Errorf("set members deleted %d did not match request for %d", rowsDeleted, len(members))
|
||||
return nil, errors.E(errors.WithMsg(fmt.Sprintf("set members deleted %d did not match request for %d", rowsDeleted, len(members))))
|
||||
}
|
||||
return msgs, nil
|
||||
}
|
||||
@@ -216,14 +220,15 @@ func deleteMembers(ctx context.Context, w db.Writer, members []interface{}) ([]*
|
||||
// set to be added. The version must match the current version of the setId
|
||||
// in the repository. If hostIds is empty, all hosts will be removed setId.
|
||||
func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId string, version uint32, hostIds []string, opt ...Option) ([]*Host, int, error) {
|
||||
const op = "static.SetSetMembers"
|
||||
if scopeId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set: static host set members: missing scope id: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if setId == "" {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set: static host set members: missing set id: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no set id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set: static host set members: version is zero: %w", errors.ErrInvalidParameter)
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
|
||||
// TODO(mgaffney) 08/2020: Oplog does not currently support bulk
|
||||
@@ -241,13 +246,13 @@ func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId st
|
||||
// this pattern.
|
||||
changes, err := r.changes(ctx, setId, hostIds)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set: static host set members: %w", err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
}
|
||||
var deletions, additions []interface{}
|
||||
for _, c := range changes {
|
||||
m, err := NewHostSetMember(setId, c.HostId)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set: static host set members: %w", err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
}
|
||||
switch c.Action {
|
||||
case "delete":
|
||||
@@ -261,7 +266,7 @@ func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId st
|
||||
if len(changes) > 0 {
|
||||
wrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set: static host set members: unable to get oplog wrapper: %w", err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(reader db.Reader, w db.Writer) error {
|
||||
@@ -299,7 +304,7 @@ func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId st
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, fmt.Errorf("set: static host set members: %w", err)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
}
|
||||
}
|
||||
return hosts, len(changes), nil
|
||||
@@ -311,6 +316,7 @@ type change struct {
|
||||
}
|
||||
|
||||
func (r *Repository) changes(ctx context.Context, setId string, hostIds []string) ([]*change, error) {
|
||||
const op = "static.changes"
|
||||
var inClauseSpots []string
|
||||
// starts at 2 because there is already a $1 in the query
|
||||
for i := 2; i < len(hostIds)+2; i++ {
|
||||
@@ -329,7 +335,7 @@ func (r *Repository) changes(ctx context.Context, setId string, hostIds []string
|
||||
}
|
||||
rows, err := r.reader.Query(ctx, query, params)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("changes: query failed: %w", err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("query failed"))
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -337,7 +343,7 @@ func (r *Repository) changes(ctx context.Context, setId string, hostIds []string
|
||||
for rows.Next() {
|
||||
var chg change
|
||||
if err := r.reader.ScanRows(rows, &chg); err != nil {
|
||||
return nil, fmt.Errorf("changes: scan row failed: %w", err)
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("scan row failed"))
|
||||
}
|
||||
changes = append(changes, &chg)
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func TestRepository_AddSetMembers_Parameters(t *testing.T) {
|
||||
args args
|
||||
want []*Host
|
||||
wantErr bool
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "empty-scope-id",
|
||||
@@ -57,7 +57,7 @@ func TestRepository_AddSetMembers_Parameters(t *testing.T) {
|
||||
version: set.Version,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "empty-set-id",
|
||||
@@ -66,7 +66,7 @@ func TestRepository_AddSetMembers_Parameters(t *testing.T) {
|
||||
version: set.Version,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "zero-version",
|
||||
@@ -75,7 +75,7 @@ func TestRepository_AddSetMembers_Parameters(t *testing.T) {
|
||||
setId: set.PublicId,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "empty-host-ids",
|
||||
@@ -84,7 +84,7 @@ func TestRepository_AddSetMembers_Parameters(t *testing.T) {
|
||||
setId: set.PublicId,
|
||||
version: set.Version,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-version",
|
||||
@@ -116,8 +116,8 @@ func TestRepository_AddSetMembers_Parameters(t *testing.T) {
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.AddSetMembers(context.Background(), tt.args.scopeId, tt.args.setId, tt.args.version, tt.args.hostIds, tt.args.opt...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
assert.Error(db.TestVerifyOplog(t, rw, tt.args.setId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
return
|
||||
@@ -239,7 +239,7 @@ func TestRepository_DeleteSetMembers_Parameters(t *testing.T) {
|
||||
args args
|
||||
want int
|
||||
wantErr bool
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "empty-scope-id",
|
||||
@@ -248,7 +248,7 @@ func TestRepository_DeleteSetMembers_Parameters(t *testing.T) {
|
||||
version: set.Version,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "empty-set-id",
|
||||
@@ -257,7 +257,7 @@ func TestRepository_DeleteSetMembers_Parameters(t *testing.T) {
|
||||
version: set.Version,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "zero-version",
|
||||
@@ -266,7 +266,7 @@ func TestRepository_DeleteSetMembers_Parameters(t *testing.T) {
|
||||
setId: set.PublicId,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "empty-host-ids",
|
||||
@@ -275,7 +275,7 @@ func TestRepository_DeleteSetMembers_Parameters(t *testing.T) {
|
||||
setId: set.PublicId,
|
||||
version: set.Version,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-version",
|
||||
@@ -308,8 +308,8 @@ func TestRepository_DeleteSetMembers_Parameters(t *testing.T) {
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.DeleteSetMembers(context.Background(), tt.args.scopeId, tt.args.setId, tt.args.version, tt.args.hostIds, tt.args.opt...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Zero(got)
|
||||
assert.Error(db.TestVerifyOplog(t, rw, tt.args.setId, db.WithOperation(oplog.OpType_OP_TYPE_DELETE), db.WithCreateNotBefore(10*time.Second)))
|
||||
return
|
||||
@@ -437,7 +437,7 @@ func TestRepository_SetSetMembers_Parameters(t *testing.T) {
|
||||
want []*Host
|
||||
wantCount int
|
||||
wantErr bool
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "empty-scope-id",
|
||||
@@ -446,7 +446,7 @@ func TestRepository_SetSetMembers_Parameters(t *testing.T) {
|
||||
version: set.Version,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "empty-set-id",
|
||||
@@ -455,7 +455,7 @@ func TestRepository_SetSetMembers_Parameters(t *testing.T) {
|
||||
version: set.Version,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "zero-version",
|
||||
@@ -464,7 +464,7 @@ func TestRepository_SetSetMembers_Parameters(t *testing.T) {
|
||||
setId: set.PublicId,
|
||||
hostIds: hostIds,
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-version",
|
||||
@@ -497,8 +497,8 @@ func TestRepository_SetSetMembers_Parameters(t *testing.T) {
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, gotCount, err := repo.SetSetMembers(context.Background(), tt.args.scopeId, tt.args.setId, tt.args.version, tt.args.hostIds, tt.args.opt...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
assert.Equal(tt.wantCount, gotCount)
|
||||
assert.Error(db.TestVerifyOplog(t, rw, tt.args.setId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
|
||||
@@ -33,23 +33,23 @@ func TestRepository_CreateSet(t *testing.T) {
|
||||
in *HostSet
|
||||
opts []Option
|
||||
want *HostSet
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "nil-HostSet",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-embedded-HostSet",
|
||||
in: &HostSet{},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-no-catalog-id",
|
||||
in: &HostSet{
|
||||
HostSet: &store.HostSet{},
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-public-id-set",
|
||||
@@ -59,7 +59,7 @@ func TestRepository_CreateSet(t *testing.T) {
|
||||
PublicId: "abcd_OOOOOOOOOO",
|
||||
},
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "valid-no-options",
|
||||
@@ -114,8 +114,8 @@ func TestRepository_CreateSet(t *testing.T) {
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.CreateSet(context.Background(), prj.GetPublicId(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
@@ -264,7 +264,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
masks []string
|
||||
want *HostSet
|
||||
wantCount int
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "nil-host-set",
|
||||
@@ -273,7 +273,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
},
|
||||
chgFn: makeNil(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-embedded-host-set",
|
||||
@@ -282,7 +282,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
},
|
||||
chgFn: makeEmbeddedNil(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "no-public-id",
|
||||
@@ -291,7 +291,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
},
|
||||
chgFn: deletePublicId(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "updating-non-existent-host-set",
|
||||
@@ -302,7 +302,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
},
|
||||
chgFn: combine(nonExistentPublicId(), changeName("test-update-name-repo")),
|
||||
masks: []string{"Name"},
|
||||
wantIsErr: errors.ErrRecordNotFound,
|
||||
wantIsErr: errors.RecordNotFound,
|
||||
},
|
||||
{
|
||||
name: "empty-field-mask",
|
||||
@@ -312,7 +312,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
},
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
wantIsErr: errors.ErrEmptyFieldMask,
|
||||
wantIsErr: errors.EmptyFieldMask,
|
||||
},
|
||||
{
|
||||
name: "read-only-fields-in-field-mask",
|
||||
@@ -323,7 +323,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
masks: []string{"PublicId", "CreateTime", "UpdateTime", "CatalogId"},
|
||||
wantIsErr: errors.ErrInvalidFieldMask,
|
||||
wantIsErr: errors.InvalidFieldMask,
|
||||
},
|
||||
{
|
||||
name: "unknown-field-in-field-mask",
|
||||
@@ -334,7 +334,7 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
masks: []string{"Bilbo"},
|
||||
wantIsErr: errors.ErrInvalidFieldMask,
|
||||
wantIsErr: errors.InvalidFieldMask,
|
||||
},
|
||||
{
|
||||
name: "change-name",
|
||||
@@ -480,8 +480,8 @@ func TestRepository_UpdateSet(t *testing.T) {
|
||||
orig = tt.chgFn(orig)
|
||||
}
|
||||
got, gotHosts, gotCount, err := repo.UpdateSet(context.Background(), prj.GetPublicId(), orig, 1, tt.masks)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Equal(tt.wantCount, gotCount, "row count")
|
||||
assert.Nil(got)
|
||||
return
|
||||
@@ -724,11 +724,11 @@ func TestRepository_LookupSet(t *testing.T) {
|
||||
in string
|
||||
want *HostSet
|
||||
wantHosts []*Host
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "with-no-public-id",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "with-non-existing-host-set-id",
|
||||
@@ -756,8 +756,8 @@ func TestRepository_LookupSet(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, gotHosts, err := repo.LookupSet(context.Background(), tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
@@ -864,11 +864,11 @@ func TestRepository_ListSets(t *testing.T) {
|
||||
in string
|
||||
opts []Option
|
||||
want []*HostSet
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "with-no-catalog-id",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "Catalog-with-no-host-sets",
|
||||
@@ -890,8 +890,8 @@ func TestRepository_ListSets(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListSets(context.Background(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
@@ -992,11 +992,11 @@ func TestRepository_DeleteSet(t *testing.T) {
|
||||
name string
|
||||
in string
|
||||
want int
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "With no public id",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "With non existing host set id",
|
||||
@@ -1018,8 +1018,8 @@ func TestRepository_DeleteSet(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.DeleteSet(context.Background(), prj.PublicId, tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Zero(got)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,23 +33,23 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
in *Host
|
||||
opts []Option
|
||||
want *Host
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "nil-Host",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-embedded-Host",
|
||||
in: &Host{},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-no-catalog-id",
|
||||
in: &Host{
|
||||
Host: &store.Host{},
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-public-id-set",
|
||||
@@ -60,7 +60,7 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "valid-no-options",
|
||||
@@ -118,7 +118,7 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
CatalogId: catalog.PublicId,
|
||||
},
|
||||
},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "invalid-address-to-short",
|
||||
@@ -128,7 +128,7 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
Address: "12",
|
||||
},
|
||||
},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "invalid-empty-address",
|
||||
@@ -138,7 +138,18 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
Address: " ",
|
||||
},
|
||||
},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "invalid_public_id",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
opts: []Option{WithPublicId("bad_prefix")},
|
||||
wantIsErr: errors.InvalidPublicId,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -150,8 +161,8 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
require.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.CreateHost(context.Background(), prj.GetPublicId(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
@@ -309,7 +320,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
masks []string
|
||||
want *Host
|
||||
wantCount int
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "nil-host",
|
||||
@@ -320,7 +331,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: makeNil(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-embedded-host",
|
||||
@@ -331,7 +342,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: makeEmbeddedNil(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "no-public-id",
|
||||
@@ -342,7 +353,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: deletePublicId(),
|
||||
masks: []string{"Name", "Description"},
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "updating-non-existent-host",
|
||||
@@ -354,7 +365,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: combine(nonExistentPublicId(), changeName("test-update-name-repo")),
|
||||
masks: []string{"Name"},
|
||||
wantIsErr: errors.ErrRecordNotFound,
|
||||
wantIsErr: errors.RecordNotFound,
|
||||
},
|
||||
{
|
||||
name: "empty-field-mask",
|
||||
@@ -365,7 +376,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
wantIsErr: errors.ErrEmptyFieldMask,
|
||||
wantIsErr: errors.EmptyFieldMask,
|
||||
},
|
||||
{
|
||||
name: "read-only-fields-in-field-mask",
|
||||
@@ -377,7 +388,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
masks: []string{"PublicId", "CreateTime", "UpdateTime", "CatalogId"},
|
||||
wantIsErr: errors.ErrInvalidFieldMask,
|
||||
wantIsErr: errors.InvalidFieldMask,
|
||||
},
|
||||
{
|
||||
name: "unknown-field-in-field-mask",
|
||||
@@ -389,7 +400,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: changeName("test-update-name-repo"),
|
||||
masks: []string{"Bilbo"},
|
||||
wantIsErr: errors.ErrInvalidFieldMask,
|
||||
wantIsErr: errors.InvalidFieldMask,
|
||||
},
|
||||
{
|
||||
name: "change-name",
|
||||
@@ -550,7 +561,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: changeAddress("11"),
|
||||
masks: []string{"Address"},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "delete-address",
|
||||
@@ -561,7 +572,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: changeAddress(""),
|
||||
masks: []string{"Address"},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "change-empty-address",
|
||||
@@ -572,7 +583,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
chgFn: changeAddress(" "),
|
||||
masks: []string{"Address"},
|
||||
wantIsErr: ErrInvalidAddress,
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -596,8 +607,8 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
orig = tt.chgFn(orig)
|
||||
}
|
||||
got, gotCount, err := repo.UpdateHost(context.Background(), prj.GetPublicId(), orig, 1, tt.masks)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Equal(tt.wantCount, gotCount, "row count")
|
||||
assert.Nil(got)
|
||||
return
|
||||
@@ -748,11 +759,11 @@ func TestRepository_LookupHost(t *testing.T) {
|
||||
name string
|
||||
in string
|
||||
want *Host
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "with-no-public-id",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "with-non-existing-host-id",
|
||||
@@ -773,8 +784,8 @@ func TestRepository_LookupHost(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.LookupHost(context.Background(), tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
@@ -802,11 +813,11 @@ func TestRepository_ListHosts(t *testing.T) {
|
||||
in string
|
||||
opts []Option
|
||||
want []*Host
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "with-no-catalog-id",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "Catalog-with-no-hosts",
|
||||
@@ -828,8 +839,8 @@ func TestRepository_ListHosts(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListHosts(context.Background(), tt.in, tt.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
@@ -930,11 +941,11 @@ func TestRepository_DeleteHost(t *testing.T) {
|
||||
name string
|
||||
in string
|
||||
want int
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "With no public id",
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "With non existing host id",
|
||||
@@ -956,8 +967,8 @@ func TestRepository_DeleteHost(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.DeleteHost(context.Background(), catalog.ScopeId, tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Zero(got)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestRepository_New(t *testing.T) {
|
||||
name string
|
||||
args args
|
||||
want *Repository
|
||||
wantIsErr error
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "valid",
|
||||
@@ -67,7 +67,7 @@ func TestRepository_New(t *testing.T) {
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-writer",
|
||||
@@ -77,7 +77,7 @@ func TestRepository_New(t *testing.T) {
|
||||
kms: kmsCache,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "nil-kms",
|
||||
@@ -87,7 +87,7 @@ func TestRepository_New(t *testing.T) {
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "all-nils",
|
||||
@@ -97,7 +97,7 @@ func TestRepository_New(t *testing.T) {
|
||||
kms: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantIsErr: errors.ErrInvalidParameter,
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@@ -105,8 +105,8 @@ func TestRepository_New(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
got, err := NewRepository(tt.args.r, tt.args.w, tt.args.kms, tt.args.opts...)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
if tt.wantIsErr != 0 {
|
||||
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user