gateway: add get org members api/action

This commit is contained in:
Simone Gotti 2019-05-14 12:58:29 +02:00
parent 21e706a150
commit 7d6223ccd4
5 changed files with 77 additions and 2 deletions

View File

@ -45,6 +45,40 @@ func (h *ActionHandler) GetOrgs(ctx context.Context, req *GetOrgsRequest) ([]*ty
return orgs, nil
}
type OrgMembersResponse struct {
Organization *types.Organization
Members []*OrgMemberResponse
}
type OrgMemberResponse struct {
User *types.User
Role types.MemberRole
}
func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) (*OrgMembersResponse, error) {
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
orgMembers, resp, err := h.configstoreClient.GetOrgMembers(ctx, orgRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
res := &OrgMembersResponse{
Organization: org,
Members: make([]*OrgMemberResponse, len(orgMembers)),
}
for i, orgMember := range orgMembers {
res.Members[i] = &OrgMemberResponse{
User: orgMember.User,
Role: orgMember.Role,
}
}
return res, nil
}
type CreateOrgRequest struct {
Name string
Visibility types.Visibility

View File

@ -416,3 +416,9 @@ func (c *Client) AddOrgMember(ctx context.Context, orgRef, userRef string, role
func (c *Client) RemoveOrgMember(ctx context.Context, orgRef, userRef string) (*http.Response, error) {
return c.getResponse(ctx, "DELETE", fmt.Sprintf("/orgs/%s/members/%s", orgRef, userRef), nil, jsonContent, nil)
}
func (c *Client) GetOrgMembers(ctx context.Context, orgRef string) (*OrgMembersResponse, *http.Response, error) {
res := &OrgMembersResponse{}
resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/orgs/%s/members", orgRef), nil, jsonContent, nil, &res)
return res, resp, err
}

View File

@ -217,6 +217,39 @@ func createOrgMemberResponse(user *types.User, role types.MemberRole) *OrgMember
}
}
type OrgMembersHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewOrgMembersHandler(logger *zap.Logger, ah *action.ActionHandler) *OrgMembersHandler {
return &OrgMembersHandler{log: logger.Sugar(), ah: ah}
}
func (h *OrgMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
orgRef := vars["orgref"]
ares, err := h.ah.GetOrgMembers(ctx, orgRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
res := &OrgMembersResponse{
Organization: createOrgResponse(ares.Organization),
Members: make([]*OrgMemberResponse, len(ares.Members)),
}
for i, m := range ares.Members {
res.Members[i] = createOrgMemberResponse(m.User, m.Role)
}
if err := httpResponse(w, http.StatusOK, res); err != nil {
h.log.Errorf("err: %+v", err)
}
}
type AddOrgMemberResponse struct {
Organization *OrgResponse `json:"organization"`
OrgMemberResponse

View File

@ -21,14 +21,14 @@ import (
"sort"
"strconv"
"github.com/pkg/errors"
gitsource "github.com/sorintlab/agola/internal/gitsources"
"github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util"
"go.uber.org/zap"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"go.uber.org/zap"
)
type CreateUserRequest struct {

View File

@ -191,6 +191,7 @@ func (g *Gateway) Run(ctx context.Context) error {
createOrgHandler := api.NewCreateOrgHandler(logger, g.ah)
deleteOrgHandler := api.NewDeleteOrgHandler(logger, g.ah)
orgMembersHandler := api.NewOrgMembersHandler(logger, g.ah)
addOrgMemberHandler := api.NewAddOrgMemberHandler(logger, g.ah)
removeOrgMemberHandler := api.NewRemoveOrgMemberHandler(logger, g.ah)
@ -272,6 +273,7 @@ func (g *Gateway) Run(ctx context.Context) error {
apirouter.Handle("/orgs", authForcedHandler(orgsHandler)).Methods("GET")
apirouter.Handle("/orgs", authForcedHandler(createOrgHandler)).Methods("POST")
apirouter.Handle("/orgs/{orgref}", authForcedHandler(deleteOrgHandler)).Methods("DELETE")
apirouter.Handle("/orgs/{orgref}/members", authForcedHandler(orgMembersHandler)).Methods("GET")
apirouter.Handle("/orgs/{orgref}/members/{userref}", authForcedHandler(addOrgMemberHandler)).Methods("PUT")
apirouter.Handle("/orgs/{orgref}/members/{userref}", authForcedHandler(removeOrgMemberHandler)).Methods("DELETE")