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"
)
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"))

View File

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

View File

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