Skip to content

Commit 4c6b485

Browse files
CLOUDP-285952: add support for flex clusters to atlas backup restores list (#3487)
1 parent 7db2572 commit 4c6b485

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

internal/cli/backup/restores/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 restoreListTemplate = `ID SNAPSHOT CLUSTER TYPE EXPIRES AT{{range valueOrEmptySlice .Results}}
4647
{{.Id}} {{.SnapshotId}} {{.TargetClusterName}} {{.DeliveryType}} {{.ExpiresAt}}{{end}}
4748
`
49+
var restoreListFlexClusterTemplate = `ID SNAPSHOT CLUSTER TYPE EXPIRES AT{{range valueOrEmptySlice .Results}}
50+
{{.Id}} {{.SnapshotId}} {{.TargetDeploymentItemName}} {{.DeliveryType}} {{.ExpirationDate}}{{end}}
51+
`
4852

4953
func (opts *ListOpts) Run() error {
54+
r, err := opts.store.RestoreFlexClusterJobs(opts.newListFlexBackupRestoreJobsAPIParams())
55+
if err == nil {
56+
opts.Template = restoreListFlexClusterTemplate
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.RestoreJobs(opts.ConfigProjectID(), opts.clusterName, listOpts)
70+
restoreJobs, err := opts.store.RestoreJobs(opts.ConfigProjectID(), opts.clusterName, listOpts)
5271
if err != nil {
5372
return err
5473
}
5574

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

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

internal/cli/backup/restores/list_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/golang/mock/gomock"
2323
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
2424
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
25+
"github.com/stretchr/testify/require"
2526
atlasv2 "go.mongodb.org/atlas-sdk/v20241113002/admin"
2627
)
2728

@@ -36,15 +37,42 @@ func TestListOpts_Run(t *testing.T) {
3637
clusterName: "Cluster0",
3738
}
3839

40+
expectedError := &atlasv2.GenericOpenAPIError{}
41+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
42+
43+
mockStore.
44+
EXPECT().
45+
RestoreFlexClusterJobs(listOpts.newListFlexBackupRestoreJobsAPIParams()).
46+
Return(nil, expectedError).
47+
Times(1)
48+
3949
mockStore.
4050
EXPECT().
4151
RestoreJobs(listOpts.ProjectID, "Cluster0", listOpts.NewListOptions()).
4252
Return(expected, nil).
4353
Times(1)
4454

45-
if err := listOpts.Run(); err != nil {
46-
t.Fatalf("Run() unexpected error: %v", err)
55+
require.NoError(t, listOpts.Run())
56+
test.VerifyOutputTemplate(t, restoreListTemplate, expected)
57+
}
58+
59+
func TestListOpts_Run_FlexCluster(t *testing.T) {
60+
ctrl := gomock.NewController(t)
61+
mockStore := mocks.NewMockRestoreJobsLister(ctrl)
62+
63+
expected := &atlasv2.PaginatedApiAtlasFlexBackupRestoreJob20241113{}
64+
65+
listOpts := &ListOpts{
66+
store: mockStore,
67+
clusterName: "Cluster0",
4768
}
4869

49-
test.VerifyOutputTemplate(t, restoreListTemplate, expected)
70+
mockStore.
71+
EXPECT().
72+
RestoreFlexClusterJobs(listOpts.newListFlexBackupRestoreJobsAPIParams()).
73+
Return(nil, nil).
74+
Times(1)
75+
76+
require.NoError(t, listOpts.Run())
77+
test.VerifyOutputTemplate(t, restoreListFlexClusterTemplate, expected)
5078
}

internal/cli/backup/restores/restores.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import (
1919
"github.com/spf13/cobra"
2020
)
2121

22+
const (
23+
cannotUseNotFlexWithFlexApisErrorCode = "CANNOT_USE_NON_FLEX_CLUSTER_IN_FLEX_API"
24+
)
25+
2226
func Builder() *cobra.Command {
2327
const use = "restores"
2428
cmd := &cobra.Command{

0 commit comments

Comments
 (0)