Skip to content

CLOUDP-57846: List global alerts in mongocli #67

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
Mar 25, 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 @@ -63,6 +63,7 @@ gen-mocks: ## Generate mocks
mockgen -source=internal/store/agents.go -destination=internal/mocks/mock_agents.go -package=mocks
mockgen -source=internal/store/checkpoints.go -destination=internal/mocks/mock_checkpoints.go -package=mocks
mockgen -source=internal/store/alerts.go -destination=internal/mocks/mock_alerts.go -package=mocks
mockgen -source=internal/store/global_alerts.go -destination=internal/mocks/mock_global_alerts.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 @@ -12,7 +12,7 @@ require (
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.20200323160302-12cb92c69adf
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200324155204-287d08b2e57b
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325124453-024ddde724d1
github.com/mwielbut/pointy v1.1.0
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/spf13/afero v1.2.2
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318163834-8b6a5ea6eb67 h1:YKz/n9K99mUOxs/pX+vpzNvbSFr56vObu2+O33upiFo=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200318163834-8b6a5ea6eb67/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200323160302-12cb92c69adf h1:dLZh7ukw/hwt6Rnjer/Zjg53spl+YfLn1N8bAnoylu8=
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200323160302-12cb92c69adf/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200324155204-287d08b2e57b h1:qMUzHZ6pJ5nk/GLyxbqPqkW7eAZoKRjNKnaCQpHoC0M=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200324155204-287d08b2e57b/go.mod h1:FhMID1fXaHZpvHPfUhcDL3pfc5JrBB+aD42HLgFoO+8=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325124453-024ddde724d1 h1:jQoPSGW4a+8mvxBDms0O9QInSPF5HIxjAIdYPJ54mlo=
github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325124453-024ddde724d1/go.mod h1:FhMID1fXaHZpvHPfUhcDL3pfc5JrBB+aD42HLgFoO+8=
github.com/mwielbut/pointy v1.1.0 h1:U5/YEfoIkaGCHv0St3CgjduqXID4FNRoyZgLM1kY9vg=
github.com/mwielbut/pointy v1.1.0/go.mod h1:MvvO+uMFj9T5DMda33HlvogsFBX7pWWKAkFIn4teYwY=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func AtlasAlertsBuilder() *cobra.Command {
cmd.AddCommand(AtlasAlertsDescribeBuilder())
cmd.AddCommand(AtlasAlertsListBuilder())
cmd.AddCommand(AtlasAlertsAcknowledgeBuilder())
cmd.AddCommand(OpsManagerGlobalBuilder())

return cmd
}
78 changes: 78 additions & 0 deletions internal/cli/ops_manager_alerts_global_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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/go-client-mongodb-atlas/mongodbatlas"
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/json"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type opsManagerAlertsGlobalListOpts struct {
store store.GlobalAlertLister
pageNum int
itemsPerPage int
status string
}

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

func (opts *opsManagerAlertsGlobalListOpts) Run() error {
alertOpts := &om.AlertsListOptions{
Status: opts.status,
ListOptions: mongodbatlas.ListOptions{
Copy link
Collaborator

Choose a reason for hiding this comment

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

just as FYI (no need for any change here) you can also do

alertOpts := &om.AlertsListOptions{}
alertOpts.PageNum = opts.pageNum

go has automatic delegation for embedded structs, this is just style and there's no advantage of one over the other, so just letting you know

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also for future work, we should do with pagination what we did with delete that we embed a struct with the common things that all paginated resources do

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will create the activity on Jira

PageNum: opts.pageNum,
ItemsPerPage: opts.itemsPerPage,
},
}

result, err := opts.store.GlobalAlerts(alertOpts)
if err != nil {
return err
}

return json.PrettyPrint(result)
}

// mongocli om|cm alert(s) global list [--status status]
func OpsManagerAlertsGlobalListBuilder() *cobra.Command {
opts := &opsManagerAlertsGlobalListOpts{}
cmd := &cobra.Command{
Use: "list",
Short: "List global alerts.",
Aliases: []string{"ls"},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)
cmd.Flags().StringVar(&opts.status, flags.Status, "", usage.Status)

return cmd
}
52 changes: 52 additions & 0 deletions internal/cli/ops_manager_alerts_global_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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"
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
"github.com/mongodb/mongocli/internal/fixtures"
"github.com/mongodb/mongocli/internal/mocks"
)

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

defer ctrl.Finish()

expected := fixtures.GlobalAlerts()

alertOpts := om.AlertsListOptions{
Status: "OPEN",
}

mockStore.
EXPECT().GlobalAlerts(&alertOpts).
Return(expected, nil).
Times(1)

opts := &opsManagerAlertsGlobalListOpts{
store: mockStore,
status: "OPEN",
}

err := opts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
29 changes: 29 additions & 0 deletions internal/cli/ops_manager_global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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/spf13/cobra"
)

func OpsManagerGlobalBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "global",
Short: "Manage Ops Manager global properties.",
}

cmd.AddCommand(OpsManagerAlertsGlobalListBuilder())
return cmd
}
37 changes: 37 additions & 0 deletions internal/fixtures/global_alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fixtures

import (
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
)

func GlobalAlert() *om.GlobalAlert {
return &om.GlobalAlert{
Alert: atlas.Alert{
ID: "3b7d2de0a4b02fd2c98146de",
GroupID: "1",
AlertConfigID: "5730f5e1e4b030a9634a3f69",
EventTypeName: "OPLOG_BEHIND",
Created: "2016-10-09T06:16:36Z",
Updated: "2016-10-10T22:03:11Z",
Status: "OPEN",
LastNotified: "2016-10-10T20:42:32Z",
ReplicaSetName: "shardedCluster-shard-0",
ClusterName: "shardedCluster",
AcknowledgedUntil: "2016-11-01T00:00:00Z",
AcknowledgingUsername: "[email protected]",
},
Tags: []string{},
Links: []*atlas.Link{},
SourceTypeName: "REPLICA_SET",
ClusterID: "572a00f2e4b051814b144e90",
}
}

func GlobalAlerts() *om.GlobalAlerts {
return &om.GlobalAlerts{
Links: []*atlas.Link{},
Results: []*om.GlobalAlert{GlobalAlert()},
TotalCount: 1,
}
}
87 changes: 87 additions & 0 deletions internal/mocks/mock_global_alerts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr"
"github.com/mongodb/mongocli/internal/config"
)

type GlobalAlertLister interface {
GlobalAlerts(*om.AlertsListOptions) (*om.GlobalAlerts, error)
}

type GlobalAlertsStore interface {
GlobalAlertLister
}

// GlobalAlerts encapsulate the logic to manage different cloud providers
func (s *Store) GlobalAlerts(opts *om.AlertsListOptions) (*om.GlobalAlerts, error) {
switch s.service {
case config.OpsManagerService:
result, _, err := s.client.(*om.Client).GlobalAlerts.List(context.Background(), opts)
return result, err
default:
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}