From 9229ae4430ebea5462919e9a1b5807a41b652885 Mon Sep 17 00:00:00 2001 From: Espen Albert Date: Thu, 21 Mar 2024 14:44:15 +0000 Subject: [PATCH] refactor: Uses mocks on admin.APIClient instead of custom ClusterService (#2056) --- .../advancedcluster/model_advanced_cluster.go | 11 +- .../model_advanced_cluster_test.go | 10 +- .../resource_advanced_cluster.go | 2 +- .../service_advanced_cluster.go | 31 --- ...resource_private_endpoint_regional_mode.go | 2 +- .../resource_privatelink_endpoint_service.go | 4 +- internal/testutil/mocksvc/cluster_service.go | 177 ------------------ 7 files changed, 14 insertions(+), 223 deletions(-) delete mode 100644 internal/service/advancedcluster/service_advanced_cluster.go delete mode 100644 internal/testutil/mocksvc/cluster_service.go diff --git a/internal/service/advancedcluster/model_advanced_cluster.go b/internal/service/advancedcluster/model_advanced_cluster.go index 00d2060bab..81aee63f5c 100644 --- a/internal/service/advancedcluster/model_advanced_cluster.go +++ b/internal/service/advancedcluster/model_advanced_cluster.go @@ -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 @@ -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 diff --git a/internal/service/advancedcluster/model_advanced_cluster_test.go b/internal/service/advancedcluster/model_advanced_cluster_test.go index 4a7f4df1f5..dd94ded6c7 100644 --- a/internal/service/advancedcluster/model_advanced_cluster_test.go +++ b/internal/service/advancedcluster/model_advanced_cluster_test.go @@ -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 { @@ -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 { diff --git a/internal/service/advancedcluster/resource_advanced_cluster.go b/internal/service/advancedcluster/resource_advanced_cluster.go index f82814093d..5d18a609f5 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster.go +++ b/internal/service/advancedcluster/resource_advanced_cluster.go @@ -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, diff --git a/internal/service/advancedcluster/service_advanced_cluster.go b/internal/service/advancedcluster/service_advanced_cluster.go deleted file mode 100644 index 7d97bea323..0000000000 --- a/internal/service/advancedcluster/service_advanced_cluster.go +++ /dev/null @@ -1,31 +0,0 @@ -package advancedcluster - -import ( - "context" - "net/http" - - "go.mongodb.org/atlas-sdk/v20231115008/admin" -) - -type ClusterService interface { - Get(ctx context.Context, groupID, clusterName string) (*admin.AdvancedClusterDescription, *http.Response, error) - List(ctx context.Context, options *admin.ListClustersApiParams) (*admin.PaginatedAdvancedClusterDescription, *http.Response, error) -} - -type ClusterServiceFromClient struct { - client *admin.APIClient -} - -func (a *ClusterServiceFromClient) Get(ctx context.Context, groupID, clusterName string) (*admin.AdvancedClusterDescription, *http.Response, error) { - return a.client.ClustersApi.GetCluster(ctx, groupID, clusterName).Execute() -} - -func (a *ClusterServiceFromClient) List(ctx context.Context, options *admin.ListClustersApiParams) (*admin.PaginatedAdvancedClusterDescription, *http.Response, error) { - return a.client.ClustersApi.ListClustersWithParams(ctx, options).Execute() -} - -func ServiceFromClient(client *admin.APIClient) ClusterService { - return &ClusterServiceFromClient{ - client: client, - } -} diff --git a/internal/service/privateendpointregionalmode/resource_private_endpoint_regional_mode.go b/internal/service/privateendpointregionalmode/resource_private_endpoint_regional_mode.go index 7f8a37f861..a9d283f587 100644 --- a/internal/service/privateendpointregionalmode/resource_private_endpoint_regional_mode.go +++ b/internal/service/privateendpointregionalmode/resource_private_endpoint_regional_mode.go @@ -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, diff --git a/internal/service/privatelinkendpointservice/resource_privatelink_endpoint_service.go b/internal/service/privatelinkendpointservice/resource_privatelink_endpoint_service.go index 15b0e9781e..66293aaaba 100644 --- a/internal/service/privatelinkendpointservice/resource_privatelink_endpoint_service.go +++ b/internal/service/privatelinkendpointservice/resource_privatelink_endpoint_service.go @@ -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, @@ -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, diff --git a/internal/testutil/mocksvc/cluster_service.go b/internal/testutil/mocksvc/cluster_service.go deleted file mode 100644 index dcfb334b40..0000000000 --- a/internal/testutil/mocksvc/cluster_service.go +++ /dev/null @@ -1,177 +0,0 @@ -// Code generated by mockery. DO NOT EDIT. - -package mocksvc - -import ( - admin "go.mongodb.org/atlas-sdk/v20231115008/admin" - - context "context" - - http "net/http" - - mock "github.com/stretchr/testify/mock" -) - -// ClusterService is an autogenerated mock type for the ClusterService type -type ClusterService struct { - mock.Mock -} - -type ClusterService_Expecter struct { - mock *mock.Mock -} - -func (_m *ClusterService) EXPECT() *ClusterService_Expecter { - return &ClusterService_Expecter{mock: &_m.Mock} -} - -// Get provides a mock function with given fields: ctx, groupID, clusterName -func (_m *ClusterService) Get(ctx context.Context, groupID string, clusterName string) (*admin.AdvancedClusterDescription, *http.Response, error) { - ret := _m.Called(ctx, groupID, clusterName) - - if len(ret) == 0 { - panic("no return value specified for Get") - } - - var r0 *admin.AdvancedClusterDescription - var r1 *http.Response - var r2 error - if rf, ok := ret.Get(0).(func(context.Context, string, string) (*admin.AdvancedClusterDescription, *http.Response, error)); ok { - return rf(ctx, groupID, clusterName) - } - if rf, ok := ret.Get(0).(func(context.Context, string, string) *admin.AdvancedClusterDescription); ok { - r0 = rf(ctx, groupID, clusterName) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*admin.AdvancedClusterDescription) - } - } - - if rf, ok := ret.Get(1).(func(context.Context, string, string) *http.Response); ok { - r1 = rf(ctx, groupID, clusterName) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*http.Response) - } - } - - if rf, ok := ret.Get(2).(func(context.Context, string, string) error); ok { - r2 = rf(ctx, groupID, clusterName) - } else { - r2 = ret.Error(2) - } - - return r0, r1, r2 -} - -// ClusterService_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get' -type ClusterService_Get_Call struct { - *mock.Call -} - -// Get is a helper method to define mock.On call -// - ctx context.Context -// - groupID string -// - clusterName string -func (_e *ClusterService_Expecter) Get(ctx interface{}, groupID interface{}, clusterName interface{}) *ClusterService_Get_Call { - return &ClusterService_Get_Call{Call: _e.mock.On("Get", ctx, groupID, clusterName)} -} - -func (_c *ClusterService_Get_Call) Run(run func(ctx context.Context, groupID string, clusterName string)) *ClusterService_Get_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(string), args[2].(string)) - }) - return _c -} - -func (_c *ClusterService_Get_Call) Return(_a0 *admin.AdvancedClusterDescription, _a1 *http.Response, _a2 error) *ClusterService_Get_Call { - _c.Call.Return(_a0, _a1, _a2) - return _c -} - -func (_c *ClusterService_Get_Call) RunAndReturn(run func(context.Context, string, string) (*admin.AdvancedClusterDescription, *http.Response, error)) *ClusterService_Get_Call { - _c.Call.Return(run) - return _c -} - -// List provides a mock function with given fields: ctx, options -func (_m *ClusterService) List(ctx context.Context, options *admin.ListClustersApiParams) (*admin.PaginatedAdvancedClusterDescription, *http.Response, error) { - ret := _m.Called(ctx, options) - - if len(ret) == 0 { - panic("no return value specified for List") - } - - var r0 *admin.PaginatedAdvancedClusterDescription - var r1 *http.Response - var r2 error - if rf, ok := ret.Get(0).(func(context.Context, *admin.ListClustersApiParams) (*admin.PaginatedAdvancedClusterDescription, *http.Response, error)); ok { - return rf(ctx, options) - } - if rf, ok := ret.Get(0).(func(context.Context, *admin.ListClustersApiParams) *admin.PaginatedAdvancedClusterDescription); ok { - r0 = rf(ctx, options) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*admin.PaginatedAdvancedClusterDescription) - } - } - - if rf, ok := ret.Get(1).(func(context.Context, *admin.ListClustersApiParams) *http.Response); ok { - r1 = rf(ctx, options) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*http.Response) - } - } - - if rf, ok := ret.Get(2).(func(context.Context, *admin.ListClustersApiParams) error); ok { - r2 = rf(ctx, options) - } else { - r2 = ret.Error(2) - } - - return r0, r1, r2 -} - -// ClusterService_List_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'List' -type ClusterService_List_Call struct { - *mock.Call -} - -// List is a helper method to define mock.On call -// - ctx context.Context -// - options *admin.ListClustersApiParams -func (_e *ClusterService_Expecter) List(ctx interface{}, options interface{}) *ClusterService_List_Call { - return &ClusterService_List_Call{Call: _e.mock.On("List", ctx, options)} -} - -func (_c *ClusterService_List_Call) Run(run func(ctx context.Context, options *admin.ListClustersApiParams)) *ClusterService_List_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(*admin.ListClustersApiParams)) - }) - return _c -} - -func (_c *ClusterService_List_Call) Return(_a0 *admin.PaginatedAdvancedClusterDescription, _a1 *http.Response, _a2 error) *ClusterService_List_Call { - _c.Call.Return(_a0, _a1, _a2) - return _c -} - -func (_c *ClusterService_List_Call) RunAndReturn(run func(context.Context, *admin.ListClustersApiParams) (*admin.PaginatedAdvancedClusterDescription, *http.Response, error)) *ClusterService_List_Call { - _c.Call.Return(run) - return _c -} - -// NewClusterService creates a new instance of ClusterService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func NewClusterService(t interface { - mock.TestingT - Cleanup(func()) -}) *ClusterService { - mock := &ClusterService{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -}