Skip to content

Commit

Permalink
CLOUDP-228592: Migrate Assigned Teams Api (#1817)
Browse files Browse the repository at this point in the history
* Initial Api change

* Initial test changes - issues with project_test.go to be resolved

* Progress on project unit-test fixes

all project unit-tests touching teams fail. Failure seems to happen in syncAssignedTeams()

* Fix dereference to pointer slice

Panic in project unit tests were caused by dereferencing pointer slices. This has now been fixed.

* Rebasing and lint fix

* Implementing review feedback

Namely, dereferencing protection with use of getter/setter functions

* Implementing review feedback
Additional getter implementation for dereferencing protection and changing team_reconciler/ensureTeamState() and associated functions to utilise type *admin.TeamResponse in place of *admin.Team.

* Implement review change requests/

Removal of setting team links and removal of unused argument projectID in teamReconciler.

* Implement review change requests
  • Loading branch information
cveticm authored Sep 27, 2024
1 parent f84111b commit 767cf8b
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 236 deletions.
7 changes: 4 additions & 3 deletions pkg/api/v1/atlasteam_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ limitations under the License.
package v1

import (
"go.mongodb.org/atlas-sdk/v20231115008/admin"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/compat"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/status"

"go.mongodb.org/atlas/mongodbatlas"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -87,8 +88,8 @@ func (in *AtlasTeam) UpdateStatus(conditions []api.Condition, options ...api.Opt
}
}

func (in *AtlasTeam) ToAtlas() (*mongodbatlas.Team, error) {
result := &mongodbatlas.Team{}
func (in *AtlasTeam) ToAtlas() (*admin.Team, error) {
result := &admin.Team{}
err := compat.JSONCopy(result, in.Spec)

return result, err
Expand Down
14 changes: 8 additions & 6 deletions pkg/api/v1/project_teams.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1

import (
"go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/atlas-sdk/v20231115008/admin"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1/common"
)
Expand All @@ -27,15 +27,17 @@ type Team struct {
Roles []TeamRole `json:"roles"`
}

func (in *Team) ToAtlas(teamID string) *mongodbatlas.ProjectTeam {
result := &mongodbatlas.ProjectTeam{
TeamID: teamID,
RoleNames: make([]string, 0, len(in.Roles)),
func (in *Team) ToAtlas(teamID string) admin.TeamRole {
roleNames := make([]string, 0, len(in.Roles))
result := admin.TeamRole{
TeamId: &teamID,
RoleNames: &roleNames,
}

for _, role := range in.Roles {
result.RoleNames = append(result.RoleNames, string(role))
roleNames = append(roleNames, string(role))
}
result.SetRoleNames(roleNames)

return result
}
85 changes: 32 additions & 53 deletions pkg/controller/atlasproject/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package atlasproject
import (
"context"
"errors"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -116,13 +117,6 @@ func TestHandleProject(t *testing.T) {
"should delete project": {
atlasClientMocker: func() *mongodbatlas.Client {
projectsMock := &atlasmocks.ProjectsClientMock{
GetProjectTeamsAssignedFunc: func(projectID string) (*mongodbatlas.TeamsAssigned, *mongodbatlas.Response, error) {
return &mongodbatlas.TeamsAssigned{
Links: nil,
Results: []*mongodbatlas.Result{},
TotalCount: 0,
}, nil, nil
},
DeleteFunc: func(projectID string) (*mongodbatlas.Response, error) {
return nil, nil
},
Expand All @@ -147,10 +141,16 @@ func TestHandleProject(t *testing.T) {
mockPeeringEndpointAPI.EXPECT().
ListPeeringConnectionsExecute(admin.ListPeeringConnectionsApiRequest{ApiService: mockPeeringEndpointAPI}).
Return(&admin.PaginatedContainerPeer{}, nil, nil)
mockTeamAPI := mockadmin.NewTeamsApi(t)
mockTeamAPI.EXPECT().ListProjectTeams(context.Background(), mock.Anything).
Return(admin.ListProjectTeamsApiRequest{ApiService: mockTeamAPI})
mockTeamAPI.EXPECT().ListProjectTeamsExecute(mock.Anything).
Return(&admin.PaginatedTeamRole{}, &http.Response{}, nil)

return &admin.APIClient{
PrivateEndpointServicesApi: mockPrivateEndpointAPI,
NetworkPeeringApi: mockPeeringEndpointAPI,
TeamsApi: mockTeamAPI,
}
},
projectServiceMocker: func() project.ProjectService {
Expand Down Expand Up @@ -288,11 +288,7 @@ func TestHandleProject(t *testing.T) {
return &mongodbatlas.EncryptionAtRest{}, nil, nil
},
}
projectAPI := &atlasmocks.ProjectsClientMock{
GetProjectTeamsAssignedFunc: func(projectID string) (*mongodbatlas.TeamsAssigned, *mongodbatlas.Response, error) {
return &mongodbatlas.TeamsAssigned{}, nil, nil
},
}
projectAPI := &atlasmocks.ProjectsClientMock{}

return &mongodbatlas.Client{
Integrations: integrations,
Expand Down Expand Up @@ -340,6 +336,11 @@ func TestHandleProject(t *testing.T) {
Return(admin.GetDataProtectionSettingsApiRequest{ApiService: backup})
backup.EXPECT().GetDataProtectionSettingsExecute(mock.AnythingOfType("admin.GetDataProtectionSettingsApiRequest")).
Return(nil, nil, nil)
mockTeamAPI := mockadmin.NewTeamsApi(t)
mockTeamAPI.EXPECT().ListProjectTeams(context.Background(), mock.Anything).
Return(admin.ListProjectTeamsApiRequest{ApiService: mockTeamAPI})
mockTeamAPI.EXPECT().ListProjectTeamsExecute(mock.Anything).
Return(nil, &http.Response{}, nil)

return &admin.APIClient{
ProjectIPAccessListApi: ipAccessList,
Expand All @@ -349,6 +350,7 @@ func TestHandleProject(t *testing.T) {
CustomDatabaseRolesApi: customRoles,
ProjectsApi: projectAPI,
CloudBackupsApi: backup,
TeamsApi: mockTeamAPI,
}
},
projectServiceMocker: func() project.ProjectService {
Expand Down Expand Up @@ -390,16 +392,10 @@ func TestHandleProject(t *testing.T) {
return &mongodbatlas.EncryptionAtRest{}, nil, nil
},
}
projectAPI := &atlasmocks.ProjectsClientMock{
GetProjectTeamsAssignedFunc: func(projectID string) (*mongodbatlas.TeamsAssigned, *mongodbatlas.Response, error) {
return &mongodbatlas.TeamsAssigned{}, nil, nil
},
}

return &mongodbatlas.Client{
Integrations: integrations,
EncryptionsAtRest: encryptionAtRest,
Projects: projectAPI,
}
},
atlasSDKMocker: func() *admin.APIClient {
Expand Down Expand Up @@ -442,6 +438,11 @@ func TestHandleProject(t *testing.T) {
Return(admin.GetDataProtectionSettingsApiRequest{ApiService: backup})
backup.EXPECT().GetDataProtectionSettingsExecute(mock.AnythingOfType("admin.GetDataProtectionSettingsApiRequest")).
Return(nil, nil, nil)
mockTeamAPI := mockadmin.NewTeamsApi(t)
mockTeamAPI.EXPECT().ListProjectTeams(context.Background(), mock.Anything).
Return(admin.ListProjectTeamsApiRequest{ApiService: mockTeamAPI})
mockTeamAPI.EXPECT().ListProjectTeamsExecute(mock.Anything).
Return(nil, &http.Response{}, nil)

return &admin.APIClient{
ProjectIPAccessListApi: ipAccessList,
Expand All @@ -451,6 +452,7 @@ func TestHandleProject(t *testing.T) {
CustomDatabaseRolesApi: customRoles,
ProjectsApi: projectAPI,
CloudBackupsApi: backup,
TeamsApi: mockTeamAPI,
}
},
projectServiceMocker: func() project.ProjectService {
Expand Down Expand Up @@ -494,16 +496,10 @@ func TestHandleProject(t *testing.T) {
return &mongodbatlas.EncryptionAtRest{}, nil, nil
},
}
projectAPI := &atlasmocks.ProjectsClientMock{
GetProjectTeamsAssignedFunc: func(projectID string) (*mongodbatlas.TeamsAssigned, *mongodbatlas.Response, error) {
return &mongodbatlas.TeamsAssigned{}, nil, nil
},
}

return &mongodbatlas.Client{
Integrations: integrations,
EncryptionsAtRest: encryptionAtRest,
Projects: projectAPI,
}
},
atlasSDKMocker: func() *admin.APIClient {
Expand Down Expand Up @@ -546,6 +542,11 @@ func TestHandleProject(t *testing.T) {
Return(admin.GetDataProtectionSettingsApiRequest{ApiService: backup})
backup.EXPECT().GetDataProtectionSettingsExecute(mock.AnythingOfType("admin.GetDataProtectionSettingsApiRequest")).
Return(nil, nil, nil)
mockTeamAPI := mockadmin.NewTeamsApi(t)
mockTeamAPI.EXPECT().ListProjectTeams(context.Background(), mock.Anything).
Return(admin.ListProjectTeamsApiRequest{ApiService: mockTeamAPI})
mockTeamAPI.EXPECT().ListProjectTeamsExecute(mock.Anything).
Return(nil, &http.Response{}, nil)

return &admin.APIClient{
ProjectIPAccessListApi: ipAccessList,
Expand All @@ -555,6 +556,7 @@ func TestHandleProject(t *testing.T) {
CustomDatabaseRolesApi: customRoles,
ProjectsApi: projectAPI,
CloudBackupsApi: backup,
TeamsApi: mockTeamAPI,
}
},
projectServiceMocker: func() project.ProjectService {
Expand Down Expand Up @@ -938,41 +940,13 @@ func TestDelete(t *testing.T) {
"should update team status when project is deleted": {
atlasClientMocker: func() *mongodbatlas.Client {
projectsMock := &atlasmocks.ProjectsClientMock{
GetProjectTeamsAssignedFunc: func(projectID string) (*mongodbatlas.TeamsAssigned, *mongodbatlas.Response, error) {
return &mongodbatlas.TeamsAssigned{
Links: nil,
Results: []*mongodbatlas.Result{
{
Links: nil,
RoleNames: nil,
TeamID: teamID,
},
},
TotalCount: 0,
}, nil, nil
},
DeleteFunc: func(projectID string) (*mongodbatlas.Response, error) {
return nil, nil
},
}
teamsMock := &atlasmocks.TeamsClientMock{
ListFunc: func(orgID string) ([]mongodbatlas.Team, *mongodbatlas.Response, error) {
return []mongodbatlas.Team{
{
ID: teamID,
Name: teamName,
Usernames: nil,
},
}, nil, nil
},
RemoveTeamFromProjectFunc: func(projectID string, teamID string) (*mongodbatlas.Response, error) {
return nil, nil
},
}

return &mongodbatlas.Client{
Projects: projectsMock,
Teams: teamsMock,
}
},
atlasSDKMocker: func() *admin.APIClient {
Expand All @@ -990,10 +964,15 @@ func TestDelete(t *testing.T) {
mockPeeringEndpointAPI.EXPECT().
ListPeeringConnectionsExecute(admin.ListPeeringConnectionsApiRequest{ApiService: mockPeeringEndpointAPI}).
Return(&admin.PaginatedContainerPeer{}, nil, nil)

mockTeamAPI := mockadmin.NewTeamsApi(t)
mockTeamAPI.EXPECT().ListProjectTeams(context.Background(), mock.Anything).
Return(admin.ListProjectTeamsApiRequest{ApiService: mockTeamAPI})
mockTeamAPI.EXPECT().ListProjectTeamsExecute(mock.Anything).
Return(nil, &http.Response{}, nil)
return &admin.APIClient{
PrivateEndpointServicesApi: mockPrivateEndpointAPI,
NetworkPeeringApi: mockPeeringEndpointAPI,
TeamsApi: mockTeamAPI,
}
},
projectServiceMocker: func() project.ProjectService {
Expand Down
Loading

0 comments on commit 767cf8b

Please sign in to comment.