Skip to content

Commit eaa2f82

Browse files
authored
CLOUDP-57862: Backup: List available snapshots, Atlas (#40)
1 parent 2184c00 commit eaa2f82

13 files changed

+469
-7
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SOURCE_FILES?=./...
44
BINARY_NAME=mongocli
55

66
DESTINATION=./bin/${BINARY_NAME}
7-
GOLANGCI_VERSION=v1.23.6
7+
GOLANGCI_VERSION=v1.23.8
88
COVERAGE=coverage.out
99

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

6263
.PHONY: build
6364
build: ## Generate a binary in ./bin

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ require (
1111
github.com/mattn/go-colorable v0.1.4 // indirect
1212
github.com/mattn/go-isatty v0.0.12 // indirect
1313
github.com/mitchellh/go-homedir v1.1.0
14-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200309214044-0a9bf30571f7
15-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200310093302-8d5d810fb4a0
14+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6
15+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0
1616
github.com/pelletier/go-toml v1.6.0 // indirect
1717
github.com/spf13/afero v1.2.2
1818
github.com/spf13/cast v1.3.1 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
100100
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
101101
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
102102
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
103-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200309214044-0a9bf30571f7 h1:ozMruEaA/TRIRBGAy0tfi8pneCsYvCM0Djdu/wENnQs=
104-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200309214044-0a9bf30571f7/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
105-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200310093302-8d5d810fb4a0 h1:3f5ftr/+/Zb7tDD6oArYoF1A1/kDvVhjEKSstXhESxI=
106-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200310093302-8d5d810fb4a0/go.mod h1:cR0vrJsJ1ZssXzfKNk9KCduPJKLHkmClDeTmkKXSJA0=
103+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6 h1:ukGCT8EiyLDm+dOJOgRqySvDjZJly5tUPUDZmQt+f0Y=
104+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200312093748-8894c1fe3bb6/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
105+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0 h1:yOYbV6B8nH8jBoSKjITRE7TkU0xeAst7mAIt+JPFUlE=
106+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200312102454-e5173cc1a3b0/go.mod h1:G9iLP4YeC2TRpxjZRddP6wKjz2JbpAtX+MlKriO7TOU=
107107
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
108108
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
109109
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

internal/cli/atlas.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func AtlasBuilder() *cobra.Command {
2727
cmd.AddCommand(AtlasDBUsersBuilder())
2828
cmd.AddCommand(AtlasWhitelistBuilder())
2929
cmd.AddCommand(AtlasAlertsBuilder())
30+
cmd.AddCommand(AtlasBackupsBuilder())
3031

3132
return cmd
3233
}

internal/cli/atlas_backups.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
)
20+
21+
func AtlasBackupsBuilder() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "backups",
24+
Aliases: []string{"backup"},
25+
Short: "Manage continuous backups for your project.",
26+
}
27+
28+
cmd.AddCommand(AtlasBackupsSnapshotsBuilder())
29+
30+
return cmd
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
)
20+
21+
func AtlasBackupsSnapshotsBuilder() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "snapshots",
24+
Aliases: []string{"snapshot"},
25+
Short: "Manage continuous snapshots for your project.",
26+
}
27+
28+
cmd.AddCommand(AtlasBackupsSnapshotsListBuilder())
29+
30+
return cmd
31+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
19+
"github.com/mongodb/mongocli/internal/flags"
20+
"github.com/mongodb/mongocli/internal/json"
21+
"github.com/mongodb/mongocli/internal/store"
22+
"github.com/mongodb/mongocli/internal/usage"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
type atlasBackupsSnapshotsListOpts struct {
27+
*globalOpts
28+
clusterName string
29+
pageNum int
30+
itemsPerPage int
31+
store store.SnapshotsLister
32+
}
33+
34+
func (opts *atlasBackupsSnapshotsListOpts) init() error {
35+
if opts.ProjectID() == "" {
36+
return errMissingProjectID
37+
}
38+
39+
var err error
40+
opts.store, err = store.New()
41+
return err
42+
}
43+
44+
func (opts *atlasBackupsSnapshotsListOpts) Run() error {
45+
listOpts := opts.newListOptions()
46+
result, err := opts.store.ContinuousSnapshots(opts.ProjectID(), opts.clusterName, listOpts)
47+
48+
if err != nil {
49+
return err
50+
}
51+
52+
return json.PrettyPrint(result)
53+
}
54+
55+
func (opts *atlasBackupsSnapshotsListOpts) newListOptions() *atlas.ListOptions {
56+
return &atlas.ListOptions{
57+
PageNum: opts.pageNum,
58+
ItemsPerPage: opts.itemsPerPage,
59+
}
60+
}
61+
62+
// mongocli atlas cluster(s) list --projectId projectId [--page N] [--limit N]
63+
func AtlasBackupsSnapshotsListBuilder() *cobra.Command {
64+
opts := &atlasBackupsSnapshotsListOpts{
65+
globalOpts: newGlobalOpts(),
66+
}
67+
cmd := &cobra.Command{
68+
Use: "list",
69+
Short: "List continuous snapshots for a project.",
70+
Aliases: []string{"ls"},
71+
Args: cobra.ExactArgs(1),
72+
PreRunE: func(cmd *cobra.Command, args []string) error {
73+
return opts.init()
74+
},
75+
RunE: func(cmd *cobra.Command, args []string) error {
76+
opts.clusterName = args[0]
77+
78+
return opts.Run()
79+
},
80+
}
81+
82+
cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
83+
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
84+
85+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
86+
87+
return cmd
88+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
"testing"
19+
20+
"github.com/golang/mock/gomock"
21+
"github.com/mongodb/mongocli/internal/fixtures"
22+
"github.com/mongodb/mongocli/internal/mocks"
23+
)
24+
25+
func TestAtlasBackupsSnapshotsList_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockSnapshotsLister(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
expected := fixtures.ContinuousSnapshots()
32+
33+
listOpts := &atlasBackupsSnapshotsListOpts{
34+
globalOpts: newGlobalOpts(),
35+
store: mockStore,
36+
clusterName: "Cluster0",
37+
}
38+
39+
mockStore.
40+
EXPECT().
41+
ContinuousSnapshots(listOpts.projectID, "Cluster0", listOpts.newListOptions()).
42+
Return(expected, nil).
43+
Times(1)
44+
45+
err := listOpts.Run()
46+
if err != nil {
47+
t.Fatalf("Run() unexpected error: %v", err)
48+
}
49+
}

internal/cli/cloud_manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func CloudManagerBuilder() *cobra.Command {
2727

2828
cmd.AddCommand(CloudManagerClustersBuilder())
2929
cmd.AddCommand(AtlasAlertsBuilder())
30+
cmd.AddCommand(AtlasBackupsBuilder())
3031

3132
return cmd
3233
}

internal/cli/ops_manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func OpsManagerBuilder() *cobra.Command {
2828
cmd.AddCommand(CloudManagerClustersBuilder())
2929
cmd.AddCommand(AtlasAlertsBuilder())
3030
cmd.AddCommand(OpsManagerOwnerBuilder())
31+
cmd.AddCommand(AtlasBackupsBuilder())
3132

3233
return cmd
3334
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package fixtures
2+
3+
import atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
4+
5+
func ContinuousSnapshots() *atlas.ContinuousSnapshots {
6+
doNotDelete := false
7+
8+
return &atlas.ContinuousSnapshots{
9+
Links: []*atlas.Link{
10+
{
11+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/6c7498dg87d9e6526801572b/clusters/Cluster0/snapshots?pageNum=1&itemsPerPage=100",
12+
Rel: "self",
13+
},
14+
},
15+
Results: []*atlas.ContinuousSnapshot{
16+
{
17+
ClusterID: "7c2487d833e9e75286093696",
18+
Complete: true,
19+
Created: &atlas.SnapshotTimestamp{
20+
Date: "2017-12-26T16:32:16Z",
21+
Increment: 1,
22+
},
23+
DoNotDelete: &doNotDelete,
24+
Expires: "2018-12-25T16:32:16Z",
25+
GroupID: "6c7498dg87d9e6526801572b",
26+
ID: "5a4279d4fcc178500596745a",
27+
LastOplogAppliedTimestamp: &atlas.SnapshotTimestamp{
28+
Date: "2017-12-26T16:32:15Z",
29+
Increment: 1,
30+
},
31+
Links: []*atlas.Link{
32+
{
33+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/groups/6c7498dg87d9e6526801572b/clusters/Cluster0/snapshots/5a4279d4fcc178500596745a",
34+
Rel: "self",
35+
},
36+
},
37+
Parts: []*atlas.Part{
38+
{
39+
ClusterID: "7c2487d833e9e75286093696",
40+
CompressionSetting: "GZIP",
41+
DataSizeBytes: 4502,
42+
EncryptionEnabled: false,
43+
FileSizeBytes: 324760,
44+
MongodVersion: "3.6.10",
45+
ReplicaSetName: "Cluster0-shard-0",
46+
StorageSizeBytes: 53248,
47+
TypeName: "REPLICA_SET",
48+
},
49+
},
50+
},
51+
},
52+
TotalCount: 1,
53+
}
54+
}

0 commit comments

Comments
 (0)