Skip to content

CLOUDP-60956: mongocli om logs download #128

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 20 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b1d8819
CLOUDP-60954 mongocli om logs collect
andreaangiolillo Apr 24, 2020
9c6b950
CLOUDP-60955 mongocli om logs list
andreaangiolillo Apr 24, 2020
eba6779
Updated OM client to v0.2.1
andreaangiolillo Apr 24, 2020
603f09f
Addressed PR comments
andreaangiolillo Apr 24, 2020
3fe95ea
Merge remote-tracking branch 'origin/CLOUDP-60954' into CLOUDP-60955
andreaangiolillo Apr 24, 2020
db86d82
Added unit test
andreaangiolillo Apr 24, 2020
7d97232
Merge remote-tracking branch 'origin/master' into CLOUDP-60955
andreaangiolillo Apr 24, 2020
dd052bb
Refactoring
andreaangiolillo Apr 24, 2020
9468ba3
Generated mock
andreaangiolillo Apr 24, 2020
c014335
Making opsManagerLogsListOptsBuilder public
andreaangiolillo Apr 24, 2020
e787ab1
Update internal/cli/ops_manager_logs_list.go
andreaangiolillo Apr 24, 2020
2d83712
Starting implementing the download command
andreaangiolillo Apr 24, 2020
ec77d7e
Addressed PR comments
andreaangiolillo Apr 24, 2020
d21f971
Merge remote-tracking branch 'origin/CLOUDP-60955' into CLOUDP-60956
andreaangiolillo Apr 24, 2020
f5f841b
Merge remote-tracking branch 'origin/master' into CLOUDP-60956
andreaangiolillo Apr 24, 2020
d15a47b
CLOUDP-60956: mongocli om logs download
andreaangiolillo Apr 24, 2020
60fcdb7
Merge remote-tracking branch 'origin/master' into CLOUDP-60956
andreaangiolillo Apr 24, 2020
5f8cbc7
Added description
andreaangiolillo Apr 24, 2020
0cbf4ff
Create fs inside opsManagerLogsDownloadOpts
andreaangiolillo Apr 24, 2020
b4e699c
Refactoring
andreaangiolillo Apr 24, 2020
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
7 changes: 3 additions & 4 deletions internal/cli/atlas_logs_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ func AtlasLogsDownloadBuilder() *cobra.Command {
fs: afero.NewOsFs(),
}
cmd := &cobra.Command{
Use: "download [hostname] [logname]",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks

Short: description.ListDisks,
Aliases: []string{"ls"},
Args: cobra.ExactArgs(2),
Use: "download [hostname] [logname]",
Short: description.ListDisks,
Args: cobra.ExactArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
},
Expand Down
1 change: 1 addition & 0 deletions internal/cli/ops_manager_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func OpsManagerLogsBuilder() *cobra.Command {

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

return cmd
}
88 changes: 88 additions & 0 deletions internal/cli/ops_manager_logs_download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 (
"io"
"os"

"github.com/mongodb/mongocli/internal/description"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

type opsManagerLogsDownloadOpts struct {
globalOpts
id string
out string
fs afero.Fs
store store.LogJobsDownloader
}

func (opts *opsManagerLogsDownloadOpts) initStore() error {
var err error
opts.store, err = store.New()
return err
}

func (opts *opsManagerLogsDownloadOpts) Run() error {
out, err := opts.newWriteCloser()
if err != nil {
return err
}

if err := opts.store.DownloadLogJob(opts.ProjectID(), opts.id, out); err != nil {
return err
}

return nil
}

func (opts *opsManagerLogsDownloadOpts) newWriteCloser() (io.WriteCloser, error) {
// Create file only if is not there already (don't overwrite)
ff := os.O_CREATE | os.O_TRUNC | os.O_WRONLY | os.O_EXCL
f, err := opts.fs.OpenFile(opts.out, ff, 0777)
return f, err
}

// mongocli om logs download id [--out out] [--projectId projectId]
func OpsManagerLogsDownloadOptsBuilder() *cobra.Command {
opts := &opsManagerLogsDownloadOpts{
fs: afero.NewOsFs(),
}
cmd := &cobra.Command{
Use: "download [id]",
Short: description.DownloadLogs,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.id = args[0]
return opts.Run()
},
}

cmd.Flags().StringVarP(&opts.out, flags.Out, flags.OutShort, "", usage.LogOut)

cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

_ = cmd.MarkFlagRequired(flags.Out)

return cmd
}
51 changes: 51 additions & 0 deletions internal/cli/ops_manager_logs_download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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"
"github.com/mongodb/mongocli/internal/mocks"
"github.com/spf13/afero"
)

func TestOpsManagerLogsDownloadOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockLogJobsDownloader(ctrl)

appFS := afero.NewMemMapFs()

opts := &opsManagerLogsDownloadOpts{
id: "1",
fs: appFS,
store: mockStore,
}

f, err := opts.newWriteCloser()
if err != nil {
t.Fatalf("newWriteCloser() unexpected error: %v", err)
}

mockStore.
EXPECT().
DownloadLogJob(opts.projectID, opts.id, f).
Return(nil).
Times(1)

if err := opts.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 @@ -84,6 +84,7 @@ const (
CreateProject = "Create a project."
DeleteProject = "Delete a project."
ListProjects = "List projects."
DownloadLogs = "Download logs from a log collection job."
OpsManager = "Ops Manager operations."
ListGlobalAlerts = "List global alerts."
Automation = "Manage Ops Manager automation config."
Expand Down
2 changes: 1 addition & 1 deletion internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ const (
Strength = "strength" // Strength flag
SizeRequestedPerFileBytes = "sizeRequestedPerFileBytes" //SizeRequestedPerFileBytes flag
Redacted = "redacted" // Redacted flag
Verbose = "verbose"
Verbose = "verbose" // Verbose flag
)
37 changes: 37 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.

15 changes: 15 additions & 0 deletions internal/store/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type LogsDownloader interface {
DownloadLog(string, string, string, io.Writer, *atlas.DateRangetOptions) error
}

type LogJobsDownloader interface {
DownloadLogJob(string, string, io.Writer) error
}

type LogCollector interface {
Collect(string, *om.LogCollectionJob) (*om.LogCollectionJob, error)
}
Expand Down Expand Up @@ -68,3 +72,14 @@ func (s *Store) DownloadLog(groupID, host, name string, out io.Writer, opts *atl
return fmt.Errorf("unsupported service: %s", s.service)
}
}

// DownloadLogJob encapsulate the logic to manage different cloud providers
func (s *Store) DownloadLogJob(groupID, jobID string, out io.Writer) error {
switch s.service {
case config.OpsManagerService, config.CloudManagerService:
_, err := s.client.(*om.Client).Logs.Download(context.Background(), groupID, jobID, out)
return err
default:
return fmt.Errorf("unsupported service: %s", s.service)
}
}