From 5c15eb4db7b8dd929df018d526ad720cf3187e33 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Mon, 13 May 2019 00:23:08 +0200 Subject: [PATCH] configstore: move getvariables to action --- .../services/configstore/action/variable.go | 21 ++++++++++++++++ internal/services/configstore/api/variable.go | 24 +++++-------------- internal/services/configstore/configstore.go | 2 +- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/internal/services/configstore/action/variable.go b/internal/services/configstore/action/variable.go index 18e6883..6486def 100644 --- a/internal/services/configstore/action/variable.go +++ b/internal/services/configstore/action/variable.go @@ -27,6 +27,27 @@ import ( uuid "github.com/satori/go.uuid" ) +func (h *ActionHandler) GetVariables(ctx context.Context, parentType types.ConfigType, parentRef string, tree bool) ([]*types.Variable, error) { + var variables []*types.Variable + err := h.readDB.Do(func(tx *db.Tx) error { + parentID, err := h.readDB.ResolveConfigID(tx, parentType, parentRef) + if err != nil { + return err + } + if tree { + variables, err = h.readDB.GetVariablesTree(tx, parentType, parentID) + } else { + variables, err = h.readDB.GetVariables(tx, parentID) + } + return err + }) + if err != nil { + return nil, err + } + + return variables, nil +} + func (h *ActionHandler) CreateVariable(ctx context.Context, variable *types.Variable) (*types.Variable, error) { if variable.Name == "" { return nil, util.NewErrBadRequest(errors.Errorf("variable name required")) diff --git a/internal/services/configstore/api/variable.go b/internal/services/configstore/api/variable.go index c47c092..da8ccb1 100644 --- a/internal/services/configstore/api/variable.go +++ b/internal/services/configstore/api/variable.go @@ -38,16 +38,17 @@ type Variable struct { type VariablesHandler struct { log *zap.SugaredLogger + ah *action.ActionHandler readDB *readdb.ReadDB } -func NewVariablesHandler(logger *zap.Logger, readDB *readdb.ReadDB) *VariablesHandler { - return &VariablesHandler{log: logger.Sugar(), readDB: readDB} +func NewVariablesHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *VariablesHandler { + return &VariablesHandler{log: logger.Sugar(), ah: ah, readDB: readDB} } func (h *VariablesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() query := r.URL.Query() - _, tree := query["tree"] parentType, parentRef, err := GetConfigTypeRef(r) @@ -56,22 +57,9 @@ func (h *VariablesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - var variables []*types.Variable - err = h.readDB.Do(func(tx *db.Tx) error { - parentID, err := h.readDB.ResolveConfigID(tx, parentType, parentRef) - if err != nil { - return err - } - if tree { - variables, err = h.readDB.GetVariablesTree(tx, parentType, parentID) - } else { - variables, err = h.readDB.GetVariables(tx, parentID) - } - return err - }) - if err != nil { + variables, err := h.ah.GetVariables(ctx, parentType, parentRef, tree) + if httpError(w, err) { h.log.Errorf("err: %+v", err) - httpError(w, err) return } diff --git a/internal/services/configstore/configstore.go b/internal/services/configstore/configstore.go index 392ebce..ad808b6 100644 --- a/internal/services/configstore/configstore.go +++ b/internal/services/configstore/configstore.go @@ -139,7 +139,7 @@ func (s *Configstore) Run(ctx context.Context) error { createSecretHandler := api.NewCreateSecretHandler(logger, s.ah) deleteSecretHandler := api.NewDeleteSecretHandler(logger, s.ah) - variablesHandler := api.NewVariablesHandler(logger, s.readDB) + variablesHandler := api.NewVariablesHandler(logger, s.ah, s.readDB) createVariableHandler := api.NewCreateVariableHandler(logger, s.ah) deleteVariableHandler := api.NewDeleteVariableHandler(logger, s.ah)