Skip to content

Commit 94ba52d

Browse files
CLOUDP-285948: add support for flex clusters to 'atlas backup snaphots get' (#3482)
1 parent 105ad39 commit 94ba52d

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

internal/cli/backup/snapshots/describe.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ 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
const describeTemplate = `ID SNAPSHOT TYPE TYPE DESCRIPTION EXPIRES AT
3132
{{.Id}} {{.SnapshotType}} {{.Type}} {{.Description}} {{.ExpiresAt}}
3233
`
3334

35+
const describeTemplateFlex = `ID STATUS MONGODB VERSION START TIME FINISH TIME EXPIRATION
36+
{{.Id}} {{.Status}} {{.MongoDBVersion}} {{.StartTime}} {{.FinishTime}} {{.Expiration}}
37+
`
38+
3439
type DescribeOpts struct {
3540
cli.ProjectOpts
3641
cli.OutputOpts
@@ -48,22 +53,39 @@ func (opts *DescribeOpts) initStore(ctx context.Context) func() error {
4853
}
4954

5055
func (opts *DescribeOpts) Run() error {
51-
r, err := opts.store.Snapshot(opts.ConfigProjectID(), opts.clusterName, opts.snapshot)
56+
r, err := opts.store.FlexClusterSnapshot(opts.ConfigProjectID(), opts.clusterName, opts.snapshot)
57+
if err == nil {
58+
opts.Template = describeTemplateFlex
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+
snapshots, err := opts.store.Snapshot(opts.ConfigProjectID(), opts.clusterName, opts.snapshot)
5272
if err != nil {
5373
return err
5474
}
5575

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

79+
// DescribeBuilder builds a cobra.Command that can run as:
5980
// atlas backup snapshots describe snapshotId --clusterName clusterName --projectId projectId.
6081
func DescribeBuilder() *cobra.Command {
6182
opts := new(DescribeOpts)
6283
cmd := &cobra.Command{
63-
Use: "describe <snapshotId>",
64-
Short: "Return the details for the specified snapshot for your project.",
65-
Long: fmt.Sprintf(usage.RequiredRole, "Project Read Only"),
66-
Args: require.ExactArgs(1),
84+
Use: "describe <snapshotId>",
85+
Aliases: []string{"get"},
86+
Short: "Return the details for the specified snapshot for your project.",
87+
Long: fmt.Sprintf(usage.RequiredRole, "Project Read Only"),
88+
Args: require.ExactArgs(1),
6789
Example: ` # Return the details for the backup snapshot with the ID 5f4007f327a3bd7b6f4103c5 for the cluster named myDemo:
6890
atlas backups snapshots describe 5f4007f327a3bd7b6f4103c5 --clusterName myDemo`,
6991
Annotations: map[string]string{

internal/cli/backup/snapshots/describe_test.go

Lines changed: 30 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

@@ -35,14 +36,40 @@ func TestDescribe_Run(t *testing.T) {
3536
store: mockStore,
3637
}
3738

39+
expectedError := &atlasv2.GenericOpenAPIError{}
40+
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})
41+
42+
mockStore.
43+
EXPECT().
44+
FlexClusterSnapshot(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.snapshot).
45+
Return(nil, expectedError).
46+
Times(1)
47+
3848
mockStore.
3949
EXPECT().
4050
Snapshot(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.snapshot).
4151
Return(&expected, nil).
4252
Times(1)
4353

44-
if err := describeOpts.Run(); err != nil {
45-
t.Fatalf("Run() unexpected error: %v", err)
46-
}
54+
require.NoError(t, describeOpts.Run())
4755
test.VerifyOutputTemplate(t, describeTemplate, expected)
4856
}
57+
58+
func TestDescribe_Run_FlexCluster(t *testing.T) {
59+
ctrl := gomock.NewController(t)
60+
mockStore := mocks.NewMockSnapshotsDescriber(ctrl)
61+
expected := &atlasv2.FlexBackupSnapshot20241113{}
62+
63+
describeOpts := &DescribeOpts{
64+
store: mockStore,
65+
}
66+
67+
mockStore.
68+
EXPECT().
69+
FlexClusterSnapshot(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.snapshot).
70+
Return(expected, nil).
71+
Times(1)
72+
73+
require.NoError(t, describeOpts.Run())
74+
test.VerifyOutputTemplate(t, describeTemplateFlex, expected)
75+
}

internal/cli/backup/snapshots/snapshots.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 = "snapshots"
2428
cmd := &cobra.Command{

0 commit comments

Comments
 (0)