Skip to content
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

CLOUDP-69433: Milestone: Snapshot schedule #496

Merged
merged 10 commits into from
Oct 14, 2020
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/stretchr/testify v1.6.1
github.com/tangzero/inflector v1.0.0
go.mongodb.org/atlas v0.5.1-0.20201007214134-b315fe7503d2
go.mongodb.org/ops-manager v0.10.2-0.20201013103217-ed2281d05d80
go.mongodb.org/ops-manager v0.10.2-0.20201013162649-52c09bdb1772
golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
gopkg.in/ini.v1 v1.57.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.mongodb.org/atlas v0.5.1-0.20201007214134-b315fe7503d2 h1:b4Ng7d2sCSgYKwLMOetbwLcPE732SiBnJqH5rQrhZOs=
go.mongodb.org/atlas v0.5.1-0.20201007214134-b315fe7503d2/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
go.mongodb.org/ops-manager v0.10.2-0.20201013103217-ed2281d05d80 h1:mWq1v8Ht5YjMht15/BXd/8gaV1qCB4ptHzsuVHREr8M=
go.mongodb.org/ops-manager v0.10.2-0.20201013103217-ed2281d05d80/go.mod h1:elCFWG6rDmQNAL0tF0lQnN3XEwwNuPeeZD1VtIocqCk=
go.mongodb.org/ops-manager v0.10.2-0.20201013162649-52c09bdb1772 h1:k5SiXTv2eO+xOYKLleNX40WEneQXdWfKPrEFOqyaTuw=
go.mongodb.org/ops-manager v0.10.2-0.20201013162649-52c09bdb1772/go.mod h1:elCFWG6rDmQNAL0tF0lQnN3XEwwNuPeeZD1VtIocqCk=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/opsmanager/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package backup
import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/cli/opsmanager/backup/config"
"github.com/mongodb/mongocli/internal/cli/opsmanager/backup/snapshots"
"github.com/spf13/cobra"
)

Expand All @@ -29,7 +30,7 @@ func Builder() *cobra.Command {
}

cmd.AddCommand(
SnapshotsBuilder(),
snapshots.Builder(),
RestoresBuilder(),
CheckpointsBuilder(),
EnableBuilder(),
Expand Down
2 changes: 0 additions & 2 deletions internal/cli/opsmanager/backup/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ const (
Restores = "Manage restore jobs for your project."
ListRestores = "Lists restore jobs for a project and cluster."
StartRestore = "Start a restore job for a project and cluster."
Snapshots = "Manage backup snapshots for your project."
ListSnapshots = "List snapshots for a project and cluster."
)
19 changes: 19 additions & 0 deletions internal/cli/opsmanager/backup/snapshots/description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 snapshots

const (
shots = "Manage backup snapshots for your project."
list = "List snapshots for a project and cluster."
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package backup
package snapshots

import (
"github.com/mongodb/mongocli/internal/cli"
Expand All @@ -28,21 +28,21 @@ const snapshotsTemplate = `ID CREATED COMPLETE{{range .Results}}
{{.ID}} {{.Created.Date}} {{.Complete}}{{end}}
`

type SnapshotsListOpts struct {
type ListOpts struct {
cli.GlobalOpts
cli.OutputOpts
cli.ListOpts
clusterID string
store store.ContinuousSnapshotsLister
}

func (opts *SnapshotsListOpts) initStore() error {
func (opts *ListOpts) initStore() error {
var err error
opts.store, err = store.New(config.Default())
return err
}

func (opts *SnapshotsListOpts) Run() error {
func (opts *ListOpts) Run() error {
listOpts := opts.NewListOptions()
r, err := opts.store.ContinuousSnapshots(opts.ConfigProjectID(), opts.clusterID, listOpts)
if err != nil {
Expand All @@ -53,11 +53,11 @@ func (opts *SnapshotsListOpts) Run() error {
}

// mongocli atlas backups snapshots list <clusterId> [--projectId projectId] [--page N] [--limit N]
func SnapshotsListBuilder() *cobra.Command {
opts := new(SnapshotsListOpts)
func ListBuilder() *cobra.Command {
opts := new(ListOpts)
cmd := &cobra.Command{
Use: "list <clusterId>",
Short: ListSnapshots,
Short: list,
Aliases: []string{"ls"},
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@

// +build unit

package backup
package snapshots

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/mocks"
"go.mongodb.org/atlas/mongodbatlas"
)

func TestAtlasBackupsSnapshotsList_Run(t *testing.T) {
func TestList_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockContinuousSnapshotsLister(ctrl)
defer ctrl.Finish()

expected := &mongodbatlas.ContinuousSnapshots{}
clusterID := "5ec2ac941271767f21cbaefe"

listOpts := &SnapshotsListOpts{
listOpts := &ListOpts{
store: mockStore,
clusterID: clusterID,
}
Expand All @@ -48,3 +50,12 @@ func TestAtlasBackupsSnapshotsList_Run(t *testing.T) {
t.Fatalf("Run() unexpected error: %v", err)
}
}

func TestListBuilder(t *testing.T) {
cli.CmdValidator(
t,
ListBuilder(),
0,
[]string{flag.ProjectID, flag.Page, flag.Limit, flag.Output},
)
}
76 changes: 76 additions & 0 deletions internal/cli/opsmanager/backup/snapshots/schedule/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 schedule

import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

var describeTemplate = `PROJECT ID ID MONTHLY SNAPSHOT RETENTION MONTHS POINT IN TIME WINDOW HOURS
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
{{.GroupID}} {{.ClusterID}} {{.MonthlySnapshotRetentionMonths}} {{.PointInTimeWindowHours}}
`

type DescribeOpts struct {
cli.GlobalOpts
cli.OutputOpts
store store.SnapshotScheduleDescriber
clusterID string
}

func (opts *DescribeOpts) initStore() error {
var err error
opts.store, err = store.New(config.Default())
return err
}

func (opts *DescribeOpts) Run() error {
r, err := opts.store.GetSnapshotSchedule(opts.ConfigProjectID(), opts.clusterID)
if err != nil {
return err
}

return opts.Print(r)
}

// mongocli ops-manager backup snapshots schedule describe --clusterId clusterId --projectId projectId
func DescribeBuilder() *cobra.Command {
opts := &DescribeOpts{}
cmd := &cobra.Command{
Use: "describe",
Aliases: []string{"get"},
Short: describe,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(
opts.initStore,
opts.InitOutput(cmd.OutOrStdout(), describeTemplate),
)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().StringVar(&opts.clusterID, flag.ClusterID, "", usage.ClusterID)
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved

cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)

return cmd
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.

// +build unit

package schedule

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/mocks"
"go.mongodb.org/ops-manager/opsmngr"
)

func TestDescribeOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockSnapshotScheduleDescriber(ctrl)
defer ctrl.Finish()

expected := &opsmngr.SnapshotSchedule{}

opts := &DescribeOpts{
store: mockStore,
}

mockStore.
EXPECT().GetSnapshotSchedule(opts.ConfigProjectID(), opts.clusterID).
Return(expected, nil).
Times(1)

err := opts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}

func TestDescribeBuilder(t *testing.T) {
cli.CmdValidator(
t,
DescribeBuilder(),
0,
[]string{flag.Output, flag.ProjectID},
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 schedule

const (
shots = "Manage backup snapshot schedules for a cluster."
describe = "Describe a snapshot schedule for a cluster."
update = "Update a snapshot schedule for a cluster."
)
35 changes: 35 additions & 0 deletions internal/cli/opsmanager/backup/snapshots/schedule/schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 schedule

import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/spf13/cobra"
)

func Builder() *cobra.Command {
const use = "schedule"
cmd := &cobra.Command{
Use: use,
Aliases: cli.GenerateAliases(use),
Short: shots,
}

cmd.AddCommand(
DescribeBuilder(),
UpdateBuilder())

return cmd
}
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.
// +build unit

package schedule

import (
"testing"

"github.com/mongodb/mongocli/internal/cli"
)

func TestBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
2,
[]string{},
)
}
Loading