From 24c6c329fe926c07c7d3e00053c42f83c7137ae1 Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Fri, 22 Oct 2021 08:51:46 -0400 Subject: [PATCH] chore(target): Remove unused option support from repo Create For the target Repository's Create method, the options of WithHostSources and WithCredentialSources where never used by the targets.Service. As such the repository does not need to support these options. See: 51812ff77937a3e5e6873995b07ae5400965ac7c --- internal/cmd/base/initial_resources.go | 5 +- .../handlers/targets/target_service_test.go | 6 ++- internal/target/repository.go | 40 +------------- .../target/tcp/repository_tcp_target_test.go | 54 ------------------- 4 files changed, 9 insertions(+), 96 deletions(-) diff --git a/internal/cmd/base/initial_resources.go b/internal/cmd/base/initial_resources.go index a0115f361..edb221e4b 100644 --- a/internal/cmd/base/initial_resources.go +++ b/internal/cmd/base/initial_resources.go @@ -508,7 +508,6 @@ func (b *Server) CreateInitialTarget(ctx context.Context) (target.Target, error) target.WithName("Generated target"), target.WithDescription("Provides an initial target in Boundary"), target.WithDefaultPort(uint32(b.DevTargetDefaultPort)), - target.WithHostSources([]string{b.DevHostSetId}), target.WithSessionMaxSeconds(uint32(b.DevTargetSessionMaxSeconds)), target.WithSessionConnectionLimit(int32(b.DevTargetSessionConnectionLimit)), target.WithPublicId(b.DevTargetId), @@ -521,6 +520,10 @@ func (b *Server) CreateInitialTarget(ctx context.Context) (target.Target, error) if err != nil { return nil, fmt.Errorf("error saving target to the db: %w", err) } + tt, _, _, err = targetRepo.AddTargetHostSources(ctx, tt.GetPublicId(), tt.GetVersion(), []string{b.DevHostSetId}) + if err != nil { + return nil, fmt.Errorf("error saving target to the db: %w", err) + } b.InfoKeys = append(b.InfoKeys, "generated target id") b.Info["generated target id"] = b.DevTargetId diff --git a/internal/servers/controller/handlers/targets/target_service_test.go b/internal/servers/controller/handlers/targets/target_service_test.go index 37b7329d2..700ad493b 100644 --- a/internal/servers/controller/handlers/targets/target_service_test.go +++ b/internal/servers/controller/handlers/targets/target_service_test.go @@ -575,11 +575,13 @@ func TestUpdate(t *testing.T) { tar, err := tcp.New(proj.GetPublicId(), target.WithName("default"), target.WithDescription("default")) tar.DefaultPort = 2 require.NoError(t, err) - gtar, _, _, err := repo.CreateTarget(context.Background(), tar, target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})) + gtar, _, _, err := repo.CreateTarget(context.Background(), tar) + require.NoError(t, err) + gtar, _, _, err = repo.AddTargetHostSources(context.Background(), gtar.GetPublicId(), gtar.GetVersion(), []string{hs[0].GetPublicId(), hs[1].GetPublicId()}) require.NoError(t, err) tar = gtar.(*tcp.Target) - var version uint32 = 1 + var version uint32 = gtar.GetVersion() resetTarget := func() { version++ diff --git a/internal/target/repository.go b/internal/target/repository.go index 04843199c..fb965b68c 100644 --- a/internal/target/repository.go +++ b/internal/target/repository.go @@ -323,7 +323,7 @@ func (r *Repository) update(ctx context.Context, target Target, version uint32, // CreateTarget inserts into the repository and returns the new Target with // its list of host sets and credential libraries. -// WithHostSources and WithCredentialSources are the only supported option. +// WithPublicId is the only supported option. func (r *Repository) CreateTarget(ctx context.Context, target Target, opt ...Option) (Target, []HostSource, []CredentialSource, error) { const op = "target.(Repository).CreateTarget" opts := GetOpts(opt...) @@ -371,24 +371,6 @@ func (r *Repository) CreateTarget(ctx context.Context, target Target, opt ...Opt return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper")) } - newHostSets := make([]interface{}, 0, len(opts.WithHostSources)) - for _, hsId := range opts.WithHostSources { - hostSet, err := NewTargetHostSet(t.GetPublicId(), hsId) - if err != nil { - return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory target host set")) - } - newHostSets = append(newHostSets, hostSet) - } - - newCredLibs := make([]interface{}, 0, len(opts.WithCredentialSources)) - for _, clId := range opts.WithCredentialSources { - credLib, err := NewCredentialLibrary(t.GetPublicId(), clId) - if err != nil { - return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create in memory target credential library")) - } - newCredLibs = append(newCredLibs, credLib) - } - metadata := t.Oplog(oplog.OpType_OP_TYPE_CREATE) var returnedTarget interface{} var returnedHostSources []HostSource @@ -409,26 +391,6 @@ func (r *Repository) CreateTarget(ctx context.Context, target Target, opt ...Opt return errors.Wrap(ctx, err, op, errors.WithMsg("unable to create target")) } msgs = append(msgs, &targetOplogMsg) - if len(newHostSets) > 0 { - hostSetOplogMsgs := make([]*oplog.Message, 0, len(newHostSets)) - if err := w.CreateItems(ctx, newHostSets, db.NewOplogMsgs(&hostSetOplogMsgs)); err != nil { - return errors.Wrap(ctx, err, op, errors.WithMsg("unable to add host sets")) - } - if returnedHostSources, err = fetchHostSources(ctx, read, t.GetPublicId()); err != nil { - return errors.Wrap(ctx, err, op, errors.WithMsg("unable to read host sources")) - } - msgs = append(msgs, hostSetOplogMsgs...) - } - if len(newCredLibs) > 0 { - credLibOplogMsgs := make([]*oplog.Message, 0, len(newCredLibs)) - if err := w.CreateItems(ctx, newCredLibs, db.NewOplogMsgs(&credLibOplogMsgs)); err != nil { - return errors.Wrap(ctx, err, op, errors.WithMsg("unable to add credential sources")) - } - if returnedCredSources, err = fetchCredentialSources(ctx, read, t.GetPublicId()); err != nil { - return errors.Wrap(ctx, err, op, errors.WithMsg("unable to read credential sources")) - } - msgs = append(msgs, credLibOplogMsgs...) - } if err := w.WriteOplogEntryWith(ctx, oplogWrapper, targetTicket, metadata, msgs); err != nil { return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog")) } diff --git a/internal/target/tcp/repository_tcp_target_test.go b/internal/target/tcp/repository_tcp_target_test.go index 21b5806ed..6ae271b5c 100644 --- a/internal/target/tcp/repository_tcp_target_test.go +++ b/internal/target/tcp/repository_tcp_target_test.go @@ -72,60 +72,6 @@ func TestRepository_CreateTarget(t *testing.T) { wantCredLibs: []string{}, wantHostSources: []string{}, }, - { - name: "valid-org-with-host-sets", - args: args{ - target: func() *tcp.Target { - target, err := tcp.New(proj.PublicId, - target.WithName("valid-org-with-host-sets"), - target.WithDescription("valid-org"), - target.WithDefaultPort(uint32(22))) - require.NoError(t, err) - return target - }(), - opt: []target.Option{target.WithHostSources(sets)}, - }, - wantErr: false, - wantHostSources: sets, - wantCredLibs: []string{}, - }, - { - name: "valid-org-with-cred-libs", - args: args{ - target: func() *tcp.Target { - target, err := tcp.New(proj.PublicId, - target.WithName("valid-org-with-cred-libs"), - target.WithDescription("valid-org"), - target.WithDefaultPort(uint32(22))) - require.NoError(t, err) - return target - }(), - opt: []target.Option{target.WithCredentialSources(clIds)}, - }, - wantErr: false, - wantCredLibs: clIds, - wantHostSources: []string{}, - }, - { - name: "valid-org-with-cred-libs-and-host-sets", - args: args{ - target: func() *tcp.Target { - target, err := tcp.New(proj.PublicId, - target.WithName("valid-org-with-cred-libs-and-host-sets"), - target.WithDescription("valid-org"), - target.WithDefaultPort(uint32(22))) - require.NoError(t, err) - return target - }(), - opt: []target.Option{ - target.WithHostSources(sets), - target.WithCredentialSources(clIds), - }, - }, - wantErr: false, - wantCredLibs: clIds, - wantHostSources: sets, - }, { name: "nil-target", args: args{