Creating plugin catalog handler (#1521)
The handler only supports create, lookup, and list for plugin based host catalogs.
This commit is contained in:
1 parent
e07792cf74
commit
f2236482ad
5 files changed
+642
-95
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
pluginhost "github.com/hashicorp/boundary/internal/host/plugin"
|
||||
"github.com/hashicorp/boundary/internal/host/static"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
hostplugin "github.com/hashicorp/boundary/internal/plugin/host"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/session"
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
@@ -21,6 +22,7 @@ type (
|
||||
ServersRepoFactory func() (*servers.Repository, error)
|
||||
StaticRepoFactory func() (*static.Repository, error)
|
||||
PluginHostRepoFactory func() (*pluginhost.Repository, error)
|
||||
HostPluginRepoFactory func() (*hostplugin.Repository, error)
|
||||
SessionRepoFactory func() (*session.Repository, error)
|
||||
TargetRepoFactory func() (*target.Repository, error)
|
||||
)
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/hashicorp/boundary/internal/plugin/host"
|
||||
"github.com/hashicorp/boundary/internal/scheduler"
|
||||
"github.com/hashicorp/boundary/internal/scheduler/job"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
@@ -55,6 +56,7 @@ type Controller struct {
|
||||
SessionRepoFn common.SessionRepoFactory
|
||||
StaticHostRepoFn common.StaticRepoFactory
|
||||
PluginHostRepoFn common.PluginHostRepoFactory
|
||||
HostPluginRepoFn common.HostPluginRepoFactory
|
||||
TargetRepoFn common.TargetRepoFactory
|
||||
|
||||
scheduler *scheduler.Scheduler
|
||||
@@ -136,6 +138,9 @@ func New(ctx context.Context, conf *Config) (*Controller, error) {
|
||||
c.PluginHostRepoFn = func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(dbase, dbase, c.kms)
|
||||
}
|
||||
c.HostPluginRepoFn = func() (*host.Repository, error) {
|
||||
return host.NewRepository(dbase, dbase, c.kms)
|
||||
}
|
||||
c.AuthTokenRepoFn = func() (*authtoken.Repository, error) {
|
||||
return authtoken.NewRepository(dbase, dbase, c.kms,
|
||||
authtoken.WithTokenTimeToLiveDuration(c.conf.RawConfig.Controller.AuthTokenTimeToLiveDuration),
|
||||
|
||||
@@ -85,7 +85,7 @@ func handleGrpcGateway(c *Controller, props HandlerProperties) (http.Handler, er
|
||||
runtime.WithErrorHandler(handlers.ErrorHandler()),
|
||||
runtime.WithForwardResponseOption(handlers.OutgoingInterceptor),
|
||||
)
|
||||
hcs, err := host_catalogs.NewService(c.StaticHostRepoFn, c.IamRepoFn)
|
||||
hcs, err := host_catalogs.NewService(c.StaticHostRepoFn, c.PluginHostRepoFn, c.HostPluginRepoFn, c.IamRepoFn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create host catalog handler service: %w", err)
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package host_catalogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/boundary/internal/host"
|
||||
"github.com/hashicorp/boundary/internal/host/plugin"
|
||||
"github.com/hashicorp/boundary/internal/host/static"
|
||||
"github.com/hashicorp/boundary/internal/host/static/store"
|
||||
"github.com/hashicorp/boundary/internal/perms"
|
||||
@@ -23,6 +26,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostcatalogs"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
@@ -61,23 +65,31 @@ func init() {
|
||||
type Service struct {
|
||||
pbs.UnimplementedHostCatalogServiceServer
|
||||
|
||||
staticRepoFn common.StaticRepoFactory
|
||||
iamRepoFn common.IamRepoFactory
|
||||
staticRepoFn common.StaticRepoFactory
|
||||
pluginHostRepoFn common.PluginHostRepoFactory
|
||||
pluginRepoFn common.HostPluginRepoFactory
|
||||
iamRepoFn common.IamRepoFactory
|
||||
}
|
||||
|
||||
var _ pbs.HostCatalogServiceServer = Service{}
|
||||
|
||||
// NewService returns a host catalog Service which handles host catalog related requests to boundary and uses the provided
|
||||
// repositories for storage and retrieval.
|
||||
func NewService(repoFn common.StaticRepoFactory, iamRepoFn common.IamRepoFactory) (Service, error) {
|
||||
func NewService(repoFn common.StaticRepoFactory, pluginHostRepoFn common.PluginHostRepoFactory, hostPluginRepoFn common.HostPluginRepoFactory, iamRepoFn common.IamRepoFactory) (Service, error) {
|
||||
const op = "host_catalogs.NewService"
|
||||
if repoFn == nil {
|
||||
return Service{}, errors.NewDeprecated(errors.InvalidParameter, op, "missing static repository")
|
||||
}
|
||||
if pluginHostRepoFn == nil {
|
||||
return Service{}, errors.NewDeprecated(errors.InvalidParameter, op, "missing plugin host repository")
|
||||
}
|
||||
if hostPluginRepoFn == nil {
|
||||
return Service{}, errors.NewDeprecated(errors.InvalidParameter, op, "missing host plugin repository")
|
||||
}
|
||||
if iamRepoFn == nil {
|
||||
return Service{}, errors.NewDeprecated(errors.InvalidParameter, op, "missing iam repository")
|
||||
}
|
||||
return Service{staticRepoFn: repoFn, iamRepoFn: iamRepoFn}, nil
|
||||
return Service{staticRepoFn: repoFn, pluginHostRepoFn: pluginHostRepoFn, pluginRepoFn: hostPluginRepoFn, iamRepoFn: iamRepoFn}, nil
|
||||
}
|
||||
|
||||
func (s Service) ListHostCatalogs(ctx context.Context, req *pbs.ListHostCatalogsRequest) (*pbs.ListHostCatalogsResponse, error) {
|
||||
@@ -315,22 +327,40 @@ func (s Service) DeleteHostCatalog(ctx context.Context, req *pbs.DeleteHostCatal
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s Service) getFromRepo(ctx context.Context, id string) (*static.HostCatalog, error) {
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func (s Service) getFromRepo(ctx context.Context, id string) (host.Catalog, error) {
|
||||
var cat host.Catalog
|
||||
switch host.SubtypeFromId(id) {
|
||||
case static.Subtype:
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hc, err := repo.LookupCatalog(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if hc == nil {
|
||||
return nil, handlers.NotFoundErrorf("Host Catalog %q doesn't exist.", id)
|
||||
}
|
||||
cat = hc
|
||||
case plugin.Subtype:
|
||||
repo, err := s.pluginHostRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hc, err := repo.LookupCatalog(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if hc == nil {
|
||||
return nil, handlers.NotFoundErrorf("Host Catalog %q doesn't exist.", id)
|
||||
}
|
||||
cat = hc
|
||||
}
|
||||
hc, err := repo.LookupCatalog(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if hc == nil {
|
||||
return nil, handlers.NotFoundErrorf("Host Catalog %q doesn't exist.", id)
|
||||
}
|
||||
return hc, nil
|
||||
return cat, nil
|
||||
}
|
||||
|
||||
func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]*static.HostCatalog, error) {
|
||||
func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]host.Catalog, error) {
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -339,21 +369,29 @@ func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]*static
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ul, nil
|
||||
var res []host.Catalog
|
||||
for _, c := range ul {
|
||||
res = append(res, c)
|
||||
}
|
||||
pluginRepo, err := s.pluginHostRepoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pl, err := pluginRepo.ListCatalogs(ctx, scopeIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, c := range pl {
|
||||
res = append(res, c)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s Service) createInRepo(ctx context.Context, projId string, item *pb.HostCatalog) (*static.HostCatalog, error) {
|
||||
func (s Service) createStaticInRepo(ctx context.Context, projId string, item *pb.HostCatalog) (*static.HostCatalog, error) {
|
||||
const op = "host_catalogs.(Service).createInRepo"
|
||||
var opts []static.Option
|
||||
if item.GetName() != nil {
|
||||
opts = append(opts, static.WithName(item.GetName().GetValue()))
|
||||
}
|
||||
if item.GetDescription() != nil {
|
||||
opts = append(opts, static.WithDescription(item.GetDescription().GetValue()))
|
||||
}
|
||||
h, err := static.NewHostCatalog(projId, opts...)
|
||||
h, err := toStorageStaticCatalog(ctx, projId, item)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to build host catalog for creation"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to build catalog for creation"))
|
||||
}
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
@@ -369,20 +407,51 @@ func (s Service) createInRepo(ctx context.Context, projId string, item *pb.HostC
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s Service) createPluginInRepo(ctx context.Context, projId string, item *pb.HostCatalog) (*plugin.HostCatalog, error) {
|
||||
const op = "host_catalogs.(Service).createInRepo"
|
||||
plgRepo, err := s.pluginRepoFn()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
plg, err := plgRepo.LookupPluginByPluginName(ctx, item.GetType())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
h, err := toStoragePluginCatalog(ctx, projId, plg.GetPublicId(), item)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to build catalog for creation"))
|
||||
}
|
||||
repo, err := s.pluginHostRepoFn()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
out, err := repo.CreateCatalog(ctx, h)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create host catalog"))
|
||||
}
|
||||
if out == nil {
|
||||
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to create host catalog but no error returned from repository.")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s Service) createInRepo(ctx context.Context, projId string, item *pb.HostCatalog) (hc host.Catalog, err error) {
|
||||
switch host.SubtypeFromType(item.GetType()) {
|
||||
case static.Subtype:
|
||||
hc, err = s.createStaticInRepo(ctx, projId, item)
|
||||
default:
|
||||
hc, err = s.createPluginInRepo(ctx, projId, item)
|
||||
}
|
||||
return hc, err
|
||||
}
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, projId, id string, mask []string, item *pb.HostCatalog) (*static.HostCatalog, error) {
|
||||
const op = "host_catalogs.(Service).updateInRepo"
|
||||
var opts []static.Option
|
||||
if desc := item.GetDescription(); desc != nil {
|
||||
opts = append(opts, static.WithDescription(desc.GetValue()))
|
||||
}
|
||||
if name := item.GetName(); name != nil {
|
||||
opts = append(opts, static.WithName(name.GetValue()))
|
||||
}
|
||||
version := item.GetVersion()
|
||||
h, err := static.NewHostCatalog(projId, opts...)
|
||||
h, err := toStorageStaticCatalog(ctx, projId, item)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to build host catalog for update"))
|
||||
}
|
||||
version := item.GetVersion()
|
||||
h.PublicId = id
|
||||
dbMask := maskManager.Translate(mask)
|
||||
if len(dbMask) == 0 {
|
||||
@@ -438,28 +507,49 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
|
||||
return res
|
||||
}
|
||||
default:
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
res.Error = err
|
||||
return res
|
||||
switch host.SubtypeFromId(id) {
|
||||
case static.Subtype:
|
||||
repo, err := s.staticRepoFn()
|
||||
if err != nil {
|
||||
res.Error = err
|
||||
return res
|
||||
}
|
||||
cat, err := repo.LookupCatalog(ctx, id)
|
||||
if err != nil {
|
||||
res.Error = err
|
||||
return res
|
||||
}
|
||||
if cat == nil {
|
||||
res.Error = handlers.NotFoundError()
|
||||
return res
|
||||
}
|
||||
parentId = cat.GetScopeId()
|
||||
opts = append(opts, auth.WithId(id))
|
||||
case plugin.Subtype:
|
||||
repo, err := s.pluginHostRepoFn()
|
||||
if err != nil {
|
||||
res.Error = err
|
||||
return res
|
||||
}
|
||||
cat, err := repo.LookupCatalog(ctx, id)
|
||||
if err != nil {
|
||||
res.Error = err
|
||||
return res
|
||||
}
|
||||
if cat == nil {
|
||||
res.Error = handlers.NotFoundError()
|
||||
return res
|
||||
}
|
||||
parentId = cat.GetScopeId()
|
||||
opts = append(opts, auth.WithId(id))
|
||||
}
|
||||
cat, err := repo.LookupCatalog(ctx, id)
|
||||
if err != nil {
|
||||
res.Error = err
|
||||
return res
|
||||
}
|
||||
if cat == nil {
|
||||
res.Error = handlers.NotFoundError()
|
||||
return res
|
||||
}
|
||||
parentId = cat.GetScopeId()
|
||||
opts = append(opts, auth.WithId(id))
|
||||
}
|
||||
opts = append(opts, auth.WithScopeId(parentId))
|
||||
return auth.Verify(ctx, opts...)
|
||||
}
|
||||
|
||||
func toProto(ctx context.Context, in *static.HostCatalog, opt ...handlers.Option) (*pb.HostCatalog, error) {
|
||||
func toProto(ctx context.Context, in host.Catalog, opt ...handlers.Option) (*pb.HostCatalog, error) {
|
||||
const op = "host_catalog_service.toProto"
|
||||
opts := handlers.GetOpts(opt...)
|
||||
if opts.WithOutputFields == nil {
|
||||
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "output fields not found when building auth method proto")
|
||||
@@ -474,7 +564,17 @@ func toProto(ctx context.Context, in *static.HostCatalog, opt ...handlers.Option
|
||||
out.ScopeId = in.GetScopeId()
|
||||
}
|
||||
if outputFields.Has(globals.TypeField) {
|
||||
out.Type = static.Subtype.String()
|
||||
switch host.SubtypeFromId(in.GetPublicId()) {
|
||||
case static.Subtype:
|
||||
out.Type = static.Subtype.String()
|
||||
default:
|
||||
// TODO: When id prefix and plugin name can diverge, update this logic.
|
||||
out.Type = "plugin"
|
||||
idParts := strings.Split(in.GetPublicId(), "_")
|
||||
if len(idParts) > 2 {
|
||||
out.Type = idParts[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
if outputFields.Has(globals.DescriptionField) && in.GetDescription() != "" {
|
||||
out.Description = wrapperspb.String(in.GetDescription())
|
||||
@@ -500,9 +600,63 @@ func toProto(ctx context.Context, in *static.HostCatalog, opt ...handlers.Option
|
||||
if outputFields.Has(globals.AuthorizedCollectionActionsField) {
|
||||
out.AuthorizedCollectionActions = opts.WithAuthorizedCollectionActions
|
||||
}
|
||||
if outputFields.Has(globals.AttributesField) {
|
||||
switch h := in.(type) {
|
||||
case *plugin.HostCatalog:
|
||||
attrs := map[string]interface{}{}
|
||||
err := json.Unmarshal(h.Attributes, &attrs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if len(attrs) > 0 {
|
||||
out.Attributes, err = structpb.NewStruct(attrs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func toStorageStaticCatalog(ctx context.Context, scopeId string, item *pb.HostCatalog) (*static.HostCatalog, error) {
|
||||
const op = "host_catalog_service.toStorageStaticCatalog"
|
||||
var opts []static.Option
|
||||
if name := item.GetName(); name != nil {
|
||||
opts = append(opts, static.WithName(name.GetValue()))
|
||||
}
|
||||
if desc := item.GetDescription(); desc != nil {
|
||||
opts = append(opts, static.WithDescription(desc.GetValue()))
|
||||
}
|
||||
hc, err := static.NewHostCatalog(scopeId, opts...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to build host catalog"))
|
||||
}
|
||||
return hc, nil
|
||||
}
|
||||
|
||||
func toStoragePluginCatalog(ctx context.Context, scopeId, plgId string, item *pb.HostCatalog) (*plugin.HostCatalog, error) {
|
||||
const op = "host_catalog_service.toStoragePluginCatalog"
|
||||
var opts []plugin.Option
|
||||
if name := item.GetName(); name != nil {
|
||||
opts = append(opts, plugin.WithName(item.GetName().GetValue()))
|
||||
}
|
||||
if desc := item.GetDescription(); desc != nil {
|
||||
opts = append(opts, plugin.WithDescription(desc.GetValue()))
|
||||
}
|
||||
if attrs := item.GetAttributes(); attrs != nil {
|
||||
opts = append(opts, plugin.WithAttributes(attrs.AsMap()))
|
||||
}
|
||||
if secrets := item.GetSecrets(); secrets != nil {
|
||||
opts = append(opts, plugin.WithSecrets(secrets.AsMap()))
|
||||
}
|
||||
hc, err := plugin.NewHostCatalog(ctx, scopeId, plgId, opts...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("Unable to build host set for creation"))
|
||||
}
|
||||
return hc, nil
|
||||
}
|
||||
|
||||
// A validateX method should exist for each method above. These methods do not make calls to any backing service but enforce
|
||||
// requirements on the structure of the request. They verify that:
|
||||
// * The path passed in is correctly formatted
|
||||
@@ -511,19 +665,23 @@ func toProto(ctx context.Context, in *static.HostCatalog, opt ...handlers.Option
|
||||
// * The type asserted by the ID and/or field is known
|
||||
// * If relevant, the type derived from the id prefix matches what is claimed by the type field
|
||||
func validateGetRequest(req *pbs.GetHostCatalogRequest) error {
|
||||
return handlers.ValidateGetRequest(handlers.NoopValidatorFn, req, static.HostCatalogPrefix)
|
||||
pluginPrefix := plugin.HostCatalogPrefix
|
||||
idParts := strings.Split(req.GetId(), "_")
|
||||
if len(idParts) > 2 && idParts[0] == plugin.HostCatalogPrefix {
|
||||
pluginPrefix = strings.Join(idParts[:2], "_")
|
||||
}
|
||||
|
||||
return handlers.ValidateGetRequest(handlers.NoopValidatorFn, req, static.HostCatalogPrefix, pluginPrefix)
|
||||
}
|
||||
|
||||
func validateCreateRequest(req *pbs.CreateHostCatalogRequest) error {
|
||||
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
|
||||
badFields := map[string]string{}
|
||||
if !handlers.ValidId(handlers.Id(req.GetItem().GetScopeId()), scope.Project.Prefix()) {
|
||||
badFields["scope_id"] = "This field must be a valid project scope id."
|
||||
badFields[globals.ScopeIdField] = "This field must be a valid project scope id."
|
||||
}
|
||||
switch host.SubtypeFromType(req.GetItem().GetType()) {
|
||||
case static.Subtype:
|
||||
default:
|
||||
badFields["type"] = fmt.Sprintf("This is a required field and must be %q.", static.Subtype.String())
|
||||
if req.GetItem().GetType() == "" {
|
||||
badFields[globals.TypeField] = "This field is required."
|
||||
}
|
||||
return badFields
|
||||
})
|
||||
@@ -543,7 +701,13 @@ func validateUpdateRequest(req *pbs.UpdateHostCatalogRequest) error {
|
||||
}
|
||||
|
||||
func validateDeleteRequest(req *pbs.DeleteHostCatalogRequest) error {
|
||||
return handlers.ValidateDeleteRequest(handlers.NoopValidatorFn, req, static.HostCatalogPrefix)
|
||||
pluginPrefix := plugin.HostCatalogPrefix
|
||||
idParts := strings.Split(req.GetId(), "_")
|
||||
if len(idParts) > 2 && idParts[0] == plugin.HostCatalogPrefix {
|
||||
pluginPrefix = strings.Join(idParts[:2], "_")
|
||||
}
|
||||
|
||||
return handlers.ValidateDeleteRequest(handlers.NoopValidatorFn, req, static.HostCatalogPrefix, pluginPrefix)
|
||||
}
|
||||
|
||||
func validateListRequest(req *pbs.ListHostCatalogsRequest) error {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -11,11 +12,12 @@ import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/boundary/internal/host/plugin"
|
||||
"github.com/hashicorp/boundary/internal/host/static"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/plugin/host"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/auth"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers/host_catalogs"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
@@ -49,36 +51,27 @@ var authorizedCollectionActions = map[string]*structpb.ListValue{
|
||||
|
||||
var testAuthorizedActions = []string{"no-op", "read", "update", "delete"}
|
||||
|
||||
func createDefaultHostCatalogAndRepo(t *testing.T) (*static.HostCatalog, *iam.Scope, common.StaticRepoFactory, common.IamRepoFactory) {
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
func TestGet_Static(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
_, pRes := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*static.Repository, error) {
|
||||
repo := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
|
||||
hc, err := static.NewHostCatalog(pRes.GetPublicId(), static.WithName("default"), static.WithDescription("default"))
|
||||
require.NoError(err, "Couldn't get new catalog.")
|
||||
repo, err := repoFn()
|
||||
require.NoError(err, "Couldn't create static repostitory")
|
||||
hcRes, err := repo.CreateCatalog(context.Background(), hc)
|
||||
require.NoError(err, "Couldn't persist new catalog.")
|
||||
|
||||
return hcRes, pRes, repoFn, iamRepoFn
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
hc, proj, repo, iamRepoFn := createDefaultHostCatalogAndRepo(t)
|
||||
toMerge := &pbs.GetHostCatalogRequest{
|
||||
Id: hc.GetPublicId(),
|
||||
}
|
||||
@@ -87,8 +80,6 @@ func TestGet(t *testing.T) {
|
||||
Id: hc.GetPublicId(),
|
||||
ScopeId: hc.GetScopeId(),
|
||||
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: proj.GetParentId()},
|
||||
Name: &wrappers.StringValue{Value: hc.GetName()},
|
||||
Description: &wrappers.StringValue{Value: hc.GetDescription()},
|
||||
CreatedTime: hc.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: hc.UpdateTime.GetTimestamp(),
|
||||
Type: "static",
|
||||
@@ -132,7 +123,98 @@ func TestGet(t *testing.T) {
|
||||
req := proto.Clone(toMerge).(*pbs.GetHostCatalogRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
s, err := host_catalogs.NewService(repo, iamRepoFn)
|
||||
s, err := host_catalogs.NewService(repo, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(err, "Couldn't create a new host catalog service.")
|
||||
|
||||
got, gErr := s.GetHostCatalog(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), req)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "GetHostCatalog(%+v) got error %v, wanted %v", req, gErr, tc.err)
|
||||
}
|
||||
|
||||
if tc.res != nil {
|
||||
tc.res.Item.Version = 1
|
||||
}
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "GetHostCatalog(%q) got response %q, wanted %q", req, got, tc.res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet_Plugin(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
|
||||
rw := db.New(conn)
|
||||
repo := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
name := "test"
|
||||
plg := host.TestPlugin(t, conn, name, name)
|
||||
hc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
|
||||
|
||||
toMerge := &pbs.GetHostCatalogRequest{
|
||||
Id: hc.GetPublicId(),
|
||||
}
|
||||
|
||||
pHostCatalog := &pb.HostCatalog{
|
||||
Id: hc.GetPublicId(),
|
||||
ScopeId: hc.GetScopeId(),
|
||||
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: proj.GetParentId()},
|
||||
CreatedTime: hc.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: hc.UpdateTime.GetTimestamp(),
|
||||
Type: name,
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
AuthorizedCollectionActions: authorizedCollectionActions,
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.GetHostCatalogRequest
|
||||
res *pbs.GetHostCatalogResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Get an Existing HostCatalog",
|
||||
req: &pbs.GetHostCatalogRequest{Id: hc.GetPublicId()},
|
||||
res: &pbs.GetHostCatalogResponse{Item: pHostCatalog},
|
||||
},
|
||||
{
|
||||
name: "Get a non existing Host Catalog",
|
||||
req: &pbs.GetHostCatalogRequest{Id: plugin.HostCatalogPrefix + "_DoesntExis"},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.NotFound),
|
||||
},
|
||||
{
|
||||
name: "Wrong id prefix",
|
||||
req: &pbs.GetHostCatalogRequest{Id: "j_1234567890"},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "space in id",
|
||||
req: &pbs.GetHostCatalogRequest{Id: plugin.HostCatalogPrefix + "_1 23456789"},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
req := proto.Clone(toMerge).(*pbs.GetHostCatalogRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
s, err := host_catalogs.NewService(repo, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(err, "Couldn't create a new host catalog service.")
|
||||
|
||||
got, gErr := s.GetHostCatalog(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), req)
|
||||
@@ -157,6 +239,12 @@ func TestList(t *testing.T) {
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iam.TestRepo(t, conn, wrapper), nil
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
repoFn := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
@@ -181,6 +269,26 @@ func TestList(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
var testPluginCatalogs []*pb.HostCatalog
|
||||
name := "test"
|
||||
plg := host.TestPlugin(t, conn, name, name)
|
||||
for i := 0 ; i < 3; i++ {
|
||||
hc := plugin.TestCatalog(t, conn, pWithCatalogs.GetPublicId(), plg.GetPublicId())
|
||||
cat := &pb.HostCatalog{
|
||||
Id: hc.GetPublicId(),
|
||||
ScopeId: hc.GetScopeId(),
|
||||
CreatedTime: hc.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: hc.GetUpdateTime().GetTimestamp(),
|
||||
Scope: &scopepb.ScopeInfo{Id: pWithCatalogs.GetPublicId(), Type: scope.Project.String(), ParentScopeId: oWithCatalogs.GetPublicId()},
|
||||
Version: 1,
|
||||
Type: name,
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
AuthorizedCollectionActions: authorizedCollectionActions,
|
||||
}
|
||||
wantSomeCatalogs = append(wantSomeCatalogs, cat)
|
||||
testPluginCatalogs = append(testPluginCatalogs, cat)
|
||||
}
|
||||
|
||||
var wantOtherCatalogs []*pb.HostCatalog
|
||||
for _, hc := range static.TestCatalogs(t, conn, pWithOtherCatalogs.GetPublicId(), 3) {
|
||||
wantOtherCatalogs = append(wantOtherCatalogs, &pb.HostCatalog{
|
||||
@@ -196,6 +304,23 @@ func TestList(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
name = "different"
|
||||
plg = host.TestPlugin(t, conn, name, name)
|
||||
for i := 0 ; i < 3; i++ {
|
||||
hc := plugin.TestCatalog(t, conn, pWithOtherCatalogs.GetPublicId(), plg.GetPublicId())
|
||||
wantOtherCatalogs = append(wantOtherCatalogs, &pb.HostCatalog{
|
||||
Id: hc.GetPublicId(),
|
||||
ScopeId: hc.GetScopeId(),
|
||||
CreatedTime: hc.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: hc.GetUpdateTime().GetTimestamp(),
|
||||
Scope: &scopepb.ScopeInfo{Id: pWithOtherCatalogs.GetPublicId(), Type: scope.Project.String(), ParentScopeId: oWithOtherCatalogs.GetPublicId()},
|
||||
Version: 1,
|
||||
Type: name,
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
AuthorizedCollectionActions: authorizedCollectionActions,
|
||||
})
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.ListHostCatalogsRequest
|
||||
@@ -242,6 +367,14 @@ func TestList(t *testing.T) {
|
||||
},
|
||||
res: &pbs.ListHostCatalogsResponse{Items: wantSomeCatalogs},
|
||||
},
|
||||
{
|
||||
name: "Filter To Catalog Test Type",
|
||||
req: &pbs.ListHostCatalogsRequest{
|
||||
ScopeId: scope.Global.String(), Recursive: true,
|
||||
Filter: `"/item/type"=="test"`,
|
||||
},
|
||||
res: &pbs.ListHostCatalogsResponse{Items: testPluginCatalogs},
|
||||
},
|
||||
{
|
||||
name: "Filter To No Catalogs",
|
||||
req: &pbs.ListHostCatalogsRequest{ScopeId: pWithCatalogs.GetPublicId(), Filter: `"/item/id"=="doesnt match"`},
|
||||
@@ -256,7 +389,7 @@ func TestList(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
s, err := host_catalogs.NewService(repoFn, iamRepoFn)
|
||||
s, err := host_catalogs.NewService(repoFn, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(err, "Couldn't create new auth_method service.")
|
||||
|
||||
// Test with non-anon user
|
||||
@@ -266,6 +399,13 @@ func TestList(t *testing.T) {
|
||||
assert.True(errors.Is(gErr, tc.err), "ListHostCatalogs() for scope %q got error %v, wanted %v", tc.req.GetScopeId(), gErr, tc.err)
|
||||
return
|
||||
}
|
||||
sort.Slice(got.Items, func(i, j int) bool {
|
||||
return got.Items[i].GetId() < got.Items[j].GetId()
|
||||
})
|
||||
sort.Slice(tc.res.Items, func(i, j int) bool {
|
||||
return tc.res.Items[i].GetId() < tc.res.Items[j].GetId()
|
||||
})
|
||||
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "ListHostCatalogs() for scope %q got response %q, wanted %q", tc.req.GetScopeId(), got, tc.res)
|
||||
|
||||
// Test with anon user
|
||||
@@ -283,9 +423,27 @@ func TestList(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
hc, proj, repo, iamRepoFn := createDefaultHostCatalogAndRepo(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
rw := db.New(conn)
|
||||
repo := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
|
||||
s, err := host_catalogs.NewService(repo, iamRepoFn)
|
||||
s, err := host_catalogs.NewService(repo, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(t, err, "Couldn't create a new host catalog service.")
|
||||
|
||||
cases := []struct {
|
||||
@@ -335,9 +493,27 @@ func TestDelete(t *testing.T) {
|
||||
func TestDelete_twice(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
hc, proj, repo, iamRepoFn := createDefaultHostCatalogAndRepo(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
rw := db.New(conn)
|
||||
repo := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
|
||||
s, err := host_catalogs.NewService(repo, iamRepoFn)
|
||||
s, err := host_catalogs.NewService(repo, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(err, "Couldn't create a new host catalog service.")
|
||||
req := &pbs.DeleteHostCatalogRequest{
|
||||
Id: hc.GetPublicId(),
|
||||
@@ -350,9 +526,27 @@ func TestDelete_twice(t *testing.T) {
|
||||
assert.True(errors.Is(gErr, handlers.ApiErrorWithCode(codes.NotFound)), "Expected permission denied for the second delete.")
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
func TestCreate_Static(t *testing.T) {
|
||||
t.Parallel()
|
||||
defaultHc, proj, repo, iamRepoFn := createDefaultHostCatalogAndRepo(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
rw := db.New(conn)
|
||||
repo := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
defaultHc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
defaultHcCreated := defaultHc.GetCreateTime().GetTimestamp().AsTime()
|
||||
toMerge := &pbs.CreateHostCatalogRequest{}
|
||||
|
||||
@@ -451,7 +645,7 @@ func TestCreate(t *testing.T) {
|
||||
req := proto.Clone(toMerge).(*pbs.CreateHostCatalogRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
s, err := host_catalogs.NewService(repo, iamRepoFn)
|
||||
s, err := host_catalogs.NewService(repo, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(err, "Failed to create a new host catalog service.")
|
||||
|
||||
got, gErr := s.CreateHostCatalog(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), req)
|
||||
@@ -483,12 +677,192 @@ func TestCreate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreate_Plugin(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
rw := db.New(conn)
|
||||
repo := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
|
||||
name := "test"
|
||||
plg := host.TestPlugin(t, conn, name, name)
|
||||
defaultHc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
|
||||
defaultHcCreated := defaultHc.GetCreateTime().GetTimestamp().AsTime()
|
||||
toMerge := &pbs.CreateHostCatalogRequest{}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
req *pbs.CreateHostCatalogRequest
|
||||
res *pbs.CreateHostCatalogResponse
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Create a valid HostCatalog",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
ScopeId: proj.GetPublicId(),
|
||||
Name: &wrappers.StringValue{Value: "name"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
Type: name,
|
||||
}},
|
||||
res: &pbs.CreateHostCatalogResponse{
|
||||
Uri: fmt.Sprintf("host-catalogs/%s_%s_", plugin.HostCatalogPrefix, name),
|
||||
Item: &pb.HostCatalog{
|
||||
ScopeId: proj.GetPublicId(),
|
||||
Scope: &scopepb.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: proj.GetParentId()},
|
||||
Name: &wrappers.StringValue{Value: "name"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
Type: name,
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
AuthorizedCollectionActions: authorizedCollectionActions,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Cant create in org",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
ScopeId: proj.GetParentId(),
|
||||
Name: &wrappers.StringValue{Value: "name"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
Type: name,
|
||||
}},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Cant create in global",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
ScopeId: scope.Global.String(),
|
||||
Name: &wrappers.StringValue{Value: "name"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
Type: name,
|
||||
}},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Create with unknown type",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
Name: &wrappers.StringValue{Value: "name"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
Type: "thisismadeup",
|
||||
}},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Create with no type",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
Name: &wrappers.StringValue{Value: "name"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
}},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Can't specify Id",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
Id: "not allowed to be set",
|
||||
}},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Can't specify Created Time",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
CreatedTime: timestamppb.Now(),
|
||||
}},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Can't specify Update Time",
|
||||
req: &pbs.CreateHostCatalogRequest{Item: &pb.HostCatalog{
|
||||
UpdatedTime: timestamppb.Now(),
|
||||
}},
|
||||
res: nil,
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
req := proto.Clone(toMerge).(*pbs.CreateHostCatalogRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
s, err := host_catalogs.NewService(repo, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(err, "Failed to create a new host catalog service.")
|
||||
|
||||
got, gErr := s.CreateHostCatalog(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), req)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "CreateHostCatalog(%+v) got error %v, wanted %v", req, gErr, tc.err)
|
||||
return
|
||||
}
|
||||
require.NoError(gErr)
|
||||
if got != nil {
|
||||
assert.Contains(got.GetUri(), tc.res.GetUri())
|
||||
assert.True(strings.HasPrefix(got.GetItem().GetId(), fmt.Sprintf("%s_%s_", plugin.HostCatalogPrefix, name)))
|
||||
gotCreateTime := got.GetItem().GetCreatedTime().AsTime()
|
||||
require.NoError(err, "Error converting proto to timestamp.")
|
||||
gotUpdateTime := got.GetItem().GetUpdatedTime().AsTime()
|
||||
require.NoError(err, "Error converting proto to timestamp")
|
||||
// Verify it is a catalog created after the test setup's default catalog
|
||||
assert.True(gotCreateTime.After(defaultHcCreated), "New catalog should have been created after default catalog. Was created %v, which is after %v", gotCreateTime, defaultHcCreated)
|
||||
assert.True(gotUpdateTime.After(defaultHcCreated), "New catalog should have been updated after default catalog. Was updated %v, which is after %v", gotUpdateTime, defaultHcCreated)
|
||||
|
||||
// Clear all values which are hard to compare against.
|
||||
got.Uri, tc.res.Uri = "", ""
|
||||
got.Item.Id, tc.res.Item.Id = "", ""
|
||||
got.Item.CreatedTime, got.Item.UpdatedTime, tc.res.Item.CreatedTime, tc.res.Item.UpdatedTime = nil, nil, nil, nil
|
||||
}
|
||||
if tc.res != nil {
|
||||
tc.res.Item.Version = 1
|
||||
}
|
||||
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "CreateHostCatalog(%q) got response %q, wanted %q", req, got, tc.res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
hc, proj, repoFn, iamRepoFn := createDefaultHostCatalogAndRepo(t)
|
||||
tested, err := host_catalogs.NewService(repoFn, iamRepoFn)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
rw := db.New(conn)
|
||||
repoFn := func() (*static.Repository, error) {
|
||||
return static.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginHostRepo := func() (*plugin.Repository, error) {
|
||||
return plugin.NewRepository(rw, rw, kms)
|
||||
}
|
||||
pluginRepo := func() (*host.Repository, error) {
|
||||
return host.NewRepository(rw, rw, kms)
|
||||
}
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
tested, err := host_catalogs.NewService(repoFn, pluginHostRepo, pluginRepo, iamRepoFn)
|
||||
require.NoError(t, err, "Failed to create a new host catalog service.")
|
||||
|
||||
hc, err := static.NewHostCatalog(proj.GetPublicId(), static.WithName("default"), static.WithDescription("default"))
|
||||
require.NoError(t, err, "Couldn't get new catalog.")
|
||||
repo, err := repoFn()
|
||||
require.NoError(t, err, "Couldn't create static repostitory")
|
||||
hc, err = repo.CreateCatalog(context.Background(), hc)
|
||||
require.NoError(t, err, "Couldn't persist new catalog.")
|
||||
|
||||
var version uint32 = 1
|
||||
|
||||
resetHostCatalog := func() {
|
||||
@@ -797,7 +1171,9 @@ func TestUpdate(t *testing.T) {
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "UpdateHostCatalog(%+v) got error %v, wanted %v", req, gErr, tc.err)
|
||||
return
|
||||
}
|
||||
require.NoError(gErr)
|
||||
|
||||
if tc.err == nil {
|
||||
defer resetHostCatalog()
|
||||
|
||||
Reference in new issue
Block a user