Skip to content

Commit 064c0d8

Browse files
CLOUDP-285953: add support for flex clusters to "atlas backup restores get"
1 parent 7db2572 commit 064c0d8

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

internal/cli/backup/restores/describe.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ 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/v20241113002/admin"
2829
)
2930

3031
type DescribeOpts struct {
@@ -47,23 +48,44 @@ var restoreDescribeTemplate = `ID SNAPSHOT CLUSTER TYPE EXPIRES AT URLs
4748
{{.Id}} {{.SnapshotId}} {{.TargetClusterName}} {{.DeliveryType}} {{.ExpiresAt}} {{range $index, $element := valueOrEmptySlice .DeliveryUrl}}{{if $index}}; {{end}}{{$element}}{{end}}
4849
`
4950

51+
var restoreDescribeFlexClusterTemplate = `ID SNAPSHOT CLUSTER TYPE EXPIRES AT URLs
52+
{{.Id}} {{.SnapshotId}} {{.TargetDeploymentItemName}} {{.DeliveryType}} {{.ExpirationDate}} {{range $index, $element := valueOrEmptySlice .SnapshotUrl}}{{if $index}}; {{end}}{{$element}}{{end}}
53+
`
54+
5055
func (opts *DescribeOpts) Run() error {
51-
r, err := opts.store.RestoreJob(opts.ConfigProjectID(), opts.clusterName, opts.id)
56+
r, err := opts.store.RestoreFlexClusterJob(opts.ConfigProjectID(), opts.clusterName, opts.id)
57+
if err == nil {
58+
opts.Template = restoreDescribeFlexClusterTemplate
59+
return opts.Print(r)
60+
}
61+
62+
apiError, ok := atlasv2.AsError(err)
63+
if !ok {
64+
return err
65+
}
66+
67+
if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode {
68+
return err
69+
}
70+
71+
restoreJob, err := opts.store.RestoreJob(opts.ConfigProjectID(), opts.clusterName, opts.id)
5272
if err != nil {
5373
return err
5474
}
5575

56-
return opts.Print(r)
76+
return opts.Print(restoreJob)
5777
}
5878

79+
// DescribeBuilder builds a cobra.Command that can run as:
5980
// atlas backup(s) restore(s) job(s) describe <ID>.
6081
func DescribeBuilder() *cobra.Command {
6182
opts := new(DescribeOpts)
6283
cmd := &cobra.Command{
63-
Use: "describe <restoreJobId>",
64-
Short: "Describe a cloud backup restore job.",
65-
Long: fmt.Sprintf(usage.RequiredRole, "Project Owner"),
66-
Args: require.ExactArgs(1),
84+
Use: "describe <restoreJobId>",
85+
Aliases: []string{"get"},
86+
Short: "Describe a cloud backup restore job.",
87+
Long: fmt.Sprintf(usage.RequiredRole, "Project Owner"),
88+
Args: require.ExactArgs(1),
6789
Annotations: map[string]string{
6890
"restoreJobIdDesc": "ID of the restore job.",
6991
},

internal/cli/backup/restores/describe_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ 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
atlasv2 "go.mongodb.org/atlas-sdk/v20241113002/admin"
2728
)
2829

@@ -40,15 +41,46 @@ func TestDescribeOpts_Run(t *testing.T) {
4041
id: "1",
4142
}
4243

44+
expectedError := &atlasv2.GenericOpenAPIError{}
45+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
46+
47+
mockStore.
48+
EXPECT().
49+
RestoreFlexClusterJob(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.id).
50+
Return(nil, expectedError).
51+
Times(1)
52+
4353
mockStore.
4454
EXPECT().
4555
RestoreJob(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.id).
4656
Return(expected, nil).
4757
Times(1)
4858

49-
if err := describeOpts.Run(); err != nil {
50-
t.Fatalf("Run() unexpected error: %v", err)
59+
require.NoError(t, describeOpts.Run())
60+
test.VerifyOutputTemplate(t, restoreDescribeTemplate, expected)
61+
}
62+
63+
func TestDescribeOpts_Run_FlexCluster(t *testing.T) {
64+
ctrl := gomock.NewController(t)
65+
mockStore := mocks.NewMockRestoreJobsDescriber(ctrl)
66+
67+
expected := &atlasv2.FlexBackupRestoreJob20241113{
68+
Id: pointer.Get("1"),
69+
}
70+
71+
describeOpts := &DescribeOpts{
72+
store: mockStore,
73+
clusterName: "Cluster0",
74+
id: "1",
5175
}
5276

53-
test.VerifyOutputTemplate(t, restoreDescribeTemplate, expected)
77+
mockStore.
78+
EXPECT().
79+
RestoreFlexClusterJob(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.id).
80+
Return(nil, nil).
81+
Times(1)
82+
83+
require.NoError(t, describeOpts.Run())
84+
85+
test.VerifyOutputTemplate(t, restoreDescribeFlexClusterTemplate, expected)
5486
}

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)