2019-02-21 16:58:25 +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/services/gateway/action"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
|
|
|
gwapitypes "agola.io/agola/services/gateway/api/types"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2019-07-31 13:17:54 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 16:58:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateRemoteSourceHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateRemoteSourceHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateRemoteSourceHandler {
|
|
|
|
return &CreateRemoteSourceHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.CreateRemoteSourceRequest
|
2019-02-21 16:58:25 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.CreateRemoteSourceRequest{
|
2019-05-07 13:59:08 +00:00
|
|
|
Name: req.Name,
|
|
|
|
APIURL: req.APIURL,
|
|
|
|
Type: req.Type,
|
|
|
|
AuthType: req.AuthType,
|
2019-05-22 14:28:42 +00:00
|
|
|
SkipVerify: req.SkipVerify,
|
2019-05-07 13:59:08 +00:00
|
|
|
Oauth2ClientID: req.Oauth2ClientID,
|
|
|
|
Oauth2ClientSecret: req.Oauth2ClientSecret,
|
|
|
|
SSHHostKey: req.SSHHostKey,
|
|
|
|
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
|
2019-07-05 11:33:29 +00:00
|
|
|
RegistrationEnabled: req.RegistrationEnabled,
|
|
|
|
LoginEnabled: req.LoginEnabled,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
rs, err := h.ah.CreateRemoteSource(ctx, creq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-09 12:53:00 +00:00
|
|
|
return
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
res := createRemoteSourceResponse(rs)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateRemoteSourceHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-23 08:29:03 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUpdateRemoteSourceHandler(log zerolog.Logger, ah *action.ActionHandler) *UpdateRemoteSourceHandler {
|
|
|
|
return &UpdateRemoteSourceHandler{log: log, ah: ah}
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UpdateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
rsRef := vars["remotesourceref"]
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.UpdateRemoteSourceRequest
|
2019-05-23 08:29:03 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-23 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
creq := &action.UpdateRemoteSourceRequest{
|
|
|
|
RemoteSourceRef: rsRef,
|
|
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
APIURL: req.APIURL,
|
|
|
|
SkipVerify: req.SkipVerify,
|
|
|
|
Oauth2ClientID: req.Oauth2ClientID,
|
|
|
|
Oauth2ClientSecret: req.Oauth2ClientSecret,
|
|
|
|
SSHHostKey: req.SSHHostKey,
|
|
|
|
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
|
2019-07-05 11:33:29 +00:00
|
|
|
RegistrationEnabled: req.RegistrationEnabled,
|
|
|
|
LoginEnabled: req.LoginEnabled,
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
|
|
|
rs, err := h.ah.UpdateRemoteSource(ctx, creq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-23 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createRemoteSourceResponse(rs)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createRemoteSourceResponse(r *cstypes.RemoteSource) *gwapitypes.RemoteSourceResponse {
|
|
|
|
rs := &gwapitypes.RemoteSourceResponse{
|
2019-07-05 11:33:29 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
AuthType: string(r.AuthType),
|
|
|
|
RegistrationEnabled: *r.RegistrationEnabled,
|
|
|
|
LoginEnabled: *r.LoginEnabled,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteSourceHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:54:16 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRemoteSourceHandler(log zerolog.Logger, ah *action.ActionHandler) *RemoteSourceHandler {
|
|
|
|
return &RemoteSourceHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RemoteSourceHandler) 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 16:58:25 +00:00
|
|
|
|
2019-05-05 12:54:16 +00:00
|
|
|
rs, err := h.ah.GetRemoteSource(ctx, rsRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createRemoteSourceResponse(rs)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteSourcesHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:54:16 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRemoteSourcesHandler(log zerolog.Logger, ah *action.ActionHandler) *RemoteSourcesHandler {
|
|
|
|
return &RemoteSourcesHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RemoteSourcesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultRunsLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("cannot parse limit: %w", err)))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("limit must be greater or equal than 0")))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > MaxRunsLimit {
|
|
|
|
limit = MaxRunsLimit
|
|
|
|
}
|
|
|
|
asc := false
|
|
|
|
if _, ok := query["asc"]; ok {
|
|
|
|
asc = true
|
|
|
|
}
|
|
|
|
|
|
|
|
start := query.Get("start")
|
|
|
|
|
2019-05-05 12:54:16 +00:00
|
|
|
areq := &action.GetRemoteSourcesRequest{
|
|
|
|
Start: start,
|
|
|
|
Limit: limit,
|
|
|
|
Asc: asc,
|
|
|
|
}
|
|
|
|
csRemoteSources, err := h.ah.GetRemoteSources(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
remoteSources := make([]*gwapitypes.RemoteSourceResponse, len(csRemoteSources))
|
2019-02-21 16:58:25 +00:00
|
|
|
for i, rs := range csRemoteSources {
|
|
|
|
remoteSources[i] = createRemoteSourceResponse(rs)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, remoteSources); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 22:03:03 +00:00
|
|
|
|
|
|
|
type DeleteRemoteSourceHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-09 22:03:03 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteRemoteSourceHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteRemoteSourceHandler {
|
|
|
|
return &DeleteRemoteSourceHandler{log: log, ah: ah}
|
2019-05-09 22:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
rsRef := vars["remotesourceref"]
|
|
|
|
|
|
|
|
err := h.ah.DeleteRemoteSource(ctx, rsRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 22:03:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 22:03:03 +00:00
|
|
|
}
|
|
|
|
}
|