Skip to content

Commit

Permalink
CLOUDP-73721: mongocli ops-manager admin backup blockstore(s) update (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo committed Oct 6, 2020
1 parent f377aa9 commit feff114
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Builder() *cobra.Command {
cmd.AddCommand(
ListBuilder(),
DescribeBuilder(),
CreateBuilder())
CreateBuilder(),
UpdateBuilder())
return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
3,
4,
[]string{},
)
}
71 changes: 15 additions & 56 deletions internal/cli/opsmanager/admin/backup/blockstore/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,20 @@ package blockstore

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"
"go.mongodb.org/ops-manager/opsmngr"
)

var createTemplate = "Blockstore configuration '{{.ID}}' created.\n"

type CreateOpts struct {
cli.OutputOpts
assignment bool
encryptedCredentials bool
ssl bool
id string
label []string
writeConcern string
uri string
loadFactor int64
maxCapacityGB int64
store store.BlockstoresCreator
backupstore.AdminOpts
store store.BlockstoresCreator
}

func (opts *CreateOpts) init() error {
Expand All @@ -47,48 +39,15 @@ func (opts *CreateOpts) init() error {
}

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

func (opts *CreateOpts) newBackupStore() *opsmngr.BackupStore {
backupStore := &opsmngr.BackupStore{
AdminBackupConfig: opsmngr.AdminBackupConfig{
ID: opts.id,
URI: opts.uri,
WriteConcern: opts.writeConcern,
Labels: opts.label,
},
}

if opts.ssl {
backupStore.SSL = &opts.ssl
}

if opts.encryptedCredentials {
backupStore.EncryptedCredentials = &opts.encryptedCredentials
}

if opts.assignment {
backupStore.AssignmentEnabled = &opts.assignment
}

if opts.maxCapacityGB != 0 {
backupStore.MaxCapacityGB = &opts.maxCapacityGB
}

if opts.loadFactor != 0 {
backupStore.LoadFactor = &opts.loadFactor
}

return backupStore
}

// mongocli ops-manager admin backup blockstore(s) create [--assignment][--encryptedCredentials][--id id][
// --label label][--loadFactor loadFactor][--maxCapacityGB maxCapacityGB][--uri uri][--ssl][--writeConcern writeConcern]
// mongocli ops-manager admin backup blockstore(s) create [--assignment][--encryptedCredentials][--id id]
// [--label label][--loadFactor loadFactor][--maxCapacityGB maxCapacityGB][--uri uri][--ssl][--writeConcern writeConcern]
func CreateBuilder() *cobra.Command {
opts := &CreateOpts{}
opts.Template = createTemplate
Expand All @@ -104,15 +63,15 @@ func CreateBuilder() *cobra.Command {
},
}

cmd.Flags().BoolVar(&opts.assignment, flag.Assignment, false, usage.Assignment)
cmd.Flags().BoolVar(&opts.encryptedCredentials, flag.EncryptedCredentials, false, usage.EncryptedCredentials)
cmd.Flags().StringVar(&opts.id, flag.ID, "", usage.BlockstoreID)
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().BoolVar(&opts.Assignment, flag.Assignment, false, usage.Assignment)
cmd.Flags().BoolVar(&opts.EncryptedCredentials, flag.EncryptedCredentials, false, usage.EncryptedCredentials)
cmd.Flags().StringVar(&opts.ID, flag.ID, "", usage.BlockstoreID)
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCreate_Run(t *testing.T) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ const (
list = "List backup blockstore configurations."
describe = "Get a backup blockstore configuration."
create = "Create a backup blockstore configuration."
update = "Update a backup blockstore configuration."
)
80 changes: 80 additions & 0 deletions internal/cli/opsmanager/admin/backup/blockstore/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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 blockstore

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 updateTemplate = "Blockstore configuration '{{.ID}}' updated.\n"

type UpdateOpts struct {
cli.OutputOpts
backupstore.AdminOpts
store store.BlockstoresUpdater
}

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

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

// mongocli ops-manager admin backup blockstore(s) update <ID> [--assignment][--encryptedCredentials]
// [--label label][--loadFactor loadFactor][--maxCapacityGB maxCapacityGB][--uri uri][--ssl][--writeConcern writeConcern]
func UpdateBuilder() *cobra.Command {
opts := &UpdateOpts{}
opts.Template = updateTemplate
cmd := &cobra.Command{
Use: "update <ID>",
Short: update,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.OutWriter = cmd.OutOrStdout()
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.ID = args[0]
return opts.Run()
},
}

cmd.Flags().BoolVar(&opts.Assignment, flag.Assignment, false, usage.Assignment)
cmd.Flags().BoolVar(&opts.EncryptedCredentials, flag.EncryptedCredentials, false, usage.EncryptedCredentials)
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)

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

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

expected := &opsmngr.BackupStore{}

opts := &UpdateOpts{
store: mockStore,
}

mockStore.
EXPECT().UpdateBlockstore(opts.NewBackupStore()).
Return(expected, nil).
Times(1)

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

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

import "go.mongodb.org/ops-manager/opsmngr"

type AdminOpts struct {
Assignment bool
EncryptedCredentials bool
SSL bool
ID string
Label []string
WriteConcern string
URI string
LoadFactor int64
MaxCapacityGB int64
}

func (opts *AdminOpts) NewBackupStore() *opsmngr.BackupStore {
backupStore := &opsmngr.BackupStore{
AdminBackupConfig: opsmngr.AdminBackupConfig{
ID: opts.ID,
URI: opts.URI,
WriteConcern: opts.WriteConcern,
Labels: opts.Label,
},
}

if opts.SSL {
backupStore.SSL = &opts.SSL
}

if opts.EncryptedCredentials {
backupStore.EncryptedCredentials = &opts.EncryptedCredentials
}

if opts.Assignment {
backupStore.AssignmentEnabled = &opts.Assignment
}

if opts.MaxCapacityGB != 0 {
backupStore.MaxCapacityGB = &opts.MaxCapacityGB
}

if opts.LoadFactor != 0 {
backupStore.LoadFactor = &opts.LoadFactor
}

return backupStore
}
40 changes: 39 additions & 1 deletion internal/mocks/mock_backup_blockstores.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit feff114

Please sign in to comment.