Skip to content

Commit 40016ce

Browse files
CLOUDP-57846: List global alerts in mongocli (#67)
1 parent 4efe5cb commit 40016ce

File tree

10 files changed

+330
-4
lines changed

10 files changed

+330
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ gen-mocks: ## Generate mocks
6363
mockgen -source=internal/store/agents.go -destination=internal/mocks/mock_agents.go -package=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
66+
mockgen -source=internal/store/global_alerts.go -destination=internal/mocks/mock_global_alerts.go -package=mocks
6667

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

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/mattn/go-isatty v0.0.12 // indirect
1313
github.com/mitchellh/go-homedir v1.1.0
1414
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200323160302-12cb92c69adf
15-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200324155204-287d08b2e57b
15+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325124453-024ddde724d1
1616
github.com/mwielbut/pointy v1.1.0
1717
github.com/pelletier/go-toml v1.6.0 // indirect
1818
github.com/spf13/afero v1.2.2

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
100100
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
101101
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
102102
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
103-
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318163834-8b6a5ea6eb67 h1:YKz/n9K99mUOxs/pX+vpzNvbSFr56vObu2+O33upiFo=
104103
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318163834-8b6a5ea6eb67/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
105104
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200323160302-12cb92c69adf h1:dLZh7ukw/hwt6Rnjer/Zjg53spl+YfLn1N8bAnoylu8=
106105
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200323160302-12cb92c69adf/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
107-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200324155204-287d08b2e57b h1:qMUzHZ6pJ5nk/GLyxbqPqkW7eAZoKRjNKnaCQpHoC0M=
108-
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200324155204-287d08b2e57b/go.mod h1:FhMID1fXaHZpvHPfUhcDL3pfc5JrBB+aD42HLgFoO+8=
106+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325124453-024ddde724d1 h1:jQoPSGW4a+8mvxBDms0O9QInSPF5HIxjAIdYPJ54mlo=
107+
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325124453-024ddde724d1/go.mod h1:FhMID1fXaHZpvHPfUhcDL3pfc5JrBB+aD42HLgFoO+8=
109108
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
110109
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
111110
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

internal/cli/atlas_alerts.go

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

3334
return cmd
3435
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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/go-client-mongodb-atlas/mongodbatlas"
19+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
20+
"github.com/mongodb/mongocli/internal/flags"
21+
"github.com/mongodb/mongocli/internal/json"
22+
"github.com/mongodb/mongocli/internal/store"
23+
"github.com/mongodb/mongocli/internal/usage"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
type opsManagerAlertsGlobalListOpts struct {
28+
store store.GlobalAlertLister
29+
pageNum int
30+
itemsPerPage int
31+
status string
32+
}
33+
34+
func (opts *opsManagerAlertsGlobalListOpts) init() error {
35+
var err error
36+
opts.store, err = store.New()
37+
return err
38+
}
39+
40+
func (opts *opsManagerAlertsGlobalListOpts) Run() error {
41+
alertOpts := &om.AlertsListOptions{
42+
Status: opts.status,
43+
ListOptions: mongodbatlas.ListOptions{
44+
PageNum: opts.pageNum,
45+
ItemsPerPage: opts.itemsPerPage,
46+
},
47+
}
48+
49+
result, err := opts.store.GlobalAlerts(alertOpts)
50+
if err != nil {
51+
return err
52+
}
53+
54+
return json.PrettyPrint(result)
55+
}
56+
57+
// mongocli om|cm alert(s) global list [--status status]
58+
func OpsManagerAlertsGlobalListBuilder() *cobra.Command {
59+
opts := &opsManagerAlertsGlobalListOpts{}
60+
cmd := &cobra.Command{
61+
Use: "list",
62+
Short: "List global alerts.",
63+
Aliases: []string{"ls"},
64+
Args: cobra.NoArgs,
65+
PreRunE: func(cmd *cobra.Command, args []string) error {
66+
return opts.init()
67+
},
68+
RunE: func(cmd *cobra.Command, args []string) error {
69+
return opts.Run()
70+
},
71+
}
72+
73+
cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
74+
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
75+
cmd.Flags().StringVar(&opts.status, flags.Status, "", usage.Status)
76+
77+
return cmd
78+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
22+
"github.com/mongodb/mongocli/internal/fixtures"
23+
"github.com/mongodb/mongocli/internal/mocks"
24+
)
25+
26+
func TestOpsManagerAlertsGlobalList_Run(t *testing.T) {
27+
ctrl := gomock.NewController(t)
28+
mockStore := mocks.NewMockGlobalAlertLister(ctrl)
29+
30+
defer ctrl.Finish()
31+
32+
expected := fixtures.GlobalAlerts()
33+
34+
alertOpts := om.AlertsListOptions{
35+
Status: "OPEN",
36+
}
37+
38+
mockStore.
39+
EXPECT().GlobalAlerts(&alertOpts).
40+
Return(expected, nil).
41+
Times(1)
42+
43+
opts := &opsManagerAlertsGlobalListOpts{
44+
store: mockStore,
45+
status: "OPEN",
46+
}
47+
48+
err := opts.Run()
49+
if err != nil {
50+
t.Fatalf("Run() unexpected error: %v", err)
51+
}
52+
}

internal/cli/ops_manager_global.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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/spf13/cobra"
19+
)
20+
21+
func OpsManagerGlobalBuilder() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "global",
24+
Short: "Manage Ops Manager global properties.",
25+
}
26+
27+
cmd.AddCommand(OpsManagerAlertsGlobalListBuilder())
28+
return cmd
29+
}

internal/fixtures/global_alerts.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package fixtures
2+
3+
import (
4+
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
5+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
6+
)
7+
8+
func GlobalAlert() *om.GlobalAlert {
9+
return &om.GlobalAlert{
10+
Alert: atlas.Alert{
11+
ID: "3b7d2de0a4b02fd2c98146de",
12+
GroupID: "1",
13+
AlertConfigID: "5730f5e1e4b030a9634a3f69",
14+
EventTypeName: "OPLOG_BEHIND",
15+
Created: "2016-10-09T06:16:36Z",
16+
Updated: "2016-10-10T22:03:11Z",
17+
Status: "OPEN",
18+
LastNotified: "2016-10-10T20:42:32Z",
19+
ReplicaSetName: "shardedCluster-shard-0",
20+
ClusterName: "shardedCluster",
21+
AcknowledgedUntil: "2016-11-01T00:00:00Z",
22+
AcknowledgingUsername: "[email protected]",
23+
},
24+
Tags: []string{},
25+
Links: []*atlas.Link{},
26+
SourceTypeName: "REPLICA_SET",
27+
ClusterID: "572a00f2e4b051814b144e90",
28+
}
29+
}
30+
31+
func GlobalAlerts() *om.GlobalAlerts {
32+
return &om.GlobalAlerts{
33+
Links: []*atlas.Link{},
34+
Results: []*om.GlobalAlert{GlobalAlert()},
35+
TotalCount: 1,
36+
}
37+
}

internal/mocks/mock_global_alerts.go

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

internal/store/global_alerts.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
21+
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
22+
"github.com/mongodb/mongocli/internal/config"
23+
)
24+
25+
type GlobalAlertLister interface {
26+
GlobalAlerts(*om.AlertsListOptions) (*om.GlobalAlerts, error)
27+
}
28+
29+
type GlobalAlertsStore interface {
30+
GlobalAlertLister
31+
}
32+
33+
// GlobalAlerts encapsulate the logic to manage different cloud providers
34+
func (s *Store) GlobalAlerts(opts *om.AlertsListOptions) (*om.GlobalAlerts, error) {
35+
switch s.service {
36+
case config.OpsManagerService:
37+
result, _, err := s.client.(*om.Client).GlobalAlerts.List(context.Background(), opts)
38+
return result, err
39+
default:
40+
return nil, fmt.Errorf("unsupported service: %s", s.service)
41+
}
42+
}

0 commit comments

Comments
 (0)