2019-02-21 15:08:30 +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-03-14 13:36:18 +00:00
|
|
|
"net/url"
|
2019-04-30 15:09:26 +00:00
|
|
|
"path"
|
2019-02-21 15:08:30 +00:00
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-02-21 15:08:30 +00:00
|
|
|
"github.com/sorintlab/agola/internal/db"
|
|
|
|
"github.com/sorintlab/agola/internal/services/configstore/command"
|
2019-03-14 13:36:18 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/configstore/common"
|
2019-02-21 15:08:30 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/configstore/readdb"
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-02-21 15:08:30 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
// Project augments types.Project with dynamic data
|
|
|
|
type Project struct {
|
|
|
|
*types.Project
|
|
|
|
|
|
|
|
// dynamic data
|
|
|
|
Path string
|
|
|
|
ParentPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func projectResponse(readDB *readdb.ReadDB, project *types.Project) (*Project, error) {
|
|
|
|
r, err := projectsResponse(readDB, []*types.Project{project})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func projectsResponse(readDB *readdb.ReadDB, projects []*types.Project) ([]*Project, error) {
|
|
|
|
resProjects := make([]*Project, len(projects))
|
|
|
|
|
|
|
|
err := readDB.Do(func(tx *db.Tx) error {
|
|
|
|
for i, project := range projects {
|
|
|
|
pp, err := readDB.GetPath(tx, project.Parent.Type, project.Parent.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// we calculate the path here from parent path since the db could not yet be
|
|
|
|
// updated on create
|
|
|
|
resProjects[i] = &Project{
|
|
|
|
Project: project,
|
|
|
|
Path: path.Join(pp, project.Name),
|
|
|
|
ParentPath: pp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return resProjects, err
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
type ProjectHandler struct {
|
2019-02-21 15:08:30 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
func NewProjectHandler(logger *zap.Logger, readDB *readdb.ReadDB) *ProjectHandler {
|
|
|
|
return &ProjectHandler{log: logger.Sugar(), readDB: readDB}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
func (h *ProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-02-21 15:08:30 +00:00
|
|
|
vars := mux.Vars(r)
|
2019-03-14 13:36:18 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-21 15:08:30 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
projectRefType, err := common.ParseRef(projectRef)
|
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var project *types.Project
|
2019-03-14 13:36:18 +00:00
|
|
|
err = h.readDB.Do(func(tx *db.Tx) error {
|
2019-02-21 15:08:30 +00:00
|
|
|
var err error
|
2019-03-14 13:36:18 +00:00
|
|
|
switch projectRefType {
|
|
|
|
case common.RefTypeID:
|
|
|
|
project, err = h.readDB.GetProject(tx, projectRef)
|
|
|
|
case common.RefTypePath:
|
|
|
|
project, err = h.readDB.GetProjectByPath(tx, projectRef)
|
|
|
|
}
|
2019-02-21 15:08:30 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
httpError(w, err)
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if project == nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrNotFound(errors.Errorf("project %q doesn't exist", projectRef)))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
resProject, err := projectResponse(h.readDB, project)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusOK, resProject); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateProjectHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ch *command.CommandHandler
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
func NewCreateProjectHandler(logger *zap.Logger, ch *command.CommandHandler, readDB *readdb.ReadDB) *CreateProjectHandler {
|
|
|
|
return &CreateProjectHandler{log: logger.Sugar(), ch: ch, readDB: readDB}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := h.ch.CreateProject(ctx, &req)
|
2019-03-14 08:34:05 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
resProject, err := projectResponse(h.readDB, project)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, resProject); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-03-14 13:36:18 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-21 15:08:30 +00:00
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
err = h.ch.DeleteProject(ctx, projectRef)
|
2019-03-14 08:34:05 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultProjectsLimit = 10
|
|
|
|
MaxProjectsLimit = 20
|
|
|
|
)
|