Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
utsav14nov committed Oct 7, 2024
1 parent 37e86bd commit de5a211
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions mocks/ShieldClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions plugins/providers/shield/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,33 @@ func (c *client) GetAdminsOfGivenResourceType(ctx context.Context, id string, re
return userEmails, err
}

func (c *client) GetTeams(ctx context.Context) ([]*Group, error) {
func (c *client) GetGroups(ctx context.Context) ([]*Group, error) {
req, err := c.newRequest(http.MethodGet, groupsEndpoint, nil, "")
if err != nil {
return nil, err
}

var teams []*Group
var groups []*Group
var response interface{}
if _, err := c.do(ctx, req, &response); err != nil {
return nil, err
}

if v, ok := response.(map[string]interface{}); ok && v[groupsConst] != nil {
err = mapstructure.Decode(v[groupsConst], &teams)
err = mapstructure.Decode(v[groupsConst], &groups)
}

for _, team := range teams {
admins, err := c.GetAdminsOfGivenResourceType(ctx, team.ID, groupsEndpoint)
for _, group := range groups {
admins, err := c.GetAdminsOfGivenResourceType(ctx, group.ID, groupsEndpoint)
if err != nil {
return nil, err
}
team.Admins = admins
group.Admins = admins
}

c.logger.Info(ctx, "Fetch teams from request", "total", len(teams), req.URL)
c.logger.Info(ctx, "Fetch groups from request", "total", len(groups), req.URL)

return teams, err
return groups, err
}

func (c *client) GetProjects(ctx context.Context) ([]*Project, error) {
Expand Down Expand Up @@ -196,7 +196,7 @@ func (c *client) GetOrganizations(ctx context.Context) ([]*Organization, error)
return organizations, err
}

func (c *client) GrantTeamAccess(ctx context.Context, resource *Group, userId string, role string) error {
func (c *client) GrantGroupAccess(ctx context.Context, resource *Group, userId string, role string) error {
body := make(map[string][]string)
body["userIds"] = append(body["userIds"], userId)

Expand All @@ -219,7 +219,7 @@ func (c *client) GrantTeamAccess(ctx context.Context, resource *Group, userId st
}
}

c.logger.Info(ctx, "Team access to the user,", "total users", len(users), req.URL)
c.logger.Info(ctx, "group access to the user,", "total users", len(users), req.URL)

return nil
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (c *client) GrantOrganizationAccess(ctx context.Context, resource *Organiza
return nil
}

func (c *client) RevokeTeamAccess(ctx context.Context, resource *Group, userId string, role string) error {
func (c *client) RevokeGroupAccess(ctx context.Context, resource *Group, userId string, role string) error {
endPoint := path.Join(groupsEndpoint, "/", resource.ID, "/", role, "/", userId)
req, err := c.newRequest(http.MethodDelete, endPoint, "", "")
if err != nil {
Expand All @@ -299,7 +299,7 @@ func (c *client) RevokeTeamAccess(ctx context.Context, resource *Group, userId s
}
}

c.logger.Info(ctx, "Remove access of the user from team,", "Users", userId, req.URL)
c.logger.Info(ctx, "Remove access of the user from group,", "Users", userId, req.URL)
return nil
}

Expand Down
24 changes: 12 additions & 12 deletions plugins/providers/shield/client_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,33 +180,33 @@ func (c *shieldNewclient) DeleteRelation(ctx context.Context, objectId string, s
return nil
}

func (c *shieldNewclient) GetTeams(ctx context.Context) ([]*Group, error) {
func (c *shieldNewclient) GetGroups(ctx context.Context) ([]*Group, error) {
req, err := c.newRequest(http.MethodGet, groupsEndpoint, nil, "")
if err != nil {
return nil, err
}

var teams []*Group
var groups []*Group
var response interface{}
if _, err := c.do(ctx, req, &response); err != nil {
return nil, err
}

if v, ok := response.(map[string]interface{}); ok && v[groupsConst] != nil {
err = mapstructure.Decode(v[groupsConst], &teams)
err = mapstructure.Decode(v[groupsConst], &groups)
}

for _, team := range teams {
admins, err := c.GetGroupRelations(ctx, team.ID, managerRoleConst)
for _, group := range groups {
admins, err := c.GetGroupRelations(ctx, group.ID, managerRoleConst)
if err != nil {
return nil, err
}
team.Admins = admins
group.Admins = admins
}

c.logger.Info(ctx, fmt.Sprintf("Fetch teams from new shield request total=%d with request %s", len(teams), req.URL))
c.logger.Info(ctx, fmt.Sprintf("Fetch groups from new shield request total=%d with request %s", len(groups), req.URL))

return teams, err
return groups, err
}

func (c *shieldNewclient) GetProjects(ctx context.Context) ([]*Project, error) {
Expand Down Expand Up @@ -268,12 +268,12 @@ func (c *shieldNewclient) GetOrganizations(ctx context.Context) ([]*Organization
return organizations, err
}

func (c *shieldNewclient) GrantTeamAccess(ctx context.Context, resource *Group, userId string, role string) error {
func (c *shieldNewclient) GrantGroupAccess(ctx context.Context, resource *Group, userId string, role string) error {
err := c.CreateRelation(ctx, resource.ID, groupNamespaceConst, fmt.Sprintf("%s:%s", userNamespaceConst, userId), role)
if err != nil {
return err
}
c.logger.Info(ctx, "Team access created for user in new shield", userId)
c.logger.Info(ctx, "group access created for user in new shield", userId)
return nil
}

Expand All @@ -295,12 +295,12 @@ func (c *shieldNewclient) GrantOrganizationAccess(ctx context.Context, resource
return nil
}

func (c *shieldNewclient) RevokeTeamAccess(ctx context.Context, resource *Group, userId string, role string) error {
func (c *shieldNewclient) RevokeGroupAccess(ctx context.Context, resource *Group, userId string, role string) error {
err := c.DeleteRelation(ctx, resource.ID, userId, role)
if err != nil {
return err
}
c.logger.Info(ctx, "Remove access of the user from team in new shield,", "Users", userId, resource.ID)
c.logger.Info(ctx, "Remove access of the user from group in new shield,", "Users", userId, resource.ID)
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions plugins/providers/shield/client_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *ShieldNewClientTestSuite) getTestRequest(method, path string, body inte
return req, nil
}

func (s *ShieldNewClientTestSuite) TestShieldNewGetTeams() {
func (s *ShieldNewClientTestSuite) TestShieldNewGetGroups() {
s.Run("should get teams and nil error on success", func() {
s.setup()

Expand Down Expand Up @@ -211,7 +211,7 @@ func (s *ShieldNewClientTestSuite) TestShieldNewGetTeams() {
teamAdminResponse2 := http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader([]byte(teamAdminResponse)))}
s.mockHttpClient.On("Do", testAdminsRequest2).Return(&teamAdminResponse2, nil).Once()

result, err1 := s.client.GetTeams(context.Background())
result, err1 := s.client.GetGroups(context.Background())
var teams []shield.Group
for _, team := range result {
teams = append(teams, *team)
Expand Down Expand Up @@ -356,7 +356,7 @@ func (s *ShieldNewClientTestSuite) TestShieldNewGetOrganizations() {
})
}

func (s *ShieldNewClientTestSuite) TestShieldNewGrantTeamAccess() {
func (s *ShieldNewClientTestSuite) TestShieldNewGrantGroupAccess() {
s.Run("should grant access to team and nil error on success", func() {
s.setup()

Expand Down Expand Up @@ -385,7 +385,7 @@ func (s *ShieldNewClientTestSuite) TestShieldNewGrantTeamAccess() {
responseUsers := http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader([]byte(responseJson)))}
s.mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&responseUsers, nil).Once()

actualError := s.client.GrantTeamAccess(context.Background(), teamObj, testUserId, role)
actualError := s.client.GrantGroupAccess(context.Background(), teamObj, testUserId, role)
s.Nil(actualError)
})
}
Expand Down Expand Up @@ -458,7 +458,7 @@ func (s *ShieldNewClientTestSuite) TestShieldNewGrantOrganizationAccess() {
})
}

func (s *ShieldNewClientTestSuite) TestShieldNewRevokeTeamAccess() {
func (s *ShieldNewClientTestSuite) TestShieldNewRevokeGroupAccess() {
s.Run("should revoke access to team and nil error on success", func() {
s.setup()
testUserId := "test_user_id"
Expand All @@ -478,7 +478,7 @@ func (s *ShieldNewClientTestSuite) TestShieldNewRevokeTeamAccess() {
responseUsers := http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader([]byte(responseJson)))}
s.mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&responseUsers, nil).Once()

actualError := s.client.RevokeTeamAccess(context.Background(), teamObj, testUserId, role)
actualError := s.client.RevokeGroupAccess(context.Background(), teamObj, testUserId, role)
s.Nil(actualError)
})
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/providers/shield/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *ClientTestSuite) getTestRequest(method, path string, body interface{},
return req, nil
}

func (s *ClientTestSuite) TestGetTeams() {
func (s *ClientTestSuite) TestGetGroups() {
s.Run("should get teams and nil error on success", func() {
s.setup()

Expand Down Expand Up @@ -210,7 +210,7 @@ func (s *ClientTestSuite) TestGetTeams() {
teamAdminResponse2 := http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader([]byte(teamAdminResponse)))}
s.mockHttpClient.On("Do", testAdminsRequest2).Return(&teamAdminResponse2, nil).Once()

result, err1 := s.client.GetTeams(context.Background())
result, err1 := s.client.GetGroups(context.Background())
var teams []shield.Group
for _, team := range result {
teams = append(teams, *team)
Expand Down Expand Up @@ -355,7 +355,7 @@ func (s *ClientTestSuite) TestGetOrganizations() {
})
}

func (s *ClientTestSuite) TestGrantTeamAccess() {
func (s *ClientTestSuite) TestGrantGroupAccess() {
s.Run("should grant access to team and nil error on success", func() {
s.setup()

Expand Down Expand Up @@ -389,7 +389,7 @@ func (s *ClientTestSuite) TestGrantTeamAccess() {
responseUsers := http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader([]byte(responseJson)))}
s.mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&responseUsers, nil).Once()

actualError := s.client.GrantTeamAccess(context.Background(), teamObj, testUserId, role)
actualError := s.client.GrantGroupAccess(context.Background(), teamObj, testUserId, role)
s.Nil(actualError)
})
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func (s *ClientTestSuite) TestGrantOrganizationAccess() {
})
}

func (s *ClientTestSuite) TestRevokeTeamAccess() {
func (s *ClientTestSuite) TestRevokeGroupAccess() {
s.Run("should revoke access to team and nil error on success", func() {
s.setup()
testUserId := "test_user_id"
Expand All @@ -492,7 +492,7 @@ func (s *ClientTestSuite) TestRevokeTeamAccess() {
responseUsers := http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader([]byte(responseJson)))}
s.mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&responseUsers, nil).Once()

actualError := s.client.RevokeTeamAccess(context.Background(), teamObj, testUserId, role)
actualError := s.client.RevokeGroupAccess(context.Background(), teamObj, testUserId, role)
s.Nil(actualError)
})
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/providers/shield/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (p *provider) GetResources(ctx context.Context, pc *domain.ProviderConfig)
var organizations []*Organization

if _, ok := resourceTypes[ResourceTypeTeam]; ok {
teams, err = client.GetTeams(ctx)
teams, err = client.GetGroups(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (p *provider) GrantAccess(ctx context.Context, pc *domain.ProviderConfig, a
return err
}
for _, p := range permissions {
if err := client.GrantTeamAccess(ctx, t, user.ID, p); err != nil {
if err := client.GrantGroupAccess(ctx, t, user.ID, p); err != nil {
return err
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func (p *provider) RevokeAccess(ctx context.Context, pc *domain.ProviderConfig,
return err
}
for _, p := range permissions {
if err := client.RevokeTeamAccess(ctx, t, user.ID, p); err != nil {
if err := client.RevokeGroupAccess(ctx, t, user.ID, p); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/providers/shield/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

type ShieldClient interface {
GetTeams(ctx context.Context) ([]*Group, error)
GetGroups(ctx context.Context) ([]*Group, error)
GetProjects(ctx context.Context) ([]*Project, error)
GetOrganizations(ctx context.Context) ([]*Organization, error)
GrantTeamAccess(ctx context.Context, team *Group, userId string, role string) error
RevokeTeamAccess(ctx context.Context, team *Group, userId string, role string) error
GrantGroupAccess(ctx context.Context, team *Group, userId string, role string) error
RevokeGroupAccess(ctx context.Context, team *Group, userId string, role string) error
GrantProjectAccess(ctx context.Context, project *Project, userId string, role string) error
RevokeProjectAccess(ctx context.Context, project *Project, userId string, role string) error
GrantOrganizationAccess(ctx context.Context, organization *Organization, userId string, role string) error
Expand Down

0 comments on commit de5a211

Please sign in to comment.