Skip to content

Commit 33be241

Browse files
CLOUDP-73727: mongocli ops-manager admin backup fileSystem(s) delete (#482)
1 parent e994a54 commit 33be241

File tree

7 files changed

+182
-3
lines changed

7 files changed

+182
-3
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 filesystem
16+
17+
import (
18+
"github.com/mongodb/mongocli/internal/cli"
19+
"github.com/mongodb/mongocli/internal/config"
20+
"github.com/mongodb/mongocli/internal/flag"
21+
"github.com/mongodb/mongocli/internal/store"
22+
"github.com/mongodb/mongocli/internal/usage"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
type DeleteOpts struct {
27+
*cli.DeleteOpts
28+
store store.FileSystemsDeleter
29+
}
30+
31+
func (opts *DeleteOpts) init() error {
32+
var err error
33+
opts.store, err = store.New(config.Default())
34+
return err
35+
}
36+
37+
func (opts *DeleteOpts) Run() error {
38+
return opts.Delete(opts.store.DeleteFileSystem)
39+
}
40+
41+
// // mongocli ops-manager admin backup fileSystem(s) delete <ID> [--force]
42+
func DeleteBuilder() *cobra.Command {
43+
opts := &DeleteOpts{
44+
DeleteOpts: cli.NewDeleteOpts("File system configuration '%s' deleted\n", "File system configuration not deleted"),
45+
}
46+
cmd := &cobra.Command{
47+
Use: "delete <ID>",
48+
Aliases: []string{"rm"},
49+
Short: delete,
50+
Args: cobra.ExactArgs(1),
51+
PreRunE: func(cmd *cobra.Command, args []string) error {
52+
if err := opts.init(); err != nil {
53+
return err
54+
}
55+
opts.Entry = args[0]
56+
return opts.Prompt()
57+
},
58+
RunE: func(cmd *cobra.Command, args []string) error {
59+
return opts.Run()
60+
},
61+
}
62+
63+
cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)
64+
65+
return cmd
66+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// +build unit
16+
17+
package filesystem
18+
19+
import (
20+
"testing"
21+
22+
"github.com/golang/mock/gomock"
23+
"github.com/mongodb/mongocli/internal/cli"
24+
"github.com/mongodb/mongocli/internal/flag"
25+
"github.com/mongodb/mongocli/internal/mocks"
26+
)
27+
28+
func TestDelete_Run(t *testing.T) {
29+
ctrl := gomock.NewController(t)
30+
mockStore := mocks.NewMockFileSystemsDeleter(ctrl)
31+
defer ctrl.Finish()
32+
33+
mockStore.
34+
EXPECT().
35+
DeleteFileSystem(gomock.Eq("5a0a1e7e0f2912c554080adc")).
36+
Return(nil).
37+
Times(1)
38+
39+
opts := &DeleteOpts{
40+
store: mockStore,
41+
DeleteOpts: &cli.DeleteOpts{
42+
Entry: "5a0a1e7e0f2912c554080adc",
43+
Confirm: true,
44+
},
45+
}
46+
err := opts.Run()
47+
if err != nil {
48+
t.Fatalf("Run() unexpected error: %v", err)
49+
}
50+
}
51+
52+
func TestDeleteBuilder(t *testing.T) {
53+
cli.CmdValidator(
54+
t,
55+
DeleteBuilder(),
56+
0,
57+
[]string{flag.Force},
58+
)
59+
}

internal/cli/opsmanager/admin/backup/filesystem/description.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ const (
1717
short = "Manage file system configurations."
1818
list = "List file system configurations."
1919
describe = "Get a file system configuration."
20+
delete = "Delete a file system configuration."
2021
)

internal/cli/opsmanager/admin/backup/filesystem/file_system.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func Builder() *cobra.Command {
3030
cmd.AddCommand(
3131
ListBuilder(),
3232
DescribeBuilder(),
33+
DeleteBuilder(),
3334
)
3435

3536
return cmd

internal/cli/opsmanager/admin/backup/filesystem/file_system_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestFileSystemBuilder(t *testing.T) {
2525
cli.CmdValidator(
2626
t,
2727
Builder(),
28-
2,
28+
3,
2929
[]string{},
3030
)
3131
}

internal/mocks/mock_backup_file_systems.go

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/file_systems.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"go.mongodb.org/ops-manager/opsmngr"
2424
)
2525

26-
//go:generate mockgen -destination=../mocks/mock_backup_file_systems.go -package=mocks github.com/mongodb/mongocli/internal/store FileSystemsLister,FileSystemsDescriber
26+
//go:generate mockgen -destination=../mocks/mock_backup_file_systems.go -package=mocks github.com/mongodb/mongocli/internal/store FileSystemsLister,FileSystemsDescriber,FileSystemsDeleter
2727

2828
type FileSystemsLister interface {
2929
ListFileSystems(*atlas.ListOptions) (*opsmngr.FileSystemStoreConfigurations, error)
@@ -33,6 +33,10 @@ type FileSystemsDescriber interface {
3333
DescribeFileSystem(string) (*opsmngr.FileSystemStoreConfiguration, error)
3434
}
3535

36+
type FileSystemsDeleter interface {
37+
DeleteFileSystem(string) error
38+
}
39+
3640
// ListFileSystems encapsulates the logic to manage different cloud providers
3741
func (s *Store) ListFileSystems(options *atlas.ListOptions) (*opsmngr.FileSystemStoreConfigurations, error) {
3842
switch s.service {
@@ -54,3 +58,14 @@ func (s *Store) DescribeFileSystem(fileSystemID string) (*opsmngr.FileSystemStor
5458
return nil, fmt.Errorf("unsupported service: %s", s.service)
5559
}
5660
}
61+
62+
// DeleteFileSystem encapsulates the logic to manage different cloud providers
63+
func (s *Store) DeleteFileSystem(fileSystemID string) error {
64+
switch s.service {
65+
case config.OpsManagerService:
66+
_, err := s.client.(*opsmngr.Client).FileSystemStoreConfig.Delete(context.Background(), fileSystemID)
67+
return err
68+
default:
69+
return fmt.Errorf("unsupported service: %s", s.service)
70+
}
71+
}

0 commit comments

Comments
 (0)