Skip to content

Commit bc55ca2

Browse files
CLOUDP-57863: List available checkpoints, atlas/cm/om (#48)
1 parent fa256c3 commit bc55ca2

10 files changed

+332
-7
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ gen-mocks: ## Generate mocks
6161
mockgen -source=internal/store/continuous_snapshots.go -destination=internal/mocks/mock_continuous_snapshots.go -package=mocks
6262
mockgen -source=internal/store/continuous_jobs.go -destination=internal/mocks/mock_continuous_jobs.go -package=mocks
6363
mockgen -source=internal/store/agents.go -destination=internal/mocks/mock_agents.go -package=mocks
64+
mockgen -source=internal/store/checkpoints.go -destination=internal/mocks/mock_checkpoints.go -package=mocks
6465

6566
.PHONY: build
6667
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.20200318094203-b65adad6f142
15-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200318095624-136324ab144a
14+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318163834-8b6a5ea6eb67
15+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200319100114-648fc659365b
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +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.20200318090919-47fd90f26eb1/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
104-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318094203-b65adad6f142 h1:A8gFrXWfOPWgDkv4/J7uxVY6ER/NFETb3TZMmX83vdg=
105-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318094203-b65adad6f142/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
106-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200318095624-136324ab144a h1:AKMyjBEQgzgI8u9sszRW0qt04ajrhLCC6YRJFstT41s=
107-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200318095624-136324ab144a/go.mod h1:KnlAMq1ZY06LOYW1QUxCWiDLrj71c52K2Z5zGlxeS4k=
103+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318163834-8b6a5ea6eb67 h1:YKz/n9K99mUOxs/pX+vpzNvbSFr56vObu2+O33upiFo=
104+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318163834-8b6a5ea6eb67/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
105+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200319100114-648fc659365b h1:ROpURq2j0CnObttgt3cm+2veOzoNwUPWWK/hoHZGBQA=
106+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200319100114-648fc659365b/go.mod h1:FhMID1fXaHZpvHPfUhcDL3pfc5JrBB+aD42HLgFoO+8=
108107
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
109108
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
110109
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

internal/cli/atlas_backups.go

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

2828
cmd.AddCommand(AtlasBackupsSnapshotsBuilder())
2929
cmd.AddCommand(AtlasBackupsRestoresBuilder())
30+
cmd.AddCommand(AtlasBackupsCheckpointsBuilder())
3031

3132
return cmd
3233
}
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 AtlasBackupsCheckpointsBuilder() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "checkpoints",
24+
Aliases: []string{"checkpoint"},
25+
Short: "Manage backup checkpoints for your project.",
26+
}
27+
28+
cmd.AddCommand(AtlasBackupsCheckpointsListBuilder())
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 atlasBackupsCheckpointsListOpts struct {
27+
*globalOpts
28+
clusterName string
29+
pageNum int
30+
itemsPerPage int
31+
store store.CheckpointsLister
32+
}
33+
34+
func (opts *atlasBackupsCheckpointsListOpts) 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 *atlasBackupsCheckpointsListOpts) Run() error {
45+
listOpts := opts.newListOptions()
46+
47+
result, err := opts.store.List(opts.ProjectID(), opts.clusterName, listOpts)
48+
49+
if err != nil {
50+
return err
51+
}
52+
53+
return json.PrettyPrint(result)
54+
}
55+
56+
func (opts *atlasBackupsCheckpointsListOpts) newListOptions() *atlas.ListOptions {
57+
return &atlas.ListOptions{
58+
PageNum: opts.pageNum,
59+
ItemsPerPage: opts.itemsPerPage,
60+
}
61+
}
62+
63+
// mongocli atlas backup(s) checkpoint(s) list clusterName [--projectId projectId]
64+
func AtlasBackupsCheckpointsListBuilder() *cobra.Command {
65+
opts := &atlasBackupsCheckpointsListOpts{
66+
globalOpts: newGlobalOpts(),
67+
}
68+
cmd := &cobra.Command{
69+
Use: "list",
70+
Aliases: []string{"ls"},
71+
Short: "List continuous backup checkpoints.",
72+
Args: cobra.ExactArgs(1),
73+
PreRunE: func(cmd *cobra.Command, args []string) error {
74+
return opts.init()
75+
},
76+
RunE: func(cmd *cobra.Command, args []string) error {
77+
opts.clusterName = args[0]
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 TestAtlasBackupsCheckpointsList_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockCheckpointsLister(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
expected := fixtures.Checkpoints()
32+
33+
listOpts := &atlasBackupsCheckpointsListOpts{
34+
globalOpts: newGlobalOpts(),
35+
store: mockStore,
36+
clusterName: "Cluster0",
37+
}
38+
39+
mockStore.
40+
EXPECT().
41+
List(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/fixtures/checkpoints.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package fixtures
2+
3+
import atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
4+
5+
func Checkpoint() *atlas.Checkpoint {
6+
return &atlas.Checkpoint{
7+
ClusterID: "6b8cd61180eef547110159d9",
8+
Completed: "2018-02-08T23:20:25Z",
9+
GroupID: "6b8cd3c380eef5349ef77gf7",
10+
ID: "5a7cdb3980eef53de5bffdcf",
11+
Links: []*atlas.Link{
12+
{
13+
Rel: "self",
14+
Href: "https://cloud.mongodb.com/api/public/v1.0/groups/6b8cd3c380eef5349ef77gf7/clusters/Cluster0/checkpoints",
15+
},
16+
},
17+
Parts: []*atlas.Part{
18+
19+
{
20+
ReplicaSetName: "Cluster0-shard-1",
21+
TypeName: "REPLICA_SET",
22+
CheckpointPart: atlas.CheckpointPart{
23+
ShardName: "Cluster0-shard-1",
24+
TokenDiscovered: true,
25+
TokenTimestamp: atlas.SnapshotTimestamp{
26+
Date: "2018-02-08T23:20:25Z",
27+
Increment: 1,
28+
},
29+
},
30+
},
31+
{
32+
ReplicaSetName: "Cluster0-shard-0",
33+
TypeName: "REPLICA_SET",
34+
CheckpointPart: atlas.CheckpointPart{
35+
ShardName: "Cluster0-shard-0",
36+
TokenDiscovered: true,
37+
TokenTimestamp: atlas.SnapshotTimestamp{
38+
Date: "2018-02-08T23:20:25Z",
39+
Increment: 1,
40+
}},
41+
},
42+
{
43+
ReplicaSetName: "Cluster0-config-0",
44+
TypeName: "CONFIG_SERVER_REPLICA_SET",
45+
CheckpointPart: atlas.CheckpointPart{
46+
TokenDiscovered: true,
47+
TokenTimestamp: atlas.SnapshotTimestamp{
48+
Date: "2018-02-08T23:20:25Z",
49+
Increment: 2,
50+
}},
51+
},
52+
},
53+
Restorable: true,
54+
Started: "2018-02-08T23:20:25Z",
55+
Timestamp: "2018-02-08T23:19:37Z",
56+
}
57+
}
58+
59+
func Checkpoints() *atlas.Checkpoints {
60+
return &atlas.Checkpoints{
61+
Results: []*atlas.Checkpoint{Checkpoint()},
62+
Links: nil,
63+
TotalCount: 1,
64+
}
65+
}

internal/mocks/mock_checkpoints.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/checkpoints.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 store
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
22+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
23+
"github.com/mongodb/mongocli/internal/config"
24+
)
25+
26+
type CheckpointsLister interface {
27+
List(string, string, *atlas.ListOptions) (*atlas.Checkpoints, error)
28+
}
29+
30+
// List encapsulate the logic to manage different cloud providers
31+
func (s *Store) List(projectID, clusterID string, opts *atlas.ListOptions) (*atlas.Checkpoints, error) {
32+
switch s.service {
33+
case config.CloudService:
34+
result, _, err := s.client.(*atlas.Client).Checkpoints.List(context.Background(), projectID, clusterID, opts)
35+
return result, err
36+
case config.CloudManagerService, config.OpsManagerService:
37+
result, _, err := s.client.(*om.Client).Checkpoints.List(context.Background(), projectID, clusterID, opts)
38+
return result, err
39+
default:
40+
return nil, fmt.Errorf("unsupported service: %s", s.service)
41+
}
42+
}

0 commit comments

Comments
 (0)