diff --git a/Makefile b/Makefile index e49c130fdf..b5d9963a35 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,7 @@ gen-mocks: ## Generate mocks mockgen -source=internal/store/hosts.go -destination=internal/mocks/mock_hosts.go -package=mocks mockgen -source=internal/store/host_databases.go -destination=internal/mocks/mock_host_databases.go -package=mocks mockgen -source=internal/store/host_disks.go -destination=internal/mocks/mock_host_disks.go -package=mocks + mockgen -source=internal/store/host_disk_measurements.go -destination=internal/mocks/mock_host_disk_measurements.go -package=mocks .PHONY: build build: ## Generate a binary in ./bin diff --git a/internal/cli/ops_manager_measurements_disks.go b/internal/cli/ops_manager_measurements_disks.go index d773266b93..a94cfe8244 100644 --- a/internal/cli/ops_manager_measurements_disks.go +++ b/internal/cli/ops_manager_measurements_disks.go @@ -27,6 +27,7 @@ func OpsManagerMeasurementsDisksBuilder() *cobra.Command { } cmd.AddCommand(OpsManagerMeasurementsDisksListBuilder()) + cmd.AddCommand(OpsManagerMeasurementsDisksDescribeBuilder()) return cmd } diff --git a/internal/cli/ops_manager_measurements_disks_describe.go b/internal/cli/ops_manager_measurements_disks_describe.go new file mode 100644 index 0000000000..36c4db7661 --- /dev/null +++ b/internal/cli/ops_manager_measurements_disks_describe.go @@ -0,0 +1,87 @@ +// 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 opsManagerMeasurementsDisksDescribeOpts struct { + globalOpts + measurementsOpts + hostID string + name string + store store.HostDiskMeasurementsLister +} + +func (opts *opsManagerMeasurementsDisksDescribeOpts) init() error { + if opts.ProjectID() == "" { + return errMissingProjectID + } + + var err error + opts.store, err = store.New() + return err +} + +func (opts *opsManagerMeasurementsDisksDescribeOpts) Run() error { + listOpts := opts.newProcessMeasurementListOptions() + result, err := opts.store.HostDiskMeasurements(opts.ProjectID(), opts.hostID, opts.name, listOpts) + + if err != nil { + return err + } + + return json.PrettyPrint(result) +} + +// mcli om measurements disk(s) describe [host:port] [name] --granularity g --period p --start start --end end [--type type] [--projectId projectId] +func OpsManagerMeasurementsDisksDescribeBuilder() *cobra.Command { + opts := &opsManagerMeasurementsDisksDescribeOpts{} + cmd := &cobra.Command{ + Use: "describe [hostId] [name]", + Short: description.ListDisks, + Args: cobra.ExactArgs(2), + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.init() + }, + RunE: func(cmd *cobra.Command, args []string) error { + opts.hostID = args[0] + opts.name = args[1] + + 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.granularity, flags.Granularity, "", usage.Granularity) + cmd.Flags().StringVar(&opts.period, flags.Period, "", usage.Period) + cmd.Flags().StringVar(&opts.start, flags.Start, "", usage.Start) + cmd.Flags().StringVar(&opts.end, flags.End, "", usage.End) + cmd.Flags().StringVar(&opts.measurementType, flags.MeasurementType, "", usage.MeasurementType) + + cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID) + + _ = cmd.MarkFlagRequired(flags.Granularity) + + return cmd +} diff --git a/internal/cli/ops_manager_measurements_disks_describe_test.go b/internal/cli/ops_manager_measurements_disks_describe_test.go new file mode 100644 index 0000000000..80d0836a49 --- /dev/null +++ b/internal/cli/ops_manager_measurements_disks_describe_test.go @@ -0,0 +1,48 @@ +// 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 TestOpsManagerMeasurementsDisksDescribeOpts_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockHostDiskMeasurementsLister(ctrl) + + defer ctrl.Finish() + + expected := fixtures.DiskMeasurements() + + listOpts := &opsManagerMeasurementsDisksDescribeOpts{ + hostID: "1", + name: "test", + store: mockStore, + } + + opts := listOpts.newProcessMeasurementListOptions() + mockStore. + EXPECT().HostDiskMeasurements(listOpts.projectID, listOpts.hostID, listOpts.name, opts). + Return(expected, nil). + Times(1) + + err := listOpts.Run() + if err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} diff --git a/internal/mocks/mock_host_disk_measurements.go b/internal/mocks/mock_host_disk_measurements.go new file mode 100644 index 0000000000..03a4a55ee1 --- /dev/null +++ b/internal/mocks/mock_host_disk_measurements.go @@ -0,0 +1,49 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: internal/store/host_disk_measurements.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" +) + +// MockHostDiskMeasurementsLister is a mock of HostDiskMeasurementsLister interface +type MockHostDiskMeasurementsLister struct { + ctrl *gomock.Controller + recorder *MockHostDiskMeasurementsListerMockRecorder +} + +// MockHostDiskMeasurementsListerMockRecorder is the mock recorder for MockHostDiskMeasurementsLister +type MockHostDiskMeasurementsListerMockRecorder struct { + mock *MockHostDiskMeasurementsLister +} + +// NewMockHostDiskMeasurementsLister creates a new mock instance +func NewMockHostDiskMeasurementsLister(ctrl *gomock.Controller) *MockHostDiskMeasurementsLister { + mock := &MockHostDiskMeasurementsLister{ctrl: ctrl} + mock.recorder = &MockHostDiskMeasurementsListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockHostDiskMeasurementsLister) EXPECT() *MockHostDiskMeasurementsListerMockRecorder { + return m.recorder +} + +// HostDiskMeasurements mocks base method +func (m *MockHostDiskMeasurementsLister) HostDiskMeasurements(arg0, arg1, arg2 string, arg3 *mongodbatlas.ProcessMeasurementListOptions) (*mongodbatlas.ProcessDiskMeasurements, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HostDiskMeasurements", arg0, arg1, arg2, arg3) + ret0, _ := ret[0].(*mongodbatlas.ProcessDiskMeasurements) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// HostDiskMeasurements indicates an expected call of HostDiskMeasurements +func (mr *MockHostDiskMeasurementsListerMockRecorder) HostDiskMeasurements(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HostDiskMeasurements", reflect.TypeOf((*MockHostDiskMeasurementsLister)(nil).HostDiskMeasurements), arg0, arg1, arg2, arg3) +} diff --git a/internal/store/host_disk_measurements.go b/internal/store/host_disk_measurements.go new file mode 100644 index 0000000000..2e65527a1e --- /dev/null +++ b/internal/store/host_disk_measurements.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 HostDiskMeasurementsLister interface { + HostDiskMeasurements(string, string, string, *atlas.ProcessMeasurementListOptions) (*atlas.ProcessDiskMeasurements, error) +} + +// HostDiskMeasurements encapsulate the logic to manage different cloud providers +func (s *Store) HostDiskMeasurements(groupID, hostID string, partitionName string, opts *atlas.ProcessMeasurementListOptions) (*atlas.ProcessDiskMeasurements, error) { + switch s.service { + case config.OpsManagerService, config.CloudManagerService: + result, _, err := s.client.(*om.Client).HostDiskMeasurements.List(context.Background(), groupID, hostID, partitionName, opts) + return result, err + default: + return nil, fmt.Errorf("unsupported service: %s", s.service) + } +}