Skip to content

Commit eb4f003

Browse files
CLOUDP-60955: mongocli om logs list (#125)
1 parent f6ec91a commit eb4f003

File tree

8 files changed

+175
-0
lines changed

8 files changed

+175
-0
lines changed

internal/cli/ops_manager_logs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ func OpsManagerLogsBuilder() *cobra.Command {
2525
Aliases: []string{"log"},
2626
Short: description.LogCollection,
2727
}
28+
2829
cmd.AddCommand(OpsManagerLogsCollectOptsBuilder())
30+
cmd.AddCommand(OpsManagerLogsListOptsBuilder())
2931

3032
return cmd
3133
}

internal/cli/ops_manager_logs_list.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cli
16+
17+
import (
18+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
19+
"github.com/mongodb/mongocli/internal/description"
20+
"github.com/mongodb/mongocli/internal/flags"
21+
"github.com/mongodb/mongocli/internal/json"
22+
"github.com/mongodb/mongocli/internal/store"
23+
"github.com/mongodb/mongocli/internal/usage"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
type opsManagerLogsListOpts struct {
28+
globalOpts
29+
verbose bool
30+
store store.LogJobLister
31+
}
32+
33+
func (opts *opsManagerLogsListOpts) initStore() error {
34+
var err error
35+
opts.store, err = store.New()
36+
return err
37+
}
38+
39+
func (opts *opsManagerLogsListOpts) Run() error {
40+
result, err := opts.store.LogCollectionJobs(opts.ProjectID(), opts.newLogListOptions())
41+
if err != nil {
42+
return err
43+
}
44+
return json.PrettyPrint(result)
45+
}
46+
47+
func (opts *opsManagerLogsListOpts) newLogListOptions() *om.LogListOptions {
48+
return &om.LogListOptions{Verbose: opts.verbose}
49+
}
50+
51+
// mongocli om logs list --verbose verbose [--projectId projectId]
52+
func OpsManagerLogsListOptsBuilder() *cobra.Command {
53+
opts := &opsManagerLogsListOpts{}
54+
cmd := &cobra.Command{
55+
Use: "list",
56+
Aliases: []string{"ls"},
57+
Short: description.ListLogCollectionJobs,
58+
PreRunE: func(cmd *cobra.Command, args []string) error {
59+
return opts.PreRunE(opts.initStore)
60+
},
61+
RunE: func(cmd *cobra.Command, args []string) error {
62+
return opts.Run()
63+
},
64+
}
65+
66+
cmd.Flags().BoolVar(&opts.verbose, flags.Verbose, false, usage.Verbose)
67+
68+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
69+
70+
return cmd
71+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package cli
15+
16+
import (
17+
"testing"
18+
19+
"github.com/golang/mock/gomock"
20+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
21+
"github.com/mongodb/mongocli/internal/mocks"
22+
)
23+
24+
func TestOpsManagerLogsListOpts_Run(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
mockStore := mocks.NewMockLogJobLister(ctrl)
27+
28+
defer ctrl.Finish()
29+
30+
expected := &om.LogCollectionJobs{}
31+
32+
listOpts := &opsManagerLogsListOpts{
33+
store: mockStore,
34+
verbose: true,
35+
}
36+
37+
mockStore.
38+
EXPECT().LogCollectionJobs(listOpts.projectID, listOpts.newLogListOptions()).
39+
Return(expected, nil).
40+
Times(1)
41+
42+
if err := listOpts.Run(); err != nil {
43+
t.Fatalf("Run() unexpected error: %v", err)
44+
}
45+
}

internal/description/description.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ A user’s roles apply to all the clusters in the project.`
9393
Global = "Manage Ops Manager global properties."
9494
LogCollection = "Manage log collection jobs."
9595
StartLogCollectionJob = "Start a job to collect logs."
96+
ListLogCollectionJobs = "List log collection jobs."
9697
Owner = "Manage Ops Manager owners."
9798
CreateOwner = "Create the first user for Ops Manager."
9899
Servers = "Manage Ops Manager servers."

internal/flags/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,5 @@ const (
116116
Strength = "strength" // Strength flag
117117
SizeRequestedPerFileBytes = "sizeRequestedPerFileBytes" //SizeRequestedPerFileBytes flag
118118
Redacted = "redacted" // Redacted flag
119+
Verbose = "verbose"
119120
)

internal/mocks/mock_logs.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/logs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ type LogCollector interface {
3232
Collect(string, *om.LogCollectionJob) (*om.LogCollectionJob, error)
3333
}
3434

35+
type LogJobLister interface {
36+
LogCollectionJobs(string, *om.LogListOptions) (*om.LogCollectionJobs, error)
37+
}
38+
39+
// LogCollectionJobs encapsulate the logic to manage different cloud providers
40+
func (s *Store) LogCollectionJobs(groupID string, opts *om.LogListOptions) (*om.LogCollectionJobs, error) {
41+
switch s.service {
42+
case config.OpsManagerService, config.CloudManagerService:
43+
log, _, err := s.client.(*om.Client).LogCollections.List(context.Background(), groupID, opts)
44+
return log, err
45+
default:
46+
return nil, fmt.Errorf("unsupported service: %s", s.service)
47+
}
48+
}
49+
50+
// Collect encapsulate the logic to manage different cloud providers
3551
func (s *Store) Collect(groupID string, newLog *om.LogCollectionJob) (*om.LogCollectionJob, error) {
3652
switch s.service {
3753
case config.OpsManagerService, config.CloudManagerService:

internal/usage/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const (
9595
Normalization = "If true, collation checks if text requires normalization and performs normalization to compare text."
9696
Backwards = "If true, strings with diacritics sort from the back to the front of the string."
9797
ClusterName = "Name of the cluster."
98+
Verbose = "If true, returns all child jobs in the response."
9899
ClusterID = "Unique identifier of the cluster."
99100
Background = "Create the index in the background."
100101
TargetProjectID = "Unique identifier of the project that contains the destination cluster for the restore job."

0 commit comments

Comments
 (0)