Skip to content

Commit 9296d08

Browse files
authored
CLOUDP-57858: List all available mongod processes/hosts, C/OM (#103)
1 parent f9db3e5 commit 9296d08

File tree

9 files changed

+278
-0
lines changed

9 files changed

+278
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ gen-mocks: ## Generate mocks
7373
mockgen -source=internal/store/indexes.go -destination=internal/mocks/mock_indexes.go -package=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
76+
mockgen -source=internal/store/hosts.go -destination=internal/mocks/mock_hosts.go -package=mocks
7677

7778
.PHONY: build
7879
build: ## Generate a binary in ./bin

internal/cli/cloud_manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func CloudManagerBuilder() *cobra.Command {
3434
cmd.AddCommand(OpsManagerSecurityBuilder())
3535
cmd.AddCommand(OpsManagerDBUsersBuilder())
3636
cmd.AddCommand(AtlasEventsBuilder())
37+
cmd.AddCommand(OpsManagerProcessesBuilder())
3738
cmd.AddCommand(OpsManagerMeasurementsBuilder())
3839

3940
return cmd

internal/cli/ops_manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func OpsManagerBuilder() *cobra.Command {
3535
cmd.AddCommand(OpsManagerDBUsersBuilder())
3636
cmd.AddCommand(OpsManagerOwnerBuilder())
3737
cmd.AddCommand(AtlasEventsBuilder())
38+
cmd.AddCommand(OpsManagerProcessesBuilder())
3839
cmd.AddCommand(OpsManagerMeasurementsBuilder())
3940

4041
return cmd

internal/cli/ops_manager_processes.go

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 OpsManagerProcessesBuilder() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "processes",
25+
Aliases: []string{"process"},
26+
Short: description.Processes,
27+
}
28+
29+
cmd.AddCommand(OpsManagerProcessListBuilder())
30+
31+
return cmd
32+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
19+
"github.com/mongodb/mongocli/internal/description"
20+
"github.com/mongodb/mongocli/internal/flags"
21+
"github.com/mongodb/mongocli/internal/json"
22+
"github.com/mongodb/mongocli/internal/store"
23+
"github.com/mongodb/mongocli/internal/usage"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
type opsManagerProcessesListOpts struct {
28+
globalOpts
29+
listOpts
30+
clusterID string
31+
store store.HostLister
32+
}
33+
34+
func (opts *opsManagerProcessesListOpts) init() error {
35+
if opts.ProjectID() == "" {
36+
return errMissingProjectID
37+
}
38+
39+
var err error
40+
opts.store, err = store.New()
41+
return err
42+
}
43+
44+
func (opts *opsManagerProcessesListOpts) Run() error {
45+
listOpts := opts.newHostListOptions()
46+
result, err := opts.store.Hosts(opts.ProjectID(), listOpts)
47+
48+
if err != nil {
49+
return err
50+
}
51+
52+
return json.PrettyPrint(result)
53+
}
54+
55+
func (opts *opsManagerProcessesListOpts) newHostListOptions() *om.HostListOptions {
56+
return &om.HostListOptions{
57+
ClusterID: opts.clusterID,
58+
ListOptions: *opts.newListOptions(),
59+
}
60+
}
61+
62+
// mongocli om process(es) list --projectId projectId [--page N] [--limit N]
63+
func OpsManagerProcessListBuilder() *cobra.Command {
64+
opts := &opsManagerProcessesListOpts{}
65+
cmd := &cobra.Command{
66+
Use: "list",
67+
Short: description.ListProcesses,
68+
Aliases: []string{"ls"},
69+
Hidden: true,
70+
Args: cobra.NoArgs,
71+
PreRunE: func(cmd *cobra.Command, args []string) error {
72+
return opts.init()
73+
},
74+
RunE: func(cmd *cobra.Command, args []string) error {
75+
return opts.Run()
76+
},
77+
}
78+
79+
cmd.Flags().StringVar(&opts.clusterID, flags.ClusterID, "", usage.Page)
80+
cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
81+
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
82+
83+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
84+
85+
return cmd
86+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
"testing"
19+
20+
"github.com/golang/mock/gomock"
21+
"github.com/mongodb/mongocli/internal/fixtures"
22+
"github.com/mongodb/mongocli/internal/mocks"
23+
)
24+
25+
func TestOpsManagerProcessesList_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockHostLister(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
expected := fixtures.Hosts()
32+
33+
listOpts := &opsManagerProcessesListOpts{
34+
store: mockStore,
35+
}
36+
37+
mockStore.
38+
EXPECT().
39+
Hosts(listOpts.projectID, listOpts.newHostListOptions()).
40+
Return(expected, nil).
41+
Times(1)
42+
43+
err := listOpts.Run()
44+
if err != nil {
45+
t.Fatalf("Run() unexpected error: %v", err)
46+
}
47+
}

internal/fixtures/hosts.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 fixtures
16+
17+
import (
18+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
19+
)
20+
21+
func Hosts() *om.Hosts {
22+
return &om.Hosts{}
23+
}

internal/mocks/mock_hosts.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/hosts.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
22+
"github.com/mongodb/mongocli/internal/config"
23+
)
24+
25+
type HostLister interface {
26+
Hosts(string, *om.HostListOptions) (*om.Hosts, error)
27+
}
28+
29+
// HostMeasurements encapsulate the logic to manage different cloud providers
30+
func (s *Store) Hosts(groupID string, opts *om.HostListOptions) (*om.Hosts, error) {
31+
switch s.service {
32+
case config.OpsManagerService, config.CloudManagerService:
33+
result, _, err := s.client.(*om.Client).Hosts.List(context.Background(), groupID, opts)
34+
return result, err
35+
default:
36+
return nil, fmt.Errorf("unsupported service: %s", s.service)
37+
}
38+
}

0 commit comments

Comments
 (0)