From e5f7e1808e217fcfd9d6f28f0f7b61d9ff67c9e6 Mon Sep 17 00:00:00 2001 From: Jim Lambert Date: Wed, 12 Aug 2020 11:09:57 -0400 Subject: [PATCH] add create and lookup for external config --- internal/kms/repository.go | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/internal/kms/repository.go b/internal/kms/repository.go index a4c07b69fd..864c89c153 100644 --- a/internal/kms/repository.go +++ b/internal/kms/repository.go @@ -1,9 +1,12 @@ package kms import ( + "context" "errors" + "fmt" "github.com/hashicorp/boundary/internal/db" + "github.com/hashicorp/boundary/internal/oplog" wrapping "github.com/hashicorp/go-kms-wrapping" ) @@ -41,3 +44,62 @@ func NewRepository(r db.Reader, w db.Writer, wrapper wrapping.Wrapper, opt ...Op defaultLimit: opts.withLimit, }, nil } + +// CreateExternalConfig inserts into the repository and returns the new external +// config with its PrivateId. There are no valid options at this time. +func (r *Repository) CreateExternalConfig(ctx context.Context, conf *ExternalConfig, opt ...Option) (*ExternalConfig, error) { + if conf == nil { + return nil, fmt.Errorf("create external config: missing conf %w", db.ErrNilParameter) + } + if conf.ExternalConfig == nil { + return nil, fmt.Errorf("create external config: missing conf store %w", db.ErrNilParameter) + } + if conf.PrivateId != "" { + return nil, fmt.Errorf("create external config: private id not empty: %w", db.ErrInvalidParameter) + } + if conf.ScopeId == "" { + return nil, fmt.Errorf("create external config: missing conf scope id: %w", db.ErrInvalidParameter) + } + if conf.Config == "" { + return nil, fmt.Errorf("create external config: missing json configuration: %w", db.ErrInvalidParameter) + } + id, err := newExternalConfigId() + if err != nil { + return nil, fmt.Errorf("create external config: %w", err) + } + c := conf.Clone().(*ExternalConfig) + c.PrivateId = id + + var returnedConfig interface{} + _, err = r.writer.DoTx( + ctx, + db.StdRetryCnt, + db.ExpBackoff{}, + func(_ db.Reader, w db.Writer) error { + returnedConfig = c.Clone() + if err := w.Create(ctx, returnedConfig, db.WithOplog(r.wrapper, c.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil { + return err + } + return nil + }, + ) + if err != nil { + return nil, fmt.Errorf("create external config: %w for %s", err, c.PrivateId) + } + return returnedConfig.(*ExternalConfig), err +} + +// LookupExternalConfig will look up an external config in the repository. If the config is not +// found, it will return nil, nil. +func (r *Repository) LookupExternalConfig(ctx context.Context, privateId string, opt ...Option) (*ExternalConfig, error) { + if privateId == "" { + return nil, fmt.Errorf("lookup external config: missing private id %w", db.ErrNilParameter) + } + + c := allocExternalConfig() + c.PrivateId = privateId + if err := r.reader.LookupById(ctx, &c); err != nil { + return nil, fmt.Errorf("lookup external config: failed %w for %s", err, privateId) + } + return &c, nil +}