diff --git a/Makefile b/Makefile index 218ef0c122..98351a1a52 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/go.mod b/go.mod index 8093104f00..72c6e719d4 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7c86ef8b21..4d6a76b529 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/cli/atlas_alerts.go b/internal/cli/atlas_alerts.go index 01ed33ba37..d802e009c9 100644 --- a/internal/cli/atlas_alerts.go +++ b/internal/cli/atlas_alerts.go @@ -29,6 +29,7 @@ func AtlasAlertsBuilder() *cobra.Command { cmd.AddCommand(AtlasAlertsDescribeBuilder()) cmd.AddCommand(AtlasAlertsListBuilder()) cmd.AddCommand(AtlasAlertsAcknowledgeBuilder()) + cmd.AddCommand(OpsManagerGlobalBuilder()) return cmd } diff --git a/internal/cli/ops_manager_alerts_global_list.go b/internal/cli/ops_manager_alerts_global_list.go new file mode 100644 index 0000000000..0fc9c0cb66 --- /dev/null +++ b/internal/cli/ops_manager_alerts_global_list.go @@ -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{ + 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 +} diff --git a/internal/cli/ops_manager_alerts_global_list_test.go b/internal/cli/ops_manager_alerts_global_list_test.go new file mode 100644 index 0000000000..494d7eee3c --- /dev/null +++ b/internal/cli/ops_manager_alerts_global_list_test.go @@ -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) + } +} diff --git a/internal/cli/ops_manager_global.go b/internal/cli/ops_manager_global.go new file mode 100644 index 0000000000..84b6041d3d --- /dev/null +++ b/internal/cli/ops_manager_global.go @@ -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 +} diff --git a/internal/fixtures/global_alerts.go b/internal/fixtures/global_alerts.go new file mode 100644 index 0000000000..b1d36181cb --- /dev/null +++ b/internal/fixtures/global_alerts.go @@ -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: "admin@example.com", + }, + 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, + } +} diff --git a/internal/mocks/mock_global_alerts.go b/internal/mocks/mock_global_alerts.go new file mode 100644 index 0000000000..e8cbd3b26b --- /dev/null +++ b/internal/mocks/mock_global_alerts.go @@ -0,0 +1,87 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: internal/store/global_alerts.go + +// Package mocks is a generated GoMock package. +package mocks + +import ( + gomock "github.com/golang/mock/gomock" + opsmngr "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr" + reflect "reflect" +) + +// MockGlobalAlertLister is a mock of GlobalAlertLister interface +type MockGlobalAlertLister struct { + ctrl *gomock.Controller + recorder *MockGlobalAlertListerMockRecorder +} + +// MockGlobalAlertListerMockRecorder is the mock recorder for MockGlobalAlertLister +type MockGlobalAlertListerMockRecorder struct { + mock *MockGlobalAlertLister +} + +// NewMockGlobalAlertLister creates a new mock instance +func NewMockGlobalAlertLister(ctrl *gomock.Controller) *MockGlobalAlertLister { + mock := &MockGlobalAlertLister{ctrl: ctrl} + mock.recorder = &MockGlobalAlertListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockGlobalAlertLister) EXPECT() *MockGlobalAlertListerMockRecorder { + return m.recorder +} + +// GlobalAlerts mocks base method +func (m *MockGlobalAlertLister) GlobalAlerts(arg0 *opsmngr.AlertsListOptions) (*opsmngr.GlobalAlerts, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GlobalAlerts", arg0) + ret0, _ := ret[0].(*opsmngr.GlobalAlerts) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GlobalAlerts indicates an expected call of GlobalAlerts +func (mr *MockGlobalAlertListerMockRecorder) GlobalAlerts(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GlobalAlerts", reflect.TypeOf((*MockGlobalAlertLister)(nil).GlobalAlerts), arg0) +} + +// MockGlobalAlertsStore is a mock of GlobalAlertsStore interface +type MockGlobalAlertsStore struct { + ctrl *gomock.Controller + recorder *MockGlobalAlertsStoreMockRecorder +} + +// MockGlobalAlertsStoreMockRecorder is the mock recorder for MockGlobalAlertsStore +type MockGlobalAlertsStoreMockRecorder struct { + mock *MockGlobalAlertsStore +} + +// NewMockGlobalAlertsStore creates a new mock instance +func NewMockGlobalAlertsStore(ctrl *gomock.Controller) *MockGlobalAlertsStore { + mock := &MockGlobalAlertsStore{ctrl: ctrl} + mock.recorder = &MockGlobalAlertsStoreMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockGlobalAlertsStore) EXPECT() *MockGlobalAlertsStoreMockRecorder { + return m.recorder +} + +// GlobalAlerts mocks base method +func (m *MockGlobalAlertsStore) GlobalAlerts(arg0 *opsmngr.AlertsListOptions) (*opsmngr.GlobalAlerts, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GlobalAlerts", arg0) + ret0, _ := ret[0].(*opsmngr.GlobalAlerts) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GlobalAlerts indicates an expected call of GlobalAlerts +func (mr *MockGlobalAlertsStoreMockRecorder) GlobalAlerts(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GlobalAlerts", reflect.TypeOf((*MockGlobalAlertsStore)(nil).GlobalAlerts), arg0) +} diff --git a/internal/store/global_alerts.go b/internal/store/global_alerts.go new file mode 100644 index 0000000000..f631510676 --- /dev/null +++ b/internal/store/global_alerts.go @@ -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) + } +}