gateway: move variable logic from api to actions

This commit is contained in:
Simone Gotti 2019-05-04 15:16:49 +02:00
parent cb78ea48bc
commit f73f0ba434
6 changed files with 193 additions and 120 deletions

View File

@ -0,0 +1,151 @@
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package action
import (
"context"
"net/http"
"github.com/pkg/errors"
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/common"
"github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util"
)
type GetVariablesRequest struct {
ParentType types.ConfigType
ParentRef string
Tree bool
RemoveOverridden bool
}
func (h *ActionHandler) GetVariables(ctx context.Context, req *GetVariablesRequest) ([]*csapi.Variable, []*csapi.Secret, error) {
var csvars []*csapi.Variable
var cssecrets []*csapi.Secret
switch req.ParentType {
case types.ConfigTypeProjectGroup:
var err error
var resp *http.Response
csvars, resp, err = h.configstoreClient.GetProjectGroupVariables(ctx, req.ParentRef, req.Tree)
if err != nil {
return nil, nil, ErrFromRemote(resp, err)
}
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, req.ParentRef, true)
if err != nil {
return nil, nil, ErrFromRemote(resp, err)
}
case types.ConfigTypeProject:
var err error
var resp *http.Response
csvars, resp, err = h.configstoreClient.GetProjectVariables(ctx, req.ParentRef, req.Tree)
if err != nil {
return nil, nil, ErrFromRemote(resp, err)
}
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, req.ParentRef, true)
if err != nil {
return nil, nil, ErrFromRemote(resp, err)
}
}
if req.RemoveOverridden {
// remove overriden variables
csvars = common.FilterOverriddenVariables(csvars)
}
return csvars, cssecrets, nil
}
type CreateVariableRequest struct {
Name string
ParentType types.ConfigType
ParentRef string
Values []types.VariableValue
}
func (h *ActionHandler) CreateVariable(ctx context.Context, req *CreateVariableRequest) (*csapi.Variable, []*csapi.Secret, error) {
if !util.ValidateName(req.Name) {
return nil, nil, util.NewErrBadRequest(errors.Errorf("invalid variable name %q", req.Name))
}
if len(req.Values) == 0 {
return nil, nil, util.NewErrBadRequest(errors.Errorf("empty variable values"))
}
v := &types.Variable{
Name: req.Name,
Parent: types.Parent{
Type: req.ParentType,
ID: req.ParentRef,
},
Values: req.Values,
}
var cssecrets []*csapi.Secret
var rv *csapi.Variable
switch req.ParentType {
case types.ConfigTypeProjectGroup:
var err error
var resp *http.Response
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, req.ParentRef, true)
if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project group %q secrets", req.ParentRef))
}
h.log.Infof("creating project group variable")
rv, resp, err = h.configstoreClient.CreateProjectGroupVariable(ctx, req.ParentRef, v)
if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create variable"))
}
case types.ConfigTypeProject:
var err error
var resp *http.Response
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, req.ParentRef, true)
if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q secrets", req.ParentRef))
}
h.log.Infof("creating project variable")
rv, resp, err = h.configstoreClient.CreateProjectVariable(ctx, req.ParentRef, v)
if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create variable"))
}
}
h.log.Infof("variable %s created, ID: %s", rv.Name, rv.ID)
return rv, cssecrets, nil
}
func (h *ActionHandler) DeleteVariable(ctx context.Context, parentType types.ConfigType, parentRef, name string) error {
var err error
var resp *http.Response
switch parentType {
case types.ConfigTypeProjectGroup:
h.log.Infof("deleting project group variable")
resp, err = h.configstoreClient.DeleteProjectGroupVariable(ctx, parentRef, name)
case types.ConfigTypeProject:
h.log.Infof("deleting project variable")
resp, err = h.configstoreClient.DeleteProjectVariable(ctx, parentRef, name)
}
if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to delete variable"))
}
return nil
}

View File

@ -19,13 +19,13 @@ import (
"net/http" "net/http"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/gateway/common" "github.com/sorintlab/agola/internal/services/gateway/common"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/pkg/errors"
) )
type VariableValue struct { type VariableValue struct {
@ -69,18 +69,18 @@ func createVariableResponse(v *csapi.Variable, secrets []*csapi.Secret) *Variabl
type VariableHandler struct { type VariableHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewVariableHandler(logger *zap.Logger, configstoreClient *csapi.Client) *VariableHandler { func NewVariableHandler(logger *zap.Logger, ah *action.ActionHandler) *VariableHandler {
return &VariableHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &VariableHandler{log: logger.Sugar(), ah: ah}
} }
func (h *VariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *VariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
query := r.URL.Query() query := r.URL.Query()
_, tree := query["tree"] _, tree := query["tree"]
_, removeoverriden := query["removeoverriden"] _, removeoverridden := query["removeoverridden"]
parentType, parentRef, err := GetConfigTypeRef(r) parentType, parentRef, err := GetConfigTypeRef(r)
if httpError(w, err) { if httpError(w, err) {
@ -88,42 +88,17 @@ func (h *VariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var csvars []*csapi.Variable areq := &action.GetVariablesRequest{
var cssecrets []*csapi.Secret ParentType: parentType,
ParentRef: parentRef,
switch parentType { Tree: tree,
case types.ConfigTypeProjectGroup: RemoveOverridden: removeoverridden,
var err error }
var resp *http.Response csvars, cssecrets, err := h.ah.GetVariables(ctx, areq)
csvars, resp, err = h.configstoreClient.GetProjectGroupVariables(ctx, parentRef, tree) if httpError(w, err) {
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, parentRef, true)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err)
return
}
case types.ConfigTypeProject:
var err error
var resp *http.Response
csvars, resp, err = h.configstoreClient.GetProjectVariables(ctx, parentRef, tree)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err)
return
}
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, parentRef, true)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err)
return
}
}
if removeoverriden {
// remove overriden variables
csvars = common.FilterOverridenVariables(csvars)
}
variables := make([]*VariableResponse, len(csvars)) variables := make([]*VariableResponse, len(csvars))
for i, v := range csvars { for i, v := range csvars {
@ -143,11 +118,11 @@ type CreateVariableRequest struct {
type CreateVariableHandler struct { type CreateVariableHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewCreateVariableHandler(logger *zap.Logger, configstoreClient *csapi.Client) *CreateVariableHandler { func NewCreateVariableHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateVariableHandler {
return &CreateVariableHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &CreateVariableHandler{log: logger.Sugar(), ah: ah}
} }
func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -164,64 +139,19 @@ func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
httpError(w, util.NewErrBadRequest(err)) httpError(w, util.NewErrBadRequest(err))
return return
} }
areq := &action.CreateVariableRequest{
if !util.ValidateName(req.Name) {
httpError(w, util.NewErrBadRequest(errors.Errorf("invalid secret name %q", req.Name)))
return
}
if len(req.Values) == 0 {
httpError(w, util.NewErrBadRequest(errors.Errorf("empty variable values")))
return
}
v := &types.Variable{
Name: req.Name, Name: req.Name,
Parent: types.Parent{ ParentType: parentType,
Type: parentType, ParentRef: parentRef,
ID: parentRef,
},
Values: req.Values, Values: req.Values,
} }
csvar, cssecrets, err := h.ah.CreateVariable(ctx, areq)
var cssecrets []*csapi.Secret if httpError(w, err) {
var rv *csapi.Variable
switch parentType {
case types.ConfigTypeProjectGroup:
var err error
var resp *http.Response
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, parentRef, true)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
h.log.Infof("creating project group variable") res := createVariableResponse(csvar, cssecrets)
rv, resp, err = h.configstoreClient.CreateProjectGroupVariable(ctx, parentRef, v)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err)
return
}
case types.ConfigTypeProject:
var err error
var resp *http.Response
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, parentRef, true)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err)
return
}
h.log.Infof("creating project variable")
rv, resp, err = h.configstoreClient.CreateProjectVariable(ctx, parentRef, v)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err)
return
}
}
h.log.Infof("variable %s created, ID: %s", rv.Name, rv.ID)
res := createVariableResponse(rv, cssecrets)
if err := httpResponse(w, http.StatusCreated, res); err != nil { if err := httpResponse(w, http.StatusCreated, res); err != nil {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
} }
@ -229,11 +159,11 @@ func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
type DeleteVariableHandler struct { type DeleteVariableHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewDeleteVariableHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteVariableHandler { func NewDeleteVariableHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteVariableHandler {
return &DeleteVariableHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &DeleteVariableHandler{log: logger.Sugar(), ah: ah}
} }
func (h *DeleteVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *DeleteVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -247,16 +177,8 @@ func (h *DeleteVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
return return
} }
var resp *http.Response err = h.ah.DeleteVariable(ctx, parentType, parentRef, variableName)
switch parentType { if httpError(w, err) {
case types.ConfigTypeProjectGroup:
h.log.Infof("deleting project group variable")
resp, err = h.configstoreClient.DeleteProjectGroupVariable(ctx, parentRef, variableName)
case types.ConfigTypeProject:
h.log.Infof("deleting project variable")
resp, err = h.configstoreClient.DeleteProjectVariable(ctx, parentRef, variableName)
}
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }

View File

@ -20,7 +20,7 @@ import (
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
) )
func FilterOverridenVariables(variables []*csapi.Variable) []*csapi.Variable { func FilterOverriddenVariables(variables []*csapi.Variable) []*csapi.Variable {
variablesMap := map[string]*csapi.Variable{} variablesMap := map[string]*csapi.Variable{}
for _, v := range variables { for _, v := range variables {
if _, ok := variablesMap[v.Name]; !ok { if _, ok := variablesMap[v.Name]; !ok {

View File

@ -22,7 +22,7 @@ import (
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
) )
func TestFilterOverridenVariables(t *testing.T) { func TestFilterOverriddenVariables(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
variables []*csapi.Variable variables []*csapi.Variable
@ -106,7 +106,7 @@ func TestFilterOverridenVariables(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
out := FilterOverridenVariables(tt.variables) out := FilterOverriddenVariables(tt.variables)
if diff := cmp.Diff(tt.out, out); diff != "" { if diff := cmp.Diff(tt.out, out); diff != "" {
t.Error(diff) t.Error(diff)

View File

@ -162,9 +162,9 @@ func (g *Gateway) Run(ctx context.Context) error {
createSecretHandler := api.NewCreateSecretHandler(logger, g.configstoreClient) createSecretHandler := api.NewCreateSecretHandler(logger, g.configstoreClient)
deleteSecretHandler := api.NewDeleteSecretHandler(logger, g.configstoreClient) deleteSecretHandler := api.NewDeleteSecretHandler(logger, g.configstoreClient)
variableHandler := api.NewVariableHandler(logger, g.configstoreClient) variableHandler := api.NewVariableHandler(logger, g.ah)
createVariableHandler := api.NewCreateVariableHandler(logger, g.configstoreClient) createVariableHandler := api.NewCreateVariableHandler(logger, g.ah)
deleteVariableHandler := api.NewDeleteVariableHandler(logger, g.configstoreClient) deleteVariableHandler := api.NewDeleteVariableHandler(logger, g.ah)
currentUserHandler := api.NewCurrentUserHandler(logger, g.configstoreClient) currentUserHandler := api.NewCurrentUserHandler(logger, g.configstoreClient)
userHandler := api.NewUserHandler(logger, g.configstoreClient) userHandler := api.NewUserHandler(logger, g.configstoreClient)

View File

@ -198,7 +198,7 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
h.log.Infof("pvars: %v", util.Dump(pvars)) h.log.Infof("pvars: %v", util.Dump(pvars))
// remove overriden variables // remove overriden variables
pvars = common.FilterOverridenVariables(pvars) pvars = common.FilterOverriddenVariables(pvars)
h.log.Infof("pvars: %v", util.Dump(pvars)) h.log.Infof("pvars: %v", util.Dump(pvars))
// get project secrets // get project secrets