diff --git a/internal/cli/ops_manager_measurements.go b/internal/cli/ops_manager_measurements.go index 2acadf034e..d8e99e05d2 100644 --- a/internal/cli/ops_manager_measurements.go +++ b/internal/cli/ops_manager_measurements.go @@ -26,6 +26,6 @@ func OpsManagerMeasurementsBuilder() *cobra.Command { } cmd.AddCommand(OpsManagerMeasurementsProcessBuilder()) - + cmd.AddCommand(OpsManagerMeasurementsDisksBuilder()) return cmd } diff --git a/internal/cli/ops_manager_measurements_disks.go b/internal/cli/ops_manager_measurements_disks.go new file mode 100644 index 0000000000..d773266b93 --- /dev/null +++ b/internal/cli/ops_manager_measurements_disks.go @@ -0,0 +1,32 @@ +// Copyright 2020 MongoDB Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cli + +import ( + "github.com/mongodb/mongocli/internal/description" + "github.com/spf13/cobra" +) + +func OpsManagerMeasurementsDisksBuilder() *cobra.Command { + cmd := &cobra.Command{ + Use: "disks", + Aliases: []string{"disk"}, + Short: description.Disks, + } + + cmd.AddCommand(OpsManagerMeasurementsDisksListBuilder()) + + return cmd +} diff --git a/internal/cli/ops_manager_measurements_disks_list.go b/internal/cli/ops_manager_measurements_disks_list.go new file mode 100644 index 0000000000..d5fcaf33e7 --- /dev/null +++ b/internal/cli/ops_manager_measurements_disks_list.go @@ -0,0 +1,77 @@ +// Copyright 2020 MongoDB Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cli + +import ( + "github.com/mongodb/mongocli/internal/description" + "github.com/mongodb/mongocli/internal/flags" + "github.com/mongodb/mongocli/internal/json" + "github.com/mongodb/mongocli/internal/store" + "github.com/mongodb/mongocli/internal/usage" + "github.com/spf13/cobra" +) + +type opsManagerMeasurementsDisksListsOpts struct { + globalOpts + listOpts + hostID string + store store.HostDisksLister +} + +func (opts *opsManagerMeasurementsDisksListsOpts) init() error { + if opts.ProjectID() == "" { + return errMissingProjectID + } + + var err error + opts.store, err = store.New() + return err +} + +func (opts *opsManagerMeasurementsDisksListsOpts) Run() error { + listOpts := opts.newListOptions() + result, err := opts.store.HostDisks(opts.ProjectID(), opts.hostID, listOpts) + + if err != nil { + return err + } + + return json.PrettyPrint(result) +} + +// mongocli om measurements process(es) disks lists [hostId] +func OpsManagerMeasurementsDisksListBuilder() *cobra.Command { + opts := &opsManagerMeasurementsDisksListsOpts{} + cmd := &cobra.Command{ + Use: "list [hostId]", + Short: description.ListDisks, + Aliases: []string{"ls"}, + Args: cobra.ExactArgs(1), + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.init() + }, + RunE: func(cmd *cobra.Command, args []string) error { + opts.hostID = args[0] + + return opts.Run() + }, + } + + cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page) + cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit) + cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID) + + return cmd +} diff --git a/internal/cli/ops_manager_measurements_disks_list_test.go b/internal/cli/ops_manager_measurements_disks_list_test.go new file mode 100644 index 0000000000..6dfe366e95 --- /dev/null +++ b/internal/cli/ops_manager_measurements_disks_list_test.go @@ -0,0 +1,46 @@ +// Copyright 2020 MongoDB Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package cli + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/mongodb/mongocli/internal/fixtures" + "github.com/mongodb/mongocli/internal/mocks" +) + +func TestOpsManagerMeasurementsDisksListsOpts_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockHostDisksLister(ctrl) + + defer ctrl.Finish() + + expected := fixtures.ProcessDisks() + + listOpts := &opsManagerMeasurementsDisksListsOpts{ + hostID: "1", + store: mockStore, + } + + opts := listOpts.newListOptions() + mockStore. + EXPECT().HostDisks(listOpts.projectID, listOpts.hostID, opts). + Return(expected, nil). + Times(1) + + if err := listOpts.Run(); err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} diff --git a/internal/mocks/mock_host_disks.go b/internal/mocks/mock_host_disks.go new file mode 100644 index 0000000000..35fa999687 --- /dev/null +++ b/internal/mocks/mock_host_disks.go @@ -0,0 +1,49 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: internal/store/host_disks.go + +// Package mocks is a generated GoMock package. +package mocks + +import ( + gomock "github.com/golang/mock/gomock" + mongodbatlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" + reflect "reflect" +) + +// MockHostDisksLister is a mock of HostDisksLister interface +type MockHostDisksLister struct { + ctrl *gomock.Controller + recorder *MockHostDisksListerMockRecorder +} + +// MockHostDisksListerMockRecorder is the mock recorder for MockHostDisksLister +type MockHostDisksListerMockRecorder struct { + mock *MockHostDisksLister +} + +// NewMockHostDisksLister creates a new mock instance +func NewMockHostDisksLister(ctrl *gomock.Controller) *MockHostDisksLister { + mock := &MockHostDisksLister{ctrl: ctrl} + mock.recorder = &MockHostDisksListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockHostDisksLister) EXPECT() *MockHostDisksListerMockRecorder { + return m.recorder +} + +// HostDisks mocks base method +func (m *MockHostDisksLister) HostDisks(arg0, arg1 string, arg2 *mongodbatlas.ListOptions) (*mongodbatlas.ProcessDisksResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HostDisks", arg0, arg1, arg2) + ret0, _ := ret[0].(*mongodbatlas.ProcessDisksResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// HostDisks indicates an expected call of HostDisks +func (mr *MockHostDisksListerMockRecorder) HostDisks(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HostDisks", reflect.TypeOf((*MockHostDisksLister)(nil).HostDisks), arg0, arg1, arg2) +} diff --git a/internal/store/host_disks.go b/internal/store/host_disks.go new file mode 100644 index 0000000000..9135cb80db --- /dev/null +++ b/internal/store/host_disks.go @@ -0,0 +1,39 @@ +// Copyright 2020 MongoDB Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package store + +import ( + "context" + "fmt" + + atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" + om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr" + "github.com/mongodb/mongocli/internal/config" +) + +type HostDisksLister interface { + HostDisks(string, string, *atlas.ListOptions) (*atlas.ProcessDisksResponse, error) +} + +// HostDisks encapsulate the logic to manage different cloud providers +func (s *Store) HostDisks(groupID, hostID string, opts *atlas.ListOptions) (*atlas.ProcessDisksResponse, error) { + switch s.service { + case config.OpsManagerService, config.CloudManagerService: + result, _, err := s.client.(*om.Client).HostDisks.List(context.Background(), groupID, hostID, opts) + return result, err + default: + return nil, fmt.Errorf("unsupported service: %s", s.service) + } +}