Skip to content

CLOUDP-57844: List matcher fields, Atlas #62

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 11 commits into from
Mar 26, 2020
Merged
23 changes: 23 additions & 0 deletions e2e/atlas_alertconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}

})
}
2 changes: 1 addition & 1 deletion e2e/atlas_alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

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.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
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions internal/cli/atlas_alert_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func AtlasAlertConfigsBuilder() *cobra.Command {
cmd.AddCommand(AtlasAlertConfigCreateBuilder())
cmd.AddCommand(AtlasAlertConfigListBuilder())
cmd.AddCommand(AtlasAlertConfigDeleteBuilder())
cmd.AddCommand(AtlasAlertConfigsFieldsBuilder())

return cmd
}
31 changes: 31 additions & 0 deletions internal/cli/atlas_alerts_config_fields.go
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 60 additions & 0 deletions internal/cli/atlas_alerts_config_fields_type.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions internal/cli/atlas_alerts_config_fields_type_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 3 additions & 4 deletions internal/cli/ops_manager_alerts_global_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/ops_manager_alerts_global_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -31,7 +31,7 @@ func TestOpsManagerAlertsGlobalList_Run(t *testing.T) {

expected := fixtures.GlobalAlerts()

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

Expand Down
12 changes: 12 additions & 0 deletions internal/fixtures/alert_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
53 changes: 53 additions & 0 deletions internal/mocks/mock_alert_configuration.go

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

5 changes: 3 additions & 2 deletions internal/mocks/mock_global_alerts.go

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

16 changes: 16 additions & 0 deletions internal/store/alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Loading