gateway: move remaining user logic from api to actions

This commit is contained in:
Simone Gotti 2019-05-05 14:45:19 +02:00
parent 96918e9bad
commit 42184d0b5b
4 changed files with 98 additions and 50 deletions

View File

@ -40,6 +40,28 @@ func isAccessTokenExpired(expiresAt time.Time) bool {
return expiresAt.Add(-expireTimeRange).Before(time.Now()) return expiresAt.Add(-expireTimeRange).Before(time.Now())
} }
func (h *ActionHandler) GetUser(ctx context.Context, userRef string) (*types.User, error) {
user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return user, nil
}
type GetUsersRequest struct {
Start string
Limit int
Asc bool
}
func (h *ActionHandler) GetUsers(ctx context.Context, req *GetUsersRequest) ([]*types.User, error) {
users, resp, err := h.configstoreClient.GetUsers(ctx, req.Start, req.Limit, req.Asc)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return users, nil
}
type CreateUserRequest struct { type CreateUserRequest struct {
UserName string UserName string
} }
@ -705,3 +727,27 @@ func (h *ActionHandler) HandleOauth2Callback(ctx context.Context, code, state st
return h.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry) return h.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry)
} }
func (h *ActionHandler) DeleteUser(ctx context.Context, userRef string) error {
resp, err := h.configstoreClient.DeleteUser(ctx, userRef)
if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to delete user"))
}
return nil
}
func (h *ActionHandler) DeleteUserLA(ctx context.Context, userRef, laID string) error {
resp, err := h.configstoreClient.DeleteUserLA(ctx, userRef, laID)
if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to delete user linked account"))
}
return nil
}
func (h *ActionHandler) DeleteUserToken(ctx context.Context, userRef, tokenName string) error {
resp, err := h.configstoreClient.DeleteUserToken(ctx, userRef, tokenName)
if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to delete user token"))
}
return nil
}

View File

@ -17,7 +17,6 @@ package api
import ( import (
"net/http" "net/http"
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/util" "github.com/sorintlab/agola/internal/util"
@ -25,9 +24,8 @@ import (
) )
type OAuth2CallbackHandler struct { type OAuth2CallbackHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ah *action.ActionHandler ah *action.ActionHandler
configstoreClient *csapi.Client
} }
type RemoteSourceAuthResult struct { type RemoteSourceAuthResult struct {
@ -35,8 +33,8 @@ type RemoteSourceAuthResult struct {
Response interface{} `json:"response,omitempty"` Response interface{} `json:"response,omitempty"`
} }
func NewOAuth2CallbackHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client) *OAuth2CallbackHandler { func NewOAuth2CallbackHandler(logger *zap.Logger, ah *action.ActionHandler) *OAuth2CallbackHandler {
return &OAuth2CallbackHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient} return &OAuth2CallbackHandler{log: logger.Sugar(), ah: ah}
} }
func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

View File

@ -23,7 +23,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
gitsource "github.com/sorintlab/agola/internal/gitsources" gitsource "github.com/sorintlab/agola/internal/gitsources"
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"
@ -72,12 +71,12 @@ func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
type DeleteUserHandler struct { type DeleteUserHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewDeleteUserHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteUserHandler { func NewDeleteUserHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserHandler {
return &DeleteUserHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &DeleteUserHandler{log: logger.Sugar(), ah: ah}
} }
func (h *DeleteUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *DeleteUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -85,8 +84,8 @@ func (h *DeleteUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
userRef := vars["userref"] userRef := vars["userref"]
resp, err := h.configstoreClient.DeleteUser(ctx, userRef) err := h.ah.DeleteUser(ctx, userRef)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -97,12 +96,12 @@ func (h *DeleteUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
type CurrentUserHandler struct { type CurrentUserHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewCurrentUserHandler(logger *zap.Logger, configstoreClient *csapi.Client) *CurrentUserHandler { func NewCurrentUserHandler(logger *zap.Logger, ah *action.ActionHandler) *CurrentUserHandler {
return &CurrentUserHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &CurrentUserHandler{log: logger.Sugar(), ah: ah}
} }
func (h *CurrentUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CurrentUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -115,8 +114,8 @@ func (h *CurrentUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
userID := userIDVal.(string) userID := userIDVal.(string)
user, resp, err := h.configstoreClient.GetUser(ctx, userID) user, err := h.ah.GetUser(ctx, userID)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -128,12 +127,12 @@ func (h *CurrentUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
type UserHandler struct { type UserHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewUserHandler(logger *zap.Logger, configstoreClient *csapi.Client) *UserHandler { func NewUserHandler(logger *zap.Logger, ah *action.ActionHandler) *UserHandler {
return &UserHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &UserHandler{log: logger.Sugar(), ah: ah}
} }
func (h *UserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *UserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -141,8 +140,8 @@ func (h *UserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
userRef := vars["userref"] userRef := vars["userref"]
user, resp, err := h.configstoreClient.GetUser(ctx, userRef) user, err := h.ah.GetUser(ctx, userRef)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -186,12 +185,12 @@ func createUserResponse(u *types.User) *UserResponse {
} }
type UsersHandler struct { type UsersHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewUsersHandler(logger *zap.Logger, configstoreClient *csapi.Client) *UsersHandler { func NewUsersHandler(logger *zap.Logger, ah *action.ActionHandler) *UsersHandler {
return &UsersHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &UsersHandler{log: logger.Sugar(), ah: ah}
} }
func (h *UsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *UsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -223,8 +222,13 @@ func (h *UsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := query.Get("start") start := query.Get("start")
csusers, resp, err := h.configstoreClient.GetUsers(ctx, start, limit, asc) areq := &action.GetUsersRequest{
if httpErrorFromRemote(w, resp, err) { Start: start,
Limit: limit,
Asc: asc,
}
csusers, err := h.ah.GetUsers(ctx, areq)
if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -308,12 +312,12 @@ func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string,
} }
type DeleteUserLAHandler struct { type DeleteUserLAHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewDeleteUserLAHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteUserLAHandler { func NewDeleteUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserLAHandler {
return &DeleteUserLAHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &DeleteUserLAHandler{log: logger.Sugar(), ah: ah}
} }
func (h *DeleteUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *DeleteUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -322,8 +326,8 @@ func (h *DeleteUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
userRef := vars["userref"] userRef := vars["userref"]
laID := vars["laid"] laID := vars["laid"]
resp, err := h.configstoreClient.DeleteUserLA(ctx, userRef, laID) err := h.ah.DeleteUserLA(ctx, userRef, laID)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }
@ -383,12 +387,12 @@ func (h *CreateUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
} }
type DeleteUserTokenHandler struct { type DeleteUserTokenHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
configstoreClient *csapi.Client ah *action.ActionHandler
} }
func NewDeleteUserTokenHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteUserTokenHandler { func NewDeleteUserTokenHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteUserTokenHandler {
return &DeleteUserTokenHandler{log: logger.Sugar(), configstoreClient: configstoreClient} return &DeleteUserTokenHandler{log: logger.Sugar(), ah: ah}
} }
func (h *DeleteUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *DeleteUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -398,8 +402,8 @@ func (h *DeleteUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
tokenName := vars["tokenname"] tokenName := vars["tokenname"]
h.log.Infof("deleting user %q token %q", userRef, tokenName) h.log.Infof("deleting user %q token %q", userRef, tokenName)
resp, err := h.configstoreClient.DeleteUserToken(ctx, userRef, tokenName) err := h.ah.DeleteUserToken(ctx, userRef, tokenName)
if httpErrorFromRemote(w, resp, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
} }

View File

@ -166,16 +166,16 @@ func (g *Gateway) Run(ctx context.Context) error {
createVariableHandler := api.NewCreateVariableHandler(logger, g.ah) createVariableHandler := api.NewCreateVariableHandler(logger, g.ah)
deleteVariableHandler := api.NewDeleteVariableHandler(logger, g.ah) deleteVariableHandler := api.NewDeleteVariableHandler(logger, g.ah)
currentUserHandler := api.NewCurrentUserHandler(logger, g.configstoreClient) currentUserHandler := api.NewCurrentUserHandler(logger, g.ah)
userHandler := api.NewUserHandler(logger, g.configstoreClient) userHandler := api.NewUserHandler(logger, g.ah)
usersHandler := api.NewUsersHandler(logger, g.configstoreClient) usersHandler := api.NewUsersHandler(logger, g.ah)
createUserHandler := api.NewCreateUserHandler(logger, g.ah) createUserHandler := api.NewCreateUserHandler(logger, g.ah)
deleteUserHandler := api.NewDeleteUserHandler(logger, g.configstoreClient) deleteUserHandler := api.NewDeleteUserHandler(logger, g.ah)
createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ah) createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ah)
deleteUserLAHandler := api.NewDeleteUserLAHandler(logger, g.configstoreClient) deleteUserLAHandler := api.NewDeleteUserLAHandler(logger, g.ah)
createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ah) createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ah)
deleteUserTokenHandler := api.NewDeleteUserTokenHandler(logger, g.configstoreClient) deleteUserTokenHandler := api.NewDeleteUserTokenHandler(logger, g.ah)
remoteSourceHandler := api.NewRemoteSourceHandler(logger, g.configstoreClient) remoteSourceHandler := api.NewRemoteSourceHandler(logger, g.configstoreClient)
createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah) createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah)
@ -200,7 +200,7 @@ func (g *Gateway) Run(ctx context.Context) error {
loginUserHandler := api.NewLoginUserHandler(logger, g.ah) loginUserHandler := api.NewLoginUserHandler(logger, g.ah)
authorizeHandler := api.NewAuthorizeHandler(logger, g.ah) authorizeHandler := api.NewAuthorizeHandler(logger, g.ah)
registerHandler := api.NewRegisterUserHandler(logger, g.ah) registerHandler := api.NewRegisterUserHandler(logger, g.ah)
oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ah, g.configstoreClient) oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ah)
router := mux.NewRouter() router := mux.NewRouter()