Skip to content

CLOUDP-60955: mongocli om logs list #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a comment in the jira ticket about making this, maybe, mongocli om log jobs list we could do that on a separate PR but just letting you know this may change

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I haven’t done it yet because it seems to be something that we still have to decide

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created this ticket https://jira.mongodb.org/browse/CLOUDP-61392 to keep track of this change

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