Skip to content

CLOUDP-57523: Download logs, Atlas #102

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 3 commits into from
Apr 15, 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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ gen-mocks: ## Generate mocks
mockgen -source=internal/store/host_measurements.go -destination=internal/mocks/mock_host_measurements.go -package=mocks
mockgen -source=internal/store/indexes.go -destination=internal/mocks/mock_indexes.go -package=mocks
mockgen -source=internal/store/processes.go -destination=internal/mocks/mock_processes.go -package=mocks
mockgen -source=internal/store/logs.go -destination=internal/mocks/mock_logs.go -package=mocks

.PHONY: build
build: ## Generate a binary in ./bin
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408164237-a1a83bd4824f
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200413163719-5574d7cba634
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200409122000-4fea663b73d0
github.com/mwielbut/pointy v1.1.0
github.com/pelletier/go-toml v1.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408153549-57b3e94c997a h1:imwIzgvl404y26TYER7vMyypu1YFMNr5g86LzeJ8GfQ=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408153549-57b3e94c997a/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408164237-a1a83bd4824f h1:xUg4b6T4XXTICPtKLbsJAUIREHWTGewXc2IS3U5CUEc=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408164237-a1a83bd4824f/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200413163719-5574d7cba634 h1:MYBRHl5kH0XezQeeP5VWxIbOEsEr/3pjjtEbkT928wI=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200413163719-5574d7cba634/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200409122000-4fea663b73d0 h1:N3t4AFyZOUHKOs3HSf7DG+DpPtPlE2zG5LyblXuwPms=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200409122000-4fea663b73d0/go.mod h1:mgMLdoXItYPnE+uv0CDYuc//lor7azeIv3gDCpjsduY=
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func AtlasBuilder() *cobra.Command {
cmd.AddCommand(AtlasBackupsBuilder())
cmd.AddCommand(AtlasEventsBuilder())
cmd.AddCommand(AtlasMeasurementsBuilder())
// cmd.AddCommand(AtlasLogsBuilder())
cmd.AddCommand(AtlasLogsBuilder())
cmd.AddCommand(AtlasProcessesBuilder())

return cmd
Expand Down
31 changes: 31 additions & 0 deletions internal/cli/atlas_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 (
"github.com/mongodb/mongocli/internal/description"
"github.com/spf13/cobra"
)

func AtlasLogsBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "logs",
Aliases: []string{"log"},
Short: description.Logs,
}
cmd.AddCommand(AtlasLogsDownloadBuilder())

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

atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
"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 atlasLogsDownloadOpts struct {
globalOpts
host string
name string
out string
start string
end string
fs afero.Fs
store store.LogsDownloader
}

func (opts *atlasLogsDownloadOpts) init() error {
if opts.ProjectID() == "" {
return errMissingProjectID
}

var err error
opts.store, err = store.New()
return err
}

func (opts *atlasLogsDownloadOpts) Run() error {
f, err := opts.newWriteCloser()
if err != nil {
return err
}
defer f.Close()

r := opts.newDateRangeOpts()
if err := opts.store.DownloadLog(opts.ProjectID(), opts.host, opts.name, f, r); err != nil {
return err
}
return nil
}

func (opts *atlasLogsDownloadOpts) output() string {
if opts.out == "" {
opts.out = opts.name
}
return opts.out
}

func (opts *atlasLogsDownloadOpts) 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

😮 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a comment to this as it's a bit mystical

f, err := opts.fs.OpenFile(opts.output(), ff, 0777)
return f, err
}

func (opts *atlasLogsDownloadOpts) newDateRangeOpts() *atlas.DateRangetOptions {
return &atlas.DateRangetOptions{
StartDate: opts.start,
EndDate: opts.end,
}
}

// mongocli atlas logs download [hostname] [logname] [--type type] [--output destination] [--projectId projectId]
func AtlasLogsDownloadBuilder() *cobra.Command {
opts := &atlasLogsDownloadOpts{
fs: afero.NewOsFs(),
}
cmd := &cobra.Command{
Use: "download [hostname] [logname]",
Short: description.ListDisks,
Aliases: []string{"ls"},
Args: cobra.ExactArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.host = args[0]
opts.name = args[1]

return opts.Run()
},
}

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

cmd.Flags().StringVar(&opts.start, flags.Start, "", usage.Start)
cmd.Flags().StringVar(&opts.end, flags.End, "", usage.End)
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)

return cmd
}
51 changes: 51 additions & 0 deletions internal/cli/atlas_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 TestAtlasLogsDownloadOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockLogsDownloader(ctrl)

appFS := afero.NewMemMapFs()

opts := &atlasLogsDownloadOpts{
name: "mongo.gz",
fs: appFS,
store: mockStore,
}

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

mockStore.
EXPECT().
DownloadLog(opts.projectID, opts.host, opts.name, f, opts.newDateRangeOpts()).
Return(nil).
Times(1)

if err := opts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
1 change: 0 additions & 1 deletion internal/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ func (p Profile) Save() error {
return err
}
}
// TODO: We can now read but not write, see https://github.com/spf13/viper/pull/813
configFile := fmt.Sprintf("%s/%s.toml", p.configDir, Name)
return viper.WriteConfigAs(configFile)
}
2 changes: 2 additions & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
Period = "period" // Period flag
PasswordShort = "p" // PasswordShort flag
Email = "email" // Email flag
Out = "out" // Out flag
OutShort = "o" // OutShort flag
Status = "status" // Status flag
Start = "start" // Start flag
End = "end" // End flag
Expand Down
49 changes: 49 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.

39 changes: 39 additions & 0 deletions internal/store/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 store

import (
"context"
"fmt"
"io"

atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
"github.com/mongodb/mongocli/internal/config"
)

type LogsDownloader interface {
DownloadLog(string, string, string, io.Writer, *atlas.DateRangetOptions) error
}

// ProcessDisks encapsulate the logic to manage different cloud providers
func (s *Store) DownloadLog(groupID, host, name string, out io.Writer, opts *atlas.DateRangetOptions) error {
switch s.service {
case config.CloudService:
_, err := s.client.(*atlas.Client).Logs.Get(context.Background(), groupID, host, name, out, opts)
return err
default:
return fmt.Errorf("unsupported service: %s", s.service)
}
}