Skip to content

Commit fe75b12

Browse files
authored
CLOUDP-322512: Use latest SDK for atlas deployments list (#3934)
1 parent 36b251f commit fe75b12

File tree

8 files changed

+124
-86
lines changed

8 files changed

+124
-86
lines changed

internal/cli/deployments/list_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import (
3030
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
3131
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
3232
"github.com/stretchr/testify/assert"
33-
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
33+
atlasv2 "go.mongodb.org/atlas-sdk/v20250312003/admin"
3434
"go.uber.org/mock/gomock"
3535
)
3636

3737
func TestList_Run(t *testing.T) {
3838
ctrl := gomock.NewController(t)
39-
mockStore := mocks.NewMockClusterLister(ctrl)
39+
mockStore := fixture.NewMockClusterLister(ctrl)
4040
mockCredentialsGetter := mocks.NewMockCredentialsGetter(ctrl)
4141
mockContainerEngine := mocks.NewMockEngine(ctrl)
4242
ctx := t.Context()
@@ -46,8 +46,8 @@ func TestList_Run(t *testing.T) {
4646
cli.TokenRefreshed = false
4747
})
4848

49-
expectedAtlasClusters := &atlasClustersPinned.PaginatedAdvancedClusterDescription{
50-
Results: &[]atlasClustersPinned.AdvancedClusterDescription{
49+
expectedAtlasClusters := &atlasv2.PaginatedClusterDescription20240805{
50+
Results: &[]atlasv2.ClusterDescription20240805{
5151
{
5252
Name: pointer.Get("atlasCluster2"),
5353
Id: pointer.Get("123"),
@@ -96,7 +96,7 @@ func TestList_Run(t *testing.T) {
9696

9797
mockStore.
9898
EXPECT().
99-
ProjectClusters(listOpts.ProjectID,
99+
LatestProjectClusters(listOpts.ProjectID,
100100
&store.ListOptions{
101101
PageNum: cli.DefaultPage,
102102
ItemsPerPage: options.MaxItemsPerPage,
@@ -141,7 +141,7 @@ localTest2 LOCAL 6.0.9 IDLE
141141

142142
func TestList_Run_NoLocal(t *testing.T) {
143143
ctrl := gomock.NewController(t)
144-
mockStore := mocks.NewMockClusterLister(ctrl)
144+
mockStore := fixture.NewMockClusterLister(ctrl)
145145
mockCredentialsGetter := mocks.NewMockCredentialsGetter(ctrl)
146146

147147
mockContainerEngine := mocks.NewMockEngine(ctrl)
@@ -152,8 +152,8 @@ func TestList_Run_NoLocal(t *testing.T) {
152152
cli.TokenRefreshed = false
153153
})
154154

155-
expectedAtlasClusters := &atlasClustersPinned.PaginatedAdvancedClusterDescription{
156-
Results: &[]atlasClustersPinned.AdvancedClusterDescription{
155+
expectedAtlasClusters := &atlasv2.PaginatedClusterDescription20240805{
156+
Results: &[]atlasv2.ClusterDescription20240805{
157157
{
158158
Name: pointer.Get("atlasCluster2"),
159159
Id: pointer.Get("123"),
@@ -189,7 +189,7 @@ func TestList_Run_NoLocal(t *testing.T) {
189189

190190
mockStore.
191191
EXPECT().
192-
ProjectClusters(listOpts.ProjectID,
192+
LatestProjectClusters(listOpts.ProjectID,
193193
&store.ListOptions{
194194
PageNum: cli.DefaultPage,
195195
ItemsPerPage: options.MaxItemsPerPage,
@@ -232,7 +232,7 @@ atlasCluster1 ATLAS 7.0.0 IDLE
232232

233233
func TestList_Run_NoAtlas(t *testing.T) {
234234
ctrl := gomock.NewController(t)
235-
mockStore := mocks.NewMockClusterLister(ctrl)
235+
mockStore := fixture.NewMockClusterLister(ctrl)
236236
mockCredentialsGetter := mocks.NewMockCredentialsGetter(ctrl)
237237
mockContainerEngine := mocks.NewMockEngine(ctrl)
238238
ctx := t.Context()
@@ -242,8 +242,8 @@ func TestList_Run_NoAtlas(t *testing.T) {
242242
cli.TokenRefreshed = false
243243
})
244244

245-
expectedAtlasClusters := &atlasClustersPinned.PaginatedAdvancedClusterDescription{
246-
Results: &[]atlasClustersPinned.AdvancedClusterDescription{
245+
expectedAtlasClusters := &atlasv2.PaginatedClusterDescription20240805{
246+
Results: &[]atlasv2.ClusterDescription20240805{
247247
{
248248
Name: pointer.Get("atlasCluster2"),
249249
Id: pointer.Get("123"),
@@ -279,7 +279,7 @@ func TestList_Run_NoAtlas(t *testing.T) {
279279

280280
mockStore.
281281
EXPECT().
282-
ProjectClusters(listOpts.ProjectID,
282+
LatestProjectClusters(listOpts.ProjectID,
283283
&store.ListOptions{
284284
PageNum: cli.DefaultPage,
285285
ItemsPerPage: options.MaxItemsPerPage,
@@ -324,7 +324,7 @@ func TestListOpts_PostRun(t *testing.T) {
324324
ctrl := gomock.NewController(t)
325325
buf := new(bytes.Buffer)
326326

327-
mockStore := mocks.NewMockClusterLister(ctrl)
327+
mockStore := fixture.NewMockClusterLister(ctrl)
328328
mockCredentialsGetter := mocks.NewMockCredentialsGetter(ctrl)
329329

330330
deploymentsTest := fixture.NewMockLocalDeploymentOpts(ctrl, "localDeployment")

internal/cli/deployments/options/deployment_opts.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ import (
3737
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
3838
"github.com/shirou/gopsutil/v4/cpu"
3939
"github.com/shirou/gopsutil/v4/mem"
40+
atlasv2 "go.mongodb.org/atlas-sdk/v20250312003/admin"
4041
)
4142

43+
//go:generate go tool go.uber.org/mock/mockgen -typed -destination=../test/fixture/deployment_opts_mocks.go -package=fixture . ClusterLister
44+
4245
const (
4346
spinnerSpeed = 100 * time.Millisecond
4447
// based on https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Clusters/operation/createCluster
@@ -100,7 +103,7 @@ type DeploymentOpts struct {
100103
CredStore store.CredentialsGetter
101104
s *spinner.Spinner
102105
DefaultSetter cli.DefaultSetterOpts
103-
AtlasClusterListStore store.ClusterLister
106+
AtlasClusterListStore ClusterLister
104107
Config ProfileReader
105108
DeploymentTelemetry DeploymentTelemetry
106109
DeploymentUUID string
@@ -113,6 +116,10 @@ type Deployment struct {
113116
StateName string
114117
}
115118

119+
type ClusterLister interface {
120+
LatestProjectClusters(string, *store.ListOptions) (*atlasv2.PaginatedClusterDescription20240805, error)
121+
}
122+
116123
func (opts *DeploymentOpts) InitStore(ctx context.Context, writer io.Writer) func() error {
117124
return func() error {
118125
var err error

internal/cli/deployments/options/deployment_opts_pre_run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (opts *DeploymentOpts) AtlasDeployments(projectID string) ([]Deployment, er
154154
ItemsPerPage: MaxItemsPerPage,
155155
}
156156

157-
atlasClusters, err := opts.AtlasClusterListStore.ProjectClusters(projectID, listOpts)
157+
atlasClusters, err := opts.AtlasClusterListStore.LatestProjectClusters(projectID, listOpts)
158158
if err != nil {
159159
return nil, err
160160
}

internal/cli/deployments/test/fixture/deployment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
type MockDeploymentOpts struct {
2323
ctrl *gomock.Controller
2424
MockCredentialsGetter *mocks.MockCredentialsGetter
25-
MockAtlasClusterListStore *mocks.MockClusterLister
25+
MockAtlasClusterListStore *MockClusterLister
2626
MockContainerEngine *mocks.MockEngine
2727
MockDeploymentTelemetry *mocks.MockDeploymentTelemetry
2828
Opts *options.DeploymentOpts

internal/cli/deployments/test/fixture/deployment_atlas.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import (
1919
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
2020
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
2121
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
22-
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
22+
atlasv2 "go.mongodb.org/atlas-sdk/v20250312003/admin"
2323
"go.uber.org/mock/gomock"
2424
)
2525

2626
func NewMockAtlasDeploymentOpts(ctrl *gomock.Controller, deploymentName string) MockDeploymentOpts {
2727
mockCredentialsGetter := mocks.NewMockCredentialsGetter(ctrl)
28-
mockAtlasClusterListStore := mocks.NewMockClusterLister(ctrl)
28+
mockAtlasClusterListStore := NewMockClusterLister(ctrl)
2929
mockDeploymentTelemetry := mocks.NewMockDeploymentTelemetry(ctrl)
3030

3131
return MockDeploymentOpts{
@@ -43,9 +43,9 @@ func NewMockAtlasDeploymentOpts(ctrl *gomock.Controller, deploymentName string)
4343
}
4444
}
4545

46-
func (m *MockDeploymentOpts) MockPaginatedAdvancedClusterDescription(state string) *atlasClustersPinned.PaginatedAdvancedClusterDescription {
47-
return &atlasClustersPinned.PaginatedAdvancedClusterDescription{
48-
Results: &[]atlasClustersPinned.AdvancedClusterDescription{
46+
func (m *MockDeploymentOpts) MockPaginatedAdvancedClusterDescription(state string) *atlasv2.PaginatedClusterDescription20240805 {
47+
return &atlasv2.PaginatedClusterDescription20240805{
48+
Results: &[]atlasv2.ClusterDescription20240805{
4949
{
5050
Name: pointer.Get(m.Opts.DeploymentName),
5151
Id: pointer.Get("123"),
@@ -70,7 +70,7 @@ func (m *MockDeploymentOpts) CommonAtlasMocksWithState(projectID string, state s
7070

7171
m.MockAtlasClusterListStore.
7272
EXPECT().
73-
ProjectClusters(projectID, gomock.Any()).
73+
LatestProjectClusters(projectID, gomock.Any()).
7474
Return(m.MockPaginatedAdvancedClusterDescription(state), nil).
7575
Times(1)
7676

internal/cli/deployments/test/fixture/deployment_opts_mocks.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mocks/mock_clusters.go

Lines changed: 2 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/clusters.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ import (
2020
atlas "go.mongodb.org/atlas/mongodbatlas"
2121
)
2222

23-
//go:generate go tool go.uber.org/mock/mockgen -destination=../mocks/mock_clusters.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store ClusterLister,ClusterDescriber
24-
25-
type ClusterLister interface { //nolint:iface // right now requires some refactor to deployment commands
26-
ProjectClusters(string, *ListOptions) (*atlasClustersPinned.PaginatedAdvancedClusterDescription, error)
27-
ListFlexClusters(*atlasv2.ListFlexClustersApiParams) (*atlasv2.PaginatedFlexClusters20241113, error)
28-
}
23+
//go:generate go tool go.uber.org/mock/mockgen -destination=../mocks/mock_clusters.go -package=mocks github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store ClusterDescriber
2924

3025
type ClusterDescriber interface { //nolint:iface // right now requires some refactor to deployment commands
3126
AtlasCluster(string, string) (*atlasClustersPinned.AdvancedClusterDescription, error)
@@ -102,6 +97,16 @@ func (s *Store) ProjectClusters(projectID string, opts *ListOptions) (*atlasClus
10297
return result, err
10398
}
10499

100+
// LatestProjectClusters lists the clusters using the latest API version.
101+
func (s *Store) LatestProjectClusters(projectID string, opts *ListOptions) (*atlasv2.PaginatedClusterDescription20240805, error) {
102+
res := s.clientv2.ClustersApi.ListClusters(s.ctx, projectID)
103+
if opts != nil {
104+
res = res.PageNum(opts.PageNum).ItemsPerPage(opts.ItemsPerPage).IncludeCount(opts.IncludeCount)
105+
}
106+
result, _, err := res.Execute()
107+
return result, err
108+
}
109+
105110
// AtlasCluster encapsulates the logic to manage different cloud providers.
106111
func (s *Store) AtlasCluster(projectID, name string) (*atlasClustersPinned.AdvancedClusterDescription, error) {
107112
result, _, err := s.clientClusters.ClustersApi.GetCluster(s.ctx, projectID, name).Execute()

0 commit comments

Comments
 (0)