agola/internal/services/gateway/api/variable.go

242 lines
6.2 KiB
Go
Raw Normal View History

2019-03-14 13:36:18 +00:00
// 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 api
import (
"encoding/json"
"net/http"
2019-07-01 09:40:20 +00:00
"agola.io/agola/internal/services/common"
csapi "agola.io/agola/internal/services/configstore/api"
cstypes "agola.io/agola/internal/services/configstore/types"
2019-07-01 09:40:20 +00:00
"agola.io/agola/internal/services/gateway/action"
"agola.io/agola/internal/util"
2019-03-14 13:36:18 +00:00
"go.uber.org/zap"
"github.com/gorilla/mux"
)
type VariableValue struct {
SecretName string `json:"secret_name"`
SecretVar string `json:"secret_var"`
MatchingSecretParentPath string `json:"matching_secret_parent_path"`
When *cstypes.When `json:"when"`
2019-03-14 13:36:18 +00:00
}
type VariableResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Values []VariableValue `json:"values"`
ParentPath string `json:"parent_path"`
}
func createVariableResponse(v *csapi.Variable, secrets []*csapi.Secret) *VariableResponse {
2019-03-14 13:36:18 +00:00
nv := &VariableResponse{
ID: v.ID,
Name: v.Name,
Values: make([]VariableValue, len(v.Values)),
ParentPath: v.ParentPath,
2019-03-14 13:36:18 +00:00
}
for i, varvalue := range v.Values {
nv.Values[i] = VariableValue{
SecretName: varvalue.SecretName,
SecretVar: varvalue.SecretVar,
When: varvalue.When,
}
// get matching secret for var value
secret := common.GetVarValueMatchingSecret(varvalue, v.ParentPath, secrets)
2019-03-14 13:36:18 +00:00
if secret != nil {
nv.Values[i].MatchingSecretParentPath = secret.ParentPath
2019-03-14 13:36:18 +00:00
}
}
return nv
}
type VariableHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
2019-03-14 13:36:18 +00:00
}
func NewVariableHandler(logger *zap.Logger, ah *action.ActionHandler) *VariableHandler {
return &VariableHandler{log: logger.Sugar(), ah: ah}
2019-03-14 13:36:18 +00:00
}
func (h *VariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.URL.Query()
_, tree := query["tree"]
_, removeoverridden := query["removeoverridden"]
2019-03-14 13:36:18 +00:00
parentType, parentRef, err := GetConfigTypeRef(r)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
areq := &action.GetVariablesRequest{
ParentType: parentType,
ParentRef: parentRef,
Tree: tree,
RemoveOverridden: removeoverridden,
2019-03-14 13:36:18 +00:00
}
csvars, cssecrets, err := h.ah.GetVariables(ctx, areq)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
2019-03-14 13:36:18 +00:00
}
variables := make([]*VariableResponse, len(csvars))
for i, v := range csvars {
variables[i] = createVariableResponse(v, cssecrets)
}
if err := httpResponse(w, http.StatusOK, variables); err != nil {
2019-04-05 14:33:00 +00:00
h.log.Errorf("err: %+v", err)
2019-03-14 13:36:18 +00:00
}
}
type CreateVariableRequest struct {
Name string `json:"name,omitempty"`
Values []cstypes.VariableValue `json:"values,omitempty"`
2019-03-14 13:36:18 +00:00
}
type CreateVariableHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
2019-03-14 13:36:18 +00:00
}
func NewCreateVariableHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateVariableHandler {
return &CreateVariableHandler{log: logger.Sugar(), ah: ah}
2019-03-14 13:36:18 +00:00
}
func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
parentType, parentRef, err := GetConfigTypeRef(r)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
var req CreateVariableRequest
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
httpError(w, util.NewErrBadRequest(err))
2019-03-14 13:36:18 +00:00
return
}
areq := &action.CreateVariableRequest{
Name: req.Name,
ParentType: parentType,
ParentRef: parentRef,
Values: req.Values,
2019-03-14 13:36:18 +00:00
}
csvar, cssecrets, err := h.ah.CreateVariable(ctx, areq)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
res := createVariableResponse(csvar, cssecrets)
if err := httpResponse(w, http.StatusCreated, res); err != nil {
2019-04-05 14:33:00 +00:00
h.log.Errorf("err: %+v", err)
2019-03-14 13:36:18 +00:00
}
}
2019-07-06 13:25:45 +00:00
type UpdateVariableRequest struct {
Name string `json:"name,omitempty"`
Values []cstypes.VariableValue `json:"values,omitempty"`
2019-07-06 13:25:45 +00:00
}
type UpdateVariableHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewUpdateVariableHandler(logger *zap.Logger, ah *action.ActionHandler) *UpdateVariableHandler {
return &UpdateVariableHandler{log: logger.Sugar(), ah: ah}
}
func (h *UpdateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
variableName := vars["variablename"]
parentType, parentRef, err := GetConfigTypeRef(r)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
var req UpdateVariableRequest
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
httpError(w, util.NewErrBadRequest(err))
return
}
areq := &action.UpdateVariableRequest{
VariableName: variableName,
Name: req.Name,
ParentType: parentType,
ParentRef: parentRef,
Values: req.Values,
}
csvar, cssecrets, err := h.ah.UpdateVariable(ctx, areq)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
res := createVariableResponse(csvar, cssecrets)
if err := httpResponse(w, http.StatusOK, res); err != nil {
h.log.Errorf("err: %+v", err)
}
}
2019-03-14 13:36:18 +00:00
type DeleteVariableHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
2019-03-14 13:36:18 +00:00
}
func NewDeleteVariableHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteVariableHandler {
return &DeleteVariableHandler{log: logger.Sugar(), ah: ah}
2019-03-14 13:36:18 +00:00
}
func (h *DeleteVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
variableName := vars["variablename"]
parentType, parentRef, err := GetConfigTypeRef(r)
if httpError(w, err) {
2019-04-05 14:33:00 +00:00
h.log.Errorf("err: %+v", err)
2019-03-14 13:36:18 +00:00
return
}
err = h.ah.DeleteVariable(ctx, parentType, parentRef, variableName)
if httpError(w, err) {
2019-04-05 14:33:00 +00:00
h.log.Errorf("err: %+v", err)
2019-03-14 13:36:18 +00:00
return
}
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
h.log.Errorf("err: %+v", err)
}
2019-03-14 13:36:18 +00:00
}