Skip to content

Commit 7db2572

Browse files
CLOUDP-285949: add support for flex clusters to atlas backup snaphots list (#3483)
1 parent 6d45da1 commit 7db2572

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

internal/cli/backup/snapshots/list.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
2525
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
2626
"github.com/spf13/cobra"
27+
atlasv2 "go.mongodb.org/atlas-sdk/v20241113002/admin"
2728
)
2829

2930
type ListOpts struct {
@@ -45,17 +46,47 @@ func (opts *ListOpts) initStore(ctx context.Context) func() error {
4546
var listTemplate = `ID TYPE STATUS CREATED AT EXPIRES AT{{range valueOrEmptySlice .Results}}
4647
{{.Id}} {{.SnapshotType}} {{.Status}} {{.CreatedAt}} {{.ExpiresAt}}{{end}}
4748
`
49+
var listTemplateFlex = `ID STATUS MONGODB VERSION START TIME FINISH TIME EXPIRATION{{range valueOrEmptySlice .Results}}
50+
{{.Id}} {{.Status}} {{.MongoDBVersion}} {{.StartTime}} {{.FinishTime}} {{.Expiration}}{{end}}
51+
`
4852

4953
func (opts *ListOpts) Run() error {
54+
r, err := opts.store.FlexClusterSnapshots(opts.newListFlexBackupsAPIParams())
55+
if err == nil {
56+
opts.Template = listTemplateFlex
57+
return opts.Print(r)
58+
}
59+
60+
apiError, ok := atlasv2.AsError(err)
61+
if !ok {
62+
return err
63+
}
64+
65+
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode {
66+
return err
67+
}
68+
5069
listOpts := opts.NewListOptions()
51-
r, err := opts.store.Snapshots(opts.ConfigProjectID(), opts.clusterName, listOpts)
70+
snapshotsList, err := opts.store.Snapshots(opts.ConfigProjectID(), opts.clusterName, listOpts)
5271
if err != nil {
5372
return err
5473
}
5574

56-
return opts.Print(r)
75+
return opts.Print(snapshotsList)
76+
}
77+
78+
func (opts *ListOpts) newListFlexBackupsAPIParams() *atlasv2.ListFlexBackupsApiParams {
79+
includeCount := !opts.ListOpts.OmitCount
80+
return &atlasv2.ListFlexBackupsApiParams{
81+
GroupId: opts.ConfigProjectID(),
82+
Name: opts.clusterName,
83+
IncludeCount: &includeCount,
84+
ItemsPerPage: &opts.ListOpts.ItemsPerPage,
85+
PageNum: &opts.ListOpts.PageNum,
86+
}
5787
}
5888

89+
// ListBuilder builds a cobra.Command that can run as:
5990
// atlas backups snapshots list <clusterName> [--projectId projectId] [--page N] [--limit N].
6091
func ListBuilder() *cobra.Command {
6192
opts := new(ListOpts)

internal/cli/backup/snapshots/list_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
2626
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
2727
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
28+
"github.com/stretchr/testify/require"
2829
atlasv2 "go.mongodb.org/atlas-sdk/v20241113002/admin"
2930
)
3031

@@ -52,14 +53,45 @@ func TestList_Run(t *testing.T) {
5253
},
5354
}
5455

56+
expectedError := &atlasv2.GenericOpenAPIError{}
57+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
58+
59+
mockStore.
60+
EXPECT().
61+
FlexClusterSnapshots(listOpts.newListFlexBackupsAPIParams()).
62+
Return(nil, expectedError).
63+
Times(1)
64+
5565
mockStore.
5666
EXPECT().
5767
Snapshots(listOpts.ProjectID, "Cluster0", listOpts.NewListOptions()).
5868
Return(expected, nil).
5969
Times(1)
6070

61-
if err := listOpts.Run(); err != nil {
62-
t.Fatalf("Run() unexpected error: %v", err)
71+
require.NoError(t, listOpts.Run())
72+
test.VerifyOutputTemplate(t, listTemplate, expected)
73+
}
74+
75+
func TestList_Run_FlexCluster(t *testing.T) {
76+
ctrl := gomock.NewController(t)
77+
mockStore := mocks.NewMockSnapshotsLister(ctrl)
78+
expected := &atlasv2.PaginatedApiAtlasFlexBackupSnapshot20241113{}
79+
buf := new(bytes.Buffer)
80+
listOpts := &ListOpts{
81+
store: mockStore,
82+
clusterName: "Cluster0",
83+
OutputOpts: cli.OutputOpts{
84+
Template: listTemplate,
85+
OutWriter: buf,
86+
},
6387
}
88+
89+
mockStore.
90+
EXPECT().
91+
FlexClusterSnapshots(listOpts.newListFlexBackupsAPIParams()).
92+
Return(expected, nil).
93+
Times(1)
94+
95+
require.NoError(t, listOpts.Run())
6496
test.VerifyOutputTemplate(t, listTemplate, expected)
6597
}

0 commit comments

Comments
 (0)