Skip to content

Commit 148308f

Browse files
authored
CLOUDP-60870: Lists process disks/paritions, OM (#104)
1 parent 9296d08 commit 148308f

6 files changed

+244
-1
lines changed

internal/cli/ops_manager_measurements.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ func OpsManagerMeasurementsBuilder() *cobra.Command {
2626
}
2727

2828
cmd.AddCommand(OpsManagerMeasurementsProcessBuilder())
29-
29+
cmd.AddCommand(OpsManagerMeasurementsDisksBuilder())
3030
return cmd
3131
}
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 OpsManagerMeasurementsDisksBuilder() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "disks",
25+
Aliases: []string{"disk"},
26+
Short: description.Disks,
27+
}
28+
29+
cmd.AddCommand(OpsManagerMeasurementsDisksListBuilder())
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 opsManagerMeasurementsDisksListsOpts struct {
27+
globalOpts
28+
listOpts
29+
hostID string
30+
store store.HostDisksLister
31+
}
32+
33+
func (opts *opsManagerMeasurementsDisksListsOpts) 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 *opsManagerMeasurementsDisksListsOpts) Run() error {
44+
listOpts := opts.newListOptions()
45+
result, err := opts.store.HostDisks(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 OpsManagerMeasurementsDisksListBuilder() *cobra.Command {
56+
opts := &opsManagerMeasurementsDisksListsOpts{}
57+
cmd := &cobra.Command{
58+
Use: "list [hostId]",
59+
Short: description.ListDisks,
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 TestOpsManagerMeasurementsDisksListsOpts_Run(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
mockStore := mocks.NewMockHostDisksLister(ctrl)
27+
28+
defer ctrl.Finish()
29+
30+
expected := fixtures.ProcessDisks()
31+
32+
listOpts := &opsManagerMeasurementsDisksListsOpts{
33+
hostID: "1",
34+
store: mockStore,
35+
}
36+
37+
opts := listOpts.newListOptions()
38+
mockStore.
39+
EXPECT().HostDisks(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_disks.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_disks.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 HostDisksLister interface {
27+
HostDisks(string, string, *atlas.ListOptions) (*atlas.ProcessDisksResponse, error)
28+
}
29+
30+
// HostDisks encapsulate the logic to manage different cloud providers
31+
func (s *Store) HostDisks(groupID, hostID string, opts *atlas.ListOptions) (*atlas.ProcessDisksResponse, error) {
32+
switch s.service {
33+
case config.OpsManagerService, config.CloudManagerService:
34+
result, _, err := s.client.(*om.Client).HostDisks.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)