diff --git a/internal/cli/ops_manager_logs.go b/internal/cli/ops_manager_logs.go index 239b6298f0..334667af7f 100644 --- a/internal/cli/ops_manager_logs.go +++ b/internal/cli/ops_manager_logs.go @@ -25,7 +25,9 @@ func OpsManagerLogsBuilder() *cobra.Command { Aliases: []string{"log"}, Short: description.LogCollection, } + cmd.AddCommand(OpsManagerLogsCollectOptsBuilder()) + cmd.AddCommand(OpsManagerLogsListOptsBuilder()) return cmd } diff --git a/internal/cli/ops_manager_logs_list.go b/internal/cli/ops_manager_logs_list.go new file mode 100644 index 0000000000..bc94efa9b8 --- /dev/null +++ b/internal/cli/ops_manager_logs_list.go @@ -0,0 +1,71 @@ +// 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 ( + om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr" + "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 opsManagerLogsListOpts struct { + globalOpts + verbose bool + store store.LogJobLister +} + +func (opts *opsManagerLogsListOpts) initStore() error { + var err error + opts.store, err = store.New() + return err +} + +func (opts *opsManagerLogsListOpts) Run() error { + result, err := opts.store.LogCollectionJobs(opts.ProjectID(), opts.newLogListOptions()) + if err != nil { + return err + } + return json.PrettyPrint(result) +} + +func (opts *opsManagerLogsListOpts) newLogListOptions() *om.LogListOptions { + return &om.LogListOptions{Verbose: opts.verbose} +} + +// mongocli om logs list --verbose verbose [--projectId projectId] +func OpsManagerLogsListOptsBuilder() *cobra.Command { + opts := &opsManagerLogsListOpts{} + cmd := &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: description.ListLogCollectionJobs, + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.PreRunE(opts.initStore) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return opts.Run() + }, + } + + cmd.Flags().BoolVar(&opts.verbose, flags.Verbose, false, usage.Verbose) + + cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID) + + return cmd +} diff --git a/internal/cli/ops_manager_logs_list_test.go b/internal/cli/ops_manager_logs_list_test.go new file mode 100644 index 0000000000..a17d96a26d --- /dev/null +++ b/internal/cli/ops_manager_logs_list_test.go @@ -0,0 +1,45 @@ +// 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" + om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr" + "github.com/mongodb/mongocli/internal/mocks" +) + +func TestOpsManagerLogsListOpts_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockLogJobLister(ctrl) + + defer ctrl.Finish() + + expected := &om.LogCollectionJobs{} + + listOpts := &opsManagerLogsListOpts{ + store: mockStore, + verbose: true, + } + + mockStore. + EXPECT().LogCollectionJobs(listOpts.projectID, listOpts.newLogListOptions()). + Return(expected, nil). + Times(1) + + if err := listOpts.Run(); err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} diff --git a/internal/description/description.go b/internal/description/description.go index 0d1dbac364..5874428ddb 100644 --- a/internal/description/description.go +++ b/internal/description/description.go @@ -93,6 +93,7 @@ A user’s roles apply to all the clusters in the project.` Global = "Manage Ops Manager global properties." LogCollection = "Manage log collection jobs." StartLogCollectionJob = "Start a job to collect logs." + ListLogCollectionJobs = "List log collection jobs." Owner = "Manage Ops Manager owners." CreateOwner = "Create the first user for Ops Manager." Servers = "Manage Ops Manager servers." diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 6414243247..d4ee74f80a 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -116,4 +116,5 @@ const ( Strength = "strength" // Strength flag SizeRequestedPerFileBytes = "sizeRequestedPerFileBytes" //SizeRequestedPerFileBytes flag Redacted = "redacted" // Redacted flag + Verbose = "verbose" ) diff --git a/internal/mocks/mock_logs.go b/internal/mocks/mock_logs.go index 7281e9d9e3..62aef1d7a7 100644 --- a/internal/mocks/mock_logs.go +++ b/internal/mocks/mock_logs.go @@ -86,3 +86,41 @@ func (mr *MockLogCollectorMockRecorder) Collect(arg0, arg1 interface{}) *gomock. mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Collect", reflect.TypeOf((*MockLogCollector)(nil).Collect), arg0, arg1) } + +// MockLogJobLister is a mock of LogJobLister interface +type MockLogJobLister struct { + ctrl *gomock.Controller + recorder *MockLogJobListerMockRecorder +} + +// MockLogJobListerMockRecorder is the mock recorder for MockLogJobLister +type MockLogJobListerMockRecorder struct { + mock *MockLogJobLister +} + +// NewMockLogJobLister creates a new mock instance +func NewMockLogJobLister(ctrl *gomock.Controller) *MockLogJobLister { + mock := &MockLogJobLister{ctrl: ctrl} + mock.recorder = &MockLogJobListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockLogJobLister) EXPECT() *MockLogJobListerMockRecorder { + return m.recorder +} + +// LogCollectionJobs mocks base method +func (m *MockLogJobLister) LogCollectionJobs(arg0 string, arg1 *opsmngr.LogListOptions) (*opsmngr.LogCollectionJobs, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LogCollectionJobs", arg0, arg1) + ret0, _ := ret[0].(*opsmngr.LogCollectionJobs) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// LogCollectionJobs indicates an expected call of LogCollectionJobs +func (mr *MockLogJobListerMockRecorder) LogCollectionJobs(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LogCollectionJobs", reflect.TypeOf((*MockLogJobLister)(nil).LogCollectionJobs), arg0, arg1) +} diff --git a/internal/store/logs.go b/internal/store/logs.go index 219ce0f035..0568bc0e35 100644 --- a/internal/store/logs.go +++ b/internal/store/logs.go @@ -32,6 +32,22 @@ type LogCollector interface { Collect(string, *om.LogCollectionJob) (*om.LogCollectionJob, error) } +type LogJobLister interface { + LogCollectionJobs(string, *om.LogListOptions) (*om.LogCollectionJobs, error) +} + +// LogCollectionJobs encapsulate the logic to manage different cloud providers +func (s *Store) LogCollectionJobs(groupID string, opts *om.LogListOptions) (*om.LogCollectionJobs, error) { + switch s.service { + case config.OpsManagerService, config.CloudManagerService: + log, _, err := s.client.(*om.Client).LogCollections.List(context.Background(), groupID, opts) + return log, err + default: + return nil, fmt.Errorf("unsupported service: %s", s.service) + } +} + +// Collect encapsulate the logic to manage different cloud providers func (s *Store) Collect(groupID string, newLog *om.LogCollectionJob) (*om.LogCollectionJob, error) { switch s.service { case config.OpsManagerService, config.CloudManagerService: diff --git a/internal/usage/usage.go b/internal/usage/usage.go index 8ba2e8cfda..aa3e240ff1 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -95,6 +95,7 @@ const ( Normalization = "If true, collation checks if text requires normalization and performs normalization to compare text." Backwards = "If true, strings with diacritics sort from the back to the front of the string." ClusterName = "Name of the cluster." + Verbose = "If true, returns all child jobs in the response." ClusterID = "Unique identifier of the cluster." Background = "Create the index in the background." TargetProjectID = "Unique identifier of the project that contains the destination cluster for the restore job."