* Update host set member test * Add ListSetMembers method signature and doc * Move repository methods for host set members to new file * Add TestSetMembers * Implement ListSetMembers * Return hosts from ListSetMembers * Implement AddSetMembers * Implement DeleteSetMembers * Use sql.QueryContext not sql.Query * Implement SetSetMembers * Refactor: extract getHosts method * Refactor: extract createMembers method * Refactor: extract common functions * Return correct count of changed In SetSetMembers, retrieving the current list of hosts in the set happens after the transaction to modify the set has completed. This retrieval can result in an error but the number of modified rows is not effected. * Make list set members an unexported method * Get hosts within the same transaction as mutations of the set * Return hosts of set from LookupSet method * Return hosts of set from UpdateSet method * Remove listSetMembers * Fix merge conflict * Add NOTE and TODOs about using new pattern in "SetMembers" methods
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package static
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/host/static/store"
|
|
"github.com/hashicorp/boundary/internal/oplog"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// A HostSet is a collection of hosts from the set's catalog.
|
|
type HostSet struct {
|
|
*store.HostSet
|
|
tableName string `gorm:"-"`
|
|
}
|
|
|
|
// NewHostSet creates a new in memory HostSet assigned to catalogId.
|
|
// Name and description are the only valid options. All other options are
|
|
// ignored.
|
|
func NewHostSet(catalogId string, opt ...Option) (*HostSet, error) {
|
|
if catalogId == "" {
|
|
return nil, fmt.Errorf("new: static host set: no catalog id: %w", db.ErrInvalidParameter)
|
|
}
|
|
|
|
opts := getOpts(opt...)
|
|
set := &HostSet{
|
|
HostSet: &store.HostSet{
|
|
CatalogId: catalogId,
|
|
Name: opts.withName,
|
|
Description: opts.withDescription,
|
|
},
|
|
}
|
|
return set, nil
|
|
}
|
|
|
|
// TableName returns the table name for the host set.
|
|
func (s *HostSet) TableName() string {
|
|
if s.tableName != "" {
|
|
return s.tableName
|
|
}
|
|
return "static_host_set"
|
|
}
|
|
|
|
// SetTableName sets the table name. If the caller attempts to
|
|
// set the name to "" the name will be reset to the default name.
|
|
func (s *HostSet) SetTableName(n string) {
|
|
s.tableName = n
|
|
}
|
|
|
|
func allocHostSet() *HostSet {
|
|
return &HostSet{
|
|
HostSet: &store.HostSet{},
|
|
}
|
|
}
|
|
|
|
func (s *HostSet) clone() *HostSet {
|
|
cp := proto.Clone(s.HostSet)
|
|
return &HostSet{
|
|
HostSet: cp.(*store.HostSet),
|
|
}
|
|
}
|
|
|
|
func (s *HostSet) oplog(op oplog.OpType) oplog.Metadata {
|
|
metadata := oplog.Metadata{
|
|
"resource-public-id": []string{s.PublicId},
|
|
"resource-type": []string{"static-host-set"},
|
|
"op-type": []string{op.String()},
|
|
}
|
|
if s.CatalogId != "" {
|
|
metadata["catalog-id"] = []string{s.CatalogId}
|
|
}
|
|
return metadata
|
|
}
|
|
|
|
func newHostSetForMembers(setId string, version uint32) *HostSet {
|
|
return &HostSet{
|
|
HostSet: &store.HostSet{
|
|
PublicId: setId,
|
|
Version: version + 1,
|
|
},
|
|
}
|
|
}
|