Skip to content

Commit

Permalink
NOISSUE - Rename GroupRolesPage -> GroupMembersPage (#539)
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
  • Loading branch information
manuio authored Nov 8, 2024
1 parent fd09e97 commit feaf40b
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 78 deletions.
2 changes: 1 addition & 1 deletion pkg/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (svc *mainfluxThings) CreateRolesByGroup(_ context.Context, token, groupID
panic("not implemented")
}

func (svc *mainfluxThings) ListRolesByGroup(_ context.Context, token, groupID string, pm things.PageMetadata) (things.GroupRolesPage, error) {
func (svc *mainfluxThings) ListRolesByGroup(_ context.Context, token, groupID string, pm things.PageMetadata) (things.GroupMembersPage, error) {
panic("not implemented")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (sdk mfSDK) UpdateRolesByGroup(roles []GroupMember, groupID, token string)
func (sdk mfSDK) RemoveRolesByGroup(ids []string, groupID, token string) error
RemoveRolesByGroup - removes existing group roles

func (sdk mfSDK) ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupRolesPage, error)
func (sdk mfSDK) ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupMembersPage, error)
ListRolesByGroup - lists roles by group

func (sdk mfSDK) CreateOrg(org Org, token string) error
Expand Down
14 changes: 7 additions & 7 deletions pkg/sdk/go/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,31 +413,31 @@ func (sdk mfSDK) RemoveRolesByGroup(ids []string, groupID, token string) error {
return nil
}

func (sdk mfSDK) ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupRolesPage, error) {
func (sdk mfSDK) ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupMembersPage, error) {
url := fmt.Sprintf("%s/%s/%s/members?offset=%d&limit=%d", sdk.thingsURL, groupsEndpoint, groupID, offset, limit)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}

resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}

if resp.StatusCode != http.StatusOK {
return GroupRolesPage{}, errors.Wrap(ErrFailedFetch, errors.New(resp.Status))
return GroupMembersPage{}, errors.Wrap(ErrFailedFetch, errors.New(resp.Status))
}

var grp GroupRolesPage
var grp GroupMembersPage
if err := json.Unmarshal(body, &grp); err != nil {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}

return grp, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/go/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ type OrgsPage struct {
pageRes
}

// GroupRolesPage contains a list of roles for a certain group with proper metadata.
type GroupRolesPage struct {
GroupRoles []GroupMember `json:"group_roles"`
// GroupMembersPage contains a list of roles for a certain group with proper metadata.
type GroupMembersPage struct {
GroupMembers []GroupMember `json:"group_members"`
pageRes
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ type SDK interface {
RemoveRolesByGroup(ids []string, groupID, token string) error

// ListRolesByGroup lists roles that are specified for a certain group.
ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupRolesPage, error)
ListRolesByGroup(groupID, token string, offset, limit uint64) (GroupMembersPage, error)

// CreateOrg registers new org.
CreateOrg(org Org, token string) error
Expand Down
12 changes: 6 additions & 6 deletions things/api/http/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ func createRolesByGroupEndpoint(svc things.Service) endpoint.Endpoint {
}

var gps []things.GroupRoles
for _, g := range req.GroupRoles {
for _, g := range req.GroupMembers {
gp := things.GroupRoles{
MemberID: g.ID,
Role: g.Role,
Expand All @@ -966,7 +966,7 @@ func updateRolesByGroupEndpoint(svc things.Service) endpoint.Endpoint {
}

var gps []things.GroupRoles
for _, g := range req.GroupRoles {
for _, g := range req.GroupMembers {
gp := things.GroupRoles{
MemberID: g.ID,
Role: g.Role,
Expand Down Expand Up @@ -1018,23 +1018,23 @@ func listRolesByGroupEndpoint(svc things.Service) endpoint.Endpoint {
}
}

func buildGroupRolesResponse(gpp things.GroupRolesPage) listGroupRolesRes {
func buildGroupRolesResponse(gpp things.GroupMembersPage) listGroupRolesRes {
res := listGroupRolesRes{
pageRes: pageRes{
Total: gpp.Total,
Limit: gpp.Limit,
Offset: gpp.Offset,
},
GroupRoles: []groupMember{},
GroupMembers: []groupMember{},
}

for _, g := range gpp.GroupRoles {
for _, g := range gpp.GroupMembers {
gp := groupMember{
Email: g.Email,
ID: g.MemberID,
Role: g.Role,
}
res.GroupRoles = append(res.GroupRoles, gp)
res.GroupMembers = append(res.GroupMembers, gp)
}

return res
Expand Down
6 changes: 3 additions & 3 deletions things/api/http/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ func (req getConnByKeyReq) validate() error {
type groupRolesReq struct {
token string
groupID string
GroupRoles []groupMember `json:"group_roles"`
GroupMembers []groupMember `json:"group_members"`
}

func (req groupRolesReq) validate() error {
Expand All @@ -552,11 +552,11 @@ func (req groupRolesReq) validate() error {
return apiutil.ErrMissingID
}

if len(req.GroupRoles) == 0 {
if len(req.GroupMembers) == 0 {
return apiutil.ErrEmptyList
}

for _, gp := range req.GroupRoles {
for _, gp := range req.GroupMembers {
if gp.Role != things.Viewer && gp.Role != things.Editor {
return apiutil.ErrInvalidRole
}
Expand Down
2 changes: 1 addition & 1 deletion things/api/http/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (res updateGroupRolesRes) Empty() bool {

type listGroupRolesRes struct {
pageRes
GroupRoles []groupMember `json:"group_roles"`
GroupMembers []groupMember `json:"group_members"`
}

func (res listGroupRolesRes) Code() int {
Expand Down
2 changes: 1 addition & 1 deletion things/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func (lm *loggingMiddleware) CreateRolesByGroup(ctx context.Context, token, grou
return lm.svc.CreateRolesByGroup(ctx, token, groupID, gps...)
}

func (lm *loggingMiddleware) ListRolesByGroup(ctx context.Context, token, groupID string, pm things.PageMetadata) (gpp things.GroupRolesPage, err error) {
func (lm *loggingMiddleware) ListRolesByGroup(ctx context.Context, token, groupID string, pm things.PageMetadata) (gpp things.GroupMembersPage, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method list_roles_by_group for id %s took %s to complete", groupID, time.Since(begin))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion things/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (ms *metricsMiddleware) CreateRolesByGroup(ctx context.Context, token, grou
return ms.svc.CreateRolesByGroup(ctx, token, groupID, gps...)
}

func (ms *metricsMiddleware) ListRolesByGroup(ctx context.Context, token, groupID string, pm things.PageMetadata) (things.GroupRolesPage, error) {
func (ms *metricsMiddleware) ListRolesByGroup(ctx context.Context, token, groupID string, pm things.PageMetadata) (things.GroupMembersPage, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_roles_by_group").Add(1)
ms.latency.With("method", "list_roles_by_group").Observe(time.Since(begin).Seconds())
Expand Down
2 changes: 1 addition & 1 deletion things/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (ts *thingsService) canAccessGroup(ctx context.Context, token, groupID, act
return err
}

gp := GroupMembers{
gp := GroupMember{
MemberID: user.Id,
GroupID: groupID,
}
Expand Down
14 changes: 7 additions & 7 deletions things/mocks/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ var _ things.RolesRepository = (*rolesRepositoryMock)(nil)

type rolesRepositoryMock struct {
mu sync.Mutex
groupRoles map[string]things.GroupMembers
groupRoles map[string]things.GroupMember
groupRolesByID map[string]things.GroupRoles
}

// NewRolesRepository returns mock of policies repository
func NewRolesRepository() things.RolesRepository {
return &rolesRepositoryMock{
groupRoles: make(map[string]things.GroupMembers),
groupRoles: make(map[string]things.GroupMember),
groupRolesByID: make(map[string]things.GroupRoles),
}
}
Expand All @@ -28,7 +28,7 @@ func (mrm *rolesRepositoryMock) SaveRolesByGroup(_ context.Context, groupID stri
defer mrm.mu.Unlock()

for _, gp := range gps {
mrm.groupRoles[groupID] = things.GroupMembers{
mrm.groupRoles[groupID] = things.GroupMember{
MemberID: gp.MemberID,
Role: gp.Role,
}
Expand All @@ -38,14 +38,14 @@ func (mrm *rolesRepositoryMock) SaveRolesByGroup(_ context.Context, groupID stri
return nil
}

func (mrm *rolesRepositoryMock) RetrieveRole(_ context.Context, gp things.GroupMembers) (string, error) {
func (mrm *rolesRepositoryMock) RetrieveRole(_ context.Context, gp things.GroupMember) (string, error) {
mrm.mu.Lock()
defer mrm.mu.Unlock()

return mrm.groupRolesByID[gp.MemberID].Role, nil
}

func (mrm *rolesRepositoryMock) RetrieveRolesByGroup(_ context.Context, groupID string, pm things.PageMetadata) (things.GroupRolesPage, error) {
func (mrm *rolesRepositoryMock) RetrieveRolesByGroup(_ context.Context, groupID string, pm things.PageMetadata) (things.GroupMembersPage, error) {
panic("not implemented")
}

Expand All @@ -63,11 +63,11 @@ func (mrm *rolesRepositoryMock) RetrieveGroupIDsByMember(_ context.Context, memb
return grIDs, nil
}

func (mrm *rolesRepositoryMock) RetrieveAllRolesByGroup(_ context.Context) ([]things.GroupMembers, error) {
func (mrm *rolesRepositoryMock) RetrieveAllRolesByGroup(_ context.Context) ([]things.GroupMember, error) {
mrm.mu.Lock()
defer mrm.mu.Unlock()

var gps []things.GroupMembers
var gps []things.GroupMember
for _, gp := range mrm.groupRoles {
gps = append(gps, gp)
}
Expand Down
40 changes: 20 additions & 20 deletions things/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
protomfx "github.com/MainfluxLabs/mainflux/pkg/proto"
)

type GroupMembers struct {
type GroupMember struct {
GroupID string
MemberID string
Email string
Expand All @@ -18,23 +18,23 @@ type GroupRoles struct {
MemberID string
Role string
}
type GroupRolesPage struct {
type GroupMembersPage struct {
PageMetadata
GroupRoles []GroupMembers
GroupMembers []GroupMember
}

type RolesRepository interface {
// SaveRolesByGroup saves group roles by group ID.
SaveRolesByGroup(ctx context.Context, groupID string, gps ...GroupRoles) error

// RetrieveRole retrieves group role by group ID.
RetrieveRole(ctc context.Context, gp GroupMembers) (string, error)
RetrieveRole(ctc context.Context, gp GroupMember) (string, error)

// RetrieveRolesByGroup retrieves page of group roles by groupID.
RetrieveRolesByGroup(ctx context.Context, groupID string, pm PageMetadata) (GroupRolesPage, error)
RetrieveRolesByGroup(ctx context.Context, groupID string, pm PageMetadata) (GroupMembersPage, error)

// RetrieveAllRolesByGroup retrieves all group roles by group ID. This is used for backup.
RetrieveAllRolesByGroup(ctx context.Context) ([]GroupMembers, error)
RetrieveAllRolesByGroup(ctx context.Context) ([]GroupMember, error)

// RetrieveGroupIDsByMember retrieves the IDs of the groups to which the member belongs
RetrieveGroupIDsByMember(ctx context.Context, memberID string) ([]string, error)
Expand All @@ -51,7 +51,7 @@ type Policies interface {
CreateRolesByGroup(ctx context.Context, token, groupID string, gps ...GroupRoles) error

// ListRolesByGroup retrieves a page of policies for a group that is identified by the provided ID.
ListRolesByGroup(ctx context.Context, token, groupID string, pm PageMetadata) (GroupRolesPage, error)
ListRolesByGroup(ctx context.Context, token, groupID string, pm PageMetadata) (GroupMembersPage, error)

// UpdateRolesByGroup updates policies of the group identified by the provided ID.
UpdateRolesByGroup(ctx context.Context, token, groupID string, gps ...GroupRoles) error
Expand Down Expand Up @@ -79,7 +79,7 @@ func (ts *thingsService) CreateRolesByGroup(ctx context.Context, token, groupID
return nil
}

func (ts *thingsService) ListRolesByGroup(ctx context.Context, token, groupID string, pm PageMetadata) (GroupRolesPage, error) {
func (ts *thingsService) ListRolesByGroup(ctx context.Context, token, groupID string, pm PageMetadata) (GroupMembersPage, error) {
ar := AuthorizeReq{
Token: token,
Object: groupID,
Expand All @@ -88,50 +88,50 @@ func (ts *thingsService) ListRolesByGroup(ctx context.Context, token, groupID st
}

if err := ts.Authorize(ctx, ar); err != nil {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}

gpp, err := ts.roles.RetrieveRolesByGroup(ctx, groupID, pm)
if err != nil {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}

var memberIDs []string
for _, gp := range gpp.GroupRoles {
for _, gp := range gpp.GroupMembers {
memberIDs = append(memberIDs, gp.MemberID)
}

var groupRoles []GroupMembers
if len(gpp.GroupRoles) > 0 {
var gms []GroupMember
if len(gpp.GroupMembers) > 0 {
usrReq := protomfx.UsersByIDsReq{Ids: memberIDs}
up, err := ts.users.GetUsersByIDs(ctx, &usrReq)
if err != nil {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}

emails := make(map[string]string)
for _, user := range up.Users {
emails[user.Id] = user.GetEmail()
}

for _, gp := range gpp.GroupRoles {
for _, gp := range gpp.GroupMembers {
email, ok := emails[gp.MemberID]
if !ok {
return GroupRolesPage{}, err
return GroupMembersPage{}, err
}

groupMember := GroupMembers{
gm := GroupMember{
MemberID: gp.MemberID,
Email: email,
Role: gp.Role,
}

groupRoles = append(groupRoles, groupMember)
gms = append(gms, gm)
}
}

page := GroupRolesPage{
GroupRoles: groupRoles,
page := GroupMembersPage{
GroupMembers: gms,
PageMetadata: PageMetadata{
Total: gpp.Total,
Offset: gpp.Offset,
Expand Down
Loading

0 comments on commit feaf40b

Please sign in to comment.