configstore: move get secret(s) to action

This commit is contained in:
Simone Gotti 2019-05-13 00:22:23 +02:00
parent 0f9445aabe
commit 295ed6e39a
3 changed files with 52 additions and 35 deletions

View File

@ -27,6 +27,45 @@ import (
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
) )
func (h *ActionHandler) GetSecret(ctx context.Context, secretID string) (*types.Secret, error) {
var secret *types.Secret
err := h.readDB.Do(func(tx *db.Tx) error {
var err error
secret, err = h.readDB.GetSecretByID(tx, secretID)
return err
})
if err != nil {
return nil, err
}
if secret == nil {
return nil, util.NewErrNotFound(errors.Errorf("secret %q doesn't exist", secretID))
}
return secret, nil
}
func (h *ActionHandler) GetSecrets(ctx context.Context, parentType types.ConfigType, parentRef string, tree bool) ([]*types.Secret, error) {
var secrets []*types.Secret
err := h.readDB.Do(func(tx *db.Tx) error {
parentID, err := h.readDB.ResolveConfigID(tx, parentType, parentRef)
if err != nil {
return err
}
if tree {
secrets, err = h.readDB.GetSecretsTree(tx, parentType, parentID)
} else {
secrets, err = h.readDB.GetSecrets(tx, parentID)
}
return err
})
if err != nil {
return nil, err
}
return secrets, nil
}
func (h *ActionHandler) CreateSecret(ctx context.Context, secret *types.Secret) (*types.Secret, error) { func (h *ActionHandler) CreateSecret(ctx context.Context, secret *types.Secret) (*types.Secret, error) {
if secret.Name == "" { if secret.Name == "" {
return nil, util.NewErrBadRequest(errors.Errorf("secret name required")) return nil, util.NewErrBadRequest(errors.Errorf("secret name required"))

View File

@ -18,7 +18,6 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"github.com/pkg/errors"
"github.com/sorintlab/agola/internal/db" "github.com/sorintlab/agola/internal/db"
"github.com/sorintlab/agola/internal/services/configstore/action" "github.com/sorintlab/agola/internal/services/configstore/action"
"github.com/sorintlab/agola/internal/services/configstore/readdb" "github.com/sorintlab/agola/internal/services/configstore/readdb"
@ -39,31 +38,22 @@ type Secret struct {
type SecretHandler struct { type SecretHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ah *action.ActionHandler
readDB *readdb.ReadDB readDB *readdb.ReadDB
} }
func NewSecretHandler(logger *zap.Logger, readDB *readdb.ReadDB) *SecretHandler { func NewSecretHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *SecretHandler {
return &SecretHandler{log: logger.Sugar(), readDB: readDB} return &SecretHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
} }
func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
secretID := vars["secretid"] secretID := vars["secretid"]
var secret *types.Secret secret, err := h.ah.GetSecret(ctx, secretID)
err := h.readDB.Do(func(tx *db.Tx) error { if httpError(w, err) {
var err error
secret, err = h.readDB.GetSecretByID(tx, secretID)
return err
})
if err != nil {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
httpError(w, err)
return
}
if secret == nil {
httpError(w, util.NewErrNotFound(errors.Errorf("secret %q doesn't exist", secretID)))
return return
} }
@ -74,16 +64,17 @@ func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
type SecretsHandler struct { type SecretsHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ah *action.ActionHandler
readDB *readdb.ReadDB readDB *readdb.ReadDB
} }
func NewSecretsHandler(logger *zap.Logger, readDB *readdb.ReadDB) *SecretsHandler { func NewSecretsHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *SecretsHandler {
return &SecretsHandler{log: logger.Sugar(), readDB: readDB} return &SecretsHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
} }
func (h *SecretsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *SecretsHandler) 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)
@ -92,22 +83,9 @@ func (h *SecretsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var secrets []*types.Secret secrets, err := h.ah.GetSecrets(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 {
secrets, err = h.readDB.GetSecretsTree(tx, parentType, parentID)
} else {
secrets, err = h.readDB.GetSecrets(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

@ -135,7 +135,7 @@ func (s *Configstore) Run(ctx context.Context) error {
updateProjectHandler := api.NewUpdateProjectHandler(logger, s.ah, s.readDB) updateProjectHandler := api.NewUpdateProjectHandler(logger, s.ah, s.readDB)
deleteProjectHandler := api.NewDeleteProjectHandler(logger, s.ah) deleteProjectHandler := api.NewDeleteProjectHandler(logger, s.ah)
secretsHandler := api.NewSecretsHandler(logger, s.readDB) secretsHandler := api.NewSecretsHandler(logger, s.ah, s.readDB)
createSecretHandler := api.NewCreateSecretHandler(logger, s.ah) createSecretHandler := api.NewCreateSecretHandler(logger, s.ah)
deleteSecretHandler := api.NewDeleteSecretHandler(logger, s.ah) deleteSecretHandler := api.NewDeleteSecretHandler(logger, s.ah)