diff --git a/internal/services/configstore/action/org.go b/internal/services/configstore/action/org.go index 03bfe12..58b47e2 100644 --- a/internal/services/configstore/action/org.go +++ b/internal/services/configstore/action/org.go @@ -22,6 +22,7 @@ import ( "github.com/sorintlab/agola/internal/datamanager" "github.com/sorintlab/agola/internal/db" + "github.com/sorintlab/agola/internal/services/configstore/readdb" "github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/util" @@ -29,6 +30,45 @@ import ( uuid "github.com/satori/go.uuid" ) +type OrgMemberResponse struct { + User *types.User + Role types.MemberRole +} + +func orgMemberResponse(orgUser *readdb.OrgUser) *OrgMemberResponse { + return &OrgMemberResponse{ + User: orgUser.User, + Role: orgUser.Role, + } +} + +func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) ([]*OrgMemberResponse, error) { + var orgUsers []*readdb.OrgUser + err := h.readDB.Do(func(tx *db.Tx) error { + var err error + org, err := h.readDB.GetOrg(tx, orgRef) + if err != nil { + return err + } + if org == nil { + return util.NewErrNotFound(errors.Errorf("org %q doesn't exist", orgRef)) + } + + orgUsers, err = h.readDB.GetOrgUsers(tx, org.ID) + return err + }) + if err != nil { + return nil, err + } + + res := make([]*OrgMemberResponse, len(orgUsers)) + for i, orgUser := range orgUsers { + res[i] = orgMemberResponse(orgUser) + } + + return res, nil +} + func (h *ActionHandler) CreateOrg(ctx context.Context, org *types.Organization) (*types.Organization, error) { if org.Name == "" { return nil, util.NewErrBadRequest(errors.Errorf("organization name required")) diff --git a/internal/services/configstore/api/client.go b/internal/services/configstore/api/client.go index 7cc0c2b..474d3b5 100644 --- a/internal/services/configstore/api/client.go +++ b/internal/services/configstore/api/client.go @@ -504,3 +504,9 @@ func (c *Client) GetOrg(ctx context.Context, orgRef string) (*types.Organization resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/orgs/%s", orgRef), nil, jsonContent, nil, org) return org, resp, err } + +func (c *Client) GetOrgMembers(ctx context.Context, orgRef string) ([]*OrgMemberResponse, *http.Response, error) { + orgMembers := []*OrgMemberResponse{} + resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/orgs/%s/members", orgRef), nil, jsonContent, nil, &orgMembers) + return orgMembers, resp, err +} diff --git a/internal/services/configstore/api/org.go b/internal/services/configstore/api/org.go index 0170a91..d21fa27 100644 --- a/internal/services/configstore/api/org.go +++ b/internal/services/configstore/api/org.go @@ -243,3 +243,45 @@ func (h *RemoveOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques h.log.Errorf("err: %+v", err) } } + +type OrgMemberResponse struct { + User *types.User + Role types.MemberRole +} + +func orgMemberResponse(orgUser *action.OrgMemberResponse) *OrgMemberResponse { + return &OrgMemberResponse{ + User: orgUser.User, + Role: orgUser.Role, + } +} + +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"] + + orgUsers, err := h.ah.GetOrgMembers(ctx, orgRef) + if httpError(w, err) { + h.log.Errorf("err: %+v", err) + return + } + + res := make([]*OrgMemberResponse, len(orgUsers)) + for i, orgUser := range orgUsers { + res[i] = orgMemberResponse(orgUser) + } + + if err := httpResponse(w, http.StatusOK, res); err != nil { + h.log.Errorf("err: %+v", err) + } +} diff --git a/internal/services/configstore/configstore.go b/internal/services/configstore/configstore.go index c46fdf7..6121437 100644 --- a/internal/services/configstore/configstore.go +++ b/internal/services/configstore/configstore.go @@ -164,6 +164,7 @@ func (s *Configstore) Run(ctx context.Context) error { createOrgHandler := api.NewCreateOrgHandler(logger, s.ah) deleteOrgHandler := api.NewDeleteOrgHandler(logger, s.ah) + orgMembersHandler := api.NewOrgMembersHandler(logger, s.ah) addOrgMemberHandler := api.NewAddOrgMemberHandler(logger, s.ah) removeOrgMemberHandler := api.NewRemoveOrgMemberHandler(logger, s.ah) @@ -218,6 +219,7 @@ func (s *Configstore) Run(ctx context.Context) error { apirouter.Handle("/orgs", orgsHandler).Methods("GET") apirouter.Handle("/orgs", createOrgHandler).Methods("POST") apirouter.Handle("/orgs/{orgref}", deleteOrgHandler).Methods("DELETE") + apirouter.Handle("/orgs/{orgref}/members", orgMembersHandler).Methods("GET") apirouter.Handle("/orgs/{orgref}/members/{userref}", addOrgMemberHandler).Methods("PUT") apirouter.Handle("/orgs/{orgref}/members/{userref}", removeOrgMemberHandler).Methods("DELETE")