Update primary error functions to take a context, deprecate old functions (#1358)
* add new error funcs which take a ctx and deprecate existing funcs * use context where available when writing error events * remove circular internal/errors pkg dep * suppress some chatty errors using errors.WithoutEvent() * convert auth oidc funcs to take context where needed to write events Co-authored-by: Jim <jlambert@hashicorp.com>
This commit is contained in:
1 parent
02ff2db66e
commit
6b78108ecf
227 files changed
+3945
-3736
No files matched your search
@@ -23,7 +23,7 @@ type Host struct {
|
||||
// ignored.
|
||||
func NewHost(catalogId string, opt ...Option) (*Host, error) {
|
||||
if catalogId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, "static.NewHost", "no catalog id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, "static.NewHost", "no catalog id")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -19,7 +19,7 @@ type HostCatalog struct {
|
||||
// ignored.
|
||||
func NewHostCatalog(scopeId string, opt ...Option) (*HostCatalog, error) {
|
||||
if scopeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, "static.NewHostCatalog", "no scope id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, "static.NewHostCatalog", "no scope id")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -18,7 +18,7 @@ type HostSet struct {
|
||||
// ignored.
|
||||
func NewHostSet(catalogId string, opt ...Option) (*HostSet, error) {
|
||||
if catalogId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, "static.NewHostSet", "no catalog id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, "static.NewHostSet", "no catalog id")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -16,10 +16,10 @@ type HostSetMember struct {
|
||||
func NewHostSetMember(hostSetId, hostId string, opt ...Option) (*HostSetMember, error) {
|
||||
const op = "static.NewHostSetMember"
|
||||
if hostSetId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no host set id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no host set id")
|
||||
}
|
||||
if hostId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no host id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no host id")
|
||||
}
|
||||
member := &HostSetMember{
|
||||
HostSetMember: &store.HostSetMember{
|
||||
|
||||
@@ -25,7 +25,7 @@ const (
|
||||
func newHostCatalogId() (string, error) {
|
||||
id, err := db.NewPublicId(HostCatalogPrefix)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "static.newHostCatalogId")
|
||||
return "", errors.WrapDeprecated(err, "static.newHostCatalogId")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func newHostCatalogId() (string, error) {
|
||||
func newHostId() (string, error) {
|
||||
id, err := db.NewPublicId(HostPrefix)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "static.newHostId")
|
||||
return "", errors.WrapDeprecated(err, "static.newHostId")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func newHostId() (string, error) {
|
||||
func newHostSetId() (string, error) {
|
||||
id, err := db.NewPublicId(HostSetPrefix)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "static.newHostSetId")
|
||||
return "", errors.WrapDeprecated(err, "static.newHostSetId")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -25,11 +25,11 @@ func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repo
|
||||
const op = "static.NewRepository"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "db.Reader")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "db.Reader")
|
||||
case w == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "db.Writer")
|
||||
case kms == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "kms")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "kms")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -24,23 +24,23 @@ import (
|
||||
func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, opt ...Option) (*Host, error) {
|
||||
const op = "static.(Repository).CreateHost"
|
||||
if h == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil Host")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil Host")
|
||||
}
|
||||
if h.Host == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded Host")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded Host")
|
||||
}
|
||||
if h.CatalogId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
if h.PublicId != "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
h.Address = strings.TrimSpace(h.Address)
|
||||
if len(h.Address) < MinHostAddressLength || len(h.Address) > MaxHostAddressLength {
|
||||
return nil, errors.New(errors.InvalidAddress, op, "invalid address")
|
||||
return nil, errors.New(ctx, errors.InvalidAddress, op, "invalid address")
|
||||
}
|
||||
h = h.clone()
|
||||
|
||||
@@ -48,7 +48,7 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
|
||||
if opts.withPublicId != "" {
|
||||
if !strings.HasPrefix(opts.withPublicId, HostPrefix+"_") {
|
||||
return nil, errors.New(
|
||||
return nil, errors.New(ctx,
|
||||
errors.InvalidPublicId,
|
||||
op,
|
||||
fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, HostPrefix),
|
||||
@@ -58,14 +58,14 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
} else {
|
||||
id, err := newHostId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
h.PublicId = id
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var newHost *Host
|
||||
@@ -74,7 +74,7 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
newHost = h.clone()
|
||||
err := w.Create(ctx, newHost, db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -82,17 +82,17 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", h.CatalogId, h.Name)))
|
||||
return nil, errors.Wrap(ctx, 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, errors.New(
|
||||
return nil, errors.New(ctx,
|
||||
errors.InvalidAddress,
|
||||
op,
|
||||
fmt.Sprintf("in catalog: %s: %q", h.CatalogId, h.Address),
|
||||
errors.WithWrap(err),
|
||||
)
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", h.CatalogId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", h.CatalogId)))
|
||||
}
|
||||
return newHost, nil
|
||||
}
|
||||
@@ -112,19 +112,19 @@ func (r *Repository) CreateHost(ctx context.Context, scopeId string, h *Host, op
|
||||
func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, version uint32, fieldMaskPaths []string, opt ...Option) (*Host, int, error) {
|
||||
const op = "static.(Repository).UpdateHost"
|
||||
if h == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil Host")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil Host")
|
||||
}
|
||||
if h.Host == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil embedded Host")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil embedded Host")
|
||||
}
|
||||
if h.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
|
||||
for _, f := range fieldMaskPaths {
|
||||
@@ -134,10 +134,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, errors.New(errors.InvalidAddress, op, "invalid address")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidAddress, op, "invalid address")
|
||||
}
|
||||
default:
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
|
||||
}
|
||||
}
|
||||
var dbMask, nullFields []string
|
||||
@@ -151,12 +151,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, errors.New(errors.EmptyFieldMask, op, "empty field mask")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "empty field mask")
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsUpdated int
|
||||
@@ -169,10 +169,10 @@ 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 {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rowsUpdated > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -180,17 +180,17 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s: name %s already exists", h.PublicId, h.Name)))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, 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, errors.New(
|
||||
return nil, db.NoRowsAffected, errors.New(ctx,
|
||||
errors.InvalidAddress,
|
||||
op,
|
||||
fmt.Sprintf("in %s: %q", h.PublicId, h.Address),
|
||||
errors.WithWrap(err),
|
||||
)
|
||||
}
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", h.PublicId)))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", h.PublicId)))
|
||||
}
|
||||
|
||||
return returnedHost, rowsUpdated, nil
|
||||
@@ -201,7 +201,7 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
func (r *Repository) LookupHost(ctx context.Context, publicId string, opt ...Option) (*Host, error) {
|
||||
const op = "static.(Repository).LookupHost"
|
||||
if publicId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
h := allocHost()
|
||||
h.PublicId = publicId
|
||||
@@ -209,7 +209,7 @@ func (r *Repository) LookupHost(ctx context.Context, publicId string, opt ...Opt
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for %s", publicId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", publicId)))
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
@@ -219,7 +219,7 @@ func (r *Repository) LookupHost(ctx context.Context, publicId string, opt ...Opt
|
||||
func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Option) ([]*Host, error) {
|
||||
const op = "static.(Repository).ListHosts"
|
||||
if catalogId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -230,7 +230,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, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return hosts, nil
|
||||
}
|
||||
@@ -241,14 +241,14 @@ func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Opt
|
||||
func (r *Repository) DeleteHost(ctx context.Context, scopeId string, publicId string, opt ...Option) (int, error) {
|
||||
const op = "static.(Repository).DeleteHost"
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return db.NoRowsAffected, errors.New(ctx, 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, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
@@ -258,17 +258,17 @@ 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 {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rowsDeleted > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been deleted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been deleted")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", publicId)))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", publicId)))
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
|
||||
@@ -23,16 +23,16 @@ import (
|
||||
func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...Option) (*HostCatalog, error) {
|
||||
const op = "static.(Repository).CreateCatalog"
|
||||
if c == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil HostCatalog")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil HostCatalog")
|
||||
}
|
||||
if c.HostCatalog == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded HostCatalog")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostCatalog")
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if c.PublicId != "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
c = c.clone()
|
||||
|
||||
@@ -40,7 +40,7 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
|
||||
if opts.withPublicId != "" {
|
||||
if !strings.HasPrefix(opts.withPublicId, HostCatalogPrefix+"_") {
|
||||
return nil, errors.New(
|
||||
return nil, errors.New(ctx,
|
||||
errors.InvalidPublicId,
|
||||
op,
|
||||
fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, HostCatalogPrefix),
|
||||
@@ -50,14 +50,14 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
} else {
|
||||
id, err := newHostCatalogId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
c.PublicId = id
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_CREATE)
|
||||
@@ -75,7 +75,7 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -83,9 +83,9 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in scope: %s: name %s already exists", c.ScopeId, c.Name)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s: name %s already exists", c.ScopeId, c.Name)))
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in scope: %s", c.ScopeId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s", c.ScopeId)))
|
||||
}
|
||||
return newHostCatalog, nil
|
||||
}
|
||||
@@ -104,19 +104,19 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...O
|
||||
func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version uint32, fieldMask []string, opt ...Option) (*HostCatalog, int, error) {
|
||||
const op = "static.(Repository).UpdateCatalog"
|
||||
if c == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil HostCatalog")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil HostCatalog")
|
||||
}
|
||||
if c.HostCatalog == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil embedded HostCatalog")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostCatalog")
|
||||
}
|
||||
if c.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if len(fieldMask) == 0 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "empty field mask")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "empty field mask")
|
||||
}
|
||||
|
||||
var dbMask, nullFields []string
|
||||
@@ -132,13 +132,13 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
dbMask = append(dbMask, "description")
|
||||
|
||||
default:
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
|
||||
}
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
c = c.clone()
|
||||
@@ -163,10 +163,10 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
db.WithVersion(&version),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rowsUpdated > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -174,9 +174,9 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
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, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s: name %s already exists", c.PublicId, c.Name)))
|
||||
}
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", c.PublicId)))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", c.PublicId)))
|
||||
}
|
||||
|
||||
return returnedCatalog, rowsUpdated, nil
|
||||
@@ -187,7 +187,7 @@ func (r *Repository) UpdateCatalog(ctx context.Context, c *HostCatalog, version
|
||||
func (r *Repository) LookupCatalog(ctx context.Context, id string, opt ...Option) (*HostCatalog, error) {
|
||||
const op = "static.(Repository).LookupCatalog"
|
||||
if id == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
c := allocCatalog()
|
||||
c.PublicId = id
|
||||
@@ -195,7 +195,7 @@ func (r *Repository) LookupCatalog(ctx context.Context, id string, opt ...Option
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
@@ -204,7 +204,7 @@ func (r *Repository) LookupCatalog(ctx context.Context, id string, opt ...Option
|
||||
func (r *Repository) ListCatalogs(ctx context.Context, scopeIds []string, opt ...Option) ([]*HostCatalog, error) {
|
||||
const op = "static.(Repository).ListCatalogs"
|
||||
if len(scopeIds) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -215,7 +215,7 @@ func (r *Repository) ListCatalogs(ctx context.Context, scopeIds []string, opt ..
|
||||
var hostCatalogs []*HostCatalog
|
||||
err := r.reader.SearchWhere(ctx, &hostCatalogs, "scope_id in (?)", []interface{}{scopeIds}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return hostCatalogs, nil
|
||||
}
|
||||
@@ -225,7 +225,7 @@ func (r *Repository) ListCatalogs(ctx context.Context, scopeIds []string, opt ..
|
||||
func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option) (int, error) {
|
||||
const op = "static.(Repository).DeleteCatalog"
|
||||
if id == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
|
||||
c := allocCatalog()
|
||||
@@ -234,14 +234,14 @@ func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option
|
||||
if errors.IsNotFoundError(err) {
|
||||
return db.NoRowsAffected, nil
|
||||
}
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for %s", id)))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", id)))
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_DELETE)
|
||||
@@ -261,17 +261,17 @@ func (r *Repository) DeleteCatalog(ctx context.Context, id string, opt ...Option
|
||||
db.WithOplog(oplogWrapper, metadata),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rowsDeleted > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been deleted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been deleted")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", c.PublicId)))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", c.PublicId)))
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
|
||||
@@ -22,19 +22,19 @@ import (
|
||||
func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet, opt ...Option) (*HostSet, error) {
|
||||
const op = "static.(Repository).CreateSet"
|
||||
if s == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil HostSet")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil HostSet")
|
||||
}
|
||||
if s.HostSet == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded HostSet")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostSet")
|
||||
}
|
||||
if s.CatalogId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
if s.PublicId != "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
s = s.clone()
|
||||
|
||||
@@ -42,7 +42,7 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
|
||||
if opts.withPublicId != "" {
|
||||
if !strings.HasPrefix(opts.withPublicId, HostSetPrefix+"_") {
|
||||
return nil, errors.New(
|
||||
return nil, errors.New(ctx,
|
||||
errors.InvalidPublicId,
|
||||
op,
|
||||
fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, HostSetPrefix),
|
||||
@@ -52,14 +52,14 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
} else {
|
||||
id, err := newHostSetId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
s.PublicId = id
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var newHostSet *HostSet
|
||||
@@ -68,7 +68,7 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
newHostSet = s.clone()
|
||||
err := w.Create(ctx, newHostSet, db.WithOplog(oplogWrapper, s.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -76,9 +76,9 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", s.CatalogId, s.Name)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", s.CatalogId, s.Name)))
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", s.CatalogId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", s.CatalogId)))
|
||||
}
|
||||
return newHostSet, nil
|
||||
}
|
||||
@@ -100,19 +100,19 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet, version uint32, fieldMaskPaths []string, opt ...Option) (*HostSet, []*Host, int, error) {
|
||||
const op = "static.(Repository).UpdateSet"
|
||||
if s == nil {
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil HostSet")
|
||||
return nil, nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil HostSet")
|
||||
}
|
||||
if s.HostSet == nil {
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "nil embedded HostSet")
|
||||
return nil, nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostSet")
|
||||
}
|
||||
if s.PublicId == "" {
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
return nil, nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
|
||||
for _, f := range fieldMaskPaths {
|
||||
@@ -120,7 +120,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, errors.New(errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
|
||||
return nil, nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
|
||||
}
|
||||
}
|
||||
var dbMask, nullFields []string
|
||||
@@ -133,7 +133,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, errors.New(errors.EmptyFieldMask, op, "empty field mask")
|
||||
return nil, nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "empty field mask")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -145,7 +145,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, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsUpdated int
|
||||
@@ -159,14 +159,14 @@ 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 {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rowsUpdated > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
}
|
||||
hosts, err = getHosts(ctx, reader, s.PublicId, limit)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -174,9 +174,9 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
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, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s: name %s already exists", s.PublicId, s.Name)))
|
||||
}
|
||||
return nil, nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", s.PublicId)))
|
||||
return nil, nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", s.PublicId)))
|
||||
}
|
||||
|
||||
return returnedHostSet, hosts, rowsUpdated, nil
|
||||
@@ -189,7 +189,7 @@ func (r *Repository) UpdateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Option) (*HostSet, []*Host, error) {
|
||||
const op = "static.(Repository).LookupSet"
|
||||
if publicId == "" {
|
||||
return nil, nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -209,19 +209,19 @@ func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Opti
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
hosts, err = getHosts(ctx, reader, s.PublicId, limit)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in %s", s.PublicId)))
|
||||
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", s.PublicId)))
|
||||
}
|
||||
|
||||
return s, hosts, nil
|
||||
@@ -232,7 +232,7 @@ func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...Opti
|
||||
func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...Option) ([]*HostSet, error) {
|
||||
const op = "static.(Repository).ListSets"
|
||||
if catalogId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no catalog id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -243,7 +243,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, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return sets, nil
|
||||
}
|
||||
@@ -254,17 +254,17 @@ func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...Opti
|
||||
func (r *Repository) DeleteSet(ctx context.Context, scopeId string, publicId string, opt ...Option) (int, error) {
|
||||
const op = "static.(Repository).DeleteSet"
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return db.NoRowsAffected, errors.New(ctx, 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, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
@@ -274,17 +274,17 @@ 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 {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rowsDeleted > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been deleted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been deleted")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", s.PublicId)))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", s.PublicId)))
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
|
||||
@@ -20,27 +20,27 @@ import (
|
||||
func (r *Repository) AddSetMembers(ctx context.Context, scopeId string, setId string, version uint32, hostIds []string, opt ...Option) ([]*Host, error) {
|
||||
const op = "static.(Repository).AddSetMembers"
|
||||
if scopeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if setId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no set id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no set id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no version")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if len(hostIds) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no host ids")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no host ids")
|
||||
}
|
||||
|
||||
// Create in-memory host set members
|
||||
members, err := r.newMembers(setId, hostIds)
|
||||
members, err := r.newMembers(ctx, setId, hostIds)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
wrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var hosts []*Host
|
||||
@@ -51,33 +51,33 @@ func (r *Repository) AddSetMembers(ctx context.Context, scopeId string, setId st
|
||||
// Create host set members
|
||||
msgs, err := createMembers(ctx, w, members)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
// Update host set version
|
||||
if err := updateVersion(ctx, w, wrapper, metadata, msgs, set, version); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
hosts, err = getHosts(ctx, reader, setId, unlimited)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
func (r *Repository) newMembers(setId string, hostIds []string) ([]interface{}, error) {
|
||||
func (r *Repository) newMembers(ctx context.Context, setId string, hostIds []string) ([]interface{}, error) {
|
||||
var members []interface{}
|
||||
for _, id := range hostIds {
|
||||
var m *HostSetMember
|
||||
m, err := NewHostSetMember(setId, id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "static.newMembers")
|
||||
return nil, errors.Wrap(ctx, err, "static.newMembers")
|
||||
}
|
||||
members = append(members, m)
|
||||
}
|
||||
@@ -87,7 +87,7 @@ 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, errors.Wrap(err, "static.createMembers")
|
||||
return nil, errors.Wrap(ctx, err, "static.createMembers")
|
||||
}
|
||||
return msgs, nil
|
||||
}
|
||||
@@ -98,19 +98,19 @@ func updateVersion(ctx context.Context, w db.Writer, wrapper wrapping.Wrapper, m
|
||||
rowsUpdated, err := w.Update(ctx, set, []string{"Version"}, nil, db.NewOplogMsg(setMsg), db.WithVersion(&version))
|
||||
switch {
|
||||
case err != nil:
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
case rowsUpdated > 1:
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
}
|
||||
msgs = append(msgs, setMsg)
|
||||
|
||||
// Write oplog
|
||||
ticket, err := w.GetTicket(set)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get ticket"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
|
||||
}
|
||||
if err := w.WriteOplogEntryWith(ctx, wrapper, ticket, metadata, msgs); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to write oplog"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -147,7 +147,7 @@ func getHosts(ctx context.Context, reader db.Reader, setId string, limit int) ([
|
||||
params,
|
||||
db.WithLimit(limit),
|
||||
); err != nil {
|
||||
return nil, errors.Wrap(err, "static.getHosts")
|
||||
return nil, errors.Wrap(ctx, err, "static.getHosts")
|
||||
}
|
||||
if len(hosts) == 0 {
|
||||
return nil, nil
|
||||
@@ -161,27 +161,27 @@ func getHosts(ctx context.Context, reader db.Reader, setId string, limit int) ([
|
||||
func (r *Repository) DeleteSetMembers(ctx context.Context, scopeId string, setId string, version uint32, hostIds []string, opt ...Option) (int, error) {
|
||||
const op = "static.(Repository).DeleteSetMembers"
|
||||
if scopeId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if setId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no set id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no set id")
|
||||
}
|
||||
if version == 0 {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
if len(hostIds) == 0 {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no host ids")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no host ids")
|
||||
}
|
||||
|
||||
// Create in-memory host set members
|
||||
members, err := r.newMembers(setId, hostIds)
|
||||
members, err := r.newMembers(ctx, setId, hostIds)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
wrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, 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 {
|
||||
@@ -191,19 +191,19 @@ func (r *Repository) DeleteSetMembers(ctx context.Context, scopeId string, setId
|
||||
// Delete host set members
|
||||
msgs, err := deleteMembers(ctx, w, members)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// Update host set version
|
||||
err = updateVersion(ctx, w, wrapper, metadata, msgs, set, version)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return len(hostIds), nil
|
||||
}
|
||||
@@ -213,10 +213,10 @@ func deleteMembers(ctx context.Context, w db.Writer, members []interface{}) ([]*
|
||||
var msgs []*oplog.Message
|
||||
rowsDeleted, err := w.DeleteItems(ctx, members, db.NewOplogMsgs(&msgs))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if 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 nil, errors.E(ctx, errors.WithMsg(fmt.Sprintf("set members deleted %d did not match request for %d", rowsDeleted, len(members))))
|
||||
}
|
||||
return msgs, nil
|
||||
}
|
||||
@@ -229,13 +229,13 @@ func deleteMembers(ctx context.Context, w db.Writer, members []interface{}) ([]*
|
||||
func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId string, version uint32, hostIds []string, opt ...Option) ([]*Host, int, error) {
|
||||
const op = "static.(Repository).SetSetMembers"
|
||||
if scopeId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if setId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no set id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no set id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no version")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no version")
|
||||
}
|
||||
|
||||
// TODO(mgaffney) 08/2020: Oplog does not currently support bulk
|
||||
@@ -253,13 +253,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, errors.Wrap(err, op)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
var deletions, additions []interface{}
|
||||
for _, c := range changes {
|
||||
m, err := NewHostSetMember(setId, c.HostId)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
switch c.Action {
|
||||
case "delete":
|
||||
@@ -273,7 +273,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, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, 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 {
|
||||
@@ -285,7 +285,7 @@ func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId st
|
||||
if len(deletions) > 0 {
|
||||
deletedMsgs, err := deleteMembers(ctx, w, deletions)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
msgs = append(msgs, deletedMsgs...)
|
||||
metadata["op-type"] = append(metadata["op-type"], oplog.OpType_OP_TYPE_DELETE.String())
|
||||
@@ -295,7 +295,7 @@ func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId st
|
||||
if len(additions) > 0 {
|
||||
createdMsgs, err := createMembers(ctx, w, additions)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
msgs = append(msgs, createdMsgs...)
|
||||
metadata["op-type"] = append(metadata["op-type"], oplog.OpType_OP_TYPE_CREATE.String())
|
||||
@@ -303,18 +303,18 @@ func (r *Repository) SetSetMembers(ctx context.Context, scopeId string, setId st
|
||||
|
||||
// Update host set version
|
||||
if err := updateVersion(ctx, w, wrapper, metadata, msgs, set, version); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
hosts, err = getHosts(ctx, reader, setId, unlimited)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
return hosts, len(changes), nil
|
||||
@@ -345,7 +345,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, errors.Wrap(err, op, errors.WithMsg("query failed"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("query failed"))
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -353,7 +353,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, errors.Wrap(err, op, errors.WithMsg("scan row failed"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("scan row failed"))
|
||||
}
|
||||
changes = append(changes, &chg)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user