Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions internal/cli/backup/restores/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
atlasv2 "go.mongodb.org/atlas-sdk/v20241113002/admin"
)

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

var restoreDescribeFlexClusterTemplate = `ID SNAPSHOT CLUSTER TYPE EXPIRES AT URLs
{{.Id}} {{.SnapshotId}} {{.TargetDeploymentItemName}} {{.DeliveryType}} {{.ExpirationDate}} {{range $index, $element := valueOrEmptySlice .SnapshotUrl}}{{if $index}}; {{end}}{{$element}}{{end}}
`

func (opts *DescribeOpts) Run() error {
r, err := opts.store.RestoreJob(opts.ConfigProjectID(), opts.clusterName, opts.id)
r, err := opts.store.RestoreFlexClusterJob(opts.ConfigProjectID(), opts.clusterName, opts.id)
if err == nil {
opts.Template = restoreDescribeFlexClusterTemplate
return opts.Print(r)
}

apiError, ok := atlasv2.AsError(err)
if !ok {
return err
}

if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode {
return err
}

restoreJob, err := opts.store.RestoreJob(opts.ConfigProjectID(), opts.clusterName, opts.id)
if err != nil {
return err
}

return opts.Print(r)
return opts.Print(restoreJob)
}

// DescribeBuilder builds a cobra.Command that can run as:
// atlas backup(s) restore(s) job(s) describe <ID>.
func DescribeBuilder() *cobra.Command {
opts := new(DescribeOpts)
cmd := &cobra.Command{
Use: "describe <restoreJobId>",
Short: "Describe a cloud backup restore job.",
Long: fmt.Sprintf(usage.RequiredRole, "Project Owner"),
Args: require.ExactArgs(1),
Use: "describe <restoreJobId>",
Aliases: []string{"get"},
Short: "Describe a cloud backup restore job.",
Long: fmt.Sprintf(usage.RequiredRole, "Project Owner"),
Args: require.ExactArgs(1),
Annotations: map[string]string{
"restoreJobIdDesc": "ID of the restore job.",
},
Expand Down
38 changes: 35 additions & 3 deletions internal/cli/backup/restores/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/v20241113002/admin"
)

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

expectedError := &atlasv2.GenericOpenAPIError{}
expectedError.SetModel(atlasv2.ApiError{ErrorCode: cannotUseNotFlexWithFlexApisErrorCode})

mockStore.
EXPECT().
RestoreFlexClusterJob(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.id).
Return(nil, expectedError).
Times(1)

mockStore.
EXPECT().
RestoreJob(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.id).
Return(expected, nil).
Times(1)

if err := describeOpts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
require.NoError(t, describeOpts.Run())
test.VerifyOutputTemplate(t, restoreDescribeTemplate, expected)
}

func TestDescribeOpts_Run_FlexCluster(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockRestoreJobsDescriber(ctrl)

expected := &atlasv2.FlexBackupRestoreJob20241113{
Id: pointer.Get("1"),
}

describeOpts := &DescribeOpts{
store: mockStore,
clusterName: "Cluster0",
id: "1",
}

test.VerifyOutputTemplate(t, restoreDescribeTemplate, expected)
mockStore.
EXPECT().
RestoreFlexClusterJob(describeOpts.ProjectID, describeOpts.clusterName, describeOpts.id).
Return(nil, nil).
Times(1)

require.NoError(t, describeOpts.Run())

test.VerifyOutputTemplate(t, restoreDescribeFlexClusterTemplate, expected)
}
4 changes: 4 additions & 0 deletions internal/cli/backup/restores/restores.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"github.com/spf13/cobra"
)

const (
cannotUseNotFlexWithFlexApisErrorCode = "CANNOT_USE_NON_FLEX_CLUSTER_IN_FLEX_API"
)

func Builder() *cobra.Command {
const use = "restores"
cmd := &cobra.Command{
Expand Down
Loading