add create and lookup for external config

pull/260/head
Jim Lambert 6 years ago
parent c1014433cd
commit e5f7e1808e

@ -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
}

Loading…
Cancel
Save