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

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

262 lines
8.2 KiB
Go
Raw Normal View History

2020-08-13 14:00:15 -04:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-08-13 14:00:15 -04:00
package static
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/boundary/globals"
2020-08-13 14:00:15 -04:00
"github.com/hashicorp/boundary/internal/db"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-19 16:47:18 -04:00
"github.com/hashicorp/boundary/internal/kms"
2020-08-13 14:00:15 -04:00
"github.com/hashicorp/boundary/internal/oplog"
)
// CreateCatalog inserts c into the repository and returns a new
// HostCatalog containing the catalog's PublicId. c is not changed. c must
2022-07-20 16:24:46 -07:00
// contain a valid ProjectID. c must not contain a PublicId. The PublicId is
2021-04-27 08:26:08 -04:00
// generated and assigned by this method. opt is ignored.
2020-08-13 14:00:15 -04:00
//
// Both c.Name and c.Description are optional. If c.Name is set, it must be
2022-07-20 16:24:46 -07:00
// unique within c.ProjectId.
2020-08-13 14:00:15 -04:00
//
// Both c.CreateTime and c.UpdateTime are ignored.
func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, opt ...Option) (*HostCatalog, error) {
const op = "static.(Repository).CreateCatalog"
2020-08-13 14:00:15 -04:00
if c == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil HostCatalog")
2020-08-13 14:00:15 -04:00
}
if c.HostCatalog == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostCatalog")
2020-08-13 14:00:15 -04:00
}
2022-07-20 16:24:46 -07:00
if c.ProjectId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no project id")
2020-08-13 14:00:15 -04:00
}
if c.PublicId != "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
2020-08-13 14:00:15 -04:00
}
c = c.clone()
2020-09-24 03:31:26 -04:00
opts := getOpts(opt...)
if opts.withPublicId != "" {
if !strings.HasPrefix(opts.withPublicId, globals.StaticHostCatalogPrefix+"_") {
return nil, errors.New(ctx,
2020-12-09 10:11:38 -08:00
errors.InvalidPublicId,
op,
fmt.Sprintf("passed-in public ID %q has wrong prefix, should be %q", opts.withPublicId, globals.StaticHostCatalogPrefix),
2020-12-09 10:11:38 -08:00
)
2020-09-24 03:31:26 -04:00
}
c.PublicId = opts.withPublicId
} else {
id, err := newHostCatalogId(ctx)
2020-09-24 03:31:26 -04:00
if err != nil {
return nil, errors.Wrap(ctx, err, op)
2020-09-24 03:31:26 -04:00
}
c.PublicId = id
2020-08-13 14:00:15 -04:00
}
2022-07-20 16:24:46 -07:00
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ProjectId, kms.KeyPurposeOplog)
2020-08-19 16:47:18 -04:00
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
2020-08-19 16:47:18 -04:00
}
2020-08-13 14:00:15 -04:00
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_CREATE)
var newHostCatalog *HostCatalog
_, err = r.writer.DoTx(
ctx,
db.StdRetryCnt,
db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
newHostCatalog = c.clone()
err := w.Create(
2020-08-13 14:00:15 -04:00
ctx,
newHostCatalog,
2020-08-19 16:47:18 -04:00
db.WithOplog(oplogWrapper, metadata),
2020-08-13 14:00:15 -04:00
)
if err != nil {
return errors.Wrap(ctx, err, op)
}
return nil
2020-08-13 14:00:15 -04:00
},
)
if err != nil {
2020-11-05 16:54:03 -05:00
if errors.IsUniqueError(err) {
2022-07-20 16:24:46 -07:00
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in project: %s: name %s already exists", c.ProjectId, c.Name)))
2020-08-13 14:00:15 -04:00
}
2022-07-20 16:24:46 -07:00
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in project: %s", c.ProjectId)))
2020-08-13 14:00:15 -04:00
}
return newHostCatalog, nil
}
// UpdateCatalog updates the repository entry for c.PublicId with the
// values in c for the fields listed in fieldMask. It returns a new
// HostCatalog containing the updated values and a count of the number of
// records updated. c is not changed.
//
// c must contain a valid PublicId. Only c.Name and c.Description can be
// updated. If c.Name is set to a non-empty string, it must be unique
2022-07-20 16:24:46 -07:00
// within c.ProjectId.
2020-08-13 14:00:15 -04:00
//
// 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.(Repository).UpdateCatalog"
2020-08-13 14:00:15 -04:00
if c == nil {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil HostCatalog")
2020-08-13 14:00:15 -04:00
}
if c.HostCatalog == nil {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostCatalog")
2020-08-13 14:00:15 -04:00
}
if c.PublicId == "" {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
2020-08-13 14:00:15 -04:00
}
2022-07-20 16:24:46 -07:00
if c.ProjectId == "" {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no project id")
2020-08-19 16:47:18 -04:00
}
2020-08-13 14:00:15 -04:00
if len(fieldMask) == 0 {
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "empty field mask")
2020-08-13 14:00:15 -04:00
}
var dbMask, nullFields []string
for _, f := range fieldMask {
switch {
case strings.EqualFold("name", f) && c.Name == "":
nullFields = append(nullFields, "name")
case strings.EqualFold("name", f) && c.Name != "":
dbMask = append(dbMask, "name")
case strings.EqualFold("description", f) && c.Description == "":
nullFields = append(nullFields, "description")
case strings.EqualFold("description", f) && c.Description != "":
dbMask = append(dbMask, "description")
default:
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
2020-08-13 14:00:15 -04:00
}
}
2020-08-19 16:47:18 -04:00
2022-07-20 16:24:46 -07:00
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ProjectId, kms.KeyPurposeOplog)
2020-08-19 16:47:18 -04:00
if err != nil {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
2020-08-19 16:47:18 -04:00
}
2020-08-13 14:00:15 -04:00
c = c.clone()
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_UPDATE)
var rowsUpdated int
var returnedCatalog *HostCatalog
2020-08-19 16:47:18 -04:00
_, err = r.writer.DoTx(
2020-08-13 14:00:15 -04:00
ctx,
db.StdRetryCnt,
db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
returnedCatalog = c.clone()
var err error
rowsUpdated, err = w.Update(
ctx,
returnedCatalog,
dbMask,
nullFields,
2020-08-19 16:47:18 -04:00
db.WithOplog(oplogWrapper, metadata),
2020-08-13 14:00:15 -04:00
db.WithVersion(&version),
)
2020-12-23 11:00:00 -08:00
if err != nil {
return errors.Wrap(ctx, err, op)
2020-12-23 11:00:00 -08:00
}
if rowsUpdated > 1 {
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
2020-08-13 14:00:15 -04:00
}
2020-12-23 11:00:00 -08:00
return nil
2020-08-13 14:00:15 -04:00
},
)
if err != nil {
2020-11-05 16:54:03 -05:00
if errors.IsUniqueError(err) {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s: name %s already exists", c.PublicId, c.Name)))
2020-08-13 14:00:15 -04:00
}
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", c.PublicId)))
2020-08-13 14:00:15 -04:00
}
return returnedCatalog, rowsUpdated, nil
}
// 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.(Repository).LookupCatalog"
2020-08-13 14:00:15 -04:00
if id == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
2020-08-13 14:00:15 -04:00
}
c := allocCatalog()
c.PublicId = id
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
2020-12-09 10:11:38 -08:00
if errors.IsNotFoundError(err) {
2020-08-13 14:00:15 -04:00
return nil, nil
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
2020-08-13 14:00:15 -04:00
}
return c, nil
}
// 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.(Repository).DeleteCatalog"
2020-08-13 14:00:15 -04:00
if id == "" {
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
2020-08-13 14:00:15 -04:00
}
c := allocCatalog()
c.PublicId = id
2020-08-19 16:47:18 -04:00
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
2020-12-09 10:11:38 -08:00
if errors.IsNotFoundError(err) {
2020-08-19 16:47:18 -04:00
return db.NoRowsAffected, nil
}
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", id)))
2020-08-19 16:47:18 -04:00
}
2022-07-20 16:24:46 -07:00
if c.ProjectId == "" {
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no project id")
2020-08-19 16:47:18 -04:00
}
2022-07-20 16:24:46 -07:00
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ProjectId, kms.KeyPurposeOplog)
2020-08-19 16:47:18 -04:00
if err != nil {
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
2020-08-19 16:47:18 -04:00
}
2020-08-13 14:00:15 -04:00
metadata := newCatalogMetadata(c, oplog.OpType_OP_TYPE_DELETE)
var rowsDeleted int
var deleteCatalog *HostCatalog
2020-08-19 16:47:18 -04:00
_, err = r.writer.DoTx(
2020-08-13 14:00:15 -04:00
ctx,
db.StdRetryCnt,
db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
deleteCatalog = c.clone()
var err error
rowsDeleted, err = w.Delete(
ctx,
deleteCatalog,
2020-08-19 16:47:18 -04:00
db.WithOplog(oplogWrapper, metadata),
2020-08-13 14:00:15 -04:00
)
2020-12-23 11:00:00 -08:00
if err != nil {
return errors.Wrap(ctx, err, op)
2020-12-23 11:00:00 -08:00
}
if rowsDeleted > 1 {
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been deleted")
2020-08-13 14:00:15 -04:00
}
2020-12-23 11:00:00 -08:00
return nil
2020-08-13 14:00:15 -04:00
},
)
if err != nil {
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", c.PublicId)))
2020-08-13 14:00:15 -04:00
}
return rowsDeleted, nil
}