Skip to content

Commit

Permalink
refactor: Uses mocks on admin.APIClient instead of custom ClusterServ…
Browse files Browse the repository at this point in the history
…ice (#2056)
  • Loading branch information
EspenAlbert authored Mar 21, 2024
1 parent 025bacb commit 9229ae4
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 223 deletions.
11 changes: 4 additions & 7 deletions internal/service/advancedcluster/model_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ func IsSharedTier(instanceSize string) bool {
return instanceSize == "M0" || instanceSize == "M2" || instanceSize == "M5"
}

func UpgradeRefreshFunc(ctx context.Context, name, projectID string, client ClusterService) retry.StateRefreshFunc {
func UpgradeRefreshFunc(ctx context.Context, name, projectID string, client admin.ClustersApi) retry.StateRefreshFunc {
return func() (any, string, error) {
cluster, resp, err := client.Get(ctx, projectID, name)
cluster, resp, err := client.GetCluster(ctx, projectID, name).Execute()

if err != nil && strings.Contains(err.Error(), "reset by peer") {
return nil, "REPEATING", nil
Expand All @@ -300,12 +300,9 @@ func UpgradeRefreshFunc(ctx context.Context, name, projectID string, client Clus
}
}

func ResourceClusterListAdvancedRefreshFunc(ctx context.Context, projectID string, client ClusterService) retry.StateRefreshFunc {
func ResourceClusterListAdvancedRefreshFunc(ctx context.Context, projectID string, clustersAPI admin.ClustersApi) retry.StateRefreshFunc {
return func() (any, string, error) {
params := &admin.ListClustersApiParams{
GroupId: projectID,
}
clusters, resp, err := client.List(ctx, params)
clusters, resp, err := clustersAPI.ListClusters(ctx, projectID).Execute()

if err != nil && strings.Contains(err.Error(), "reset by peer") {
return nil, "REPEATING", nil
Expand Down
10 changes: 6 additions & 4 deletions internal/service/advancedcluster/model_advanced_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ func TestUpgradeRefreshFunc(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testObject := mocksvc.NewClusterService(t)
testObject := mocksvc.NewClustersApi(t)

testObject.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(tc.mockCluster, tc.mockResponse, tc.mockError)
testObject.EXPECT().GetCluster(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetClusterApiRequest{ApiService: testObject}).Once()
testObject.EXPECT().GetClusterExecute(mock.Anything).Return(tc.mockCluster, tc.mockResponse, tc.mockError).Once()

result, stateName, err := advancedcluster.UpgradeRefreshFunc(context.Background(), dummyClusterName, dummyProjectID, testObject)()
if (err != nil) != tc.expectedError {
Expand Down Expand Up @@ -208,9 +209,10 @@ func TestResourceListAdvancedRefreshFunc(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testObject := mocksvc.NewClusterService(t)
testObject := mocksvc.NewClustersApi(t)

testObject.On("List", mock.Anything, mock.Anything, mock.Anything).Return(tc.mockCluster, tc.mockResponse, tc.mockError)
testObject.EXPECT().ListClusters(mock.Anything, mock.Anything).Return(admin.ListClustersApiRequest{ApiService: testObject}).Once()
testObject.EXPECT().ListClustersExecute(mock.Anything).Return(tc.mockCluster, tc.mockResponse, tc.mockError).Once()

result, stateName, err := advancedcluster.ResourceClusterListAdvancedRefreshFunc(context.Background(), dummyProjectID, testObject)()
if (err != nil) != tc.expectedError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ func upgradeCluster(ctx context.Context, connV2 *admin.APIClient, request *admin
stateConf := &retry.StateChangeConf{
Pending: []string{"CREATING", "UPDATING", "REPAIRING"},
Target: []string{"IDLE"},
Refresh: UpgradeRefreshFunc(ctx, name, projectID, ServiceFromClient(connV2)),
Refresh: UpgradeRefreshFunc(ctx, name, projectID, connV2.ClustersApi),
Timeout: timeout,
MinTimeout: 30 * time.Second,
Delay: 1 * time.Minute,
Expand Down
31 changes: 0 additions & 31 deletions internal/service/advancedcluster/service_advanced_cluster.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate(ctx context.Context,
stateConf := &retry.StateChangeConf{
Pending: []string{"REPEATING", "PENDING"},
Target: []string{"IDLE", "DELETED"},
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, advancedcluster.ServiceFromClient(connV2)),
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, connV2.ClustersApi),
Timeout: d.Timeout(timeoutKey.(string)),
MinTimeout: 5 * time.Second,
Delay: 3 * time.Second,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkCreate(ctx context.Context, d
clusterConf := &retry.StateChangeConf{
Pending: []string{"REPEATING", "PENDING"},
Target: []string{"IDLE", "DELETED"},
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, advancedcluster.ServiceFromClient(connV2)),
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, connV2.ClustersApi),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 5 * time.Second,
Delay: 5 * time.Minute,
Expand Down Expand Up @@ -317,7 +317,7 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkDelete(ctx context.Context, d
clusterConf := &retry.StateChangeConf{
Pending: []string{"REPEATING", "PENDING"},
Target: []string{"IDLE", "DELETED"},
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, advancedcluster.ServiceFromClient(connV2)),
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, connV2.ClustersApi),
Timeout: d.Timeout(schema.TimeoutDelete),
MinTimeout: 5 * time.Second,
Delay: 5 * time.Minute,
Expand Down
177 changes: 0 additions & 177 deletions internal/testutil/mocksvc/cluster_service.go

This file was deleted.

0 comments on commit 9229ae4

Please sign in to comment.