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

Sync commands #495

Merged
merged 4 commits into from
Oct 13, 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
2 changes: 2 additions & 0 deletions internal/cli/opsmanager/admin/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/mongodb/mongocli/internal/cli/opsmanager/admin/backup/filesystem"
"github.com/mongodb/mongocli/internal/cli/opsmanager/admin/backup/oplog"
"github.com/mongodb/mongocli/internal/cli/opsmanager/admin/backup/s3"
"github.com/mongodb/mongocli/internal/cli/opsmanager/admin/backup/sync"
"github.com/spf13/cobra"
)

Expand All @@ -36,6 +37,7 @@ func Builder() *cobra.Command {
filesystem.Builder(),
s3.Builder(),
oplog.Builder(),
sync.Builder(),
)

return cmd
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/opsmanager/admin/backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestBackupBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
4,
5,
[]string{},
)
}
82 changes: 82 additions & 0 deletions internal/cli/opsmanager/admin/backup/sync/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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 sync

import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/cli/opsmanager/admin/backupstore"
"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 createTemplate = "Sync configuration '{{.ID}}' created.\n"

type CreateOpts struct {
cli.OutputOpts
backupstore.AdminOpts
store store.SyncsCreator
}

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

func (opts *CreateOpts) Run() error {
r, err := opts.store.CreateSync(opts.NewBackupStore())
if err != nil {
return err
}
return opts.Print(r)
}

// mongocli ops-manager admin backup sync create [--assignment][--encryptedCredentials][--name name]
// [--label label][--loadFactor loadFactor][--maxCapacityGB maxCapacityGB][--uri uri][--ssl][--writeConcern writeConcern]
func CreateBuilder() *cobra.Command {
opts := &CreateOpts{}
opts.Template = createTemplate
cmd := &cobra.Command{
Use: "create",
Short: create,
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.OutWriter = cmd.OutOrStdout()
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().BoolVar(&opts.Assignment, flag.Assignment, false, usage.SyncAssignment)
cmd.Flags().BoolVar(&opts.EncryptedCredentials, flag.EncryptedCredentials, false, usage.EncryptedCredentials)
cmd.Flags().StringVar(&opts.ID, flag.Name, "", usage.SyncName)
cmd.Flags().StringSliceVar(&opts.Label, flag.Label, []string{}, usage.Label)
cmd.Flags().Int64Var(&opts.LoadFactor, flag.LoadFactor, 0, usage.LoadFactor)
cmd.Flags().Int64Var(&opts.MaxCapacityGB, flag.MaxCapacityGB, 0, usage.MaxCapacityGB)
cmd.Flags().StringVar(&opts.URI, flag.URI, "", usage.BlockstoreURI)
cmd.Flags().BoolVar(&opts.SSL, flag.SSL, false, usage.BlockstoreSSL)
cmd.Flags().StringVar(&opts.WriteConcern, flag.WriteConcern, "", usage.WriteConcern)

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

_ = cmd.MarkFlagRequired(flag.Name)
_ = cmd.MarkFlagRequired(flag.URI)

return cmd
}
58 changes: 58 additions & 0 deletions internal/cli/opsmanager/admin/backup/sync/create_test.go
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 sync

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 TestCreate_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockSyncsCreator(ctrl)
defer ctrl.Finish()

expected := &opsmngr.BackupStore{}

createOpts := &CreateOpts{
store: mockStore,
}

mockStore.
EXPECT().CreateSync(createOpts.NewBackupStore()).
Return(expected, nil).
Times(1)

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

func TestCreateBuilder(t *testing.T) {
cli.CmdValidator(
t,
CreateBuilder(),
0,
[]string{flag.Output, flag.Name, flag.SSL, flag.EncryptedCredentials, flag.LoadFactor,
flag.MaxCapacityGB, flag.Assignment, flag.Label, flag.URI, flag.WriteConcern},
)
}
66 changes: 66 additions & 0 deletions internal/cli/opsmanager/admin/backup/sync/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 sync

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"
)

type DeleteOpts struct {
*cli.DeleteOpts
store store.SyncsDeleter
}

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

func (opts *DeleteOpts) Run() error {
return opts.Delete(opts.store.DeleteSync)
}

// mongocli ops-manager admin backup sync delete <ID> [--force]
func DeleteBuilder() *cobra.Command {
opts := &DeleteOpts{
DeleteOpts: cli.NewDeleteOpts("Sync configuration '%s' deleted\n", "Sync configuration not deleted"),
}
cmd := &cobra.Command{
Use: "delete <ID>",
Aliases: []string{"rm"},
Short: delete,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := opts.init(); err != nil {
return err
}
opts.Entry = args[0]
return opts.Prompt()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)

return cmd
}
59 changes: 59 additions & 0 deletions internal/cli/opsmanager/admin/backup/sync/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 sync

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"
)

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

mockStore.
EXPECT().
DeleteSync(gomock.Eq("5a0a1e7e0f2912c554080adc")).
Return(nil).
Times(1)

opts := &DeleteOpts{
store: mockStore,
DeleteOpts: &cli.DeleteOpts{
Entry: "5a0a1e7e0f2912c554080adc",
Confirm: true,
},
}
err := opts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}

func TestDeleteBuilder(t *testing.T) {
cli.CmdValidator(
t,
DeleteBuilder(),
0,
[]string{flag.Force},
)
}
73 changes: 73 additions & 0 deletions internal/cli/opsmanager/admin/backup/sync/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 sync

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 = `NAME URI SSL LOAD FACTOR
{{.ID}} {{.URI}} {{.SSL}} {{.LoadFactor}}
`

type DescribeOpts struct {
cli.OutputOpts
store store.SyncsDescriber
syncID 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.GetSync(opts.syncID)
if err != nil {
return err
}

return opts.Print(r)
}

// mongocli ops-manager admin backup sync describe <name>
func DescribeBuilder() *cobra.Command {
opts := &DescribeOpts{}
opts.Template = describeTemplate
cmd := &cobra.Command{
Use: "describe <name>",
Aliases: []string{"get"},
Short: describe,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.OutWriter = cmd.OutOrStdout()
return opts.initStore()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.syncID = args[0]
return opts.Run()
},
}

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

return cmd
}
Loading