gateway: move remaining org logic from api to actions

This commit is contained in:
Simone Gotti 2019-05-05 14:36:14 +02:00
parent c889c2c1c2
commit 96918e9bad
3 changed files with 56 additions and 23 deletions

View File

@ -23,6 +23,28 @@ import (
"github.com/pkg/errors" "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 { type CreateOrgRequest struct {
Name string Name string
@ -53,3 +75,11 @@ func (h *ActionHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*
return org, nil 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
}

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"
@ -77,12 +76,12 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
type DeleteOrgHandler struct { type DeleteOrgHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewDeleteOrgHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteOrgHandler { func NewDeleteOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteOrgHandler {
return &DeleteOrgHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &DeleteOrgHandler{log: logger.Sugar(), ah: ah}
} }
func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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) vars := mux.Vars(r)
orgRef := vars["orgref"] orgRef := vars["orgref"]
resp, err := h.configstoreClient.DeleteOrg(ctx, orgRef) err := h.ah.DeleteOrg(ctx, orgRef)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -102,12 +101,12 @@ func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
type OrgHandler struct { type OrgHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewOrgHandler(logger *zap.Logger, configstoreClient *csapi.Client) *OrgHandler { func NewOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgHandler {
return &OrgHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &OrgHandler{log: logger.Sugar(), ah: ah}
} }
func (h *OrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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) vars := mux.Vars(r)
orgRef := vars["orgref"] orgRef := vars["orgref"]
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef) org, err := h.ah.GetOrg(ctx, orgRef)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -141,17 +140,16 @@ func createOrgResponse(o *types.Organization) *OrgResponse {
} }
type OrgsHandler struct { type OrgsHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewOrgsHandler(logger *zap.Logger, configstoreClient *csapi.Client) *OrgsHandler { func NewOrgsHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgsHandler {
return &OrgsHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &OrgsHandler{log: logger.Sugar(), ah: ah}
} }
func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *OrgsHandler) 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")
@ -178,8 +176,13 @@ func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := query.Get("start") start := query.Get("start")
csorgs, resp, err := h.configstoreClient.GetOrgs(ctx, start, limit, asc) areq := &action.GetOrgsRequest{
if httpErrorFromRemote(w, resp, err) { Start: start,
Limit: limit,
Asc: asc,
}
csorgs, err := h.ah.GetOrgs(ctx, areq)
if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }

View File

@ -181,10 +181,10 @@ func (g *Gateway) Run(ctx context.Context) error {
createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah) createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah)
remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.configstoreClient) remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.configstoreClient)
orgHandler := api.NewOrgHandler(logger, g.configstoreClient) orgHandler := api.NewOrgHandler(logger, g.ah)
orgsHandler := api.NewOrgsHandler(logger, g.configstoreClient) orgsHandler := api.NewOrgsHandler(logger, g.ah)
createOrgHandler := api.NewCreateOrgHandler(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) runHandler := api.NewRunHandler(logger, g.runserviceClient)
runsHandler := api.NewRunsHandler(logger, g.runserviceClient) runsHandler := api.NewRunsHandler(logger, g.runserviceClient)