Add sync interval to host sets (#1680)

This adds a parameter to host sets allowing the sync interval to be
user-specified on a per-host-set basis. A value of -1 disables syncing;
a value of null uses the current default.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-11-11 11:28:08 -05:00
1 parent dba7c6b531
commit 30e9f944a2
27 files changed
+752 -231

No files matched your search

@@ -11,7 +11,7 @@ import (
"github.com/hashicorp/boundary/internal/host/plugin"
plugstore "github.com/hashicorp/boundary/internal/host/plugin/store"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/host/static/store"
staticstore "github.com/hashicorp/boundary/internal/host/static/store"
"github.com/hashicorp/boundary/internal/libs/endpoint"
"github.com/hashicorp/boundary/internal/perms"
hostplugin "github.com/hashicorp/boundary/internal/plugin/host"
@@ -32,7 +32,7 @@ import (
)
var (
maskManager handlers.MaskManager
maskManager = map[subtypes.Subtype]handlers.MaskManager{}
// IdActions contains the set of actions that can be performed on
// individual resources
@@ -64,7 +64,10 @@ var (
func init() {
var err error
if maskManager, err = handlers.NewMaskManager(handlers.MaskDestination{&store.HostSet{}}, handlers.MaskSource{&pb.HostSet{}}); err != nil {
if maskManager[static.Subtype], err = handlers.NewMaskManager(handlers.MaskDestination{&staticstore.HostSet{}, &staticstore.UnimplementedSetFields{}}, handlers.MaskSource{&pb.HostSet{}}); err != nil {
panic(err)
}
if maskManager[plugin.Subtype], err = handlers.NewMaskManager(handlers.MaskDestination{&plugstore.HostSet{}}, handlers.MaskSource{&pb.HostSet{}}); err != nil {
panic(err)
}
}
@@ -517,7 +520,7 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, catalogId string, re
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("Unable to build host set for update"))
}
h.PublicId = req.GetId()
dbMask := maskManager.Translate(req.GetUpdateMask().GetPaths())
dbMask := maskManager[static.Subtype].Translate(req.GetUpdateMask().GetPaths())
if len(dbMask) == 0 {
return nil, nil, handlers.InvalidArgumentErrorf("No valid fields included in the update mask.", map[string]string{"update_mask": "No valid fields provided in the update mask."})
}
@@ -817,7 +820,10 @@ func toProto(ctx context.Context, in host.Set, hosts []host.Host, opt ...handler
switch h := in.(type) {
case *plugin.HostSet:
if outputFields.Has(globals.PreferredEndpointsField) {
out.PreferredEndpoints = h.GetPreferredEndpoints()
out.PreferredEndpoints = h.PreferredEndpoints
}
if outputFields.Has(globals.SyncIntervalSecondsField) && h.GetSyncIntervalSeconds() != 0 {
out.SyncIntervalSeconds = &wrapperspb.Int32Value{Value: h.GetSyncIntervalSeconds()}
}
if outputFields.Has(globals.AttributesField) {
attrs := &structpb.Struct{}
@@ -865,6 +871,9 @@ func toStoragePluginSet(ctx context.Context, catalogId string, item *pb.HostSet)
if item.GetPreferredEndpoints() != nil {
opts = append(opts, plugin.WithPreferredEndpoints(item.GetPreferredEndpoints()))
}
if item.GetSyncIntervalSeconds() != nil {
opts = append(opts, plugin.WithSyncIntervalSeconds(item.GetSyncIntervalSeconds().GetValue()))
}
hs, err := plugin.NewHostSet(ctx, catalogId, opts...)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("Unable to build host set for creation"))
@@ -904,6 +913,11 @@ func validateCreateRequest(ctx context.Context, req *pbs.CreateHostSetRequest) e
if req.GetItem().GetType() != "" && req.GetItem().GetType() != plugin.Subtype.String() {
badFields[globals.TypeField] = "Doesn't match the parent resource's type."
}
if val := req.GetItem().GetSyncIntervalSeconds(); val != nil {
if val.GetValue() == 0 || val.GetValue() < -1 {
badFields[globals.SyncIntervalSecondsField] = "Must be -1 or a positive integer."
}
}
}
return badFields
})
@@ -923,6 +937,12 @@ func validateUpdateRequest(ctx context.Context, req *pbs.UpdateHostSetRequest) e
if req.GetItem().GetType() != "" && req.GetItem().GetType() != static.Subtype.String() {
badFields[globals.TypeField] = "Cannot modify the resource type."
}
case plugin.Subtype:
if val := req.GetItem().GetSyncIntervalSeconds(); val != nil {
if val.GetValue() == 0 || val.GetValue() < -1 {
badFields[globals.SyncIntervalSecondsField] = "Must be -1 or a positive integer."
}
}
}
return badFields
}, static.HostSetPrefix, plugin.HostSetPrefix)
@@ -37,6 +37,7 @@ import (
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
var testAuthorizedActions = map[subtypes.Subtype][]string{
@@ -168,7 +169,7 @@ func TestGet_Plugin(t *testing.T) {
}
hc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
hs := plugin.TestSet(t, conn, kms, sche, hc, plgm, plugin.WithPreferredEndpoints(prefEndpoints))
hs := plugin.TestSet(t, conn, kms, sche, hc, plgm, plugin.WithPreferredEndpoints(prefEndpoints), plugin.WithSyncIntervalSeconds(-1))
toMerge := &pbs.GetHostSetRequest{}
@@ -184,8 +185,9 @@ func TestGet_Plugin(t *testing.T) {
Name: plg.GetName(),
Description: plg.GetDescription(),
},
PreferredEndpoints: prefEndpoints,
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
PreferredEndpoints: prefEndpoints,
SyncIntervalSeconds: &wrappers.Int32Value{Value: -1},
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
}
cases := []struct {
@@ -373,7 +375,7 @@ func TestList_Plugin(t *testing.T) {
var wantHs []*pb.HostSet
for i := 0; i < 10; i++ {
h := plugin.TestSet(t, conn, kms, sche, hc, plgm, plugin.WithPreferredEndpoints(preferredEndpoints))
h := plugin.TestSet(t, conn, kms, sche, hc, plgm, plugin.WithPreferredEndpoints(preferredEndpoints), plugin.WithSyncIntervalSeconds(5))
wantHs = append(wantHs, &pb.HostSet{
Id: h.GetPublicId(),
HostCatalogId: h.GetCatalogId(),
@@ -383,12 +385,13 @@ func TestList_Plugin(t *testing.T) {
Name: plg.GetName(),
Description: plg.GetDescription(),
},
CreatedTime: h.GetCreateTime().GetTimestamp(),
UpdatedTime: h.GetUpdateTime().GetTimestamp(),
Version: h.GetVersion(),
Type: plugin.Subtype.String(),
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
PreferredEndpoints: preferredEndpoints,
CreatedTime: h.GetCreateTime().GetTimestamp(),
UpdatedTime: h.GetUpdateTime().GetTimestamp(),
Version: h.GetVersion(),
Type: plugin.Subtype.String(),
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
PreferredEndpoints: preferredEndpoints,
SyncIntervalSeconds: &wrappers.Int32Value{Value: 5},
})
}
@@ -856,10 +859,11 @@ func TestCreate_Plugin(t *testing.T) {
{
name: "No Attributes",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "No Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "No Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
SyncIntervalSeconds: &wrapperspb.Int32Value{Value: -1},
}},
res: &pbs.CreateHostSetResponse{
Uri: fmt.Sprintf("host-sets/%s_", plugin.HostSetPrefix),
@@ -871,21 +875,23 @@ func TestCreate_Plugin(t *testing.T) {
Name: plg.GetName(),
Description: plg.GetDescription(),
},
Name: &wrappers.StringValue{Value: "No Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
Name: &wrappers.StringValue{Value: "No Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
SyncIntervalSeconds: &wrapperspb.Int32Value{Value: -1},
},
},
},
{
name: "With Attributes",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "With Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
Attributes: testInputAttrs,
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "With Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
SyncIntervalSeconds: &wrapperspb.Int32Value{Value: 90},
Attributes: testInputAttrs,
}},
res: &pbs.CreateHostSetResponse{
Uri: fmt.Sprintf("host-sets/%s_", plugin.HostSetPrefix),
@@ -897,11 +903,12 @@ func TestCreate_Plugin(t *testing.T) {
Name: plg.GetName(),
Description: plg.GetDescription(),
},
Name: &wrappers.StringValue{Value: "With Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
Attributes: testOutputAttrs,
Name: &wrappers.StringValue{Value: "With Attributes"},
Description: &wrappers.StringValue{Value: "desc"},
Type: plugin.Subtype.String(),
SyncIntervalSeconds: &wrapperspb.Int32Value{Value: 90},
AuthorizedActions: testAuthorizedActions[plugin.Subtype],
Attributes: testOutputAttrs,
},
},
},