Add generic read/update/delete commands (#3992)

* Add generic read/update/delete commands

This adds three generic commands that use the resource prefix to
determine the subcommand to run and pass through args over to that
subcommand. Since this occurs very early on and to avoid loading
essentially every internal resource package, I moved subtype definitions
to globals.

This replaces #3967

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

* De-duplicate Subtype code (#4031)

Co-authored-by: Timothy Messier <tim.messier@gmail.com>

---------

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Co-authored-by: Timothy Messier <tim.messier@gmail.com>
This commit is contained in:
authored and GitHub committed 2023-11-15 19:55:46 -05:00
1 parent 5c84a8861a
commit b6df5693b5
59 files changed
+1032 -857

No files matched your search

@@ -6,13 +6,14 @@ package subtypes
import (
"fmt"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/structpb"
)
func convertAttributesToSubtype(msg proto.Message, st Subtype) error {
func convertAttributesToSubtype(msg proto.Message, st globals.Subtype) error {
r := msg.ProtoReflect()
d := r.Descriptor()
@@ -55,7 +56,7 @@ func convertAttributesToSubtype(msg proto.Message, st Subtype) error {
return nil
}
func convertAttributesToDefault(msg proto.Message, st Subtype) error {
func convertAttributesToDefault(msg proto.Message, st globals.Subtype) error {
r := msg.ProtoReflect()
d := r.Descriptor()
+5 -4
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"sync"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/sdk/pbs/controller/protooptions"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
@@ -33,7 +34,7 @@ func init() {
})
}
type fieldMap map[Subtype]protoreflect.FieldDescriptor
type fieldMap map[globals.Subtype]protoreflect.FieldDescriptor
type attributeRegistry struct {
sync.RWMutex
@@ -72,7 +73,7 @@ func (ak *attributeRegistry) register(d protoreflect.MessageDescriptor) error {
opts := f.Options().(*descriptorpb.FieldOptions)
st := proto.GetExtension(opts, protooptions.E_Subtype).(string)
if st != "" {
km[Subtype(st)] = f
km[globals.Subtype(st)] = f
}
}
@@ -99,7 +100,7 @@ func (ak *attributeRegistry) register(d protoreflect.MessageDescriptor) error {
// attributeField retrieves the FieldDescriptor for a given subtype's
// attribute fields. If the corresponding protobuf message has not been
// registered it will return an error.
func (ak *attributeRegistry) attributeField(d protoreflect.MessageDescriptor, t Subtype) (protoreflect.FieldDescriptor, error) {
func (ak *attributeRegistry) attributeField(d protoreflect.MessageDescriptor, t globals.Subtype) (protoreflect.FieldDescriptor, error) {
ak.RLock()
defer ak.RUnlock()
@@ -124,6 +125,6 @@ func (ak *attributeRegistry) attributeField(d protoreflect.MessageDescriptor, t
// attributeField is used by the AttributeTransformInterceptor to retrieve
// the proto FieldDescriptor for a given subtype.
func attributeField(d protoreflect.MessageDescriptor, t Subtype) (protoreflect.FieldDescriptor, error) {
func attributeField(d protoreflect.MessageDescriptor, t globals.Subtype) (protoreflect.FieldDescriptor, error) {
return globalAttributeRegistry.attributeField(d, t)
}
+5 -4
View File
@@ -6,6 +6,7 @@ package subtypes
import (
"testing"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/gen/testing/attribute"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -17,7 +18,7 @@ func TestProtoAttributeKey(t *testing.T) {
cases := []struct {
name string
msg proto.Message
subtype Subtype
subtype globals.Subtype
expected protoreflect.FullName
}{
{
@@ -35,7 +36,7 @@ func TestProtoAttributeKey(t *testing.T) {
{
"TestResource/unknown",
&attribute.TestResource{},
UnknownSubtype,
globals.UnknownSubtype,
"testing.attribute.v1.TestResource.attributes",
},
}
@@ -54,7 +55,7 @@ func TestProtoAttributeKeyErrors(t *testing.T) {
cases := []struct {
name string
msg proto.Message
subtype Subtype
subtype globals.Subtype
expectedErr string
}{
{
@@ -72,7 +73,7 @@ func TestProtoAttributeKeyErrors(t *testing.T) {
{
"TestNoAttributes/unknown",
&attribute.TestNoAttributes{},
UnknownSubtype,
globals.UnknownSubtype,
"proto message testing.attribute.v1.TestNoAttributes not registered",
},
}
+7 -26
View File
@@ -7,31 +7,14 @@ import (
"context"
"errors"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
"github.com/hashicorp/boundary/sdk/pbs/controller/protooptions"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
func messageDomain(m proto.Message) string {
r := m.ProtoReflect()
fd := r.Descriptor().ParentFile()
if fd == nil {
return ""
}
opts, ok := fd.Options().(*descriptorpb.FileOptions)
if !ok {
return ""
}
domain := proto.GetExtension(opts, protooptions.E_Domain).(string)
return domain
}
// transformRequestAttributes will modify the request proto.Message, setting
// any subtype attribute fields into the corresponding strongly-typed struct
// for the subtype. It looks for some specific structure in the proto.Message
@@ -98,8 +81,6 @@ func messageDomain(m proto.Message) string {
// Also note that for any of the id based lookups to function, the file that contains
// the proto.Message definition must set the "domain" custom option.
func transformRequestAttributes(req proto.Message) error {
domain := messageDomain(req)
r := req.ProtoReflect()
fields := r.Descriptor().Fields()
@@ -114,7 +95,7 @@ func transformRequestAttributes(req proto.Message) error {
return m.Get(fd).String()
}
var st Subtype
var st globals.Subtype
switch {
case itemField != nil:
itemR := itemField.Message()
@@ -134,18 +115,18 @@ func transformRequestAttributes(req proto.Message) error {
switch {
case idField != nil && id != "":
st = SubtypeFromId(domain, id)
st = globals.ResourceInfoFromPrefix(id).Subtype
case sourceIdField != nil && sourceId != "":
st = SubtypeFromId(domain, sourceId)
st = globals.ResourceInfoFromPrefix(sourceId).Subtype
case typeField != nil && t != "":
st = Subtype(t)
st = globals.Subtype(t)
default: // need either type or id
return nil
}
return convertAttributesToSubtype(item, st)
case idField != nil && attributesField != nil:
id := r.Get(idField).String()
st = SubtypeFromId(domain, id)
st = globals.ResourceInfoFromPrefix(id).Subtype
return convertAttributesToSubtype(req, st)
}
return nil
@@ -172,7 +153,7 @@ func transformResponseItemAttributes(item proto.Message) error {
return nil
}
st := Subtype(item.ProtoReflect().Get(typeField).String())
st := globals.Subtype(item.ProtoReflect().Get(typeField).String())
return convertAttributesToDefault(item, st)
}
+4 -2
View File
@@ -7,8 +7,10 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
"github.com/hashicorp/boundary/internal/gen/testing/attribute"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
@@ -17,8 +19,8 @@ import (
)
func init() {
Register("test", Subtype("sub_resource"), "trsr")
Register("test", Subtype("resource_plugin"), "trrp")
globals.RegisterPrefixToResourceInfo("trsr", resource.Unknown, "test", globals.Subtype("sub_resource"))
globals.RegisterPrefixToResourceInfo("trrp", resource.Unknown, "test", globals.Subtype("resource_plugin"))
}
func TestTransformRequestAttributes(t *testing.T) {
+12 -23
View File
@@ -10,26 +10,15 @@ import (
"strings"
"sync"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/errors"
)
// Subtype variables identify a boundary resource subtype.
type Subtype string
const (
UnknownSubtype Subtype = "unknown"
)
// String returns the string representation of a Subtype
func (t Subtype) String() string {
return string(t)
}
// Registry stores a collection of boundary resource subtypes along with their
// prefixes and allows for translating prefixes back to registered subtypes.
type Registry struct {
subtypesPrefixes map[string]Subtype
knownSubtypes map[Subtype]any
subtypesPrefixes map[string]globals.Subtype
knownSubtypes map[globals.Subtype]any
sync.RWMutex
}
@@ -37,39 +26,39 @@ type Registry struct {
// NewRegistry creates a new boundary resource subtype registry.
func NewRegistry() *Registry {
return &Registry{
subtypesPrefixes: make(map[string]Subtype),
knownSubtypes: make(map[Subtype]any),
subtypesPrefixes: make(map[string]globals.Subtype),
knownSubtypes: make(map[globals.Subtype]any),
}
}
// SubtypeFromType returns the Subtype from the provided string or if
// no Subtype was registered with that string Unknown is returned.
func (r *Registry) SubtypeFromType(t string) Subtype {
func (r *Registry) SubtypeFromType(t string) globals.Subtype {
r.RLock()
defer r.RUnlock()
st := Subtype(t)
st := globals.Subtype(t)
if _, ok := r.knownSubtypes[st]; !ok {
return UnknownSubtype
return globals.UnknownSubtype
}
return st
}
// SubtypeFromId returns the Subtype from the provided id if the id's prefix
// was registered with a Subtype. Otherwise Unknown is returned.
func (r *Registry) SubtypeFromId(id string) Subtype {
func (r *Registry) SubtypeFromId(id string) globals.Subtype {
r.RLock()
defer r.RUnlock()
i := strings.Index(id, "_")
if i == -1 {
return UnknownSubtype
return globals.UnknownSubtype
}
prefix := id[:i]
subtype, ok := r.subtypesPrefixes[prefix]
if !ok {
return UnknownSubtype
return globals.UnknownSubtype
}
return subtype
}
@@ -89,7 +78,7 @@ func (r *Registry) Prefixes() []string {
// Register registers all the prefixes for a provided Subtype. Register returns
// an error if the subtype has already been registered or if any of the
// prefixes are associated with another subtype.
func (r *Registry) Register(ctx context.Context, subtype Subtype, prefixes ...string) error {
func (r *Registry) Register(ctx context.Context, subtype globals.Subtype, prefixes ...string) error {
r.Lock()
defer r.Unlock()
-156
View File
@@ -1,156 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package subtypes
import (
"context"
"fmt"
"strings"
"sync"
"github.com/hashicorp/boundary/internal/errors"
)
var globalRegistry = newRegistry()
// newGlobalRegistry creates a new boundary resource subtype registry.
func newRegistry() *registry {
return &registry{
subtypesPrefixes: make(map[string]map[string]Subtype),
knownSubtypes: make(map[string]map[Subtype]any),
}
}
// registry stores a collection of boundary resource subtypes along with their
// prefixes and allows for translating prefixes back to registered subtypes.
type registry struct {
subtypesPrefixes map[string]map[string]Subtype
knownSubtypes map[string]map[Subtype]any
sync.RWMutex
}
// SubtypeFromType returns the Subtype from the provided string or if
// no Subtype was registered with that string Unknown is returned.
func (r *registry) subtypeFromType(domain, t string) Subtype {
r.RLock()
defer r.RUnlock()
knownSubtypes, ok := r.knownSubtypes[domain]
if !ok {
return UnknownSubtype
}
st := Subtype(t)
if _, ok := knownSubtypes[st]; !ok {
return UnknownSubtype
}
return st
}
// SubtypeFromId returns the Subtype from the provided id if the id's prefix
// was registered with a Subtype. Otherwise Unknown is returned.
func (r *registry) subtypeFromId(domain, id string) Subtype {
r.RLock()
defer r.RUnlock()
i := strings.Index(id, "_")
if i == -1 {
return UnknownSubtype
}
prefix := id[:i]
subtypePrefixes, ok := r.subtypesPrefixes[domain]
if !ok {
return UnknownSubtype
}
subtype, ok := subtypePrefixes[prefix]
if !ok {
return UnknownSubtype
}
return subtype
}
func (r *registry) prefixes(domain string) []string {
r.RLock()
defer r.RUnlock()
subtypePrefixes, ok := r.subtypesPrefixes[domain]
if !ok {
return nil
}
ret := make([]string, 0, len(subtypePrefixes))
for p := range subtypePrefixes {
ret = append(ret, p)
}
return ret
}
// Register registers all the prefixes for a provided Subtype. Register returns
// an error if the subtype has already been registered or if any of the
// prefixes are associated with another subtype.
func (r *registry) register(ctx context.Context, domain string, subtype Subtype, prefixes ...string) error {
r.Lock()
defer r.Unlock()
const op = "subtypes.(registry).register"
knownSubtypes, present := r.knownSubtypes[domain]
if !present {
knownSubtypes = make(map[Subtype]any)
r.knownSubtypes[domain] = knownSubtypes
r.subtypesPrefixes[domain] = make(map[string]Subtype)
}
if _, present := knownSubtypes[subtype]; present {
return errors.New(
ctx,
errors.SubtypeAlreadyRegistered,
op,
fmt.Sprintf("subtype %q already registered in domain %s", subtype, domain),
)
}
knownSubtypes[subtype] = nil
for _, prefix := range prefixes {
prefix = strings.TrimSpace(prefix)
if st, ok := r.subtypesPrefixes[domain][prefix]; ok {
return errors.New(
ctx,
errors.SubtypeAlreadyRegistered,
op,
fmt.Sprintf("prefix %q is already registered to subtype %q in domain %s", prefix, st, domain),
)
}
r.subtypesPrefixes[domain][prefix] = subtype
}
return nil
}
// SubtypeFromType returns the Subtype from the provided string or if
// no Subtype was registered with that string Unknown is returned.
func SubtypeFromType(domain, t string) Subtype {
return globalRegistry.subtypeFromType(domain, t)
}
// SubtypeFromId returns the Subtype from the provided id if the id's prefix
// was registered with a Subtype. Otherwise Unknown is returned.
func SubtypeFromId(domain, id string) Subtype {
return globalRegistry.subtypeFromId(domain, id)
}
// Prefixes returns the list of all known Prefixes for a domain.
func Prefixes(domain string) []string {
return globalRegistry.prefixes(domain)
}
// Register registers all the prefixes for a provided Subtype. Register returns
// an error if the subtype has already been registered or if any of the
// prefixes are associated with another subtype.
func Register(domain string, subtype Subtype, prefixes ...string) error {
ctx := context.TODO()
return globalRegistry.register(ctx, domain, subtype, prefixes...)
}
-79
View File
@@ -1,79 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package subtypes_test
import (
"context"
"testing"
"github.com/hashicorp/boundary/internal/types/subtypes"
"github.com/stretchr/testify/assert"
)
func TestSubtypeFromId(t *testing.T) {
testSubtype := subtypes.Subtype("test")
r := subtypes.NewRegistry()
r.Register(context.Background(), testSubtype, "tttst")
tests := []struct {
name string
given string
want subtypes.Subtype
}{
{"empty-string", "", subtypes.UnknownSubtype},
{"no-prefix-delimiter", "tttst1234", subtypes.UnknownSubtype},
{"prefix-first", "_tttst_1234", subtypes.UnknownSubtype},
{"unknown-prefix", "kaz_1234", subtypes.UnknownSubtype},
{"prefix-no-id", "tttst_", testSubtype},
{"vault-prefix", "tttst_1234", testSubtype},
{"prefix-no-delimiter-no-id", "tttst", subtypes.UnknownSubtype},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := r.SubtypeFromId(tt.given)
assert.Equalf(t, tt.want, got, "given: %s", tt.given)
if got != tt.want {
t.Errorf("(%s): expected %s, actual %s", tt.given, tt.want, got)
}
})
}
}
func TestSubtypeFromType(t *testing.T) {
testSubtype := subtypes.Subtype("test")
r := subtypes.NewRegistry()
r.Register(context.Background(), testSubtype, "tttst")
tests := []struct {
name string
given string
want subtypes.Subtype
}{
{"empty-string", "", subtypes.UnknownSubtype},
{"correct-string", "test", testSubtype},
{"captialized", "TEST", subtypes.UnknownSubtype},
{"typo", "testt", subtypes.UnknownSubtype},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := r.SubtypeFromType(tt.given)
assert.Equalf(t, tt.want, got, "given: %s", tt.given)
if got != tt.want {
t.Errorf("(%s): expected %s, actual %s", tt.given, tt.want, got)
}
})
}
}
func TestRegister(t *testing.T) {
r := subtypes.NewRegistry()
ctx := context.Background()
assert.NoError(t, r.Register(ctx, "test", "testprefix"))
// registering multiple subtypes should be fine.
assert.NoError(t, r.Register(ctx, "second", "secondprefix"))
// registering another prefix with a different subtype errors.
assert.Error(t, r.Register(ctx, "third", "testprefix"))
// Registering the same subtype twice errors.
assert.Error(t, r.Register(ctx, "test", "repeatedprefix"))
}