Skip to content

CLOUDP-57862: Backup: List available snapshots, Atlas #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2020
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SOURCE_FILES?=./...
BINARY_NAME=mongocli

DESTINATION=./bin/${BINARY_NAME}
GOLANGCI_VERSION=v1.23.6
GOLANGCI_VERSION=v1.23.8
COVERAGE=coverage.out

VERSION=$(shell git describe --always --tags)
Expand Down Expand Up @@ -58,6 +58,7 @@ gen-mocks: ## Generate mocks
mockgen -source=internal/store/projects.go -destination=internal/mocks/mock_projects.go -package=mocks
mockgen -source=internal/store/organizations.go -destination=internal/mocks/mock_organizations.go -package=mocks
mockgen -source=internal/store/owners.go -destination=internal/mocks/mock_owners.go -package=mocks
mockgen -source=internal/store/continuous_snapshots.go -destination=internal/mocks/mock_continuous_snapshots.go -package=mocks

.PHONY: build
build: ## Generate a binary in ./bin
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require (
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200309214044-0a9bf30571f7
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200310093302-8d5d810fb4a0
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/spf13/afero v1.2.2
github.com/spf13/cast v1.3.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200309214044-0a9bf30571f7 h1:ozMruEaA/TRIRBGAy0tfi8pneCsYvCM0Djdu/wENnQs=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200309214044-0a9bf30571f7/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200310093302-8d5d810fb4a0 h1:3f5ftr/+/Zb7tDD6oArYoF1A1/kDvVhjEKSstXhESxI=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200310093302-8d5d810fb4a0/go.mod h1:cR0vrJsJ1ZssXzfKNk9KCduPJKLHkmClDeTmkKXSJA0=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6 h1:ukGCT8EiyLDm+dOJOgRqySvDjZJly5tUPUDZmQt+f0Y=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0 h1:yOYbV6B8nH8jBoSKjITRE7TkU0xeAst7mAIt+JPFUlE=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0/go.mod h1:G9iLP4YeC2TRpxjZRddP6wKjz2JbpAtX+MlKriO7TOU=
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func AtlasBuilder() *cobra.Command {
cmd.AddCommand(AtlasDBUsersBuilder())
cmd.AddCommand(AtlasWhitelistBuilder())
cmd.AddCommand(AtlasAlertsBuilder())
cmd.AddCommand(AtlasBackupsBuilder())

return cmd
}
31 changes: 31 additions & 0 deletions internal/cli/atlas_backups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"github.com/spf13/cobra"
)

func AtlasBackupsBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "backups",
Aliases: []string{"backup"},
Short: "Manage continuous backups for your project.",
}

cmd.AddCommand(AtlasBackupsSnapshotsBuilder())

return cmd
}
31 changes: 31 additions & 0 deletions internal/cli/atlas_backups_snapshots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"github.com/spf13/cobra"
)

func AtlasBackupsSnapshotsBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "snapshots",
Aliases: []string{"snapshot"},
Short: "Manage continuous snapshots for your project.",
}

cmd.AddCommand(AtlasBackupsSnapshotsListBuilder())

return cmd
}
88 changes: 88 additions & 0 deletions internal/cli/atlas_backups_snapshots_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/json"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type atlasBackupsSnapshotsListOpts struct {
*globalOpts
clusterName string
pageNum int
itemsPerPage int
store store.SnapshotsLister
}

func (opts *atlasBackupsSnapshotsListOpts) init() error {
if opts.ProjectID() == "" {
return errMissingProjectID
}

var err error
opts.store, err = store.New()
return err
}

func (opts *atlasBackupsSnapshotsListOpts) Run() error {
listOpts := opts.newListOptions()
result, err := opts.store.ContinuousSnapshots(opts.ProjectID(), opts.clusterName, listOpts)

if err != nil {
return err
}

return json.PrettyPrint(result)
}

func (opts *atlasBackupsSnapshotsListOpts) newListOptions() *atlas.ListOptions {
return &atlas.ListOptions{
PageNum: opts.pageNum,
ItemsPerPage: opts.itemsPerPage,
}
}

// mongocli atlas cluster(s) list --projectId projectId [--page N] [--limit N]
func AtlasBackupsSnapshotsListBuilder() *cobra.Command {
opts := &atlasBackupsSnapshotsListOpts{
globalOpts: newGlobalOpts(),
}
cmd := &cobra.Command{
Use: "list",
Short: "List continuous snapshots for a project.",
Aliases: []string{"ls"},
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.clusterName = args[0]

return opts.Run()
},
}

cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)

cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

return cmd
}
49 changes: 49 additions & 0 deletions internal/cli/atlas_backups_snapshots_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/fixtures"
"github.com/mongodb/mongocli/internal/mocks"
)

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

defer ctrl.Finish()

expected := fixtures.ContinuousSnapshots()

listOpts := &atlasBackupsSnapshotsListOpts{
globalOpts: newGlobalOpts(),
store: mockStore,
clusterName: "Cluster0",
}

mockStore.
EXPECT().
ContinuousSnapshots(listOpts.projectID, "Cluster0", listOpts.newListOptions()).
Return(expected, nil).
Times(1)

err := listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
1 change: 1 addition & 0 deletions internal/cli/cloud_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func CloudManagerBuilder() *cobra.Command {

cmd.AddCommand(CloudManagerClustersBuilder())
cmd.AddCommand(AtlasAlertsBuilder())
cmd.AddCommand(AtlasBackupsBuilder())

return cmd
}
1 change: 1 addition & 0 deletions internal/cli/ops_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func OpsManagerBuilder() *cobra.Command {
cmd.AddCommand(CloudManagerClustersBuilder())
cmd.AddCommand(AtlasAlertsBuilder())
cmd.AddCommand(OpsManagerOwnerBuilder())
cmd.AddCommand(AtlasBackupsBuilder())

return cmd
}
54 changes: 54 additions & 0 deletions internal/fixtures/continuous_snapshots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package fixtures

import atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"

func ContinuousSnapshots() *atlas.ContinuousSnapshots {
doNotDelete := false

return &atlas.ContinuousSnapshots{
Links: []*atlas.Link{
{
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/6c7498dg87d9e6526801572b/clusters/Cluster0/snapshots?pageNum=1&itemsPerPage=100",
Rel: "self",
},
},
Results: []*atlas.ContinuousSnapshot{
{
ClusterID: "7c2487d833e9e75286093696",
Complete: true,
Created: &atlas.SnapshotTimestamp{
Date: "2017-12-26T16:32:16Z",
Increment: 1,
},
DoNotDelete: &doNotDelete,
Expires: "2018-12-25T16:32:16Z",
GroupID: "6c7498dg87d9e6526801572b",
ID: "5a4279d4fcc178500596745a",
LastOplogAppliedTimestamp: &atlas.SnapshotTimestamp{
Date: "2017-12-26T16:32:15Z",
Increment: 1,
},
Links: []*atlas.Link{
{
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/6c7498dg87d9e6526801572b/clusters/Cluster0/snapshots/5a4279d4fcc178500596745a",
Rel: "self",
},
},
Parts: []*atlas.Part{
{
ClusterID: "7c2487d833e9e75286093696",
CompressionSetting: "GZIP",
DataSizeBytes: 4502,
EncryptionEnabled: false,
FileSizeBytes: 324760,
MongodVersion: "3.6.10",
ReplicaSetName: "Cluster0-shard-0",
StorageSizeBytes: 53248,
TypeName: "REPLICA_SET",
},
},
},
},
TotalCount: 1,
}
}
Loading