gateway: move remaining remotesource logic from api to actions

This commit is contained in:
Simone Gotti 2019-05-05 14:54:16 +02:00
parent 42184d0b5b
commit 6b5bd40417
3 changed files with 49 additions and 16 deletions

View File

@ -23,6 +23,28 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func (h *ActionHandler) GetRemoteSource(ctx context.Context, rsRef string) (*types.RemoteSource, error) {
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, rsRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return rs, nil
}
type GetRemoteSourcesRequest struct {
Start string
Limit int
Asc bool
}
func (h *ActionHandler) GetRemoteSources(ctx context.Context, req *GetRemoteSourcesRequest) ([]*types.RemoteSource, error) {
remoteSources, resp, err := h.configstoreClient.GetRemoteSources(ctx, req.Start, req.Limit, req.Asc)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return remoteSources, nil
}
type CreateRemoteSourceRequest struct { type CreateRemoteSourceRequest struct {
Name string Name string
APIURL string APIURL string
@ -82,3 +104,11 @@ func (h *ActionHandler) CreateRemoteSource(ctx context.Context, req *CreateRemot
return rs, nil return rs, nil
} }
func (h *ActionHandler) DeleteRemoteSource(ctx context.Context, rsRef string) error {
resp, err := h.configstoreClient.DeleteRemoteSource(ctx, rsRef)
if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to delete remote source"))
}
return nil
}

View File

@ -20,7 +20,6 @@ import (
"strconv" "strconv"
"github.com/pkg/errors" "github.com/pkg/errors"
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
@ -93,12 +92,12 @@ func createRemoteSourceResponse(r *types.RemoteSource) *RemoteSourceResponse {
} }
type RemoteSourceHandler struct { type RemoteSourceHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewRemoteSourceHandler(logger *zap.Logger, configstoreClient *csapi.Client) *RemoteSourceHandler { func NewRemoteSourceHandler(logger *zap.Logger, ah *action.ActionHandler) *RemoteSourceHandler {
return &RemoteSourceHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &RemoteSourceHandler{log: logger.Sugar(), ah: ah}
} }
func (h *RemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *RemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -106,8 +105,8 @@ func (h *RemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
vars := mux.Vars(r) vars := mux.Vars(r)
rsRef := vars["remotesourceref"] rsRef := vars["remotesourceref"]
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, rsRef) rs, err := h.ah.GetRemoteSource(ctx, rsRef)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -119,17 +118,16 @@ func (h *RemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
type RemoteSourcesHandler struct { type RemoteSourcesHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewRemoteSourcesHandler(logger *zap.Logger, configstoreClient *csapi.Client) *RemoteSourcesHandler { func NewRemoteSourcesHandler(logger *zap.Logger, ah *action.ActionHandler) *RemoteSourcesHandler {
return &RemoteSourcesHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &RemoteSourcesHandler{log: logger.Sugar(), ah: ah}
} }
func (h *RemoteSourcesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *RemoteSourcesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
query := r.URL.Query() query := r.URL.Query()
limitS := query.Get("limit") limitS := query.Get("limit")
@ -156,8 +154,13 @@ func (h *RemoteSourcesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
start := query.Get("start") start := query.Get("start")
csRemoteSources, resp, err := h.configstoreClient.GetRemoteSources(ctx, start, limit, asc) areq := &action.GetRemoteSourcesRequest{
if httpErrorFromRemote(w, resp, err) { Start: start,
Limit: limit,
Asc: asc,
}
csRemoteSources, err := h.ah.GetRemoteSources(ctx, areq)
if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }

View File

@ -177,9 +177,9 @@ func (g *Gateway) Run(ctx context.Context) error {
createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ah) createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ah)
deleteUserTokenHandler := api.NewDeleteUserTokenHandler(logger, g.ah) deleteUserTokenHandler := api.NewDeleteUserTokenHandler(logger, g.ah)
remoteSourceHandler := api.NewRemoteSourceHandler(logger, g.configstoreClient) remoteSourceHandler := api.NewRemoteSourceHandler(logger, g.ah)
createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah) createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah)
remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.configstoreClient) remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.ah)
orgHandler := api.NewOrgHandler(logger, g.ah) orgHandler := api.NewOrgHandler(logger, g.ah)
orgsHandler := api.NewOrgsHandler(logger, g.ah) orgsHandler := api.NewOrgsHandler(logger, g.ah)