Skip to content

Commit 827a46d

Browse files
authored
CLOUDP-60875: List host databases, OM (#105)
1 parent 148308f commit 827a46d

8 files changed

+249
-2
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ gen-mocks: ## Generate mocks
7474
mockgen -source=internal/store/processes.go -destination=internal/mocks/mock_processes.go -package=mocks
7575
mockgen -source=internal/store/logs.go -destination=internal/mocks/mock_logs.go -package=mocks
7676
mockgen -source=internal/store/hosts.go -destination=internal/mocks/mock_hosts.go -package=mocks
77+
mockgen -source=internal/store/host_databases.go -destination=internal/mocks/mock_host_databases.go -package=mocks
78+
mockgen -source=internal/store/host_disks.go -destination=internal/mocks/mock_host_disks.go -package=mocks
7779

7880
.PHONY: build
7981
build: ## Generate a binary in ./bin

internal/cli/atlas_measurements_databases_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func (opts *atlasMeasurementsDatabasesListsOpts) Run() error {
5252
return json.PrettyPrint(result)
5353
}
5454

55-
// mongocli atlas measurements process(es) disks lists host:port
55+
// mongocli atlas measurements process(es) disks lists [host:port]
5656
func AtlasMeasurementsDatabasesListBuilder() *cobra.Command {
5757
opts := &atlasMeasurementsDatabasesListsOpts{}
5858
cmd := &cobra.Command{
59-
Use: "list",
59+
Use: "list [host:port]",
6060
Short: description.ListDatabases,
6161
Aliases: []string{"ls"},
6262
Args: cobra.ExactArgs(1),

internal/cli/ops_manager_measurements.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ func OpsManagerMeasurementsBuilder() *cobra.Command {
2727

2828
cmd.AddCommand(OpsManagerMeasurementsProcessBuilder())
2929
cmd.AddCommand(OpsManagerMeasurementsDisksBuilder())
30+
cmd.AddCommand(OpsManagerMeasurementsDatabasesBuilder())
31+
3032
return cmd
3133
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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/mongodb/mongocli/internal/description"
19+
"github.com/spf13/cobra"
20+
)
21+
22+
func OpsManagerMeasurementsDatabasesBuilder() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "databases",
25+
Aliases: []string{"database"},
26+
Short: description.Databases,
27+
}
28+
29+
cmd.AddCommand(OpsManagerMeasurementsDatabasesListBuilder())
30+
31+
return cmd
32+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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/mongodb/mongocli/internal/description"
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 opsManagerMeasurementsDatabasesListsOpts struct {
27+
globalOpts
28+
listOpts
29+
hostID string
30+
store store.HostDatabaseLister
31+
}
32+
33+
func (opts *opsManagerMeasurementsDatabasesListsOpts) init() error {
34+
if opts.ProjectID() == "" {
35+
return errMissingProjectID
36+
}
37+
38+
var err error
39+
opts.store, err = store.New()
40+
return err
41+
}
42+
43+
func (opts *opsManagerMeasurementsDatabasesListsOpts) Run() error {
44+
listOpts := opts.newListOptions()
45+
result, err := opts.store.HostDatabases(opts.ProjectID(), opts.hostID, listOpts)
46+
47+
if err != nil {
48+
return err
49+
}
50+
51+
return json.PrettyPrint(result)
52+
}
53+
54+
// mongocli om measurements process(es) disks lists [hostID]
55+
func OpsManagerMeasurementsDatabasesListBuilder() *cobra.Command {
56+
opts := &opsManagerMeasurementsDatabasesListsOpts{}
57+
cmd := &cobra.Command{
58+
Use: "list [hostID]",
59+
Short: description.ListDatabases,
60+
Aliases: []string{"ls"},
61+
Args: cobra.ExactArgs(1),
62+
PreRunE: func(cmd *cobra.Command, args []string) error {
63+
return opts.init()
64+
},
65+
RunE: func(cmd *cobra.Command, args []string) error {
66+
opts.hostID = args[0]
67+
68+
return opts.Run()
69+
},
70+
}
71+
72+
cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
73+
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
74+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
75+
76+
return cmd
77+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
package cli
15+
16+
import (
17+
"testing"
18+
19+
"github.com/golang/mock/gomock"
20+
"github.com/mongodb/mongocli/internal/fixtures"
21+
"github.com/mongodb/mongocli/internal/mocks"
22+
)
23+
24+
func TestOpsManagerMeasurementsDatabasesListsOpts_Run(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
mockStore := mocks.NewMockHostDatabaseLister(ctrl)
27+
28+
defer ctrl.Finish()
29+
30+
expected := fixtures.ProcessDatabases()
31+
32+
listOpts := &opsManagerMeasurementsDatabasesListsOpts{
33+
hostID: "1",
34+
store: mockStore,
35+
}
36+
37+
opts := listOpts.newListOptions()
38+
mockStore.
39+
EXPECT().HostDatabases(listOpts.projectID, listOpts.hostID, opts).
40+
Return(expected, nil).
41+
Times(1)
42+
43+
if err := listOpts.Run(); err != nil {
44+
t.Fatalf("Run() unexpected error: %v", err)
45+
}
46+
}

internal/mocks/mock_host_databases.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/host_databases.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 HostDatabaseLister interface {
27+
HostDatabases(string, string, *atlas.ListOptions) (*atlas.ProcessDatabasesResponse, error)
28+
}
29+
30+
// HostDatabases encapsulate the logic to manage different cloud providers
31+
func (s *Store) HostDatabases(groupID, hostID string, opts *atlas.ListOptions) (*atlas.ProcessDatabasesResponse, error) {
32+
switch s.service {
33+
case config.OpsManagerService, config.CloudManagerService:
34+
result, _, err := s.client.(*om.Client).HostDatabases.List(context.Background(), groupID, hostID, opts)
35+
return result, err
36+
default:
37+
return nil, fmt.Errorf("unsupported service: %s", s.service)
38+
}
39+
}

0 commit comments

Comments
 (0)