agola/internal/services/configstore/api/project.go
Simone Gotti 3642be6f21 */api: Use helpers for error handling
* client: always parse the json error message field and return its contents
* Use ErrBadRequest and ErrNotFound in every handler and command
* Gateway: by default pass underlying service error (configstore, runservice) to
client keeping the status code and message. In future, if some errors must be
masked, we should change the specific parts that need special handling.
2019-04-09 14:53:00 +02:00

147 lines
3.7 KiB
Go

// 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"
"net/url"
"github.com/pkg/errors"
"github.com/sorintlab/agola/internal/db"
"github.com/sorintlab/agola/internal/services/configstore/command"
"github.com/sorintlab/agola/internal/services/configstore/common"
"github.com/sorintlab/agola/internal/services/configstore/readdb"
"github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util"
"github.com/gorilla/mux"
"go.uber.org/zap"
)
type ProjectHandler struct {
log *zap.SugaredLogger
readDB *readdb.ReadDB
}
func NewProjectHandler(logger *zap.Logger, readDB *readdb.ReadDB) *ProjectHandler {
return &ProjectHandler{log: logger.Sugar(), readDB: readDB}
}
func (h *ProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
projectRef, err := url.PathUnescape(vars["projectref"])
if err != nil {
httpError(w, util.NewErrBadRequest(err))
return
}
projectRefType, err := common.ParseRef(projectRef)
if err != nil {
httpError(w, util.NewErrBadRequest(err))
return
}
var project *types.Project
err = h.readDB.Do(func(tx *db.Tx) error {
var err error
switch projectRefType {
case common.RefTypeID:
project, err = h.readDB.GetProject(tx, projectRef)
case common.RefTypePath:
project, err = h.readDB.GetProjectByPath(tx, projectRef)
}
return err
})
if err != nil {
h.log.Errorf("err: %+v", err)
httpError(w, err)
return
}
if project == nil {
httpError(w, util.NewErrNotFound(errors.Errorf("project %q doesn't exist", projectRef)))
return
}
if err := httpResponse(w, http.StatusOK, project); err != nil {
h.log.Errorf("err: %+v", err)
}
}
type CreateProjectHandler struct {
log *zap.SugaredLogger
ch *command.CommandHandler
readDB *readdb.ReadDB
}
func NewCreateProjectHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateProjectHandler {
return &CreateProjectHandler{log: logger.Sugar(), ch: ch}
}
func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var req types.Project
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
httpError(w, util.NewErrBadRequest(err))
return
}
project, err := h.ch.CreateProject(ctx, &req)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
if err := httpResponse(w, http.StatusCreated, project); err != nil {
h.log.Errorf("err: %+v", err)
}
}
type DeleteProjectHandler struct {
log *zap.SugaredLogger
ch *command.CommandHandler
}
func NewDeleteProjectHandler(logger *zap.Logger, ch *command.CommandHandler) *DeleteProjectHandler {
return &DeleteProjectHandler{log: logger.Sugar(), ch: ch}
}
func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
projectRef, err := url.PathUnescape(vars["projectref"])
if err != nil {
httpError(w, util.NewErrBadRequest(err))
return
}
err = h.ch.DeleteProject(ctx, projectRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
}
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
h.log.Errorf("err: %+v", err)
}
}
const (
DefaultProjectsLimit = 10
MaxProjectsLimit = 20
)