Skip to content

Commit b7ac4af

Browse files
CLOUDP-285949: add support for flex clusters to atlas backup snaphots list
1 parent 94ba52d commit b7ac4af

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

internal/cli/backup/snapshots/list.go

Lines changed: 29 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 {
@@ -47,15 +48,41 @@ var listTemplate = `ID TYPE STATUS CREATED AT EXPIRES AT{{range valueOrEmptySlic
4748
`
4849

4950
func (opts *ListOpts) Run() error {
51+
r, err := opts.store.FlexClusterSnapshots(opts.newListFlexBackupsAPIParams())
52+
if err == nil {
53+
return opts.Print(r)
54+
}
55+
56+
apiError, ok := atlasv2.AsError(err)
57+
if !ok {
58+
return err
59+
}
60+
61+
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode {
62+
return err
63+
}
64+
5065
listOpts := opts.NewListOptions()
51-
r, err := opts.store.Snapshots(opts.ConfigProjectID(), opts.clusterName, listOpts)
66+
snapshotsList, err := opts.store.Snapshots(opts.ConfigProjectID(), opts.clusterName, listOpts)
5267
if err != nil {
5368
return err
5469
}
5570

56-
return opts.Print(r)
71+
return opts.Print(snapshotsList)
72+
}
73+
74+
func (opts *ListOpts) newListFlexBackupsAPIParams() *atlasv2.ListFlexBackupsApiParams {
75+
includeCount := !opts.ListOpts.OmitCount
76+
return &atlasv2.ListFlexBackupsApiParams{
77+
GroupId: opts.ConfigProjectID(),
78+
Name: opts.clusterName,
79+
IncludeCount: &includeCount,
80+
ItemsPerPage: &opts.ListOpts.ItemsPerPage,
81+
PageNum: &opts.ListOpts.PageNum,
82+
}
5783
}
5884

85+
// ListBuilder builds a cobra.Command that can run as:
5986
// atlas backups snapshots list <clusterName> [--projectId projectId] [--page N] [--limit N].
6087
func ListBuilder() *cobra.Command {
6188
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)