gateway: move remaining project(group) logic from api to actions

This commit is contained in:
Simone Gotti 2019-05-05 14:27:22 +02:00
parent 64044df94d
commit c889c2c1c2
5 changed files with 93 additions and 62 deletions

View File

@ -28,6 +28,14 @@ import (
"github.com/pkg/errors"
)
func (h *ActionHandler) GetProject(ctx context.Context, projectRef string) (*csapi.Project, error) {
project, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return project, nil
}
type CreateProjectRequest struct {
CurrentUserID string
Name string
@ -200,3 +208,11 @@ func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string)
return h.SetupProject(ctx, rs, user, la, p)
}
func (h *ActionHandler) DeleteProject(ctx context.Context, projectRef string) error {
resp, err := h.configstoreClient.DeleteProject(ctx, projectRef)
if err != nil {
return ErrFromRemote(resp, err)
}
return nil
}

View File

@ -25,6 +25,30 @@ import (
"github.com/pkg/errors"
)
func (h *ActionHandler) GetProjectGroup(ctx context.Context, projectGroupRef string) (*csapi.ProjectGroup, error) {
projectGroup, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return projectGroup, nil
}
func (h *ActionHandler) GetProjectGroupSubgroups(ctx context.Context, projectGroupRef string) ([]*csapi.ProjectGroup, error) {
projectGroups, resp, err := h.configstoreClient.GetProjectGroupSubgroups(ctx, projectGroupRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return projectGroups, nil
}
func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*csapi.Project, error) {
projects, resp, err := h.configstoreClient.GetProjectGroupProjects(ctx, projectGroupRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return projects, nil
}
type CreateProjectGroupRequest struct {
CurrentUserID string
Name string

View File

@ -39,14 +39,13 @@ type CreateProjectRequest struct {
}
type CreateProjectHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
configstoreClient *csapi.Client
exposedURL string
log *zap.SugaredLogger
ah *action.ActionHandler
exposedURL string
}
func NewCreateProjectHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectHandler {
return &CreateProjectHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL}
func NewCreateProjectHandler(logger *zap.Logger, ah *action.ActionHandler, exposedURL string) *CreateProjectHandler {
return &CreateProjectHandler{log: logger.Sugar(), ah: ah, exposedURL: exposedURL}
}
func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -90,14 +89,13 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
}
type ProjectReconfigHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
configstoreClient *csapi.Client
exposedURL string
log *zap.SugaredLogger
ah *action.ActionHandler
exposedURL string
}
func NewProjectReconfigHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *ProjectReconfigHandler {
return &ProjectReconfigHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL}
func NewProjectReconfigHandler(logger *zap.Logger, ah *action.ActionHandler, exposedURL string) *ProjectReconfigHandler {
return &ProjectReconfigHandler{log: logger.Sugar(), ah: ah, exposedURL: exposedURL}
}
func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -119,12 +117,12 @@ func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
}
type DeleteProjectHandler struct {
log *zap.SugaredLogger
configstoreClient *csapi.Client
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewDeleteProjectHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteProjectHandler {
return &DeleteProjectHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
func NewDeleteProjectHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteProjectHandler {
return &DeleteProjectHandler{log: logger.Sugar(), ah: ah}
}
func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -136,14 +134,8 @@ func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
return
}
project, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if httpErrorFromRemote(w, resp, err) {
h.log.Errorf("err: %+v", err)
return
}
resp, err = h.configstoreClient.DeleteProject(ctx, project.ID)
if httpErrorFromRemote(w, resp, err) {
err = h.ah.DeleteProject(ctx, projectRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
@ -154,12 +146,12 @@ func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
}
type ProjectHandler struct {
log *zap.SugaredLogger
configstoreClient *csapi.Client
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewProjectHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectHandler {
return &ProjectHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
func NewProjectHandler(logger *zap.Logger, ah *action.ActionHandler) *ProjectHandler {
return &ProjectHandler{log: logger.Sugar(), ah: ah}
}
func (h *ProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -171,8 +163,8 @@ func (h *ProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
project, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if httpErrorFromRemote(w, resp, err) {
project, err := h.ah.GetProject(ctx, projectRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}

View File

@ -36,14 +36,13 @@ type CreateProjectGroupRequest struct {
}
type CreateProjectGroupHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
configstoreClient *csapi.Client
exposedURL string
log *zap.SugaredLogger
ah *action.ActionHandler
exposedURL string
}
func NewCreateProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectGroupHandler {
return &CreateProjectGroupHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL}
func NewCreateProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler, exposedURL string) *CreateProjectGroupHandler {
return &CreateProjectGroupHandler{log: logger.Sugar(), ah: ah, exposedURL: exposedURL}
}
func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -84,12 +83,12 @@ func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
}
type ProjectGroupHandler struct {
log *zap.SugaredLogger
configstoreClient *csapi.Client
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewProjectGroupHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectGroupHandler {
return &ProjectGroupHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
func NewProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler) *ProjectGroupHandler {
return &ProjectGroupHandler{log: logger.Sugar(), ah: ah}
}
func (h *ProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -101,8 +100,8 @@ func (h *ProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
return
}
projectGroup, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
if httpErrorFromRemote(w, resp, err) {
projectGroup, err := h.ah.GetProjectGroup(ctx, projectGroupRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
@ -114,12 +113,12 @@ func (h *ProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
}
type ProjectGroupProjectsHandler struct {
log *zap.SugaredLogger
configstoreClient *csapi.Client
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewProjectGroupProjectsHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectGroupProjectsHandler {
return &ProjectGroupProjectsHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
func NewProjectGroupProjectsHandler(logger *zap.Logger, ah *action.ActionHandler) *ProjectGroupProjectsHandler {
return &ProjectGroupProjectsHandler{log: logger.Sugar(), ah: ah}
}
func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -131,8 +130,8 @@ func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.R
return
}
csprojects, resp, err := h.configstoreClient.GetProjectGroupProjects(ctx, projectGroupRef)
if httpErrorFromRemote(w, resp, err) {
csprojects, err := h.ah.GetProjectGroupProjects(ctx, projectGroupRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
@ -148,12 +147,12 @@ func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.R
}
type ProjectGroupSubgroupsHandler struct {
log *zap.SugaredLogger
configstoreClient *csapi.Client
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewProjectGroupSubgroupsHandler(logger *zap.Logger, configstoreClient *csapi.Client) *ProjectGroupSubgroupsHandler {
return &ProjectGroupSubgroupsHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
func NewProjectGroupSubgroupsHandler(logger *zap.Logger, ah *action.ActionHandler) *ProjectGroupSubgroupsHandler {
return &ProjectGroupSubgroupsHandler{log: logger.Sugar(), ah: ah}
}
func (h *ProjectGroupSubgroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -165,8 +164,8 @@ func (h *ProjectGroupSubgroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.
return
}
cssubgroups, resp, err := h.configstoreClient.GetProjectGroupSubgroups(ctx, projectGroupRef)
if httpErrorFromRemote(w, resp, err) {
cssubgroups, err := h.ah.GetProjectGroupSubgroups(ctx, projectGroupRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}

View File

@ -148,15 +148,15 @@ func (g *Gateway) Run(ctx context.Context) error {
webhooksHandler := &webhooksHandler{log: log, ah: g.ah, configstoreClient: g.configstoreClient, runserviceClient: g.runserviceClient, apiExposedURL: g.c.APIExposedURL}
projectGroupHandler := api.NewProjectGroupHandler(logger, g.configstoreClient)
projectGroupSubgroupsHandler := api.NewProjectGroupSubgroupsHandler(logger, g.configstoreClient)
projectGroupProjectsHandler := api.NewProjectGroupProjectsHandler(logger, g.configstoreClient)
createProjectGroupHandler := api.NewCreateProjectGroupHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL)
projectGroupHandler := api.NewProjectGroupHandler(logger, g.ah)
projectGroupSubgroupsHandler := api.NewProjectGroupSubgroupsHandler(logger, g.ah)
projectGroupProjectsHandler := api.NewProjectGroupProjectsHandler(logger, g.ah)
createProjectGroupHandler := api.NewCreateProjectGroupHandler(logger, g.ah, g.c.APIExposedURL)
projectHandler := api.NewProjectHandler(logger, g.configstoreClient)
createProjectHandler := api.NewCreateProjectHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL)
deleteProjectHandler := api.NewDeleteProjectHandler(logger, g.configstoreClient)
projectReconfigHandler := api.NewProjectReconfigHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL)
projectHandler := api.NewProjectHandler(logger, g.ah)
createProjectHandler := api.NewCreateProjectHandler(logger, g.ah, g.c.APIExposedURL)
deleteProjectHandler := api.NewDeleteProjectHandler(logger, g.ah)
projectReconfigHandler := api.NewProjectReconfigHandler(logger, g.ah, g.c.APIExposedURL)
secretHandler := api.NewSecretHandler(logger, g.ah)
createSecretHandler := api.NewCreateSecretHandler(logger, g.ah)