Remove Prefix Id and Plugin Name fields (#1563)

* Remove Prefix Id and Plugin Name fields.  Creating catalogs now require either the plugin id or the name of the plugin resource (but not both).  Host Catalogs and Host Sets API resources now have a read only plugin field populated with plugin info.  Host Catalogs have a plugin id field as a top level field.
This commit is contained in:
Todd authored and GitHub committed 2021-09-24 13:16:13 -07:00
1 parent e4e788553f
commit a4ea99047f
51 files changed
+1314 -1052

No files matched your search

@@ -24,7 +24,7 @@ func TestHostCatalogSecret_New(t *testing.T) {
kkms := kms.TestKms(t, conn, wrapper)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test", "test")
plg := host.TestPlugin(t, conn, "test")
cat := TestCatalog(t, conn, prj.GetPublicId(), plg.GetPublicId())
type args struct {
@@ -126,7 +126,7 @@ func TestHostCatalogSecret_Custom_Queries(t *testing.T) {
kkms := kms.TestKms(t, conn, wrapper)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test", "prefix")
plg := host.TestPlugin(t, conn, "test")
cat := TestCatalog(t, conn, prj.GetPublicId(), plg.GetPublicId())
databaseWrapper, err := kkms.GetWrapper(ctx, prj.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
+11 -11
View File
@@ -22,7 +22,7 @@ func TestHostCatalog_Create(t *testing.T) {
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test", "prefix")
plg := host.TestPlugin(t, conn, "test")
type args struct {
pluginId string
@@ -149,7 +149,7 @@ func TestHostCatalog_Create(t *testing.T) {
assert.Emptyf(t, got.PublicId, "PublicId set")
assert.Equal(t, tt.want, got)
id, err := newHostCatalogId(ctx, plg.GetIdPrefix())
id, err := newHostCatalogId(ctx)
assert.NoError(t, err)
tt.want.PublicId = id
@@ -180,31 +180,31 @@ func TestHostCatalog_Create_DuplicateNames(t *testing.T) {
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
_, prj2 := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test1", "prefix1")
plg2 := host.TestPlugin(t, conn, "test2", "prefix2")
plg := host.TestPlugin(t, conn, "test1")
plg2 := host.TestPlugin(t, conn, "test2")
got, err := NewHostCatalog(ctx, prj.GetPublicId(), plg.GetPublicId(), WithName("duplicate"))
require.NoError(t, err)
got.PublicId, err = newHostCatalogId(ctx, plg.GetIdPrefix())
got.PublicId, err = newHostCatalogId(ctx)
require.NoError(t, err)
require.NoError(t, w.Create(ctx, got))
// Can't create another resource with the same name in the same scope
got.PublicId, err = newHostCatalogId(ctx, plg.GetIdPrefix())
got.PublicId, err = newHostCatalogId(ctx)
require.NoError(t, err)
assert.Error(t, w.Create(ctx, got))
// Can't create another resource with same name in same scope even for different plugin
got, err = NewHostCatalog(ctx, prj.GetPublicId(), plg2.GetPublicId(), WithName("duplicate"))
require.NoError(t, err)
got.PublicId, err = newHostCatalogId(ctx, plg2.GetIdPrefix())
got.PublicId, err = newHostCatalogId(ctx)
require.NoError(t, err)
assert.Error(t, w.Create(ctx, got))
// Can create another resource with same name in different scope even for same plugin
got, err = NewHostCatalog(ctx, prj2.GetPublicId(), plg.GetPublicId(), WithName("duplicate"))
require.NoError(t, err)
got.PublicId, err = newHostCatalogId(ctx, plg.GetIdPrefix())
got.PublicId, err = newHostCatalogId(ctx)
require.NoError(t, err)
assert.NoError(t, w.Create(ctx, got))
}
@@ -213,7 +213,7 @@ func TestHostCatalog_Delete(t *testing.T) {
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test", "prefix")
plg := host.TestPlugin(t, conn, "test")
cat := TestCatalog(t, conn, prj.GetPublicId(), plg.GetPublicId())
ignoredCat := TestCatalog(t, conn, prj.GetPublicId(), plg.GetPublicId())
_ = ignoredCat
@@ -264,7 +264,7 @@ func TestHostCatalog_Delete_Cascading(t *testing.T) {
t.Run("delete-scope", func(t *testing.T) {
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "deletescope", "deletescope")
plg := host.TestPlugin(t, conn, "deletescope")
cat := TestCatalog(t, conn, prj.GetPublicId(), plg.GetPublicId())
deleted, err := w.Delete(ctx, prj)
@@ -278,7 +278,7 @@ func TestHostCatalog_Delete_Cascading(t *testing.T) {
t.Run("delete-plugin", func(t *testing.T) {
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "deleteplugin", "deleteplugin")
plg := host.TestPlugin(t, conn, "deleteplugin")
cat := TestCatalog(t, conn, prj.GetPublicId(), plg.GetPublicId())
deleted, err := w.Delete(ctx, plg)
+2 -2
View File
@@ -18,7 +18,7 @@ func TestHostSet_Create(t *testing.T) {
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test", "prefix")
plg := host.TestPlugin(t, conn, "test")
cat := TestCatalog(t, conn, prj.PublicId, plg.GetPublicId())
cat2 := TestCatalog(t, conn, prj.PublicId, plg.GetPublicId())
@@ -168,7 +168,7 @@ func TestHostSet_Create(t *testing.T) {
assert.Emptyf(t, got.PublicId, "PublicId set")
assert.Equal(t, tt.want, got)
id, err := newHostSetId(ctx, plg.GetIdPrefix())
id, err := newHostSetId(ctx)
assert.NoError(t, err)
tt.want.PublicId = id
+6 -9
View File
@@ -2,7 +2,6 @@ package plugin
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
@@ -19,24 +18,22 @@ func init() {
// PublicId prefixes for the resources in the plugin package.
const (
// TODO: Pull these out of being constants and have them derivable at run time.
HostCatalogPrefix = "hcplg"
HostSetPrefix = "hsplg"
HostCatalogPrefix = "hc"
HostSetPrefix = "hs"
Subtype = subtypes.Subtype("plugin")
)
func newHostCatalogId(ctx context.Context, upre string) (string, error) {
prefix := fmt.Sprintf("%s_%s", HostCatalogPrefix, upre)
id, err := db.NewPublicId(prefix)
func newHostCatalogId(ctx context.Context) (string, error) {
id, err := db.NewPublicId(HostCatalogPrefix)
if err != nil {
return "", errors.Wrap(ctx, err, "plugin.newHostCatalogId")
}
return id, nil
}
func newHostSetId(ctx context.Context, upre string) (string, error) {
prefix := fmt.Sprintf("%s_%s", HostSetPrefix, upre)
id, err := db.NewPublicId(prefix)
func newHostSetId(ctx context.Context) (string, error) {
id, err := db.NewPublicId(HostSetPrefix)
if err != nil {
return "", errors.Wrap(ctx, err, "plugin.newHostSetId")
}
@@ -24,8 +24,8 @@ func TestPluginCatalog_ImmutableFields(t *testing.T) {
wrapper := db.TestWrapper(t)
ts := timestamp.Timestamp{Timestamp: &timestamppb.Timestamp{Seconds: 0, Nanos: 0}}
o, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test", "prefix")
plg2 := host.TestPlugin(t, conn, "test2", "prefix2")
plg := host.TestPlugin(t, conn, "test")
plg2 := host.TestPlugin(t, conn, "test2")
new := TestCatalog(t, conn, prj.PublicId, plg.GetPublicId())
tests := []struct {
@@ -100,7 +100,7 @@ func TestPluginSet_ImmutableFields(t *testing.T) {
ts := timestamp.Timestamp{Timestamp: &timestamppb.Timestamp{Seconds: 0, Nanos: 0}}
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := host.TestPlugin(t, conn, "test", "prefix")
plg := host.TestPlugin(t, conn, "test")
cat := TestCatalog(t, conn, prj.PublicId, plg.GetPublicId())
new := TestSet(t, conn, kmsCache, cat, map[string]plgpb.HostPluginServiceServer{
plg.GetPublicId(): &TestPluginServer{},
+57 -41
View File
@@ -9,7 +9,7 @@ import (
"github.com/hashicorp/boundary/internal/host"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/oplog"
hostplg "github.com/hashicorp/boundary/internal/plugin/host"
hostplugin "github.com/hashicorp/boundary/internal/plugin/host"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostcatalogs"
plgpb "github.com/hashicorp/boundary/sdk/pbs/plugin"
"google.golang.org/grpc/codes"
@@ -28,55 +28,45 @@ import (
// not included in the returned *HostCatalog.
//
// Both c.CreateTime and c.UpdateTime are ignored.
func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Option) (*HostCatalog, error) {
func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Option) (*HostCatalog, *hostplugin.Plugin, error) {
const op = "plugin.(Repository).CreateCatalog"
if c == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil HostCatalog")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "nil HostCatalog")
}
if c.HostCatalog == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostCatalog")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostCatalog")
}
if c.ScopeId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
}
if c.PublicId != "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
}
if c.PluginId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no plugin id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no plugin id")
}
if c.Attributes == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil attributes")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "nil attributes")
}
c = c.clone()
plg := hostplg.NewPlugin("", "")
plg.PublicId = c.PluginId
if err := r.reader.LookupByPublicId(ctx, plg); err != nil {
if errors.IsNotFoundError(err) {
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidParameter), errors.WithMsg(fmt.Sprintf("can't find plugin with id: %q", c.GetPluginId())))
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("unable to get host plugin with id %q", c.PluginId)))
}
id, err := newHostCatalogId(ctx, plg.GetIdPrefix())
id, err := newHostCatalogId(ctx)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
c.PublicId = id
plgClient, ok := r.plugins[plg.GetPublicId()]
plgClient, ok := r.plugins[c.GetPluginId()]
if !ok || plgClient == nil {
return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("expected plugin %q not available", plg.GetPluginName()))
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("plugin %q not available", c.GetPluginId()))
}
plgHc, err := toPluginCatalog(ctx, c)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
plgResp, err := plgClient.OnCreateCatalog(ctx, &plgpb.OnCreateCatalogRequest{Catalog: plgHc})
if err != nil {
if status.Code(err) != codes.Unimplemented {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
}
@@ -84,20 +74,20 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
if plgResp != nil && plgResp.GetPersisted().GetSecrets() != nil {
hcSecret, err = newHostCatalogSecret(ctx, id, plgResp.GetPersisted().GetSecrets())
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
dbWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeDatabase)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get db wrapper"))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get db wrapper"))
}
if err := hcSecret.encrypt(ctx, dbWrapper); err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
}
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
}
var newHostCatalog *HostCatalog
@@ -142,39 +132,47 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
if err != nil {
if errors.IsUniqueError(err) {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s: name %s already exists", c.ScopeId, c.Name)))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s: name %s already exists", c.ScopeId, c.Name)))
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s", c.ScopeId)))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s", c.ScopeId)))
}
return newHostCatalog, nil
plg, err := r.getPlugin(ctx, newHostCatalog.GetPluginId())
if err != nil {
return nil, nil, errors.Wrap(ctx, err, op)
}
return newHostCatalog, plg, nil
}
// LookupCatalog returns the HostCatalog for id. Returns nil, nil if no
// HostCatalog is found for id.
func (r *Repository) LookupCatalog(ctx context.Context, id string, _ ...Option) (*HostCatalog, error) {
func (r *Repository) LookupCatalog(ctx context.Context, id string, _ ...Option) (*HostCatalog, *hostplugin.Plugin, error) {
const op = "plugin.(Repository).LookupCatalog"
if id == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
}
c, err := r.getCatalog(ctx, id)
if err != nil {
if errors.IsNotFoundError(err) {
return nil, nil
return nil, nil, nil
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
}
return c, nil
plg, err := r.getPlugin(ctx, c.GetPluginId())
if err != nil {
return nil, nil, errors.Wrap(ctx, err, op)
}
return c, plg, nil
}
// ListCatalogs returns a slice of HostCatalogs for the scope IDs. WithLimit is the only option supported.
func (r *Repository) ListCatalogs(ctx context.Context, scopeIds []string, opt ...host.Option) ([]*HostCatalog, error) {
func (r *Repository) ListCatalogs(ctx context.Context, scopeIds []string, opt ...host.Option) ([]*HostCatalog, []*hostplugin.Plugin, error) {
const op = "plugin.(Repository).ListCatalogs"
if len(scopeIds) == 0 {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
}
opts, err := host.GetOpts(opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
limit := r.defaultLimit
if opts.WithLimit != 0 {
@@ -183,9 +181,17 @@ func (r *Repository) ListCatalogs(ctx context.Context, scopeIds []string, opt ..
}
var hostCatalogs []*HostCatalog
if err := r.reader.SearchWhere(ctx, &hostCatalogs, "scope_id in (?)", []interface{}{scopeIds}, db.WithLimit(limit)); err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
return hostCatalogs, nil
plgIds := make([]string, 0, len(hostCatalogs))
for _, c := range hostCatalogs {
plgIds = append(plgIds, c.PluginId)
}
var plgs []*hostplugin.Plugin
if err := r.reader.SearchWhere(ctx, &plgs, "public_id in (?)", []interface{}{plgIds}); err != nil {
return nil, nil, errors.Wrap(ctx, err, op)
}
return hostCatalogs, plgs, nil
}
// getCatalog retrieves the *HostCatalog with the provided id. If it is not found or there
@@ -200,6 +206,16 @@ func (r *Repository) getCatalog(ctx context.Context, id string) (*HostCatalog, e
return c, nil
}
func (r *Repository) getPlugin(ctx context.Context, id string) (*hostplugin.Plugin, error) {
const op = "plugin.(Repository).getPlugin"
c := hostplugin.NewPlugin()
c.PublicId = id
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
}
return c, nil
}
// getPersistedDataForCatalog returns the persisted data for a catalog if
// present. c must have a valid Public Id and Scope Id set.
// TODO: consider merging the functions for getting catalog and persisted data into a view.
@@ -28,8 +28,8 @@ func TestRepository_CreateCatalog(t *testing.T) {
rw := db.New(conn)
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := hostplg.TestPlugin(t, conn, "test", "test")
unimplementedPlugin := hostplg.TestPlugin(t, conn, "unimplemented", "unimplemented")
plg := hostplg.TestPlugin(t, conn, "test")
unimplementedPlugin := hostplg.TestPlugin(t, conn, "unimplemented")
// gotPluginAttrs tracks which attributes a plugin has received through a closure and can be compared in the
// test against the expected values sent to the plugin.
@@ -250,7 +250,7 @@ func TestRepository_CreateCatalog(t *testing.T) {
repo, err := NewRepository(rw, rw, kmsCache, plgm)
assert.NoError(err)
assert.NotNil(repo)
got, err := repo.CreateCatalog(ctx, tt.in, tt.opts...)
got, _, err := repo.CreateCatalog(ctx, tt.in, tt.opts...)
if tt.wantIsErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Nil(got)
@@ -311,7 +311,7 @@ func TestRepository_CreateCatalog(t *testing.T) {
},
}
got, err := repo.CreateCatalog(context.Background(), in)
got, _, err := repo.CreateCatalog(context.Background(), in)
assert.NoError(err)
assert.NotNil(got)
assertPluginBasedPublicId(t, HostCatalogPrefix, got.PublicId)
@@ -320,7 +320,7 @@ func TestRepository_CreateCatalog(t *testing.T) {
assert.Equal(in.Description, got.Description)
assert.Equal(got.CreateTime, got.UpdateTime)
got2, err := repo.CreateCatalog(context.Background(), in)
got2, _, err := repo.CreateCatalog(context.Background(), in)
assert.Truef(errors.Match(errors.T(errors.NotUnique), err), "want err code: %v got err: %v", errors.NotUnique, err)
assert.Nil(got2)
})
@@ -342,7 +342,7 @@ func TestRepository_CreateCatalog(t *testing.T) {
in2 := in.clone()
in.ScopeId = prj.GetPublicId()
got, err := repo.CreateCatalog(context.Background(), in)
got, _, err := repo.CreateCatalog(context.Background(), in)
assert.NoError(err)
assert.NotNil(got)
assertPluginBasedPublicId(t, HostCatalogPrefix, got.PublicId)
@@ -352,7 +352,7 @@ func TestRepository_CreateCatalog(t *testing.T) {
assert.Equal(got.CreateTime, got.UpdateTime)
in2.ScopeId = org.GetPublicId()
got2, err := repo.CreateCatalog(context.Background(), in2)
got2, _, err := repo.CreateCatalog(context.Background(), in2)
assert.NoError(err)
assert.NotNil(got2)
assertPluginBasedPublicId(t, HostCatalogPrefix, got2.PublicId)
@@ -367,7 +367,7 @@ func assertPluginBasedPublicId(t *testing.T, prefix, actual string) {
t.Helper()
assert.NotEmpty(t, actual)
parts := strings.Split(actual, "_")
assert.Equalf(t, 3, len(parts), "want two '_' in PublicId, got %d in %q", len(parts), actual)
assert.Equalf(t, 2, len(parts), "want 1 '_' in PublicId, got %d in %q", len(parts), actual)
assert.Equalf(t, prefix, parts[0], "PublicId want prefix: %q, got: %q in %q", prefix, parts[0], actual)
}
@@ -377,12 +377,12 @@ func TestRepository_LookupCatalog(t *testing.T) {
rw := db.New(conn)
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
plg := hostplg.TestPlugin(t, conn, "test", "test")
plg := hostplg.TestPlugin(t, conn, "test")
plgm := map[string]plgpb.HostPluginServiceServer{
plg.GetPublicId(): &TestPluginServer{},
}
cat := TestCatalog(t, conn, prj.PublicId, plg.GetPublicId())
badId, err := newHostCatalogId(ctx, plg.GetIdPrefix())
badId, err := newHostCatalogId(ctx)
assert.NoError(t, err)
assert.NotNil(t, badId)
@@ -419,7 +419,7 @@ func TestRepository_LookupCatalog(t *testing.T) {
assert.NoError(err)
assert.NotNil(repo)
got, err := repo.LookupCatalog(ctx, tt.id)
got, _, err := repo.LookupCatalog(ctx, tt.id)
if tt.wantErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantErr), err), "want err: %q got: %q", tt.wantErr, err)
return
@@ -443,7 +443,7 @@ func TestRepository_ListCatalogs_Multiple_Scopes(t *testing.T) {
wrapper := db.TestWrapper(t)
rw := db.New(conn)
kms := kms.TestKms(t, conn, wrapper)
plg := hostplg.TestPlugin(t, conn, "test", "test")
plg := hostplg.TestPlugin(t, conn, "test")
plgm := map[string]plgpb.HostPluginServiceServer{
plg.GetPublicId(): &TestPluginServer{},
}
@@ -463,7 +463,8 @@ func TestRepository_ListCatalogs_Multiple_Scopes(t *testing.T) {
}
}
got, err := repo.ListCatalogs(context.Background(), projs)
got, plgs, err := repo.ListCatalogs(context.Background(), projs)
require.NoError(t, err)
assert.Equal(t, total, len(got))
assert.Contains(t, plgs, plg)
}
+55 -49
View File
@@ -13,7 +13,7 @@ import (
"github.com/hashicorp/boundary/internal/host"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/oplog"
hostplg "github.com/hashicorp/boundary/internal/plugin/host"
hostplugin "github.com/hashicorp/boundary/internal/plugin/host"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostsets"
plgpb "github.com/hashicorp/boundary/sdk/pbs/plugin"
"google.golang.org/grpc/codes"
@@ -29,70 +29,64 @@ import (
//
// Both s.Name and s.Description are optional. If s.Name is set, it must be
// unique within s.CatalogId.
func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet, _ ...Option) (*HostSet, error) {
func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet, _ ...Option) (*HostSet, *hostplugin.Plugin, error) {
const op = "plugin.(Repository).CreateSet"
if s == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil HostSet")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "nil HostSet")
}
if s.HostSet == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostSet")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded HostSet")
}
if s.CatalogId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
}
if s.PublicId != "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
}
if scopeId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
}
if s.Attributes == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil attributes")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "nil attributes")
}
s = s.clone()
c, err := r.getCatalog(ctx, s.CatalogId)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("looking up catalog"))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("looking up catalog"))
}
per, err := r.getPersistedDataForCatalog(ctx, c)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("looking up persisted data"))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("looking up persisted data"))
}
plg := hostplg.NewPlugin("", "")
plg.PublicId = c.GetPluginId()
if err := r.reader.LookupByPublicId(ctx, plg); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get host plugin"))
}
id, err := newHostSetId(ctx, plg.GetIdPrefix())
id, err := newHostSetId(ctx)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
s.PublicId = id
plgClient, ok := r.plugins[plg.GetPublicId()]
plgClient, ok := r.plugins[c.GetPluginId()]
if !ok || plgClient == nil {
return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("plugin with plugin name %q not available", plg.GetPluginName()))
return nil, nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("plugin %q not available", c.GetPluginId()))
}
plgHc, err := toPluginCatalog(ctx, c)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
plgHs, err := toPluginSet(ctx, s)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
if _, err := plgClient.OnCreateSet(ctx, &plgpb.OnCreateSetRequest{Catalog: plgHc, Set: plgHs, Persisted: per}); err != nil {
if status.Code(err) != codes.Unimplemented {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
}
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
}
var preferredEndpoints []interface{}
@@ -101,7 +95,7 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
for i, e := range s.PreferredEndpoints {
obj, err := host.NewPreferredEndpoint(ctx, s.PublicId, uint32(i+1), e)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
preferredEndpoints = append(preferredEndpoints, obj)
}
@@ -146,67 +140,71 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
if err != nil {
if errors.IsUniqueError(err) {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", s.CatalogId, s.Name)))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s: name %s already exists", s.CatalogId, s.Name)))
}
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", s.CatalogId)))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in catalog: %s", s.CatalogId)))
}
return returnedHostSet, nil
plg, err := r.getPlugin(ctx, c.GetPluginId())
if err != nil {
return nil, nil, errors.Wrap(ctx, err, op)
}
return returnedHostSet, plg, nil
}
// LookupSet will look up a host set in the repository and return the host
// set. If the host set is not found, it will return nil, nil.
// All options are ignored.
func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...host.Option) (*HostSet, error) {
func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...host.Option) (*HostSet, *hostplugin.Plugin, error) {
const op = "plugin.(Repository).LookupSet"
if publicId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
}
sets, err := r.getSets(ctx, publicId, "", opt...)
sets, plg, err := r.getSets(ctx, publicId, "", opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
switch {
case len(sets) == 0:
return nil, nil // not an error to return no rows for a "lookup"
return nil, nil, nil // not an error to return no rows for a "lookup"
case len(sets) > 1:
return nil, errors.New(ctx, errors.NotSpecificIntegrity, op, fmt.Sprintf("%s matched more than 1 ", publicId))
return nil, nil, errors.New(ctx, errors.NotSpecificIntegrity, op, fmt.Sprintf("%s matched more than 1 ", publicId))
default:
return sets[0], nil
return sets[0], plg, nil
}
}
// ListSets returns a slice of HostSets for the catalogId. WithLimit is the
// only option supported.
func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...host.Option) ([]*HostSet, error) {
func (r *Repository) ListSets(ctx context.Context, catalogId string, opt ...host.Option) ([]*HostSet, *hostplugin.Plugin, error) {
const op = "plugin.(Repository).ListSets"
if catalogId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing catalog id")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing catalog id")
}
sets, err := r.getSets(ctx, "", catalogId, opt...)
sets, plg, err := r.getSets(ctx, "", catalogId, opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
return sets, nil
return sets, plg, nil
}
func (r *Repository) getSets(ctx context.Context, publicId string, catalogId string, opt ...host.Option) ([]*HostSet, error) {
func (r *Repository) getSets(ctx context.Context, publicId string, catalogId string, opt ...host.Option) ([]*HostSet, *hostplugin.Plugin, error) {
const op = "plugin.(Repository).getSets"
const aggregateDelimiter = "|"
const priorityDelimiter = "="
if publicId == "" && catalogId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing search criteria: both host set id and catalog id are empty")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing search criteria: both host set id and catalog id are empty")
}
if publicId != "" && catalogId != "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "searching for both a host set id and a catalog id is not supported")
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "searching for both a host set id and a catalog id is not supported")
}
opts, err := host.GetOpts(opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
return nil, nil, errors.Wrap(ctx, err, op)
}
limit := r.defaultLimit
@@ -237,12 +235,13 @@ func (r *Repository) getSets(ctx context.Context, publicId string, catalogId str
var aggHostSets []*hostSetAgg
if err := r.reader.SearchWhere(ctx, &aggHostSets, where, args, dbArgs...); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", publicId)))
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in %s", publicId)))
}
if len(aggHostSets) == 0 {
return nil, nil
return nil, nil, nil
}
plgId := aggHostSets[0].PluginId
sets := make([]*HostSet, 0, len(aggHostSets))
for _, agg := range aggHostSets {
@@ -285,7 +284,7 @@ func (r *Repository) getSets(ctx context.Context, publicId string, catalogId str
return indexi < indexj
})
if sortErr != nil {
return nil, sortErr
return nil, nil, sortErr
}
for i, ep := range eps {
// At this point they're in the correct order, but we still
@@ -298,8 +297,15 @@ func (r *Repository) getSets(ctx context.Context, publicId string, catalogId str
}
sets = append(sets, hs)
}
var plg *hostplugin.Plugin
if plgId != "" {
plg, err = r.getPlugin(ctx, plgId)
if err != nil {
return nil, nil, errors.Wrap(ctx, err, op)
}
}
return sets, nil
return sets, plg, nil
}
// hostSetAgg is a view that aggregates the host set's value objects in to
@@ -31,8 +31,8 @@ func TestRepository_CreateSet(t *testing.T) {
kms := kms.TestKms(t, conn, wrapper)
iamRepo := iam.TestRepo(t, conn, wrapper)
_, prj := iam.TestScopes(t, iamRepo)
plg := hostplg.TestPlugin(t, conn, "create", "create")
unimplementedPlugin := hostplg.TestPlugin(t, conn, "unimplemented", "unimplemented")
plg := hostplg.TestPlugin(t, conn, "create")
unimplementedPlugin := hostplg.TestPlugin(t, conn, "unimplemented")
var pluginReceivedAttrs *structpb.Struct
plgm := map[string]plgpb.HostPluginServiceServer{
@@ -209,7 +209,7 @@ func TestRepository_CreateSet(t *testing.T) {
repo, err := NewRepository(rw, rw, kms, plgm)
require.NoError(err)
require.NotNil(repo)
got, err := repo.CreateSet(context.Background(), prj.GetPublicId(), tt.in, tt.opts...)
got, plgInfo, err := repo.CreateSet(context.Background(), prj.GetPublicId(), tt.in, tt.opts...)
if tt.wantIsErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Nil(got)
@@ -226,6 +226,7 @@ func TestRepository_CreateSet(t *testing.T) {
wantedPluginAttributes := &structpb.Struct{}
require.NoError(proto.Unmarshal(tt.want.Attributes, wantedPluginAttributes))
assert.Empty(cmp.Diff(wantedPluginAttributes, pluginReceivedAttrs, protocmp.Transform()))
assert.Empty(cmp.Diff(plgInfo, plg, protocmp.Transform()))
assert.NoError(db.TestVerifyOplog(t, rw, got.GetPublicId(), db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second)))
})
@@ -248,7 +249,7 @@ func TestRepository_CreateSet(t *testing.T) {
},
}
got, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in)
got, _, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in)
require.NoError(err)
require.NotNil(got)
assert.True(strings.HasPrefix(got.GetPublicId(), HostSetPrefix))
@@ -257,7 +258,7 @@ func TestRepository_CreateSet(t *testing.T) {
assert.Equal(in.Description, got.GetDescription())
assert.Equal(got.GetCreateTime(), got.GetUpdateTime())
got2, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in)
got2, _, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in)
assert.Truef(errors.Match(errors.T(errors.NotUnique), err), "want err code: %v got err: %v", errors.NotUnique, err)
assert.Nil(got2)
})
@@ -281,7 +282,7 @@ func TestRepository_CreateSet(t *testing.T) {
in2 := in.clone()
in.CatalogId = catalogA.PublicId
got, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in)
got, _, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in)
require.NoError(err)
require.NotNil(got)
assert.True(strings.HasPrefix(got.GetPublicId(), HostSetPrefix))
@@ -291,7 +292,7 @@ func TestRepository_CreateSet(t *testing.T) {
assert.Equal(got.GetCreateTime(), got.GetUpdateTime())
in2.CatalogId = catalogB.PublicId
got2, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in2)
got2, _, err := repo.CreateSet(context.Background(), prj.GetPublicId(), in2)
require.NoError(err)
require.NotNil(got2)
assert.True(strings.HasPrefix(got.GetPublicId(), HostSetPrefix))
@@ -310,7 +311,7 @@ func TestRepository_LookupSet(t *testing.T) {
kms := kms.TestKms(t, conn, wrapper)
iamRepo := iam.TestRepo(t, conn, wrapper)
_, prj := iam.TestScopes(t, iamRepo)
plg := hostplg.TestPlugin(t, conn, "lookup", "lookup")
plg := hostplg.TestPlugin(t, conn, "lookup")
plgm := map[string]plgpb.HostPluginServiceServer{
plg.GetPublicId(): &TestPluginServer{},
}
@@ -319,7 +320,7 @@ func TestRepository_LookupSet(t *testing.T) {
hostSet := TestSet(t, conn, kms, catalog, map[string]plgpb.HostPluginServiceServer{
plg.GetPublicId(): &TestPluginServer{},
})
hostSetId, err := newHostSetId(ctx, plg.GetIdPrefix())
hostSetId, err := newHostSetId(ctx)
require.NoError(t, err)
tests := []struct {
@@ -350,7 +351,7 @@ func TestRepository_LookupSet(t *testing.T) {
repo, err := NewRepository(rw, rw, kms, plgm)
assert.NoError(err)
require.NotNil(repo)
got, err := repo.LookupSet(ctx, tt.in)
got, _, err := repo.LookupSet(ctx, tt.in)
if tt.wantIsErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Nil(got)
@@ -372,7 +373,7 @@ func TestRepository_ListSets(t *testing.T) {
iamRepo := iam.TestRepo(t, conn, wrapper)
_, prj := iam.TestScopes(t, iamRepo)
plg := hostplg.TestPlugin(t, conn, "list", "list")
plg := hostplg.TestPlugin(t, conn, "list")
plgm := map[string]plgpb.HostPluginServiceServer{
plg.GetPublicId(): &TestPluginServer{},
}
@@ -385,6 +386,8 @@ func TestRepository_ListSets(t *testing.T) {
TestSet(t, conn, kms, catalogA, plgm),
}
printoutTable(t, rw)
tests := []struct {
name string
in string
@@ -415,7 +418,7 @@ func TestRepository_ListSets(t *testing.T) {
repo, err := NewRepository(rw, rw, kms, plgm)
assert.NoError(err)
require.NotNil(repo)
got, err := repo.ListSets(context.Background(), tt.in, tt.opts...)
got, gotPlg, err := repo.ListSets(context.Background(), tt.in, tt.opts...)
if tt.wantIsErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Nil(got)
@@ -427,6 +430,9 @@ func TestRepository_ListSets(t *testing.T) {
protocmp.Transform(),
}
assert.Empty(cmp.Diff(tt.want, got, opts...))
if got != nil {
assert.Empty(cmp.Diff(plg, gotPlg, protocmp.Transform()))
}
})
}
}
@@ -439,7 +445,7 @@ func TestRepository_ListSets_Limits(t *testing.T) {
iamRepo := iam.TestRepo(t, conn, wrapper)
_, prj := iam.TestScopes(t, iamRepo)
plg := hostplg.TestPlugin(t, conn, "listlimit", "listlimit")
plg := hostplg.TestPlugin(t, conn, "listlimit")
plgm := map[string]plgpb.HostPluginServiceServer{
plg.GetPublicId(): &TestPluginServer{},
}
@@ -501,9 +507,19 @@ func TestRepository_ListSets_Limits(t *testing.T) {
repo, err := NewRepository(rw, rw, kms, plgm, tt.repoOpts...)
assert.NoError(err)
require.NotNil(repo)
got, err := repo.ListSets(context.Background(), hostSets[0].CatalogId, tt.listOpts...)
got, gotPlg, err := repo.ListSets(context.Background(), hostSets[0].CatalogId, tt.listOpts...)
require.NoError(err)
assert.Len(got, tt.wantLen)
assert.Empty(cmp.Diff(plg, gotPlg, protocmp.Transform()))
})
}
}
func printoutTable(t *testing.T, rw *db.Db) {
ctx := context.Background()
hsas := []*hostSetAgg{}
require.NoError(t, rw.SearchWhere(ctx, &hsas, "", nil))
for _, hs := range hsas {
t.Logf("%#v", hs)
}
}
+5 -5
View File
@@ -25,11 +25,11 @@ func TestCatalog(t *testing.T, conn *gorm.DB, scopeId, pluginId string, opt ...O
require.NoError(t, err)
assert.NotNil(t, cat)
plg := host.NewPlugin("", "")
plg := host.NewPlugin()
plg.PublicId = pluginId
require.NoError(t, w.LookupByPublicId(ctx, plg))
id, err := newHostCatalogId(ctx, plg.GetIdPrefix())
id, err := newHostCatalogId(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, id)
cat.PublicId = id
@@ -54,15 +54,15 @@ func TestSet(t *testing.T, conn *gorm.DB, kmsCache *kms.Kms, hc *HostCatalog, pl
require.NoError(err)
require.NotNil(set)
plg := host.NewPlugin("", "")
plg := host.NewPlugin()
plg.PublicId = hc.GetPluginId()
require.NoError(rw.LookupByPublicId(ctx, plg))
id, err := newHostSetId(ctx, plg.GetIdPrefix())
id, err := newHostSetId(ctx)
require.NoError(err)
require.NotEmpty(id)
set, err = repo.CreateSet(ctx, hc.ScopeId, set, opt...)
set, _, err = repo.CreateSet(ctx, hc.ScopeId, set, opt...)
require.NoError(err)
return set
+4 -6
View File
@@ -1,8 +1,6 @@
package plugin
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/boundary/internal/db"
@@ -22,13 +20,13 @@ func Test_TestCatalogs(t *testing.T) {
require.NotNil(proj)
assert.NotEmpty(proj.GetPublicId())
plg := host.TestPlugin(t, conn, "test", "prefix")
plg := host.TestPlugin(t, conn, "test")
require.NotNil(plg)
assert.NotEmpty(plg.GetPublicId())
cs := TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId(), WithName("foo"), WithDescription("bar"))
assert.NotEmpty(cs.GetPublicId())
assert.True(strings.HasPrefix(cs.GetPublicId(), fmt.Sprintf("%s_%s", HostCatalogPrefix, "prefix")))
db.AssertPublicId(t, HostCatalogPrefix, cs.GetPublicId())
assert.Equal("foo", cs.GetName())
assert.Equal("bar", cs.GetDescription())
}
@@ -42,14 +40,14 @@ func Test_TestSet(t *testing.T) {
require.NotNil(prj)
assert.NotEmpty(prj.GetPublicId())
plg := host.TestPlugin(t, conn, "test", "prefix")
plg := host.TestPlugin(t, conn, "test")
require.NotNil(plg)
assert.NotEmpty(plg.GetPublicId())
c := TestCatalog(t, conn, prj.GetPublicId(), plg.GetPublicId())
set := TestSet(t, conn, kmsCache, c, map[string]plgpb.HostPluginServiceServer{plg.GetPublicId(): &TestPluginServer{}}, WithName("foo"), WithDescription("bar"))
assert.NotEmpty(set.GetPublicId())
assert.True(strings.HasPrefix(set.GetPublicId(), fmt.Sprintf("%s_%s", HostSetPrefix, "prefix")))
db.AssertPublicId(t, HostSetPrefix, set.GetPublicId())
assert.Equal("foo", set.GetName())
assert.Equal("bar", set.GetDescription())
}