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

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

410 lines
14 KiB
Go
Raw Normal View History

2022-07-20 16:24:46 -07:00
// Copyright (c) HashiCorp, Inc.
2020-08-17 17:06:31 -04:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-08-17 17:06:31 -04:00
package static
import (
"context"
2023-10-30 14:40:30 -07:00
"database/sql"
2020-08-17 17:06:31 -04:00
"fmt"
2020-08-18 16:50:26 -04:00
"strings"
2023-10-30 14:40:30 -07:00
"time"
2020-08-17 17:06:31 -04:00
"github.com/hashicorp/boundary/globals"
2020-08-17 17:06:31 -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-17 17:06:31 -04:00
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/util"
"github.com/hashicorp/go-dbw"
2020-08-17 17:06:31 -04:00
)
// CreateHost inserts h into the repository and returns a new Host
// containing the host's PublicId. h is not changed. h must contain a valid
// CatalogId. h must not contain a PublicId. The PublicId is generated and
// assigned by this method. opt is ignored.
//
// h must contain a valid Address.
//
// Both h.Name and h.Description are optional. If h.Name is set, it must be
// unique within h.CatalogId.
2022-07-20 16:24:46 -07:00
func (r *Repository) CreateHost(ctx context.Context, projectId string, h *Host, opt ...Option) (*Host, error) {
const op = "static.(Repository).CreateHost"
2020-08-17 17:06:31 -04:00
if h == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil Host")
2020-08-17 17:06:31 -04:00
}
if h.Host == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded Host")
2020-08-17 17:06:31 -04:00
}
if h.CatalogId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
2020-08-17 17:06:31 -04:00
}
if h.PublicId != "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
2020-08-17 17:06:31 -04:00
}
2022-07-20 16:24:46 -07:00
if projectId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no project id")
2020-08-19 16:47:18 -04:00
}
var err error
h.Address, err = util.ParseAddress(ctx, h.Address)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidAddress), errors.WithMsg("invalid address"))
}
2020-08-17 17:06:31 -04:00
h = h.clone()
2020-09-24 03:31:26 -04:00
opts := getOpts(opt...)
if opts.withPublicId != "" {
if !strings.HasPrefix(opts.withPublicId, globals.StaticHostPrefix+"_") {
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.StaticHostPrefix),
2020-12-09 10:11:38 -08:00
)
2020-09-24 03:31:26 -04:00
}
h.PublicId = opts.withPublicId
} else {
id, err := newHostId(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
}
h.PublicId = id
2020-08-17 17:06:31 -04:00
}
2022-07-20 16:24:46 -07:00
oplogWrapper, err := r.kms.GetWrapper(ctx, 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-17 17:06:31 -04:00
var newHost *Host
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
newHost = h.clone()
err := w.Create(ctx, newHost, db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_CREATE)))
if err != nil {
return errors.Wrap(ctx, err, op)
}
return nil
2020-08-17 17:06:31 -04:00
},
)
if err != nil {
2020-11-05 16:54:03 -05:00
if errors.IsUniqueError(err) {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", h.CatalogId, h.Name)))
2020-08-17 17:06:31 -04:00
}
2020-11-05 16:54:03 -05:00
if errors.IsCheckConstraintError(err) || errors.IsNotNullError(err) {
return nil, errors.New(ctx,
2020-12-09 10:11:38 -08:00
errors.InvalidAddress,
op,
fmt.Sprintf("in catalog: %s: %q", h.CatalogId, h.Address),
errors.WithWrap(err),
)
2020-08-17 17:06:31 -04:00
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", h.CatalogId)))
2020-08-17 17:06:31 -04:00
}
return newHost, nil
}
2020-08-18 16:50:26 -04:00
// UpdateHost updates the repository entry for h.PublicId with the values
// in h for the fields listed in fieldMaskPaths. It returns a new Host
// containing the updated values and a count of the number of records
// updated. h is not changed.
//
// h must contain a valid PublicId. Only h.Name, h.Description, and
// h.Address can be updated. If h.Name is set to a non-empty string, it
// must be unique within h.CatalogId. If h.Address is set, it must contain
// a valid address.
//
// 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.
2022-07-20 16:24:46 -07:00
func (r *Repository) UpdateHost(ctx context.Context, projectId string, h *Host, version uint32, fieldMaskPaths []string, opt ...Option) (*Host, int, error) {
const op = "static.(Repository).UpdateHost"
2020-08-18 16:50:26 -04:00
if h == nil {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil Host")
2020-08-18 16:50:26 -04:00
}
if h.Host == nil {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "nil embedded Host")
2020-08-18 16:50:26 -04:00
}
if h.PublicId == "" {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
2020-08-18 16:50:26 -04:00
}
if version == 0 {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no version")
2020-08-18 16:50:26 -04:00
}
2022-07-20 16:24:46 -07:00
if projectId == "" {
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no project id")
2020-08-19 16:47:18 -04:00
}
2020-08-18 16:50:26 -04:00
for _, f := range fieldMaskPaths {
switch {
case strings.EqualFold("Name", f):
case strings.EqualFold("Description", f):
case strings.EqualFold("Address", f):
var err error
h.Address, err = util.ParseAddress(ctx, h.Address)
if err != nil {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidAddress), errors.WithMsg("invalid address"))
}
2020-08-18 16:50:26 -04:00
default:
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
2020-08-18 16:50:26 -04:00
}
}
var dbMask, nullFields []string
dbMask, nullFields = dbw.BuildUpdatePaths(
2022-10-25 18:29:38 -05:00
map[string]any{
2020-08-18 16:50:26 -04:00
"Name": h.Name,
"Description": h.Description,
"Address": h.Address,
},
fieldMaskPaths,
nil,
2020-08-18 16:50:26 -04:00
)
if len(dbMask) == 0 && len(nullFields) == 0 {
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "empty field mask")
2020-08-18 16:50:26 -04:00
}
2022-07-20 16:24:46 -07:00
oplogWrapper, err := r.kms.GetWrapper(ctx, 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-18 16:50:26 -04:00
var rowsUpdated int
var returnedHost *Host
2020-08-19 16:47:18 -04:00
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
func(r db.Reader, w db.Writer) error {
2020-08-18 16:50:26 -04:00
returnedHost = h.clone()
var err error
rowsUpdated, err = w.Update(ctx, returnedHost, dbMask, nullFields,
2020-08-19 16:47:18 -04:00
db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_UPDATE)),
2020-08-18 16:50:26 -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-18 16:50:26 -04:00
}
ha := &hostAgg{
PublicId: h.PublicId,
}
if err := r.LookupByPublicId(ctx, ha); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("failed to lookup host after update"))
}
returnedHost.SetIds = ha.getSetIds()
2020-12-23 11:00:00 -08:00
return nil
2020-08-18 16:50:26 -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", h.PublicId, h.Name)))
2020-08-18 16:50:26 -04:00
}
2020-11-05 16:54:03 -05:00
if errors.IsCheckConstraintError(err) || errors.IsNotNullError(err) {
return nil, db.NoRowsAffected, errors.New(ctx,
2020-12-09 10:11:38 -08:00
errors.InvalidAddress,
op,
fmt.Sprintf("in %s: %q", h.PublicId, h.Address),
errors.WithWrap(err),
)
2020-08-18 16:50:26 -04:00
}
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", h.PublicId)))
2020-08-18 16:50:26 -04:00
}
return returnedHost, rowsUpdated, nil
}
2020-08-19 14:14:14 -04:00
// 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.(Repository).LookupHost"
2020-08-19 14:14:14 -04:00
if publicId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
2020-08-19 14:14:14 -04:00
}
ha := &hostAgg{
PublicId: publicId,
}
if err := r.reader.LookupByPublicId(ctx, ha); err != nil {
2020-12-09 10:11:38 -08:00
if errors.IsNotFoundError(err) {
2020-08-19 14:14:14 -04:00
return nil, nil
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", publicId)))
2020-08-19 14:14:14 -04:00
}
return ha.toHost(), nil
2020-08-19 14:14:14 -04:00
}
2023-10-30 14:40:30 -07:00
// listHosts returns a slice of Hosts for the catalogId.
// Supported options:
// - WithLimit which overrides the limit set in the Repository object
// - WithStartPageAfterItem which sets where to start listing from
func (r *Repository) listHosts(ctx context.Context, catalogId string, opt ...Option) ([]*Host, time.Time, error) {
const op = "static.(Repository).listHosts"
2020-08-19 14:14:14 -04:00
if catalogId == "" {
2023-10-30 14:40:30 -07:00
return nil, time.Time{}, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
2020-08-19 14:14:14 -04:00
}
opts := getOpts(opt...)
limit := r.defaultLimit
if opts.withLimit != 0 {
// non-zero signals an override of the default limit for the repo.
limit = opts.withLimit
}
2023-10-30 14:40:30 -07:00
query := fmt.Sprintf(listHostsTemplate, limit)
args := []any{sql.Named("catalog_id", catalogId)}
if opts.withStartPageAfterItem != nil {
query = fmt.Sprintf(listHostsPageTemplate, limit)
args = append(args,
sql.Named("last_item_create_time", opts.withStartPageAfterItem.GetCreateTime()),
sql.Named("last_item_id", opts.withStartPageAfterItem.GetPublicId()),
)
}
return r.queryHosts(ctx, query, args)
}
// listHostsRefresh returns a slice of Hosts for the catalogId.
// Supported options:
// - WithLimit which overrides the limit set in the Repository object
// - WithStartPageAfterItem which sets where to start listing from
func (r *Repository) listHostsRefresh(ctx context.Context, catalogId string, updatedAfter time.Time, opt ...Option) ([]*Host, time.Time, error) {
const op = "static.(Repository).listHostsRefresh"
switch {
case catalogId == "":
return nil, time.Time{}, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
case updatedAfter.IsZero():
return nil, time.Time{}, errors.New(ctx, errors.InvalidParameter, op, "missing updated after time")
}
opts := getOpts(opt...)
limit := r.defaultLimit
if opts.withLimit != 0 {
// non-zero signals an override of the default limit for the repo.
limit = opts.withLimit
2020-08-19 14:14:14 -04:00
}
2023-10-30 14:40:30 -07:00
query := fmt.Sprintf(listHostsRefreshTemplate, limit)
args := []any{
sql.Named("catalog_id", catalogId),
sql.Named("updated_after_time", updatedAfter),
}
if opts.withStartPageAfterItem != nil {
query = fmt.Sprintf(listHostsRefreshPageTemplate, limit)
args = append(args,
sql.Named("last_item_update_time", opts.withStartPageAfterItem.GetUpdateTime()),
sql.Named("last_item_id", opts.withStartPageAfterItem.GetPublicId()),
)
}
2023-10-30 14:40:30 -07:00
return r.queryHosts(ctx, query, args)
}
func (r *Repository) queryHosts(ctx context.Context, query string, args []any) ([]*Host, time.Time, error) {
const op = "static.(Repository).queryHosts"
var hosts []*Host
var transactionTimestamp time.Time
if _, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(r db.Reader, w db.Writer) error {
rows, err := r.Query(ctx, query, args)
if err != nil {
return err
}
defer rows.Close()
var foundHosts []*hostAgg
for rows.Next() {
if err := r.ScanRows(ctx, rows, &foundHosts); err != nil {
return err
}
}
2024-01-22 14:03:40 -05:00
if err := rows.Err(); err != nil {
return err
}
2023-10-30 14:40:30 -07:00
hosts = make([]*Host, 0, len(foundHosts))
for _, ha := range foundHosts {
hosts = append(hosts, ha.toHost())
}
transactionTimestamp, err = r.Now(ctx)
return err
}); err != nil {
return nil, time.Time{}, errors.Wrap(ctx, err, op)
}
return hosts, transactionTimestamp, nil
2020-08-19 14:14:14 -04:00
}
// DeleteHost deletes the host for the provided id from the repository
// returning a count of the number of records deleted. All options are
// ignored.
2022-07-20 16:24:46 -07:00
func (r *Repository) DeleteHost(ctx context.Context, projectId string, publicId string, opt ...Option) (int, error) {
const op = "static.(Repository).DeleteHost"
2020-08-19 14:14:14 -04:00
if publicId == "" {
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
2020-08-19 14:14:14 -04:00
}
h := allocHost()
h.PublicId = publicId
2022-07-20 16:24:46 -07:00
oplogWrapper, err := r.kms.GetWrapper(ctx, 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-19 14:14:14 -04:00
var rowsDeleted int
2020-08-19 16:47:18 -04:00
_, err = r.writer.DoTx(
2020-08-19 14:14:14 -04:00
ctx, db.StdRetryCnt, db.ExpBackoff{},
func(_ db.Reader, w db.Writer) (err error) {
dh := h.clone()
2020-08-19 16:47:18 -04:00
rowsDeleted, err = w.Delete(ctx, dh, db.WithOplog(oplogWrapper, h.oplog(oplog.OpType_OP_TYPE_DELETE)))
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-19 14:14:14 -04:00
}
2020-12-23 11:00:00 -08:00
return nil
2020-08-19 14:14:14 -04:00
},
)
if err != nil {
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", publicId)))
2020-08-19 14:14:14 -04:00
}
return rowsDeleted, nil
}
2023-10-30 14:40:30 -07:00
// listDeletedHostIds lists the public IDs of any hosts deleted since the timestamp provided,
// and the timestamp of the transaction within which the hosts were listed.
func (r *Repository) listDeletedHostIds(ctx context.Context, since time.Time) ([]string, time.Time, error) {
const op = "static.(Repository).listDeletedHostIds"
var deleteHosts []*deletedHost
var transactionTimestamp time.Time
if _, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(r db.Reader, _ db.Writer) error {
if err := r.SearchWhere(ctx, &deleteHosts, "delete_time >= ?", []any{since}); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("failed to query deleted hosts"))
}
var err error
transactionTimestamp, err = r.Now(ctx)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("failed to get transaction timestamp"))
}
return nil
}); err != nil {
return nil, time.Time{}, err
}
var hostIds []string
for _, t := range deleteHosts {
hostIds = append(hostIds, t.PublicId)
}
return hostIds, transactionTimestamp, nil
}
// estimatedHostCount returns an estimate of the total number of static hosts.
func (r *Repository) estimatedHostCount(ctx context.Context) (int, error) {
const op = "static.(Repository).estimatedHostCount"
rows, err := r.reader.Query(ctx, estimateCountHosts, nil)
if err != nil {
return 0, errors.Wrap(ctx, err, op, errors.WithMsg("failed to query static hosts"))
}
var count int
for rows.Next() {
if err := r.reader.ScanRows(ctx, rows, &count); err != nil {
return 0, errors.Wrap(ctx, err, op, errors.WithMsg("failed to query static hosts"))
}
}
2024-01-22 14:03:40 -05:00
if err := rows.Err(); err != nil {
return 0, errors.Wrap(ctx, err, op, errors.WithMsg("failed to query static hosts"))
}
2023-10-30 14:40:30 -07:00
return count, nil
}