Skip to content

Commit 2523385

Browse files
CLOUDP-57849:List events, Atlas (#81)
1 parent d61513d commit 2523385

File tree

12 files changed

+545
-1
lines changed

12 files changed

+545
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ gen-mocks: ## Generate mocks
6464
mockgen -source=internal/store/checkpoints.go -destination=internal/mocks/mock_checkpoints.go -package=mocks
6565
mockgen -source=internal/store/alerts.go -destination=internal/mocks/mock_alerts.go -package=mocks
6666
mockgen -source=internal/store/global_alerts.go -destination=internal/mocks/mock_global_alerts.go -package=mocks
67+
mockgen -source=internal/store/events.go -destination=internal/mocks/mock_events.go -package=mocks
6768

6869
.PHONY: build
6970
build: ## Generate a binary in ./bin

e2e/atlas_events_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
// +build e2e
15+
16+
package e2e_test
17+
18+
import (
19+
"encoding/json"
20+
"os"
21+
"os/exec"
22+
"path/filepath"
23+
"testing"
24+
25+
"github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
26+
)
27+
28+
func TestAtlasEvents(t *testing.T) {
29+
cliPath, err := filepath.Abs("../bin/mongocli")
30+
if err != nil {
31+
t.Fatalf("unexpected error: %v", err)
32+
}
33+
_, err = os.Stat(cliPath)
34+
35+
if err != nil {
36+
t.Fatalf("unexpected error: %v", err)
37+
}
38+
39+
atlasEntity := "atlas"
40+
eventsEntity := "events"
41+
42+
t.Run("ListProjectEvent", func(t *testing.T) {
43+
44+
cmd := exec.Command(cliPath,
45+
atlasEntity,
46+
eventsEntity,
47+
"list",
48+
"--projectId=5e429f2e06822c6eac4d59c9",
49+
)
50+
51+
cmd.Env = os.Environ()
52+
resp, err := cmd.CombinedOutput()
53+
54+
if err != nil {
55+
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
56+
}
57+
58+
events := mongodbatlas.EventResponse{}
59+
err = json.Unmarshal(resp, &events)
60+
61+
if err != nil {
62+
t.Fatalf("unexpected error: %v", err)
63+
}
64+
65+
if len(events.Results) == 0 {
66+
t.Errorf("got=%#v\nwant>0\n", len(events.Results))
67+
}
68+
69+
})
70+
71+
t.Run("ListOrganizationEvent", func(t *testing.T) {
72+
73+
cmd := exec.Command(cliPath,
74+
atlasEntity,
75+
eventsEntity,
76+
"list",
77+
"--orgId=5e429e7706822c6eac4d5970",
78+
"--minDate=2020-04-01",
79+
)
80+
81+
cmd.Env = os.Environ()
82+
resp, err := cmd.CombinedOutput()
83+
84+
if err != nil {
85+
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
86+
}
87+
88+
events := mongodbatlas.EventResponse{}
89+
err = json.Unmarshal(resp, &events)
90+
91+
if err != nil {
92+
t.Fatalf("unexpected error: %v", err)
93+
}
94+
95+
if len(events.Results) == 0 {
96+
t.Errorf("got=%#v\nwant>0\n", len(events.Results))
97+
}
98+
99+
})
100+
101+
}

internal/cli/atlas.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func AtlasBuilder() *cobra.Command {
2929
cmd.AddCommand(AtlasWhitelistBuilder())
3030
cmd.AddCommand(AtlasAlertsBuilder())
3131
cmd.AddCommand(AtlasBackupsBuilder())
32+
cmd.AddCommand(AtlasEventsBuilder())
3233

3334
return cmd
3435
}

internal/cli/atlas_events.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 AtlasEventsBuilder() *cobra.Command {
23+
cmd := &cobra.Command{
24+
Use: "events",
25+
Aliases: []string{"event"},
26+
Short: description.Events,
27+
}
28+
29+
cmd.AddCommand(AtlasEventsListBuilder())
30+
31+
return cmd
32+
}

internal/cli/atlas_events_list.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
"errors"
18+
19+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
20+
"github.com/mongodb/mongocli/internal/description"
21+
"github.com/mongodb/mongocli/internal/flags"
22+
"github.com/mongodb/mongocli/internal/json"
23+
"github.com/mongodb/mongocli/internal/store"
24+
"github.com/mongodb/mongocli/internal/usage"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
type atlasEventsListOpts struct {
29+
*globalOpts
30+
pageNum int
31+
itemsPerPage int
32+
eventType string
33+
minDate string
34+
maxDate string
35+
store store.EventsStore
36+
}
37+
38+
func (opts *atlasEventsListOpts) init() error {
39+
var err error
40+
opts.store, err = store.New()
41+
return err
42+
}
43+
44+
func (opts *atlasEventsListOpts) Run() error {
45+
listOpts := opts.newEventListOptions()
46+
47+
var result *atlas.EventResponse
48+
var err error
49+
50+
if opts.orgID != "" {
51+
result, err = opts.store.OrganizationEvents(opts.orgID, listOpts)
52+
} else if opts.projectID != "" {
53+
result, err = opts.store.ProjectEvents(opts.projectID, &listOpts.ListOptions)
54+
} else {
55+
return errors.New("--projectID or --orgID must be set")
56+
}
57+
58+
if err != nil {
59+
return err
60+
}
61+
62+
return json.PrettyPrint(result)
63+
}
64+
65+
func (opts *atlasEventsListOpts) newEventListOptions() *atlas.EventListOptions {
66+
67+
return &atlas.EventListOptions{
68+
ListOptions: atlas.ListOptions{
69+
PageNum: opts.pageNum,
70+
ItemsPerPage: opts.itemsPerPage,
71+
},
72+
EventType: opts.eventType,
73+
MinDate: opts.minDate,
74+
MaxDate: opts.maxDate,
75+
}
76+
}
77+
78+
// mongocli atlas event(s) list [--projectId projectId] [--source source][--type type] [--page N] [--limit N] [--mindate minDate] [--maxDate maxDate]
79+
func AtlasEventsListBuilder() *cobra.Command {
80+
opts := &atlasEventsListOpts{
81+
globalOpts: newGlobalOpts(),
82+
}
83+
cmd := &cobra.Command{
84+
Use: "list",
85+
Short: description.ListEvents,
86+
Aliases: []string{"ls"},
87+
Args: cobra.NoArgs,
88+
PreRunE: func(cmd *cobra.Command, args []string) error {
89+
return opts.init()
90+
},
91+
RunE: func(cmd *cobra.Command, args []string) error {
92+
return opts.Run()
93+
},
94+
}
95+
96+
cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
97+
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
98+
99+
cmd.Flags().StringVar(&opts.eventType, flags.Type, "", usage.Event)
100+
cmd.Flags().StringVar(&opts.maxDate, flags.MaxDate, "", usage.MaxDate)
101+
cmd.Flags().StringVar(&opts.minDate, flags.MinDate, "", usage.MaxDate)
102+
103+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
104+
cmd.Flags().StringVar(&opts.orgID, flags.OrgID, "", usage.OrgID)
105+
106+
return cmd
107+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golang/mock/gomock"
7+
"github.com/mongodb/mongocli/internal/fixtures"
8+
"github.com/mongodb/mongocli/internal/mocks"
9+
)
10+
11+
func TestAtlasOrganizationEventsList_Run(t *testing.T) {
12+
ctrl := gomock.NewController(t)
13+
mockStore := mocks.NewMockEventsStore(ctrl)
14+
15+
defer ctrl.Finish()
16+
17+
expected := fixtures.Events()
18+
19+
listOpts := &atlasEventsListOpts{
20+
globalOpts: newGlobalOpts(),
21+
store: mockStore,
22+
}
23+
listOpts.orgID = "1"
24+
25+
mockStore.
26+
EXPECT().OrganizationEvents(listOpts.orgID, listOpts.newEventListOptions()).
27+
Return(expected, nil).
28+
Times(1)
29+
30+
err := listOpts.Run()
31+
if err != nil {
32+
t.Fatalf("Run() unexpected error: %v", err)
33+
}
34+
}
35+
36+
func TestAtlasProjectEventsList_Run(t *testing.T) {
37+
ctrl := gomock.NewController(t)
38+
mockStore := mocks.NewMockEventsStore(ctrl)
39+
40+
defer ctrl.Finish()
41+
42+
expected := fixtures.Events()
43+
44+
listOpts := &atlasEventsListOpts{
45+
globalOpts: newGlobalOpts(),
46+
store: mockStore,
47+
}
48+
49+
listOpts.projectID = "1"
50+
mockStore.
51+
EXPECT().ProjectEvents(listOpts.projectID, &listOpts.newEventListOptions().ListOptions).
52+
Return(expected, nil).
53+
Times(1)
54+
55+
err := listOpts.Run()
56+
if err != nil {
57+
t.Fatalf("Run() unexpected error: %v", err)
58+
}
59+
}

internal/description/description.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ A user’s roles apply to all the clusters in the project.`
5151
CreateDBUser = "Create a database user for a project."
5252
DeleteDBUser = "Delete a database user for a project."
5353
ListDBUsers = "List Atlas database users for a project."
54+
ListEvents = "List events for an organization or project"
5455
UpdateDBUser = "Update a MongoDB dbuser in Atlas."
5556
Whitelist = "Manage the IP whitelist for a project."
5657
CreateWhitelist = "Create an IP whitelist for a project."
@@ -85,4 +86,5 @@ A user’s roles apply to all the clusters in the project.`
8586
ListServer = "List all available servers running an automation agent for the given project."
8687
Security = "Manage clusters security configuration."
8788
EnableSecurity = "Enable authentication mechanisms for the project."
89+
Events = "Manage events for your project."
8890
)

internal/fixtures/events.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package fixtures
2+
3+
import atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
4+
5+
func Event() *atlas.Event {
6+
return &atlas.Event{
7+
Created: "2018-06-19T15:06:15Z",
8+
EventTypeName: "JOINED_ORG",
9+
ID: "5b48f4d2d7e33a1c0c60597e",
10+
IsGlobalAdmin: false,
11+
Links: []*atlas.Link{
12+
{
13+
Rel: "http://mms.mongodb.com/org",
14+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/5b478b3afc4625789ce616a3",
15+
},
16+
{
17+
Rel: "http://mms.mongodb.com/org",
18+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/users/6b610e1087d9d66b272f0c86",
19+
},
20+
{
21+
Rel: "http://mms.mongodb.com/org",
22+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/5b478b3afc4625789ce616a3/events/5b48f4d2d7e33a1c0c60597e",
23+
},
24+
},
25+
OrgID: "5b478b3afc4625789ce616a3",
26+
RemoteAddress: "198.51.100.64",
27+
TargetUsername: "[email protected]",
28+
UserID: "6b610e1087d9d66b272f0c86",
29+
Username: "[email protected]",
30+
}
31+
}
32+
33+
func Events() *atlas.EventResponse {
34+
return &atlas.EventResponse{
35+
Links: []*atlas.Link{},
36+
Results: []*atlas.Event{Event()},
37+
TotalCount: 1,
38+
}
39+
}

internal/flags/flags.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ const (
8888
PointInTimeUTCMillis = "pointInTimeUTCMillis" // PointInTimeUTCMillis flag
8989
Expires = "expires" // Expires flag
9090
MaxDownloads = "maxDownloads" // MaxDownloads flag
91-
ExpirationHours = "expirationHours" // ExpirationHours
91+
ExpirationHours = "expirationHours" // ExpirationHours flag
92+
MaxDate = "maxDate" // MaxDate flag
93+
MinDate = "minDate" // MinDate flag
9294
)

0 commit comments

Comments
 (0)