Skip to content

Commit f9db3e5

Browse files
authored
CLOUDP-57523: Download logs, Atlas (#102)
1 parent 0846f94 commit f9db3e5

File tree

11 files changed

+291
-5
lines changed

11 files changed

+291
-5
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ gen-mocks: ## Generate mocks
7272
mockgen -source=internal/store/host_measurements.go -destination=internal/mocks/mock_host_measurements.go -package=mocks
7373
mockgen -source=internal/store/indexes.go -destination=internal/mocks/mock_indexes.go -package=mocks
7474
mockgen -source=internal/store/processes.go -destination=internal/mocks/mock_processes.go -package=mocks
75+
mockgen -source=internal/store/logs.go -destination=internal/mocks/mock_logs.go -package=mocks
7576

7677
.PHONY: build
7778
build: ## Generate a binary in ./bin

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.4 // indirect
1212
github.com/mattn/go-isatty v0.0.12 // indirect
1313
github.com/mitchellh/go-homedir v1.1.0
14-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408164237-a1a83bd4824f
14+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200413163719-5574d7cba634
1515
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200409122000-4fea663b73d0
1616
github.com/mwielbut/pointy v1.1.0
1717
github.com/pelletier/go-toml v1.6.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
102102
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
103103
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408153549-57b3e94c997a h1:imwIzgvl404y26TYER7vMyypu1YFMNr5g86LzeJ8GfQ=
104104
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408153549-57b3e94c997a/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
105-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408164237-a1a83bd4824f h1:xUg4b6T4XXTICPtKLbsJAUIREHWTGewXc2IS3U5CUEc=
106-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200408164237-a1a83bd4824f/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
105+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200413163719-5574d7cba634 h1:MYBRHl5kH0XezQeeP5VWxIbOEsEr/3pjjtEbkT928wI=
106+
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200413163719-5574d7cba634/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
107107
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200409122000-4fea663b73d0 h1:N3t4AFyZOUHKOs3HSf7DG+DpPtPlE2zG5LyblXuwPms=
108108
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200409122000-4fea663b73d0/go.mod h1:mgMLdoXItYPnE+uv0CDYuc//lor7azeIv3gDCpjsduY=
109109
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=

internal/cli/atlas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func AtlasBuilder() *cobra.Command {
3131
cmd.AddCommand(AtlasBackupsBuilder())
3232
cmd.AddCommand(AtlasEventsBuilder())
3333
cmd.AddCommand(AtlasMeasurementsBuilder())
34-
// cmd.AddCommand(AtlasLogsBuilder())
34+
cmd.AddCommand(AtlasLogsBuilder())
3535
cmd.AddCommand(AtlasProcessesBuilder())
3636

3737
return cmd

internal/cli/atlas_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 AtlasLogsBuilder() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "logs",
25+
Aliases: []string{"log"},
26+
Short: description.Logs,
27+
}
28+
cmd.AddCommand(AtlasLogsDownloadBuilder())
29+
30+
return cmd
31+
}

internal/cli/atlas_logs_download.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
"io"
19+
"os"
20+
21+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
22+
"github.com/mongodb/mongocli/internal/description"
23+
"github.com/mongodb/mongocli/internal/flags"
24+
"github.com/mongodb/mongocli/internal/store"
25+
"github.com/mongodb/mongocli/internal/usage"
26+
"github.com/spf13/afero"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
type atlasLogsDownloadOpts struct {
31+
globalOpts
32+
host string
33+
name string
34+
out string
35+
start string
36+
end string
37+
fs afero.Fs
38+
store store.LogsDownloader
39+
}
40+
41+
func (opts *atlasLogsDownloadOpts) init() error {
42+
if opts.ProjectID() == "" {
43+
return errMissingProjectID
44+
}
45+
46+
var err error
47+
opts.store, err = store.New()
48+
return err
49+
}
50+
51+
func (opts *atlasLogsDownloadOpts) Run() error {
52+
f, err := opts.newWriteCloser()
53+
if err != nil {
54+
return err
55+
}
56+
defer f.Close()
57+
58+
r := opts.newDateRangeOpts()
59+
if err := opts.store.DownloadLog(opts.ProjectID(), opts.host, opts.name, f, r); err != nil {
60+
return err
61+
}
62+
return nil
63+
}
64+
65+
func (opts *atlasLogsDownloadOpts) output() string {
66+
if opts.out == "" {
67+
opts.out = opts.name
68+
}
69+
return opts.out
70+
}
71+
72+
func (opts *atlasLogsDownloadOpts) newWriteCloser() (io.WriteCloser, error) {
73+
// Create file only if is not there already (don't overwrite)
74+
ff := os.O_CREATE | os.O_TRUNC | os.O_WRONLY | os.O_EXCL
75+
f, err := opts.fs.OpenFile(opts.output(), ff, 0777)
76+
return f, err
77+
}
78+
79+
func (opts *atlasLogsDownloadOpts) newDateRangeOpts() *atlas.DateRangetOptions {
80+
return &atlas.DateRangetOptions{
81+
StartDate: opts.start,
82+
EndDate: opts.end,
83+
}
84+
}
85+
86+
// mongocli atlas logs download [hostname] [logname] [--type type] [--output destination] [--projectId projectId]
87+
func AtlasLogsDownloadBuilder() *cobra.Command {
88+
opts := &atlasLogsDownloadOpts{
89+
fs: afero.NewOsFs(),
90+
}
91+
cmd := &cobra.Command{
92+
Use: "download [hostname] [logname]",
93+
Short: description.ListDisks,
94+
Aliases: []string{"ls"},
95+
Args: cobra.ExactArgs(2),
96+
PreRunE: func(cmd *cobra.Command, args []string) error {
97+
return opts.init()
98+
},
99+
RunE: func(cmd *cobra.Command, args []string) error {
100+
opts.host = args[0]
101+
opts.name = args[1]
102+
103+
return opts.Run()
104+
},
105+
}
106+
107+
cmd.Flags().StringVarP(&opts.out, flags.Out, flags.OutShort, "", usage.End)
108+
109+
cmd.Flags().StringVar(&opts.start, flags.Start, "", usage.Start)
110+
cmd.Flags().StringVar(&opts.end, flags.End, "", usage.End)
111+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
112+
113+
return cmd
114+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
"testing"
19+
20+
"github.com/golang/mock/gomock"
21+
"github.com/mongodb/mongocli/internal/mocks"
22+
"github.com/spf13/afero"
23+
)
24+
25+
func TestAtlasLogsDownloadOpts_Run(t *testing.T) {
26+
ctrl := gomock.NewController(t)
27+
mockStore := mocks.NewMockLogsDownloader(ctrl)
28+
29+
appFS := afero.NewMemMapFs()
30+
31+
opts := &atlasLogsDownloadOpts{
32+
name: "mongo.gz",
33+
fs: appFS,
34+
store: mockStore,
35+
}
36+
37+
f, err := opts.newWriteCloser()
38+
if err != nil {
39+
t.Fatalf("newWriteCloser() unexpected error: %v", err)
40+
}
41+
42+
mockStore.
43+
EXPECT().
44+
DownloadLog(opts.projectID, opts.host, opts.name, f, opts.newDateRangeOpts()).
45+
Return(nil).
46+
Times(1)
47+
48+
if err := opts.Run(); err != nil {
49+
t.Fatalf("Run() unexpected error: %v", err)
50+
}
51+
}

internal/config/profile.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ func (p Profile) Save() error {
192192
return err
193193
}
194194
}
195-
// TODO: We can now read but not write, see https://github.com/spf13/viper/pull/813
196195
configFile := fmt.Sprintf("%s/%s.toml", p.configDir, Name)
197196
return viper.WriteConfigAs(configFile)
198197
}

internal/flags/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const (
3636
Period = "period" // Period flag
3737
PasswordShort = "p" // PasswordShort flag
3838
Email = "email" // Email flag
39+
Out = "out" // Out flag
40+
OutShort = "o" // OutShort flag
3941
Status = "status" // Status flag
4042
Start = "start" // Start flag
4143
End = "end" // End flag

internal/mocks/mock_logs.go

Lines changed: 49 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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 store
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"io"
21+
22+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
23+
"github.com/mongodb/mongocli/internal/config"
24+
)
25+
26+
type LogsDownloader interface {
27+
DownloadLog(string, string, string, io.Writer, *atlas.DateRangetOptions) error
28+
}
29+
30+
// ProcessDisks encapsulate the logic to manage different cloud providers
31+
func (s *Store) DownloadLog(groupID, host, name string, out io.Writer, opts *atlas.DateRangetOptions) error {
32+
switch s.service {
33+
case config.CloudService:
34+
_, err := s.client.(*atlas.Client).Logs.Get(context.Background(), groupID, host, name, out, opts)
35+
return err
36+
default:
37+
return fmt.Errorf("unsupported service: %s", s.service)
38+
}
39+
}

0 commit comments

Comments
 (0)