diff --git a/e2e/atlas_alertconfig_test.go b/e2e/atlas_alertconfig_test.go index 7427f627ba..f3e2b16be0 100644 --- a/e2e/atlas_alertconfig_test.go +++ b/e2e/atlas_alertconfig_test.go @@ -23,7 +23,9 @@ import ( "strconv" "testing" + "github.com/go-test/deep" "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" + "github.com/mongodb/mongocli/internal/fixtures" ) const ( @@ -129,4 +131,25 @@ func TestAtlasAlertConfig(t *testing.T) { } }) + t.Run("List Matcher Fields", func(t *testing.T) { + cmd := exec.Command(cliPath, atlasEntity, alertsEntity, configEntity, "fields", "type") + cmd.Env = os.Environ() + resp, err := cmd.CombinedOutput() + + if err != nil { + t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) + } + + fields := []string{} + err = json.Unmarshal(resp, &fields) + + if err != nil { + t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) + } + + if diff := deep.Equal(fields, fixtures.MatcherFieldsType()); diff != nil { + t.Error(diff) + } + + }) } diff --git a/e2e/atlas_alerts_test.go b/e2e/atlas_alerts_test.go index 43829c4915..cff06e623a 100644 --- a/e2e/atlas_alerts_test.go +++ b/e2e/atlas_alerts_test.go @@ -133,7 +133,7 @@ func TestAtlasAlerts(t *testing.T) { t.Fatalf("unexpected error: %v", err) } - if len(alerts.Results) == 0 { + if len(alerts.Results) > 0 { t.Errorf("got=%#v\nwant=0\n", len(alerts.Results)) } diff --git a/go.mod b/go.mod index 47bb342dca..9d1ceeace6 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.20200325161849-bca1d320f71c - github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325124453-024ddde724d1 + github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325183336-61ab48668700 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 52399bc2df..63d01d77b1 100644 --- a/go.sum +++ b/go.sum @@ -100,11 +100,10 @@ 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/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU= github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200325161849-bca1d320f71c h1:6VkFHczRJ51vCROsvPhCdVk8V1agzmSOCNnwTAwvLdU= github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200325161849-bca1d320f71c/go.mod h1:LS8O0YLkA+sbtOb3fZLF10yY3tJM+1xATXMJ3oU35LU= -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/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325183336-61ab48668700 h1:vtmlFXWGmjjr/ES65ouqmVr7wgxkccZD43CRWg7Xa0Y= +github.com/mongodb/go-client-mongodb-ops-manager v0.0.2-0.20200325183336-61ab48668700/go.mod h1:8pOYwdn1FUx6U8872daMLPA371Ju9BTX5IwmczU4nn4= 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_alert_config.go b/internal/cli/atlas_alert_config.go index 213f87d61e..234eb3053c 100644 --- a/internal/cli/atlas_alert_config.go +++ b/internal/cli/atlas_alert_config.go @@ -29,6 +29,7 @@ func AtlasAlertConfigsBuilder() *cobra.Command { cmd.AddCommand(AtlasAlertConfigCreateBuilder()) cmd.AddCommand(AtlasAlertConfigListBuilder()) cmd.AddCommand(AtlasAlertConfigDeleteBuilder()) + cmd.AddCommand(AtlasAlertConfigsFieldsBuilder()) return cmd } diff --git a/internal/cli/atlas_alerts_config_fields.go b/internal/cli/atlas_alerts_config_fields.go new file mode 100644 index 0000000000..91a4b44073 --- /dev/null +++ b/internal/cli/atlas_alerts_config_fields.go @@ -0,0 +1,31 @@ +// 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 AtlasAlertConfigsFieldsBuilder() *cobra.Command { + cmd := &cobra.Command{ + Use: "fields", + Aliases: []string{"field"}, + Short: "Manage alert configuration fields for your project.", + } + + cmd.AddCommand(AtlasAlertsConfigsFieldsBuilder()) + + return cmd +} diff --git a/internal/cli/atlas_alerts_config_fields_type.go b/internal/cli/atlas_alerts_config_fields_type.go new file mode 100644 index 0000000000..0f4fe86281 --- /dev/null +++ b/internal/cli/atlas_alerts_config_fields_type.go @@ -0,0 +1,60 @@ +// 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/mongocli/internal/json" + "github.com/mongodb/mongocli/internal/store" + "github.com/spf13/cobra" +) + +type atlasAlertsConfigFieldsTypeOpts struct { + store store.MatcherFieldsLister +} + +func (opts *atlasAlertsConfigFieldsTypeOpts) init() error { + var err error + opts.store, err = store.New() + return err +} + +func (opts *atlasAlertsConfigFieldsTypeOpts) Run() error { + result, err := opts.store.MatcherFields() + + if err != nil { + return err + } + + return json.PrettyPrint(result) +} + +// mongocli atlas alerts config(s) fields type +func AtlasAlertsConfigsFieldsBuilder() *cobra.Command { + opts := &atlasAlertsConfigFieldsTypeOpts{} + cmd := &cobra.Command{ + Use: "type", + Short: "List alert configurations available field types.", + Aliases: []string{"types"}, + 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() + }, + } + + return cmd +} diff --git a/internal/cli/atlas_alerts_config_fields_type_test.go b/internal/cli/atlas_alerts_config_fields_type_test.go new file mode 100644 index 0000000000..207022ccaa --- /dev/null +++ b/internal/cli/atlas_alerts_config_fields_type_test.go @@ -0,0 +1,47 @@ +// 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" + "github.com/mongodb/mongocli/internal/fixtures" + "github.com/mongodb/mongocli/internal/mocks" +) + +func TestAtlasAlertConfigFieldsType_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockMatcherFieldsLister(ctrl) + + defer ctrl.Finish() + + expected := fixtures.MatcherFieldsType() + + listOpts := &atlasAlertsConfigFieldsTypeOpts{ + store: mockStore, + } + + mockStore. + EXPECT(). + MatcherFields(). + Return(expected, nil). + Times(1) + + err := listOpts.Run() + if err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +} diff --git a/internal/cli/ops_manager_alerts_global_list.go b/internal/cli/ops_manager_alerts_global_list.go index 0fc9c0cb66..3f09ea7cbb 100644 --- a/internal/cli/ops_manager_alerts_global_list.go +++ b/internal/cli/ops_manager_alerts_global_list.go @@ -15,8 +15,7 @@ package cli import ( - "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" - om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr" + atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" "github.com/mongodb/mongocli/internal/flags" "github.com/mongodb/mongocli/internal/json" "github.com/mongodb/mongocli/internal/store" @@ -38,9 +37,9 @@ func (opts *opsManagerAlertsGlobalListOpts) init() error { } func (opts *opsManagerAlertsGlobalListOpts) Run() error { - alertOpts := &om.AlertsListOptions{ + alertOpts := &atlas.AlertsListOptions{ Status: opts.status, - ListOptions: mongodbatlas.ListOptions{ + ListOptions: atlas.ListOptions{ PageNum: opts.pageNum, ItemsPerPage: opts.itemsPerPage, }, diff --git a/internal/cli/ops_manager_alerts_global_list_test.go b/internal/cli/ops_manager_alerts_global_list_test.go index 494d7eee3c..7edcb74d6e 100644 --- a/internal/cli/ops_manager_alerts_global_list_test.go +++ b/internal/cli/ops_manager_alerts_global_list_test.go @@ -18,7 +18,7 @@ import ( "testing" "github.com/golang/mock/gomock" - om "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr" + atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" "github.com/mongodb/mongocli/internal/fixtures" "github.com/mongodb/mongocli/internal/mocks" ) @@ -31,7 +31,7 @@ func TestOpsManagerAlertsGlobalList_Run(t *testing.T) { expected := fixtures.GlobalAlerts() - alertOpts := om.AlertsListOptions{ + alertOpts := atlas.AlertsListOptions{ Status: "OPEN", } diff --git a/internal/fixtures/alert_configs.go b/internal/fixtures/alert_configs.go index fcaf79b52c..08f20abbd8 100644 --- a/internal/fixtures/alert_configs.go +++ b/internal/fixtures/alert_configs.go @@ -53,3 +53,15 @@ func AlertConfig() *atlas.AlertConfiguration { func AlertConfigs() []atlas.AlertConfiguration { return []atlas.AlertConfiguration{*AlertConfig()} } + +func MatcherFieldsType() []string { + return []string{ + "TYPE_NAME", + "HOSTNAME", + "PORT", + "HOSTNAME_AND_PORT", + "REPLICA_SET_NAME", + "SHARD_NAME", + "CLUSTER_NAME", + } +} diff --git a/internal/mocks/mock_alert_configuration.go b/internal/mocks/mock_alert_configuration.go index f28d0e78e3..dd749cb382 100644 --- a/internal/mocks/mock_alert_configuration.go +++ b/internal/mocks/mock_alert_configuration.go @@ -123,6 +123,44 @@ func (mr *MockAlertConfigurationDeleterMockRecorder) DeleteAlertConfiguration(ar return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAlertConfiguration", reflect.TypeOf((*MockAlertConfigurationDeleter)(nil).DeleteAlertConfiguration), arg0, arg1) } +// MockMatcherFieldsLister is a mock of MatcherFieldsLister interface +type MockMatcherFieldsLister struct { + ctrl *gomock.Controller + recorder *MockMatcherFieldsListerMockRecorder +} + +// MockMatcherFieldsListerMockRecorder is the mock recorder for MockMatcherFieldsLister +type MockMatcherFieldsListerMockRecorder struct { + mock *MockMatcherFieldsLister +} + +// NewMockMatcherFieldsLister creates a new mock instance +func NewMockMatcherFieldsLister(ctrl *gomock.Controller) *MockMatcherFieldsLister { + mock := &MockMatcherFieldsLister{ctrl: ctrl} + mock.recorder = &MockMatcherFieldsListerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockMatcherFieldsLister) EXPECT() *MockMatcherFieldsListerMockRecorder { + return m.recorder +} + +// MatcherFields mocks base method +func (m *MockMatcherFieldsLister) MatcherFields() ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "MatcherFields") + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// MatcherFields indicates an expected call of MatcherFields +func (mr *MockMatcherFieldsListerMockRecorder) MatcherFields() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MatcherFields", reflect.TypeOf((*MockMatcherFieldsLister)(nil).MatcherFields)) +} + // MockAlertConfigurationStore is a mock of AlertConfigurationStore interface type MockAlertConfigurationStore struct { ctrl *gomock.Controller @@ -189,3 +227,18 @@ func (mr *MockAlertConfigurationStoreMockRecorder) DeleteAlertConfiguration(arg0 mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAlertConfiguration", reflect.TypeOf((*MockAlertConfigurationStore)(nil).DeleteAlertConfiguration), arg0, arg1) } + +// MatcherFields mocks base method +func (m *MockAlertConfigurationStore) MatcherFields() ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "MatcherFields") + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// MatcherFields indicates an expected call of MatcherFields +func (mr *MockAlertConfigurationStoreMockRecorder) MatcherFields() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MatcherFields", reflect.TypeOf((*MockAlertConfigurationStore)(nil).MatcherFields)) +} diff --git a/internal/mocks/mock_global_alerts.go b/internal/mocks/mock_global_alerts.go index e8cbd3b26b..c9b857fe7d 100644 --- a/internal/mocks/mock_global_alerts.go +++ b/internal/mocks/mock_global_alerts.go @@ -6,6 +6,7 @@ package mocks import ( gomock "github.com/golang/mock/gomock" + mongodbatlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" opsmngr "github.com/mongodb/go-client-mongodb-ops-manager/opsmngr" reflect "reflect" ) @@ -34,7 +35,7 @@ func (m *MockGlobalAlertLister) EXPECT() *MockGlobalAlertListerMockRecorder { } // GlobalAlerts mocks base method -func (m *MockGlobalAlertLister) GlobalAlerts(arg0 *opsmngr.AlertsListOptions) (*opsmngr.GlobalAlerts, error) { +func (m *MockGlobalAlertLister) GlobalAlerts(arg0 *mongodbatlas.AlertsListOptions) (*opsmngr.GlobalAlerts, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GlobalAlerts", arg0) ret0, _ := ret[0].(*opsmngr.GlobalAlerts) @@ -72,7 +73,7 @@ func (m *MockGlobalAlertsStore) EXPECT() *MockGlobalAlertsStoreMockRecorder { } // GlobalAlerts mocks base method -func (m *MockGlobalAlertsStore) GlobalAlerts(arg0 *opsmngr.AlertsListOptions) (*opsmngr.GlobalAlerts, error) { +func (m *MockGlobalAlertsStore) GlobalAlerts(arg0 *mongodbatlas.AlertsListOptions) (*opsmngr.GlobalAlerts, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GlobalAlerts", arg0) ret0, _ := ret[0].(*opsmngr.GlobalAlerts) diff --git a/internal/store/alert_configuration.go b/internal/store/alert_configuration.go index 4c4b211413..dd599e66f9 100644 --- a/internal/store/alert_configuration.go +++ b/internal/store/alert_configuration.go @@ -35,10 +35,15 @@ type AlertConfigurationDeleter interface { DeleteAlertConfiguration(string, string) error } +type MatcherFieldsLister interface { + MatcherFields() ([]string, error) +} + type AlertConfigurationStore interface { AlertConfigurationLister AlertConfigurationCreator AlertConfigurationDeleter + MatcherFieldsLister } // AlertConfigurations encapsulate the logic to manage different cloud providers @@ -82,3 +87,14 @@ func (s *Store) DeleteAlertConfiguration(projectID, id string) error { return fmt.Errorf("unsupported service: %s", s.service) } } + +// MatcherFields encapsulate the logic to manage different cloud providers +func (s *Store) MatcherFields() ([]string, error) { + switch s.service { + case config.CloudService: + result, _, err := s.client.(*atlas.Client).AlertConfigurations.ListMatcherFields(context.Background()) + return result, err + default: + return nil, fmt.Errorf("unsupported service: %s", s.service) + } +} diff --git a/internal/store/global_alerts.go b/internal/store/global_alerts.go index f631510676..216b4d5d1f 100644 --- a/internal/store/global_alerts.go +++ b/internal/store/global_alerts.go @@ -18,12 +18,13 @@ import ( "context" "fmt" + atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" 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) + GlobalAlerts(*atlas.AlertsListOptions) (*om.GlobalAlerts, error) } type GlobalAlertsStore interface { @@ -31,7 +32,7 @@ type GlobalAlertsStore interface { } // GlobalAlerts encapsulate the logic to manage different cloud providers -func (s *Store) GlobalAlerts(opts *om.AlertsListOptions) (*om.GlobalAlerts, error) { +func (s *Store) GlobalAlerts(opts *atlas.AlertsListOptions) (*om.GlobalAlerts, error) { switch s.service { case config.OpsManagerService: result, _, err := s.client.(*om.Client).GlobalAlerts.List(context.Background(), opts)