configstore: move getvariables to action

This commit is contained in:
Simone Gotti 2019-05-13 00:23:08 +02:00
parent 295ed6e39a
commit 5c15eb4db7
3 changed files with 28 additions and 19 deletions

View File

@ -27,6 +27,27 @@ import (
uuid "github.com/satori/go.uuid" 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) { func (h *ActionHandler) CreateVariable(ctx context.Context, variable *types.Variable) (*types.Variable, error) {
if variable.Name == "" { if variable.Name == "" {
return nil, util.NewErrBadRequest(errors.Errorf("variable name required")) return nil, util.NewErrBadRequest(errors.Errorf("variable name required"))

View File

@ -38,16 +38,17 @@ type Variable struct {
type VariablesHandler struct { type VariablesHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ah *action.ActionHandler
readDB *readdb.ReadDB readDB *readdb.ReadDB
} }
func NewVariablesHandler(logger *zap.Logger, readDB *readdb.ReadDB) *VariablesHandler { func NewVariablesHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *VariablesHandler {
return &VariablesHandler{log: logger.Sugar(), readDB: readDB} return &VariablesHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
} }
func (h *VariablesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *VariablesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.URL.Query() query := r.URL.Query()
_, tree := query["tree"] _, tree := query["tree"]
parentType, parentRef, err := GetConfigTypeRef(r) parentType, parentRef, err := GetConfigTypeRef(r)
@ -56,22 +57,9 @@ func (h *VariablesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var variables []*types.Variable variables, err := h.ah.GetVariables(ctx, parentType, parentRef, tree)
err = h.readDB.Do(func(tx *db.Tx) error { if httpError(w, err) {
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 {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
httpError(w, err)
return return
} }

View File

@ -139,7 +139,7 @@ func (s *Configstore) Run(ctx context.Context) error {
createSecretHandler := api.NewCreateSecretHandler(logger, s.ah) createSecretHandler := api.NewCreateSecretHandler(logger, s.ah)
deleteSecretHandler := api.NewDeleteSecretHandler(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) createVariableHandler := api.NewCreateVariableHandler(logger, s.ah)
deleteVariableHandler := api.NewDeleteVariableHandler(logger, s.ah) deleteVariableHandler := api.NewDeleteVariableHandler(logger, s.ah)