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

78 lines
1.7 KiB
Go
Raw Normal View History

2020-08-13 14:00:15 -04:00
package static
import (
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-13 14:00:15 -04:00
"github.com/hashicorp/boundary/internal/host/static/store"
2020-08-17 17:06:31 -04:00
"github.com/hashicorp/boundary/internal/oplog"
"google.golang.org/protobuf/proto"
2020-08-13 14:00:15 -04:00
)
const (
MinHostAddressLength = 3
MaxHostAddressLength = 255
)
2020-08-13 14:00:15 -04:00
// A Host contains a static address.
type Host struct {
*store.Host
tableName string `gorm:"-"`
}
// NewHost creates a new in memory Host for address assigned to catalogId.
// Name and description are the only valid options. All other options are
// ignored.
2020-08-20 08:20:26 -07:00
func NewHost(catalogId string, opt ...Option) (*Host, error) {
2020-08-13 14:00:15 -04:00
if catalogId == "" {
2020-12-09 10:11:38 -08:00
return nil, errors.New(errors.InvalidParameter, "static.NewHost", "no catalog id")
2020-08-13 14:00:15 -04:00
}
opts := getOpts(opt...)
host := &Host{
Host: &store.Host{
CatalogId: catalogId,
2020-08-20 08:20:26 -07:00
Address: opts.withAddress,
Name: opts.withName,
Description: opts.withDescription,
2020-08-13 14:00:15 -04:00
},
}
return host, nil
}
// TableName returns the table name for the host.
func (h *Host) TableName() string {
if h.tableName != "" {
return h.tableName
}
return "static_host"
}
// SetTableName sets the table name. If the caller attempts to
// set the name to "" the name will be reset to the default name.
func (h *Host) SetTableName(n string) {
h.tableName = n
}
2020-08-17 17:06:31 -04:00
func allocHost() *Host {
return &Host{
Host: &store.Host{},
}
}
func (h *Host) clone() *Host {
cp := proto.Clone(h.Host)
return &Host{
Host: cp.(*store.Host),
}
}
func (h *Host) oplog(op oplog.OpType) oplog.Metadata {
metadata := oplog.Metadata{
"resource-public-id": []string{h.PublicId},
"resource-type": []string{"static-host"},
"op-type": []string{op.String()},
}
if h.CatalogId != "" {
metadata["catalog-id"] = []string{h.CatalogId}
}
return metadata
}