From 96918e9bad8b85b1c3be9074217d24b9c7cbd23f Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Sun, 5 May 2019 14:36:14 +0200 Subject: [PATCH] gateway: move remaining org logic from api to actions --- internal/services/gateway/action/org.go | 30 +++++++++++++++++ internal/services/gateway/api/org.go | 43 +++++++++++++------------ internal/services/gateway/gateway.go | 6 ++-- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/internal/services/gateway/action/org.go b/internal/services/gateway/action/org.go index dd7319d..9112b24 100644 --- a/internal/services/gateway/action/org.go +++ b/internal/services/gateway/action/org.go @@ -23,6 +23,28 @@ import ( "github.com/pkg/errors" ) +func (h *ActionHandler) GetOrg(ctx context.Context, orgRef string) (*types.Organization, error) { + org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef) + if err != nil { + return nil, ErrFromRemote(resp, err) + } + return org, nil +} + +type GetOrgsRequest struct { + Start string + Limit int + Asc bool +} + +func (h *ActionHandler) GetOrgs(ctx context.Context, req *GetOrgsRequest) ([]*types.Organization, error) { + orgs, resp, err := h.configstoreClient.GetOrgs(ctx, req.Start, req.Limit, req.Asc) + if err != nil { + return nil, ErrFromRemote(resp, err) + } + return orgs, nil +} + type CreateOrgRequest struct { Name string @@ -53,3 +75,11 @@ func (h *ActionHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (* return org, nil } + +func (h *ActionHandler) DeleteOrg(ctx context.Context, orgRef string) error { + resp, err := h.configstoreClient.DeleteOrg(ctx, orgRef) + if err != nil { + return ErrFromRemote(resp, errors.Wrapf(err, "failed to delete org")) + } + return nil +} diff --git a/internal/services/gateway/api/org.go b/internal/services/gateway/api/org.go index dd99c4d..68c4b20 100644 --- a/internal/services/gateway/api/org.go +++ b/internal/services/gateway/api/org.go @@ -20,7 +20,6 @@ import ( "strconv" "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/types" "github.com/sorintlab/agola/internal/util" @@ -77,12 +76,12 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } type DeleteOrgHandler struct { - log *zap.SugaredLogger - configstoreClient *csapi.Client + log *zap.SugaredLogger + ah *action.ActionHandler } -func NewDeleteOrgHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteOrgHandler { - return &DeleteOrgHandler{log: logger.Sugar(), configstoreClient: configstoreClient} +func NewDeleteOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteOrgHandler { + return &DeleteOrgHandler{log: logger.Sugar(), ah: ah} } func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -90,8 +89,8 @@ func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) orgRef := vars["orgref"] - resp, err := h.configstoreClient.DeleteOrg(ctx, orgRef) - if httpErrorFromRemote(w, resp, err) { + err := h.ah.DeleteOrg(ctx, orgRef) + if httpError(w, err) { h.log.Errorf("err: %+v", err) return } @@ -102,12 +101,12 @@ func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } type OrgHandler struct { - log *zap.SugaredLogger - configstoreClient *csapi.Client + log *zap.SugaredLogger + ah *action.ActionHandler } -func NewOrgHandler(logger *zap.Logger, configstoreClient *csapi.Client) *OrgHandler { - return &OrgHandler{log: logger.Sugar(), configstoreClient: configstoreClient} +func NewOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgHandler { + return &OrgHandler{log: logger.Sugar(), ah: ah} } func (h *OrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -115,8 +114,8 @@ func (h *OrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) orgRef := vars["orgref"] - org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef) - if httpErrorFromRemote(w, resp, err) { + org, err := h.ah.GetOrg(ctx, orgRef) + if httpError(w, err) { h.log.Errorf("err: %+v", err) return } @@ -141,17 +140,16 @@ func createOrgResponse(o *types.Organization) *OrgResponse { } type OrgsHandler struct { - log *zap.SugaredLogger - configstoreClient *csapi.Client + log *zap.SugaredLogger + ah *action.ActionHandler } -func NewOrgsHandler(logger *zap.Logger, configstoreClient *csapi.Client) *OrgsHandler { - return &OrgsHandler{log: logger.Sugar(), configstoreClient: configstoreClient} +func NewOrgsHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgsHandler { + return &OrgsHandler{log: logger.Sugar(), ah: ah} } func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - query := r.URL.Query() limitS := query.Get("limit") @@ -178,8 +176,13 @@ func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { start := query.Get("start") - csorgs, resp, err := h.configstoreClient.GetOrgs(ctx, start, limit, asc) - if httpErrorFromRemote(w, resp, err) { + areq := &action.GetOrgsRequest{ + Start: start, + Limit: limit, + Asc: asc, + } + csorgs, err := h.ah.GetOrgs(ctx, areq) + if httpError(w, err) { h.log.Errorf("err: %+v", err) return } diff --git a/internal/services/gateway/gateway.go b/internal/services/gateway/gateway.go index 007f251..326a3da 100644 --- a/internal/services/gateway/gateway.go +++ b/internal/services/gateway/gateway.go @@ -181,10 +181,10 @@ func (g *Gateway) Run(ctx context.Context) error { createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah) remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.configstoreClient) - orgHandler := api.NewOrgHandler(logger, g.configstoreClient) - orgsHandler := api.NewOrgsHandler(logger, g.configstoreClient) + orgHandler := api.NewOrgHandler(logger, g.ah) + orgsHandler := api.NewOrgsHandler(logger, g.ah) createOrgHandler := api.NewCreateOrgHandler(logger, g.ah) - deleteOrgHandler := api.NewDeleteOrgHandler(logger, g.configstoreClient) + deleteOrgHandler := api.NewDeleteOrgHandler(logger, g.ah) runHandler := api.NewRunHandler(logger, g.runserviceClient) runsHandler := api.NewRunsHandler(logger, g.runserviceClient)