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
35 changes: 33 additions & 2 deletions internal/cli/backup/restores/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 ListOpts struct {
Expand All @@ -45,17 +46,47 @@ func (opts *ListOpts) initStore(ctx context.Context) func() error {
var restoreListTemplate = `ID SNAPSHOT CLUSTER TYPE EXPIRES AT{{range valueOrEmptySlice .Results}}
{{.Id}} {{.SnapshotId}} {{.TargetClusterName}} {{.DeliveryType}} {{.ExpiresAt}}{{end}}
`
var restoreListFlexClusterTemplate = `ID SNAPSHOT CLUSTER TYPE EXPIRES AT{{range valueOrEmptySlice .Results}}
{{.Id}} {{.SnapshotId}} {{.TargetDeploymentItemName}} {{.DeliveryType}} {{.ExpirationDate}}{{end}}
`

func (opts *ListOpts) Run() error {
r, err := opts.store.RestoreFlexClusterJobs(opts.newListFlexBackupRestoreJobsAPIParams())
if err == nil {
opts.Template = restoreListFlexClusterTemplate
return opts.Print(r)
}

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

if apiError.ErrorCode != cannotUseNotFlexWithFlexApisErrorCode {
return err
}

listOpts := opts.NewListOptions()
r, err := opts.store.RestoreJobs(opts.ConfigProjectID(), opts.clusterName, listOpts)
restoreJobs, err := opts.store.RestoreJobs(opts.ConfigProjectID(), opts.clusterName, listOpts)
if err != nil {
return err
}

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

func (opts *ListOpts) newListFlexBackupRestoreJobsAPIParams() *atlasv2.ListFlexBackupRestoreJobsApiParams {
includeCount := !opts.OmitCount
return &atlasv2.ListFlexBackupRestoreJobsApiParams{
GroupId: opts.ConfigProjectID(),
Name: opts.clusterName,
IncludeCount: &includeCount,
ItemsPerPage: &opts.ItemsPerPage,
PageNum: &opts.PageNum,
}
}

// ListBuilder builds a cobra.Command that can run as:
// atlas backup(s) restore(s) job(s) list <clusterName> [--page N] [--limit N].
func ListBuilder() *cobra.Command {
opts := new(ListOpts)
Expand Down
34 changes: 31 additions & 3 deletions internal/cli/backup/restores/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/v20241113002/admin"
)

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

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

mockStore.
EXPECT().
RestoreFlexClusterJobs(listOpts.newListFlexBackupRestoreJobsAPIParams()).
Return(nil, expectedError).
Times(1)

mockStore.
EXPECT().
RestoreJobs(listOpts.ProjectID, "Cluster0", listOpts.NewListOptions()).
Return(expected, nil).
Times(1)

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

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

expected := &atlasv2.PaginatedApiAtlasFlexBackupRestoreJob20241113{}

listOpts := &ListOpts{
store: mockStore,
clusterName: "Cluster0",
}

test.VerifyOutputTemplate(t, restoreListTemplate, expected)
mockStore.
EXPECT().
RestoreFlexClusterJobs(listOpts.newListFlexBackupRestoreJobsAPIParams()).
Return(nil, nil).
Times(1)

require.NoError(t, listOpts.Run())
test.VerifyOutputTemplate(t, restoreListFlexClusterTemplate, 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