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"
|
|
|
|
"strconv"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/db"
|
|
|
|
"agola.io/agola/internal/services/configstore/action"
|
|
|
|
"agola.io/agola/internal/services/configstore/readdb"
|
|
|
|
"agola.io/agola/internal/services/types"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 15:08:30 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
type RemoteSourceHandler struct {
|
2019-02-21 15:08:30 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
func NewRemoteSourceHandler(logger *zap.Logger, readDB *readdb.ReadDB) *RemoteSourceHandler {
|
|
|
|
return &RemoteSourceHandler{log: logger.Sugar(), readDB: readDB}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
func (h *RemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-21 15:08:30 +00:00
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
rsRef := vars["remotesourceref"]
|
2019-02-21 15:08:30 +00:00
|
|
|
|
|
|
|
var remoteSource *types.RemoteSource
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-02-21 15:08:30 +00:00
|
|
|
var err error
|
2019-05-03 09:07:53 +00:00
|
|
|
remoteSource, err = h.readDB.GetRemoteSource(tx, rsRef)
|
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 remoteSource == nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
httpError(w, util.NewErrNotFound(errors.Errorf("remote source %q doesn't exist", rsRef)))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, remoteSource); 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 CreateRemoteSourceHandler struct {
|
2019-07-02 12:46:00 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateRemoteSourceHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateRemoteSourceHandler {
|
|
|
|
return &CreateRemoteSourceHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req types.RemoteSource
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
remoteSource, err := h.ah.CreateRemoteSource(ctx, &req)
|
2019-03-14 08:34:05 +00:00
|
|
|
if httpError(w, err) {
|
2019-05-23 08:29:03 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, remoteSource); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateRemoteSourceHandler struct {
|
2019-07-02 12:46:00 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewUpdateRemoteSourceHandler(logger *zap.Logger, ah *action.ActionHandler) *UpdateRemoteSourceHandler {
|
|
|
|
return &UpdateRemoteSourceHandler{log: logger.Sugar(), ah: ah}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UpdateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
rsRef := vars["remotesourceref"]
|
|
|
|
|
|
|
|
var remoteSource *types.RemoteSource
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&remoteSource); err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
areq := &action.UpdateRemoteSourceRequest{
|
|
|
|
RemoteSourceRef: rsRef,
|
|
|
|
RemoteSource: remoteSource,
|
|
|
|
}
|
|
|
|
remoteSource, err := h.ah.UpdateRemoteSource(ctx, areq)
|
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, remoteSource); 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 DeleteRemoteSourceHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewDeleteRemoteSourceHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteRemoteSourceHandler {
|
|
|
|
return &DeleteRemoteSourceHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
rsRef := vars["remotesourceref"]
|
2019-02-21 15:08:30 +00:00
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
err := h.ah.DeleteRemoteSource(ctx, rsRef)
|
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 (
|
|
|
|
DefaultRemoteSourcesLimit = 10
|
|
|
|
MaxRemoteSourcesLimit = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
type RemoteSourcesHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoteSourcesHandler(logger *zap.Logger, readDB *readdb.ReadDB) *RemoteSourcesHandler {
|
|
|
|
return &RemoteSourcesHandler{log: logger.Sugar(), readDB: readDB}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RemoteSourcesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-21 15:08:30 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultRemoteSourcesLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("cannot parse limit: %w", err)))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("limit must be greater or equal than 0")))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > MaxRemoteSourcesLimit {
|
|
|
|
limit = MaxRemoteSourcesLimit
|
|
|
|
}
|
|
|
|
asc := false
|
|
|
|
if _, ok := query["asc"]; ok {
|
|
|
|
asc = true
|
|
|
|
}
|
|
|
|
|
|
|
|
start := query.Get("start")
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
remoteSources, err := h.readDB.GetRemoteSources(ctx, start, limit, asc)
|
2019-02-21 15:08:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, remoteSources); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
}
|