feat(host): Add host set information to static host (#1828)
* feat(host): Add host set information to static host Updates LookupHost and ListHost API to include sets a static host is part of, as like with plugin hosts. * test: updated TestList_Plugin and TestCrud to be more consistent under race conditions
This commit is contained in:
1 parent
84f6cc698c
commit
fa00a06bef
9 files changed
+305
-35
No files matched your search
@@ -1,6 +1,10 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
@@ -15,7 +19,8 @@ const (
|
||||
// A Host contains a static address.
|
||||
type Host struct {
|
||||
*store.Host
|
||||
tableName string `gorm:"-"`
|
||||
SetIds []string `gorm:"-"`
|
||||
tableName string `gorm:"-"`
|
||||
}
|
||||
|
||||
// NewHost creates a new in memory Host for address assigned to catalogId.
|
||||
@@ -70,9 +75,18 @@ func allocHost() *Host {
|
||||
|
||||
func (h *Host) clone() *Host {
|
||||
cp := proto.Clone(h.Host)
|
||||
return &Host{
|
||||
nh := &Host{
|
||||
Host: cp.(*store.Host),
|
||||
}
|
||||
switch {
|
||||
case h.SetIds == nil:
|
||||
case len(h.SetIds) == 0:
|
||||
nh.SetIds = make([]string, 0)
|
||||
default:
|
||||
nh.SetIds = make([]string, len(h.SetIds))
|
||||
copy(nh.SetIds, h.SetIds)
|
||||
}
|
||||
return nh
|
||||
}
|
||||
|
||||
func (h *Host) oplog(op oplog.OpType) oplog.Metadata {
|
||||
@@ -86,3 +100,55 @@ func (h *Host) oplog(op oplog.OpType) oplog.Metadata {
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
// GetSetIds returns host set ids
|
||||
func (h *Host) GetSetIds() []string {
|
||||
return h.SetIds
|
||||
}
|
||||
|
||||
type hostAgg struct {
|
||||
PublicId string `gorm:"primary_key"`
|
||||
CatalogId string
|
||||
Name string
|
||||
Description string
|
||||
CreateTime *timestamp.Timestamp
|
||||
UpdateTime *timestamp.Timestamp
|
||||
Version uint32
|
||||
Address string
|
||||
SetIds string
|
||||
}
|
||||
|
||||
func (agg *hostAgg) toHost() *Host {
|
||||
h := allocHost()
|
||||
h.PublicId = agg.PublicId
|
||||
h.CatalogId = agg.CatalogId
|
||||
h.Name = agg.Name
|
||||
h.Description = agg.Description
|
||||
h.CreateTime = agg.CreateTime
|
||||
h.UpdateTime = agg.UpdateTime
|
||||
h.Version = agg.Version
|
||||
h.Address = agg.Address
|
||||
h.SetIds = agg.getSetIds()
|
||||
return h
|
||||
}
|
||||
|
||||
// TableName returns the table name for gorm
|
||||
func (agg *hostAgg) TableName() string {
|
||||
return "static_host_with_set_memberships"
|
||||
}
|
||||
|
||||
// GetPublicId returns the host public id as a string
|
||||
func (agg *hostAgg) GetPublicId() string {
|
||||
return agg.PublicId
|
||||
}
|
||||
|
||||
// GetSetIds returns a list of all associated host sets to the host
|
||||
func (agg *hostAgg) getSetIds() []string {
|
||||
const aggregateDelimiter = "|"
|
||||
var ids []string
|
||||
if agg.SetIds != "" {
|
||||
ids = strings.Split(agg.SetIds, aggregateDelimiter)
|
||||
sort.Strings(ids)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
@@ -174,6 +174,13 @@ func (r *Repository) UpdateHost(ctx context.Context, scopeId string, h *Host, ve
|
||||
if rowsUpdated > 1 {
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
}
|
||||
ha := &hostAgg{
|
||||
PublicId: h.PublicId,
|
||||
}
|
||||
if err := r.reader.LookupByPublicId(ctx, ha); err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("failed to lookup host after update"))
|
||||
}
|
||||
returnedHost.SetIds = ha.getSetIds()
|
||||
return nil
|
||||
},
|
||||
)
|
||||
@@ -203,15 +210,16 @@ func (r *Repository) LookupHost(ctx context.Context, publicId string, opt ...Opt
|
||||
if publicId == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
h := allocHost()
|
||||
h.PublicId = publicId
|
||||
if err := r.reader.LookupByPublicId(ctx, h); err != nil {
|
||||
ha := &hostAgg{
|
||||
PublicId: publicId,
|
||||
}
|
||||
if err := r.reader.LookupByPublicId(ctx, ha); err != nil {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", publicId)))
|
||||
}
|
||||
return h, nil
|
||||
return ha.toHost(), nil
|
||||
}
|
||||
|
||||
// ListHosts returns a slice of Hosts for the catalogId.
|
||||
@@ -227,11 +235,16 @@ func (r *Repository) ListHosts(ctx context.Context, catalogId string, opt ...Opt
|
||||
// non-zero signals an override of the default limit for the repo.
|
||||
limit = opts.withLimit
|
||||
}
|
||||
var hosts []*Host
|
||||
err := r.reader.SearchWhere(ctx, &hosts, "catalog_id = ?", []interface{}{catalogId}, db.WithLimit(limit))
|
||||
var aggs []*hostAgg
|
||||
err := r.reader.SearchWhere(ctx, &aggs, "catalog_id = ?", []interface{}{catalogId}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
hosts := make([]*Host, 0, len(aggs))
|
||||
for _, ha := range aggs {
|
||||
hosts = append(hosts, ha.toHost())
|
||||
}
|
||||
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package static
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
dbassert "github.com/hashicorp/boundary/internal/db/assert"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
@@ -604,6 +606,10 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
assert.NoError(err)
|
||||
require.NotNil(orig)
|
||||
|
||||
set := TestSets(t, conn, catalog.GetPublicId(), 1)[0]
|
||||
TestSetMembers(t, conn, set.PublicId, []*Host{orig})
|
||||
wantSetIds := []string{set.PublicId}
|
||||
|
||||
if tt.chgFn != nil {
|
||||
orig = tt.chgFn(orig)
|
||||
}
|
||||
@@ -628,6 +634,7 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
dbassert.IsNull(got, "name")
|
||||
return
|
||||
}
|
||||
assert.Equal(wantSetIds, got.SetIds)
|
||||
assert.Equal(tt.want.Name, got.Name)
|
||||
if tt.want.Description == "" {
|
||||
dbassert.IsNull(got, "description")
|
||||
@@ -798,6 +805,72 @@ func TestRepository_LookupHost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_LookupHost_HostSets(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
catalog := TestCatalogs(t, conn, prj.PublicId, 1)[0]
|
||||
hosts := TestHosts(t, conn, catalog.PublicId, 3)
|
||||
hostA, hostB, hostC := hosts[0], hosts[1], hosts[2]
|
||||
setB := TestSets(t, conn, catalog.PublicId, 1)[0]
|
||||
setsC := TestSets(t, conn, catalog.PublicId, 5)
|
||||
TestSetMembers(t, conn, setB.PublicId, []*Host{hostB})
|
||||
hostB.SetIds = []string{setB.PublicId}
|
||||
for _, s := range setsC {
|
||||
hostC.SetIds = append(hostC.SetIds, s.PublicId)
|
||||
TestSetMembers(t, conn, s.PublicId, []*Host{hostC})
|
||||
}
|
||||
sort.Slice(hostC.SetIds, func(i, j int) bool {
|
||||
return hostC.SetIds[i] < hostC.SetIds[j]
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
want *Host
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "with-zero-hostsets",
|
||||
in: hostA.PublicId,
|
||||
want: hostA,
|
||||
},
|
||||
{
|
||||
name: "with-one-hostset",
|
||||
in: hostB.PublicId,
|
||||
want: hostB,
|
||||
},
|
||||
{
|
||||
name: "with-many-hostsets",
|
||||
in: hostC.PublicId,
|
||||
want: hostC,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.LookupHost(context.Background(), tt.in)
|
||||
assert.Empty(
|
||||
cmp.Diff(
|
||||
tt.want,
|
||||
got,
|
||||
cmpopts.IgnoreUnexported(Host{}, store.Host{}),
|
||||
cmpopts.IgnoreTypes(×tamp.Timestamp{}),
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListHosts(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
@@ -857,6 +930,80 @@ func TestRepository_ListHosts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListHosts_HostSets(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
|
||||
// testing for full and empty hostset associations
|
||||
catalogs := TestCatalogs(t, conn, prj.PublicId, 3)
|
||||
catalogA, catalogB, catalogC := catalogs[0], catalogs[1], catalogs[2]
|
||||
hostsA := TestHosts(t, conn, catalogA.PublicId, 3)
|
||||
setA := TestSets(t, conn, catalogA.PublicId, 1)[0]
|
||||
TestSetMembers(t, conn, setA.PublicId, hostsA)
|
||||
for _, h := range hostsA {
|
||||
h.SetIds = []string{setA.PublicId}
|
||||
}
|
||||
hostsB := TestHosts(t, conn, catalogB.PublicId, 3)
|
||||
|
||||
// testing for mixed hosts with individual hostsets and empty hostsets
|
||||
hostsC := TestHosts(t, conn, catalogC.PublicId, 5)
|
||||
hostsC0 := TestHosts(t, conn, catalogC.PublicId, 2)
|
||||
setC := TestSets(t, conn, catalogC.PublicId, 5)
|
||||
for i, h := range hostsC {
|
||||
h.SetIds = []string{setC[i].PublicId}
|
||||
TestSetMembers(t, conn, setC[i].PublicId, []*Host{hostsC[i]})
|
||||
}
|
||||
hostsC = append(hostsC, hostsC0...)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
want []*Host
|
||||
}{
|
||||
{
|
||||
name: "with-hostsets",
|
||||
in: catalogA.PublicId,
|
||||
want: hostsA,
|
||||
},
|
||||
{
|
||||
name: "empty-hostsets",
|
||||
in: catalogB.PublicId,
|
||||
want: hostsB,
|
||||
},
|
||||
{
|
||||
name: "mixed-hostsets",
|
||||
in: catalogC.PublicId,
|
||||
want: hostsC,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.ListHosts(context.Background(), tt.in)
|
||||
require.NoError(err)
|
||||
assert.Empty(
|
||||
cmp.Diff(
|
||||
tt.want,
|
||||
got,
|
||||
cmpopts.IgnoreUnexported(Host{}, store.Host{}),
|
||||
cmpopts.IgnoreTypes(×tamp.Timestamp{}),
|
||||
cmpopts.SortSlices(func(x, y *Host) bool {
|
||||
return x.GetPublicId() < y.GetPublicId()
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListHosts_Limits(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
|
||||
Reference in new issue
Block a user