configstore: add get org members api/action
This commit is contained in:
parent
8f959c4500
commit
21e706a150
|
@ -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"))
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
Loading…
Reference in New Issue