Skip to content

Commit

Permalink
CLOUDP-60955: mongocli om logs list (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo authored Apr 24, 2020
1 parent f6ec91a commit eb4f003
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/cli/ops_manager_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func OpsManagerLogsBuilder() *cobra.Command {
Aliases: []string{"log"},
Short: description.LogCollection,
}

cmd.AddCommand(OpsManagerLogsCollectOptsBuilder())
cmd.AddCommand(OpsManagerLogsListOptsBuilder())

return cmd
}
71 changes: 71 additions & 0 deletions internal/cli/ops_manager_logs_list.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions internal/cli/ops_manager_logs_list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions internal/description/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@ const (
Strength = "strength" // Strength flag
SizeRequestedPerFileBytes = "sizeRequestedPerFileBytes" //SizeRequestedPerFileBytes flag
Redacted = "redacted" // Redacted flag
Verbose = "verbose"
)
38 changes: 38 additions & 0 deletions internal/mocks/mock_logs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions internal/store/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions internal/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down

0 comments on commit eb4f003

Please sign in to comment.