Files
threshold/internal/host/static/repository_host.go
T
Michael Gaffney e18cdc52b3 Create host (#276)
* Change testHostCatalog methods to be like other resource test methods

* Implement CreateHost

* Make allocHost return a pointer to a Host
2020-08-17 17:06:31 -04:00

62 lines
1.9 KiB
Go

package static
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/oplog"
)
// 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.
func (r *Repository) CreateHost(ctx context.Context, h *Host, opt ...Option) (*Host, error) {
if h == nil {
return nil, fmt.Errorf("create: static host: %w", db.ErrNilParameter)
}
if h.Host == nil {
return nil, fmt.Errorf("create: static host: embedded Host: %w", db.ErrNilParameter)
}
if h.CatalogId == "" {
return nil, fmt.Errorf("create: static host: no catalog id: %w", db.ErrInvalidParameter)
}
if h.PublicId != "" {
return nil, fmt.Errorf("create: static host: public id not empty: %w", db.ErrInvalidParameter)
}
h = h.clone()
id, err := newHostId()
if err != nil {
return nil, fmt.Errorf("create: static host: %w", err)
}
h.PublicId = id
var newHost *Host
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
newHost = h.clone()
return w.Create(ctx, newHost, db.WithOplog(r.wrapper, h.oplog(oplog.OpType_OP_TYPE_CREATE)))
},
)
if err != nil {
if db.IsUniqueError(err) {
return nil, fmt.Errorf("create: static host: in catalog: %s: name %s already exists: %w",
h.CatalogId, h.Name, db.ErrNotUnique)
}
if db.IsCheckConstraintError(err) {
return nil, fmt.Errorf("create: static host: in catalog: %s: %q: %w",
h.CatalogId, h.Address, ErrInvalidAddress)
}
return nil, fmt.Errorf("create: static host: in catalog: %s: %w", h.CatalogId, err)
}
return newHost, nil
}