Skip to content

Commit 19373da

Browse files
CLOUDP-285944: Add support for Flex Cluster to atlas cluster ls
# Dedicated Cluster ``` ./bin/atlas cluster list -P prod ID NAME MDB VER STATE 6756d725072a5702c704a0ad ClusterM10 8.0.3 CREATING ~/workspace/mongodb-atlas-cli ``` # Flex Cluster ``` ./bin/atlas cluster list -P prod --tier FLEX ID NAME MDB VER STATE 6756d6f81633e20b114c96b9 ClusterFlex 8.0.3 IDLE ```
1 parent eadf6bc commit 19373da

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

docs/command/atlas-clusters-list.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Options
6161
- string
6262
- false
6363
- Hexadecimal string that identifies the project to use. This option overrides the settings in the configuration file or environment variable.
64+
* - --tier
65+
- string
66+
- false
67+
- Tier for each data-bearing server in the cluster. To learn more about cluster tiers, see https://dochub.mongodb.org/core/cluster-tier-atlas.
6468

6569
Inherited Options
6670
-----------------

internal/cli/clusters/list.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import (
2525
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
2626
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
2727
"github.com/spf13/cobra"
28+
atlasv2 "go.mongodb.org/atlas-sdk/v20241113001/admin"
2829
)
2930

3031
type ListOpts struct {
3132
cli.GlobalOpts
3233
cli.OutputOpts
3334
cli.ListOpts
35+
tier string
3436
store store.ClusterLister
3537
}
3638

@@ -47,6 +49,14 @@ var listTemplate = `ID NAME MDB VER STATE{{range valueOrEmptySlice .Results}}
4749
`
4850

4951
func (opts *ListOpts) Run() error {
52+
if opts.tier == atlasFlex {
53+
return opts.RunFlexCluster()
54+
}
55+
56+
return opts.RunDedicatedCluster()
57+
}
58+
59+
func (opts *ListOpts) RunDedicatedCluster() error {
5060
listOpts := opts.NewAtlasListOptions()
5161
r, err := opts.store.ProjectClusters(opts.ConfigProjectID(), listOpts)
5262
if err != nil {
@@ -56,7 +66,27 @@ func (opts *ListOpts) Run() error {
5666
return opts.Print(r)
5767
}
5868

59-
// atlas cluster(s) list --projectId projectId [--page N] [--limit N].
69+
func (opts *ListOpts) RunFlexCluster() error {
70+
r, err := opts.store.ListFlexClusters(opts.newListFlexClustersAPIParams())
71+
if err != nil {
72+
return err
73+
}
74+
75+
return opts.Print(r)
76+
}
77+
78+
func (opts *ListOpts) newListFlexClustersAPIParams() *atlasv2.ListFlexClustersApiParams {
79+
includeCount := !opts.OmitCount
80+
return &atlasv2.ListFlexClustersApiParams{
81+
GroupId: opts.ConfigProjectID(),
82+
IncludeCount: &includeCount,
83+
ItemsPerPage: &opts.ItemsPerPage,
84+
PageNum: &opts.PageNum,
85+
}
86+
}
87+
88+
// ListBuilder builds a cobra.Command that can run as:
89+
// atlas cluster(s) list --projectId projectId [--page N] [--limit N] [--tier tier].
6090
func ListBuilder() *cobra.Command {
6191
opts := &ListOpts{}
6292
cmd := &cobra.Command{
@@ -85,6 +115,7 @@ func ListBuilder() *cobra.Command {
85115
cmd.Flags().IntVar(&opts.PageNum, flag.Page, cli.DefaultPage, usage.Page)
86116
cmd.Flags().IntVar(&opts.ItemsPerPage, flag.Limit, cli.DefaultPageLimit, usage.Limit)
87117
cmd.Flags().BoolVar(&opts.OmitCount, flag.OmitCount, false, usage.OmitCount)
118+
cmd.Flags().StringVar(&opts.tier, flag.Tier, "", usage.Tier)
88119

89120
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
90121
opts.AddOutputOptFlags(cmd)

internal/cli/clusters/list_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import (
2323
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
2424
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
2525
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
26+
"github.com/stretchr/testify/require"
2627
atlasClustersPinned "go.mongodb.org/atlas-sdk/v20240530005/admin"
28+
atlasv2 "go.mongodb.org/atlas-sdk/v20241113001/admin"
2729
)
2830

29-
func TestList_Run(t *testing.T) {
31+
func TestList_RunDedicatedCluster(t *testing.T) {
3032
ctrl := gomock.NewController(t)
3133
mockStore := mocks.NewMockClusterLister(ctrl)
3234

@@ -49,9 +51,34 @@ func TestList_Run(t *testing.T) {
4951
Return(expected, nil).
5052
Times(1)
5153

52-
if err := listOpts.Run(); err != nil {
53-
t.Fatalf("Run() unexpected error: %v", err)
54+
require.NoError(t, listOpts.Run())
55+
}
56+
57+
func TestList_RunFlexCluster(t *testing.T) {
58+
ctrl := gomock.NewController(t)
59+
mockStore := mocks.NewMockClusterLister(ctrl)
60+
61+
expected := &atlasv2.PaginatedFlexClusters20241113{
62+
Results: &[]atlasv2.FlexClusterDescription20241113{
63+
{
64+
Name: pointer.Get("test"),
65+
Id: pointer.Get("123"),
66+
},
67+
},
5468
}
69+
70+
listOpts := &ListOpts{
71+
store: mockStore,
72+
tier: atlasFlex,
73+
}
74+
75+
mockStore.
76+
EXPECT().
77+
ListFlexClusters(listOpts.newListFlexClustersAPIParams()).
78+
Return(expected, nil).
79+
Times(1)
80+
81+
require.NoError(t, listOpts.Run())
5582
}
5683

5784
func TestListTemplate(t *testing.T) {

0 commit comments

Comments
 (0)