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

+3 -3
View File
@@ -7,9 +7,9 @@ import (
"net"
"time"
"github.com/hashicorp/boundary/globals"
intglobals "github.com/hashicorp/boundary/internal/globals"
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/internal/types/subtypes"
)
// GetOpts - iterate the inbound Options and return a struct
@@ -35,7 +35,7 @@ type options struct {
WithProjectIds []string
WithProjectName string
WithUserId string
WithType subtypes.Subtype
WithType globals.Subtype
WithHostSources []string
WithCredentialLibraries []*CredentialLibrary
WithStaticCredentials []*StaticCredential
@@ -148,7 +148,7 @@ func WithUserId(userId string) Option {
}
// WithType provides an option to search by a target type
func WithType(t subtypes.Subtype) Option {
func WithType(t globals.Subtype) Option {
return func(o *options) {
o.WithType = t
}
+2 -2
View File
@@ -6,10 +6,10 @@ package target
import (
"testing"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/credential"
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/internal/target/store"
"github.com/hashicorp/boundary/internal/types/subtypes"
"github.com/stretchr/testify/assert"
)
@@ -103,7 +103,7 @@ func Test_GetOpts(t *testing.T) {
assert.Equal(opts, testOpts)
})
t.Run("WithType", func(t *testing.T) {
subtype := subtypes.Subtype("testtype")
subtype := globals.Subtype("testtype")
assert := assert.New(t)
opts := GetOpts(WithType(subtype))
testOpts := getDefaultOptions()
+21 -22
View File
@@ -8,11 +8,12 @@ import (
"fmt"
"sync"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/types/subtypes"
"github.com/hashicorp/boundary/internal/types/resource"
)
const domain = "target"
const Domain = "target"
// NewFunc is a function that creates a Target with the provided project and options.
type NewFunc func(ctx context.Context, projectId string, opt ...Option) (Target, error)
@@ -57,12 +58,12 @@ type registryEntry struct {
}
type registry struct {
m map[subtypes.Subtype]*registryEntry
m map[globals.Subtype]*registryEntry
sync.RWMutex
}
func (r *registry) set(s subtypes.Subtype, entry *registryEntry) {
func (r *registry) set(s globals.Subtype, entry *registryEntry) {
r.Lock()
defer r.Unlock()
@@ -71,14 +72,12 @@ func (r *registry) set(s subtypes.Subtype, entry *registryEntry) {
panic(fmt.Sprintf("target subtype %s already registered", s))
}
if err := subtypes.Register(domain, s, entry.prefix); err != nil {
panic(err)
}
globals.RegisterPrefixToResourceInfo(entry.prefix, resource.Target, Domain, s)
r.m[s] = entry
}
func (r *registry) get(s subtypes.Subtype) (*registryEntry, bool) {
func (r *registry) get(s globals.Subtype) (*registryEntry, bool) {
r.RLock()
defer r.RUnlock()
@@ -89,7 +88,7 @@ func (r *registry) get(s subtypes.Subtype) (*registryEntry, bool) {
return nil, ok
}
func (r *registry) newFunc(s subtypes.Subtype) (NewFunc, bool) {
func (r *registry) newFunc(s globals.Subtype) (NewFunc, bool) {
entry, ok := r.get(s)
if !ok {
return nil, ok
@@ -97,7 +96,7 @@ func (r *registry) newFunc(s subtypes.Subtype) (NewFunc, bool) {
return entry.targetHooks.NewTarget, ok
}
func (r *registry) allocFunc(s subtypes.Subtype) (AllocFunc, bool) {
func (r *registry) allocFunc(s globals.Subtype) (AllocFunc, bool) {
entry, ok := r.get(s)
if !ok {
return nil, ok
@@ -105,7 +104,7 @@ func (r *registry) allocFunc(s subtypes.Subtype) (AllocFunc, bool) {
return entry.targetHooks.AllocTarget, ok
}
func (r *registry) vetFunc(s subtypes.Subtype) (VetFunc, bool) {
func (r *registry) vetFunc(s globals.Subtype) (VetFunc, bool) {
entry, ok := r.get(s)
if !ok {
return nil, ok
@@ -113,7 +112,7 @@ func (r *registry) vetFunc(s subtypes.Subtype) (VetFunc, bool) {
return entry.targetHooks.Vet, ok
}
func (r *registry) vetForUpdateFunc(s subtypes.Subtype) (VetForUpdateFunc, bool) {
func (r *registry) vetForUpdateFunc(s globals.Subtype) (VetForUpdateFunc, bool) {
entry, ok := r.get(s)
if !ok {
return nil, ok
@@ -121,7 +120,7 @@ func (r *registry) vetForUpdateFunc(s subtypes.Subtype) (VetForUpdateFunc, bool)
return entry.targetHooks.VetForUpdate, ok
}
func (r *registry) vetCredentialSourcesFunc(s subtypes.Subtype) (VetCredentialSourcesFunc, bool) {
func (r *registry) vetCredentialSourcesFunc(s globals.Subtype) (VetCredentialSourcesFunc, bool) {
entry, ok := r.get(s)
if !ok {
return nil, ok
@@ -130,7 +129,7 @@ func (r *registry) vetCredentialSourcesFunc(s subtypes.Subtype) (VetCredentialSo
return entry.targetHooks.VetCredentialSources, ok
}
func (r *registry) idPrefix(s subtypes.Subtype) (string, bool) {
func (r *registry) idPrefix(s globals.Subtype) (string, bool) {
entry, ok := r.get(s)
if !ok {
return "", ok
@@ -139,28 +138,28 @@ func (r *registry) idPrefix(s subtypes.Subtype) (string, bool) {
}
var subtypeRegistry = registry{
m: make(map[subtypes.Subtype]*registryEntry),
m: make(map[globals.Subtype]*registryEntry),
}
// SubtypeFromType returns the Subtype from the provided string or if
// no Subtype was registered with that string Unknown is returned.
func SubtypeFromType(t string) subtypes.Subtype {
return subtypes.SubtypeFromType(domain, t)
func SubtypeFromType(t string) globals.Subtype {
return globals.Subtype(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(id string) subtypes.Subtype {
return subtypes.SubtypeFromId(domain, id)
func SubtypeFromId(id string) globals.Subtype {
return globals.ResourceInfoFromPrefix(id).Subtype
}
// Prefixes returns the list of all known target Prefixes.
func Prefixes() []string {
return subtypes.Prefixes(domain)
return globals.PrefixesFromDomain(Domain)
}
// New creates a Target of the given subtype and projectId.
func New(ctx context.Context, subtype subtypes.Subtype, projectId string, opt ...Option) (Target, error) {
func New(ctx context.Context, subtype globals.Subtype, projectId string, opt ...Option) (Target, error) {
const op = "target.New"
nf, ok := subtypeRegistry.newFunc(subtype)
if !ok {
@@ -172,7 +171,7 @@ func New(ctx context.Context, subtype subtypes.Subtype, projectId string, opt ..
// Register registers repository hooks and the prefixes for a provided Subtype. Register
// panics if the subtype has already been registered or if any of the
// prefixes are associated with another subtype.
func Register(s subtypes.Subtype, th targetHooks, prefix string) {
func Register(s globals.Subtype, th targetHooks, prefix string) {
subtypeRegistry.set(s, &registryEntry{
targetHooks: th,
prefix: prefix,
@@ -9,12 +9,12 @@ import (
"fmt"
"strings"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/credential"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/types/subtypes"
"github.com/hashicorp/go-secure-stdlib/strutil"
)
@@ -536,7 +536,7 @@ func fetchCredentialSources(ctx context.Context, r db.Reader, targetId string) (
return ret, nil
}
func (r *Repository) createSources(ctx context.Context, tId string, tSubtype subtypes.Subtype, credSources CredentialSources) ([]*CredentialLibrary, []*StaticCredential, error) {
func (r *Repository) createSources(ctx context.Context, tId string, tSubtype globals.Subtype, credSources CredentialSources) ([]*CredentialLibrary, []*StaticCredential, error) {
const op = "target.(Repository).createSources"
// Get a list of unique ids being attached to the target, to be used for looking up the source type (library or static)
+4 -4
View File
@@ -8,12 +8,12 @@ import (
goerrs "errors"
"fmt"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/boundary"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/target/store"
"github.com/hashicorp/boundary/internal/types/subtypes"
)
// Target is a commmon interface for all target subtypes
@@ -25,7 +25,7 @@ type Target interface {
GetName() string
GetDescription() string
GetVersion() uint32
GetType() subtypes.Subtype
GetType() globals.Subtype
GetCreateTime() *timestamp.Timestamp
GetUpdateTime() *timestamp.Timestamp
GetSessionMaxSeconds() uint32
@@ -140,8 +140,8 @@ func (t targetView) GetUserId() string {
return ""
}
func (t *targetView) Subtype() subtypes.Subtype {
return subtypes.Subtype(t.Type)
func (t *targetView) Subtype() globals.Subtype {
return globals.Subtype(t.Type)
}
// targetSubtype converts the target view to the concrete subtype
+2 -3
View File
@@ -18,13 +18,12 @@ import (
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/targettest/store"
"github.com/hashicorp/boundary/internal/types/subtypes"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
const (
Subtype = subtypes.Subtype("tcp")
Subtype = globals.Subtype("tcp")
)
// Target is a target.Target used for tests.
@@ -103,7 +102,7 @@ func (t *Target) GetVersion() uint32 {
return t.Version
}
func (t *Target) GetType() subtypes.Subtype {
func (t *Target) GetType() globals.Subtype {
return Subtype
}
+3 -3
View File
@@ -11,19 +11,19 @@ import (
"fmt"
"strings"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/oplog"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/tcp/store"
"github.com/hashicorp/boundary/internal/types/subtypes"
"google.golang.org/protobuf/proto"
)
const (
defaultTableName = "target_tcp"
Subtype = subtypes.Subtype("tcp")
Subtype = globals.Subtype("tcp")
)
// Target is a resources that represets a networked service
@@ -132,7 +132,7 @@ func (t *Target) Oplog(op oplog.OpType) oplog.Metadata {
return metadata
}
func (t *Target) GetType() subtypes.Subtype {
func (t *Target) GetType() globals.Subtype {
return Subtype
}