Skip to content

Commit 720e880

Browse files
authored
CLOUDP-57859: Describe the given process, OM (#108)
1 parent be0c816 commit 720e880

7 files changed

+159
-9
lines changed

internal/cli/ops_manager_processes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func OpsManagerProcessesBuilder() *cobra.Command {
2525
Aliases: []string{"process"},
2626
Short: description.Processes,
2727
}
28-
2928
cmd.AddCommand(OpsManagerProcessListBuilder())
29+
cmd.AddCommand(OpsManagerProcessDescribeBuilder())
3030

3131
return cmd
3232
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 opsManagerProcessesDescribeOpts struct {
27+
globalOpts
28+
hostID string
29+
store store.HostDescriber
30+
}
31+
32+
func (opts *opsManagerProcessesDescribeOpts) init() error {
33+
if opts.ProjectID() == "" {
34+
return errMissingProjectID
35+
}
36+
37+
var err error
38+
opts.store, err = store.New()
39+
return err
40+
}
41+
42+
func (opts *opsManagerProcessesDescribeOpts) Run() error {
43+
result, err := opts.store.Host(opts.ProjectID(), opts.hostID)
44+
45+
if err != nil {
46+
return err
47+
}
48+
49+
return json.PrettyPrint(result)
50+
}
51+
52+
// mongocli om process(es) describe [processID] [--projectId projectId]
53+
func OpsManagerProcessDescribeBuilder() *cobra.Command {
54+
opts := &opsManagerProcessesDescribeOpts{}
55+
cmd := &cobra.Command{
56+
Use: "describe [processID]",
57+
Short: description.ListProcesses,
58+
Aliases: []string{"d"},
59+
Hidden: true,
60+
Args: cobra.ExactArgs(1),
61+
PreRunE: func(cmd *cobra.Command, args []string) error {
62+
return opts.init()
63+
},
64+
RunE: func(cmd *cobra.Command, args []string) error {
65+
opts.hostID = args[0]
66+
return opts.Run()
67+
},
68+
}
69+
70+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
71+
72+
return cmd
73+
}

internal/fixtures/hosts.go renamed to internal/cli/ops_manager_processes_describe_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,36 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package fixtures
15+
package cli
1616

1717
import (
18-
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
18+
"testing"
19+
20+
"github.com/golang/mock/gomock"
21+
"github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
22+
"github.com/mongodb/mongocli/internal/mocks"
1923
)
2024

21-
func Hosts() *om.Hosts {
22-
return &om.Hosts{}
25+
func TestOpsManagerProcessesDescribe_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockHostDescriber(ctrl)
28+
29+
defer ctrl.Finish()
30+
31+
expected := &opsmngr.Host{}
32+
33+
opts := &opsManagerProcessesDescribeOpts{
34+
store: mockStore,
35+
}
36+
37+
mockStore.
38+
EXPECT().
39+
Host(opts.projectID, opts.hostID).
40+
Return(expected, nil).
41+
Times(1)
42+
43+
err := opts.Run()
44+
if err != nil {
45+
t.Fatalf("Run() unexpected error: %v", err)
46+
}
2347
}

internal/cli/ops_manager_processes_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func OpsManagerProcessListBuilder() *cobra.Command {
7676
},
7777
}
7878

79-
cmd.Flags().StringVar(&opts.clusterID, flags.ClusterID, "", usage.Page)
79+
cmd.Flags().StringVar(&opts.clusterID, flags.ClusterID, "", usage.ClusterID)
8080
cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
8181
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
8282

internal/cli/ops_manager_processes_list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/golang/mock/gomock"
21-
"github.com/mongodb/mongocli/internal/fixtures"
21+
"github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
2222
"github.com/mongodb/mongocli/internal/mocks"
2323
)
2424

@@ -28,7 +28,7 @@ func TestOpsManagerProcessesList_Run(t *testing.T) {
2828

2929
defer ctrl.Finish()
3030

31-
expected := fixtures.Hosts()
31+
expected := &opsmngr.Hosts{}
3232

3333
listOpts := &opsManagerProcessesListOpts{
3434
store: mockStore,

internal/mocks/mock_hosts.go

Lines changed: 38 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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ type HostLister interface {
2626
Hosts(string, *om.HostListOptions) (*om.Hosts, error)
2727
}
2828

29-
// HostMeasurements encapsulate the logic to manage different cloud providers
29+
type HostDescriber interface {
30+
Host(string, string) (*om.Host, error)
31+
}
32+
33+
// Hosts encapsulate the logic to manage different cloud providers
3034
func (s *Store) Hosts(groupID string, opts *om.HostListOptions) (*om.Hosts, error) {
3135
switch s.service {
3236
case config.OpsManagerService, config.CloudManagerService:
@@ -36,3 +40,14 @@ func (s *Store) Hosts(groupID string, opts *om.HostListOptions) (*om.Hosts, erro
3640
return nil, fmt.Errorf("unsupported service: %s", s.service)
3741
}
3842
}
43+
44+
// Host encapsulate the logic to manage different cloud providers
45+
func (s *Store) Host(groupID, hostID string) (*om.Host, error) {
46+
switch s.service {
47+
case config.OpsManagerService, config.CloudManagerService:
48+
result, _, err := s.client.(*om.Client).Hosts.Get(context.Background(), groupID, hostID)
49+
return result, err
50+
default:
51+
return nil, fmt.Errorf("unsupported service: %s", s.service)
52+
}
53+
}

0 commit comments

Comments
 (0)