Skip to content

Commit d6b839b

Browse files
CLOUDP-60954: mongocli om logs collect (#123)
1 parent 2cc7bf9 commit d6b839b

File tree

12 files changed

+295
-49
lines changed

12 files changed

+295
-49
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/mattn/go-colorable v0.1.6 // indirect
1212
github.com/mitchellh/go-homedir v1.1.0
1313
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200423095452-c3661e38ae5c
14-
github.com/mongodb/go-client-mongodb-ops-manager v0.2.0
14+
github.com/mongodb/go-client-mongodb-ops-manager v0.2.1
1515
github.com/pelletier/go-toml v1.6.0 // indirect
1616
github.com/spf13/afero v1.2.2
1717
github.com/spf13/cast v1.3.1 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ github.com/mongodb/go-client-mongodb-atlas v0.2.0 h1:UH02byrzAlboirAMgV0sMIJqXbE
104104
github.com/mongodb/go-client-mongodb-atlas v0.2.0/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
105105
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200423095452-c3661e38ae5c h1:k1l2qLwwy4eCOli7f3oRWDbtIvXQF5SpO7JAXXcW3qI=
106106
github.com/mongodb/go-client-mongodb-atlas v0.2.1-0.20200423095452-c3661e38ae5c/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
107-
github.com/mongodb/go-client-mongodb-ops-manager v0.2.0 h1:OfC6UXGJqivsMTNnxnZBu+e4Oz4s/9RdSwpv23eWBus=
108-
github.com/mongodb/go-client-mongodb-ops-manager v0.2.0/go.mod h1:IWna/sWCgLSOLUeOYLg2nYf6HJcQnnqNI73K5NEMq9Q=
107+
github.com/mongodb/go-client-mongodb-ops-manager v0.2.1 h1:60nEpqO5P74K5Y81OP3HHNt3e8n0NKKDX5wWqZDLuLY=
108+
github.com/mongodb/go-client-mongodb-ops-manager v0.2.1/go.mod h1:IWna/sWCgLSOLUeOYLg2nYf6HJcQnnqNI73K5NEMq9Q=
109109
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
110110
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
111111
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

internal/cli/cloud_manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func CloudManagerBuilder() *cobra.Command {
3636
cmd.AddCommand(AtlasEventsBuilder())
3737
cmd.AddCommand(OpsManagerProcessesBuilder())
3838
cmd.AddCommand(OpsManagerMeasurementsBuilder())
39+
cmd.AddCommand(OpsManagerLogsBuilder())
3940

4041
return cmd
4142
}

internal/cli/ops_manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func OpsManagerBuilder() *cobra.Command {
3737
cmd.AddCommand(AtlasEventsBuilder())
3838
cmd.AddCommand(OpsManagerProcessesBuilder())
3939
cmd.AddCommand(OpsManagerMeasurementsBuilder())
40+
cmd.AddCommand(OpsManagerLogsBuilder())
4041

4142
return cmd
4243
}

internal/cli/ops_manager_logs.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
"github.com/mongodb/mongocli/internal/description"
19+
"github.com/spf13/cobra"
20+
)
21+
22+
func OpsManagerLogsBuilder() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "logs",
25+
Aliases: []string{"log"},
26+
Short: description.LogCollection,
27+
}
28+
cmd.AddCommand(OpsManagerLogsCollectOptsBuilder())
29+
30+
return cmd
31+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
"fmt"
19+
"strings"
20+
21+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
22+
"github.com/mongodb/mongocli/internal/description"
23+
"github.com/mongodb/mongocli/internal/flags"
24+
"github.com/mongodb/mongocli/internal/json"
25+
"github.com/mongodb/mongocli/internal/search"
26+
"github.com/mongodb/mongocli/internal/store"
27+
"github.com/mongodb/mongocli/internal/usage"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
type opsManagerLogsCollectOpts struct {
32+
globalOpts
33+
resourceType string
34+
resourceName string
35+
logTypes []string
36+
sizeRequestedPerFileBytes int64
37+
redacted bool
38+
store store.LogCollector
39+
}
40+
41+
func (opts *opsManagerLogsCollectOpts) initStore() error {
42+
var err error
43+
opts.store, err = store.New()
44+
return err
45+
}
46+
47+
func (opts *opsManagerLogsCollectOpts) Run() error {
48+
result, err := opts.store.Collect(opts.ProjectID(), opts.newLog())
49+
if err != nil {
50+
return err
51+
}
52+
return json.PrettyPrint(result)
53+
}
54+
55+
func (opts *opsManagerLogsCollectOpts) newLog() *om.LogCollectionJob {
56+
return &om.LogCollectionJob{
57+
ResourceType: opts.resourceType,
58+
ResourceName: opts.resourceName,
59+
Redacted: &opts.redacted,
60+
SizeRequestedPerFileBytes: opts.sizeRequestedPerFileBytes,
61+
LogTypes: opts.logTypes,
62+
}
63+
}
64+
65+
// mongocli om logs collect resourceType resourceName --sizeRequestedPerFileBytes size --type type --redacted redacted [--projectId projectId]
66+
func OpsManagerLogsCollectOptsBuilder() *cobra.Command {
67+
opts := &opsManagerLogsCollectOpts{}
68+
cmd := &cobra.Command{
69+
Use: "collect [resourceType] [resourceName]",
70+
Short: description.StartLogCollectionJob,
71+
Args: func(cmd *cobra.Command, args []string) error {
72+
if len(args) != 2 {
73+
return fmt.Errorf("accepts %d arg(s), received %d", 2, len(args))
74+
}
75+
76+
args[0] = strings.ToLower(args[0])
77+
if !search.StringInSlice(cmd.ValidArgs, args[0]) {
78+
return fmt.Errorf("invalid resource type '%s', expected one of %v", args[0], cmd.ValidArgs)
79+
}
80+
return nil
81+
},
82+
ValidArgs: []string{"cluster", "process", "replicaset"},
83+
PreRunE: func(cmd *cobra.Command, args []string) error {
84+
return opts.PreRunE(opts.initStore)
85+
},
86+
RunE: func(cmd *cobra.Command, args []string) error {
87+
opts.resourceType = args[0]
88+
opts.resourceName = args[1]
89+
return opts.Run()
90+
},
91+
}
92+
93+
cmd.Flags().StringArrayVar(&opts.logTypes, flags.Type, nil, usage.LogTypes)
94+
cmd.Flags().Int64Var(&opts.sizeRequestedPerFileBytes, flags.SizeRequestedPerFileBytes, 0, usage.SizeRequestedPerFileBytes)
95+
cmd.Flags().BoolVar(&opts.redacted, flags.Redacted, false, usage.LogRedacted)
96+
97+
_ = cmd.MarkFlagRequired(flags.SizeRequestedPerFileBytes)
98+
_ = cmd.MarkFlagRequired(flags.Type)
99+
100+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
101+
102+
return cmd
103+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 TestOpsManagerLogsCollectOpts_Run(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
mockStore := mocks.NewMockLogCollector(ctrl)
27+
28+
defer ctrl.Finish()
29+
30+
expected := &om.LogCollectionJob{ID: "1"}
31+
32+
listOpts := &opsManagerLogsCollectOpts{
33+
redacted: false,
34+
sizeRequestedPerFileBytes: 64,
35+
resourceType: "CLUSTER",
36+
resourceName: "",
37+
logTypes: []string{"AUTOMATION_AGENT"},
38+
store: mockStore,
39+
}
40+
41+
mockStore.
42+
EXPECT().Collect(listOpts.projectID, listOpts.newLog()).
43+
Return(expected, nil).
44+
Times(1)
45+
46+
if err := listOpts.Run(); err != nil {
47+
t.Fatalf("Run() unexpected error: %v", err)
48+
}
49+
}

internal/description/description.go

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,50 +53,52 @@ const (
5353
The dbusers command retrieves, creates and modifies the MongoDB database users in your cluster.
5454
Each user has a set of roles that provide access to the project’s databases.
5555
A user’s roles apply to all the clusters in the project.`
56-
CreateDBUser = "Create a database user for a project."
57-
DeleteDBUser = "Delete a database user for a project."
58-
ListDBUsers = "List Atlas database users for a project."
59-
ListEvents = "List events for an organization or project"
60-
UpdateDBUser = "Update a MongoDB dbuser in Atlas."
61-
ProcessMeasurements = "Get measurements for a given host."
62-
Disks = "List available disks or disks measurements for a given host."
63-
ListDisks = "List available disks for a given host."
64-
DescribeDisks = "Describe disks measurements for a given host partition."
65-
Databases = "List available databases or databases measurements for a given host."
66-
ListDatabases = "List available databases for a given host."
67-
Whitelist = "Manage the IP whitelist for a project."
68-
CreateWhitelist = "Create an IP whitelist for a project."
69-
DeleteWhitelist = "Delete a database user for a project."
70-
DescribeWhitelist = "Describe an Atlas whitelist."
71-
ListWhitelist = "List Atlas whitelist for a project."
72-
CloudManager = "Cloud Manager operations."
73-
ShutdownOMCluster = "Shutdown a Cloud Manager cluster."
74-
StartUpOMCluster = "Startup a Cloud Manager cluster."
75-
UpdateOMCluster = "Update a Cloud Manager cluster."
76-
ConfigDescription = "Configure the tool."
77-
SetConfig = "Configure the tool."
78-
IAM = "Authentication operations."
79-
Organization = "Organization operations."
80-
OrganizationLong = "Create, list and manage your MongoDB organizations."
81-
CreateOrganization = "Create an organization."
82-
DeleteOrganization = "Delete an organization."
83-
ListOrganizations = "List organizations."
84-
Projects = "Project operations."
85-
ProjectsLong = "Create, list and manage your MongoDB projects."
86-
CreateProject = "Create a project."
87-
DeleteProject = "Delete a project."
88-
ListProjects = "List projects."
89-
OpsManager = "Ops Manager operations."
90-
ListGlobalAlerts = "List global alerts."
91-
Automation = "Manage Ops Manager automation config."
92-
ShowAutomationStatus = "Show the current status of the automation config."
93-
Global = "Manage Ops Manager global properties."
94-
Owner = "Manage Ops Manager owners."
95-
CreateOwner = "Create the first user for Ops Manager."
96-
Servers = "Manage Ops Manager servers."
97-
ListServer = "List all available servers running an automation agent for the given project."
98-
Security = "Manage clusters security configuration."
99-
EnableSecurity = "Enable authentication mechanisms for the project."
100-
Events = "Manage events for your project."
101-
Measurements = "Get measurements on the state of the MongoDB process."
56+
CreateDBUser = "Create a database user for a project."
57+
DeleteDBUser = "Delete a database user for a project."
58+
ListDBUsers = "List Atlas database users for a project."
59+
ListEvents = "List events for an organization or project"
60+
UpdateDBUser = "Update a MongoDB dbuser in Atlas."
61+
ProcessMeasurements = "Get measurements for a given host."
62+
Disks = "List available disks or disks measurements for a given host."
63+
ListDisks = "List available disks for a given host."
64+
DescribeDisks = "Describe disks measurements for a given host partition."
65+
Databases = "List available databases or databases measurements for a given host."
66+
ListDatabases = "List available databases for a given host."
67+
Whitelist = "Manage the IP whitelist for a project."
68+
CreateWhitelist = "Create an IP whitelist for a project."
69+
DeleteWhitelist = "Delete a database user for a project."
70+
DescribeWhitelist = "Describe an Atlas whitelist."
71+
ListWhitelist = "List Atlas whitelist for a project."
72+
CloudManager = "Cloud Manager operations."
73+
ShutdownOMCluster = "Shutdown a Cloud Manager cluster."
74+
StartUpOMCluster = "Startup a Cloud Manager cluster."
75+
UpdateOMCluster = "Update a Cloud Manager cluster."
76+
ConfigDescription = "Configure the tool."
77+
SetConfig = "Configure the tool."
78+
IAM = "Authentication operations."
79+
Organization = "Organization operations."
80+
OrganizationLong = "Create, list and manage your MongoDB organizations."
81+
CreateOrganization = "Create an organization."
82+
DeleteOrganization = "Delete an organization."
83+
ListOrganizations = "List organizations."
84+
Projects = "Project operations."
85+
ProjectsLong = "Create, list and manage your MongoDB projects."
86+
CreateProject = "Create a project."
87+
DeleteProject = "Delete a project."
88+
ListProjects = "List projects."
89+
OpsManager = "Ops Manager operations."
90+
ListGlobalAlerts = "List global alerts."
91+
Automation = "Manage Ops Manager automation config."
92+
ShowAutomationStatus = "Show the current status of the automation config."
93+
Global = "Manage Ops Manager global properties."
94+
LogCollection = "Manage log collection jobs."
95+
StartLogCollectionJob = "Start a job to collect logs."
96+
Owner = "Manage Ops Manager owners."
97+
CreateOwner = "Create the first user for Ops Manager."
98+
Servers = "Manage Ops Manager servers."
99+
ListServer = "List all available servers running an automation agent for the given project."
100+
Security = "Manage clusters security configuration."
101+
EnableSecurity = "Enable authentication mechanisms for the project."
102+
Events = "Manage events for your project."
103+
Measurements = "Get measurements on the state of the MongoDB process."
102104
)

internal/flags/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,6 @@ const (
115115
Normalization = "normalization" // Normalization flag
116116
Backwards = "backwards" // Backwards flag
117117
Strength = "strength" // Strength flag
118+
SizeRequestedPerFileBytes = "sizeRequestedPerFileBytes" //SizeRequestedPerFileBytes flag
119+
Redacted = "redacted" // Redacted flag
118120
)

internal/mocks/mock_logs.go

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

0 commit comments

Comments
 (0)