Skip to content

Commit 1a8e42d

Browse files
authored
CLOUDP-326352: Fixes backup commands to fallback to flex if not found (#4023)
1 parent 51ffe20 commit 1a8e42d

File tree

12 files changed

+63
-19
lines changed

12 files changed

+63
-19
lines changed

internal/cli/backup/restores/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (opts *DescribeOpts) Run() error {
7171
return err
7272
}
7373

74-
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != featureUnsupported {
74+
if apiError.ErrorCode != CannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != FeatureUnsupported && apiError.ErrorCode != ClusterNotFoundErrorCode {
7575
return err
7676
}
7777

internal/cli/backup/restores/describe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestDescribeOpts_Run(t *testing.T) {
4141
}
4242

4343
expectedError := &atlasv2.GenericOpenAPIError{}
44-
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
44+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: CannotUseNotFlexWithFlexApisErrorCode})
4545

4646
mockStore.
4747
EXPECT().

internal/cli/backup/restores/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (opts *ListOpts) Run() error {
6969
return err
7070
}
7171

72-
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != featureUnsupported {
72+
if apiError.ErrorCode != CannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != FeatureUnsupported && apiError.ErrorCode != ClusterNotFoundErrorCode {
7373
return err
7474
}
7575

internal/cli/backup/restores/list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestListOpts_Run(t *testing.T) {
3737
}
3838

3939
expectedError := &atlasv2.GenericOpenAPIError{}
40-
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
40+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: CannotUseNotFlexWithFlexApisErrorCode})
4141

4242
mockStore.
4343
EXPECT().

internal/cli/backup/restores/restores.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
)
2121

2222
const (
23-
cannotUseNotFlexWithFlexApisErrorCode = "CANNOT_USE_NON_FLEX_CLUSTER_IN_FLEX_API"
24-
featureUnsupported = "FEATURE_UNSUPPORTED"
23+
CannotUseNotFlexWithFlexApisErrorCode = "CANNOT_USE_NON_FLEX_CLUSTER_IN_FLEX_API"
24+
FeatureUnsupported = "FEATURE_UNSUPPORTED"
25+
ClusterNotFoundErrorCode = "CLUSTER_NOT_FOUND"
26+
DeliveryTypeDownload = "download"
2527
)
2628

2729
func Builder() *cobra.Command {

internal/cli/backup/restores/watch.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import (
2727
atlasv2 "go.mongodb.org/atlas-sdk/v20250312005/admin"
2828
)
2929

30-
const deliveryTypeDownload = "download"
30+
var watchTemplate = "\nRestore completed.\n"
31+
var result *atlasv2.DiskBackupSnapshotRestoreJob
3132

3233
type WatchOpts struct {
3334
cli.ProjectOpts
@@ -38,9 +39,6 @@ type WatchOpts struct {
3839
store RestoreJobsDescriber
3940
}
4041

41-
var watchTemplate = "\nRestore completed.\n"
42-
var result *atlasv2.DiskBackupSnapshotRestoreJob
43-
4442
func (opts *WatchOpts) initStore(ctx context.Context) func() error {
4543
return func() error {
4644
var err error
@@ -64,7 +62,7 @@ func stopWatcher(result *atlasv2.DiskBackupSnapshotRestoreJob) bool {
6462
return true
6563
}
6664

67-
if result.GetDeliveryType() == deliveryTypeDownload && len(result.GetDeliveryUrl()) > 0 {
65+
if result.GetDeliveryType() == DeliveryTypeDownload && len(result.GetDeliveryUrl()) > 0 {
6866
return true
6967
}
7068

@@ -118,7 +116,7 @@ func (opts *WatchOpts) newIsFlexCluster() error {
118116
return err
119117
}
120118

121-
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != featureUnsupported {
119+
if apiError.ErrorCode != CannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != FeatureUnsupported && apiError.ErrorCode != ClusterNotFoundErrorCode {
122120
return err
123121
}
124122

internal/cli/backup/snapshots/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (opts *DescribeOpts) Run() error {
7171
return err
7272
}
7373

74-
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != featureUnsupported {
74+
if apiError.ErrorCode != CannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != FeatureUnsupported && apiError.ErrorCode != ClusterNotFoundErrorCode {
7575
return err
7676
}
7777

internal/cli/backup/snapshots/describe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestDescribe_Run(t *testing.T) {
3636
}
3737

3838
expectedError := &atlasv2.GenericOpenAPIError{}
39-
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
39+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: CannotUseNotFlexWithFlexApisErrorCode})
4040

4141
mockStore.
4242
EXPECT().

internal/cli/backup/snapshots/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (opts *ListOpts) Run() error {
6969
return err
7070
}
7171

72-
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != featureUnsupported {
72+
if apiError.ErrorCode != CannotUseNotFlexWithFlexApisErrorCode && apiError.ErrorCode != FeatureUnsupported && apiError.ErrorCode != ClusterNotFoundErrorCode {
7373
return err
7474
}
7575

internal/cli/backup/snapshots/list_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestList_Run(t *testing.T) {
5353
}
5454

5555
expectedError := &atlasv2.GenericOpenAPIError{}
56-
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
56+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: CannotUseNotFlexWithFlexApisErrorCode})
5757

5858
mockStore.
5959
EXPECT().
@@ -94,3 +94,46 @@ func TestList_Run_FlexCluster(t *testing.T) {
9494
require.NoError(t, listOpts.Run())
9595
test.VerifyOutputTemplate(t, listTemplate, expected)
9696
}
97+
98+
func TestList_Run_ClusterNotFoundFallback(t *testing.T) {
99+
ctrl := gomock.NewController(t)
100+
mockStore := NewMockLister(ctrl)
101+
buf := new(bytes.Buffer)
102+
expected := &atlasv2.PaginatedCloudBackupReplicaSet{
103+
Results: &[]atlasv2.DiskBackupReplicaSet{
104+
{
105+
CloudProvider: pointer.Get("AWS"),
106+
Id: pointer.Get("5f9b0b5e0b5e9d6b6e0b5e9d"),
107+
SnapshotType: pointer.Get("cloud"),
108+
},
109+
*atlasv2.NewDiskBackupReplicaSet(),
110+
},
111+
}
112+
113+
listOpts := &ListOpts{
114+
store: mockStore,
115+
clusterName: "Cluster0",
116+
OutputOpts: cli.OutputOpts{
117+
Template: listTemplate,
118+
OutWriter: buf,
119+
},
120+
}
121+
122+
expectedError := &atlasv2.GenericOpenAPIError{}
123+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: ClusterNotFoundErrorCode})
124+
125+
mockStore.
126+
EXPECT().
127+
FlexClusterSnapshots(listOpts.newListFlexBackupsAPIParams()).
128+
Return(nil, expectedError).
129+
Times(1)
130+
131+
mockStore.
132+
EXPECT().
133+
Snapshots(listOpts.ProjectID, "Cluster0", listOpts.NewAtlasListOptions()).
134+
Return(expected, nil).
135+
Times(1)
136+
137+
require.NoError(t, listOpts.Run())
138+
test.VerifyOutputTemplate(t, listTemplate, expected)
139+
}

0 commit comments

Comments
 (0)