Skip to content

Commit 78bd58e

Browse files
CLOUDP-57831: CLI: Deleting an Atlas Alert Configuration (#32)
* CLOUDP-57831: CLI: Deleting an Atlas Alert Configuration
1 parent 59e4a0b commit 78bd58e

8 files changed

+275
-65
lines changed

e2e/atlas_alertconfig_test.go

Lines changed: 84 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616
package e2e
1717

1818
import (
19+
"encoding/json"
1920
"os"
2021
"os/exec"
2122
"path/filepath"
23+
"strconv"
2224
"testing"
25+
26+
"github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
2327
)
2428

25-
//const (
26-
// group = "GROUP"
27-
// eventTypeName = "NO_PRIMARY"
28-
// intervalMin = 5
29-
// delayMin = 0
30-
//)
29+
const (
30+
group = "GROUP"
31+
eventTypeName = "NO_PRIMARY"
32+
intervalMin = 5
33+
delayMin = 0
34+
)
3135

3236
func TestAtlasAlertConfig(t *testing.T) {
3337
cliPath, err := filepath.Abs("../bin/mcli")
@@ -41,65 +45,82 @@ func TestAtlasAlertConfig(t *testing.T) {
4145
}
4246

4347
atlasEntity := "atlas"
44-
alertConfigEntity := "alerts configs"
45-
46-
//t.Run("Create", func(t *testing.T) {
47-
// cmd := exec.Command(cliPath,
48-
// atlasEntity,
49-
// alertConfigEntity,
50-
// "create",
51-
// "--event",
52-
// eventTypeName,
53-
// "--enabled=true",
54-
// "--notificationTypeName",
55-
// group,
56-
// "--notificationIntervalMin",
57-
// strconv.Itoa(intervalMin),
58-
// "--notificationDelayMin",
59-
// strconv.Itoa(delayMin),
60-
// "--notificationSmsEnabled=false",
61-
// "--notificationEmailEnabled=true")
62-
// cmd.Env = os.Environ()
63-
// resp, err := cmd.CombinedOutput()
64-
//
65-
// if err != nil {
66-
// t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
67-
// }
68-
//
69-
// alert := mongodbatlas.AlertConfiguration{}
70-
// err = json.Unmarshal(resp, &alert)
71-
// if err != nil {
72-
// t.Fatalf("unexpected error: %v", err)
73-
// }
74-
//
75-
// if alert.EventTypeName != eventTypeName {
76-
// t.Errorf("got=%#v\nwant=%#v\n", alert.EventTypeName, eventTypeName)
77-
// }
78-
//
79-
// if len(alert.Notifications) != 1 {
80-
// t.Errorf("len(alert.Notifications) got=%#v\nwant=%#v\n", len(alert.Notifications), 1)
81-
// }
82-
//
83-
// if *alert.Notifications[0].DelayMin != delay_min {
84-
// t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].DelayMin, delay_min)
85-
// }
86-
//
87-
// if alert.Notifications[0].TypeName != group {
88-
// t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].TypeName, group)
89-
// }
90-
//
91-
// if alert.Notifications[0].IntervalMin != interval_min {
92-
// t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].IntervalMin, interval_min)
93-
// }
94-
//
95-
// if *alert.Notifications[0].SMSEnabled != false {
96-
// t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].SMSEnabled, false)
97-
// }
98-
//
99-
//})
48+
alertsEntity := "alerts"
49+
configEntity := "configs"
50+
51+
var alertID string
52+
53+
t.Run("Create", func(t *testing.T) {
54+
cmd := exec.Command(cliPath,
55+
atlasEntity,
56+
alertsEntity,
57+
configEntity,
58+
"create",
59+
"--event",
60+
eventTypeName,
61+
"--enabled=true",
62+
"--notificationType",
63+
group,
64+
"--notificationIntervalMin",
65+
strconv.Itoa(intervalMin),
66+
"--notificationDelayMin",
67+
strconv.Itoa(delayMin),
68+
"--notificationSmsEnabled=false",
69+
"--notificationEmailEnabled=true")
70+
cmd.Env = os.Environ()
71+
resp, err := cmd.CombinedOutput()
72+
73+
if err != nil {
74+
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
75+
}
76+
77+
alert := mongodbatlas.AlertConfiguration{}
78+
err = json.Unmarshal(resp, &alert)
79+
80+
if err != nil {
81+
t.Fatalf("unexpected error: %v", err)
82+
}
83+
84+
if alert.EventTypeName != eventTypeName {
85+
t.Errorf("got=%#v\nwant=%#v\n", alert.EventTypeName, eventTypeName)
86+
}
87+
88+
if len(alert.Notifications) != 1 {
89+
t.Errorf("len(alert.Notifications) got=%#v\nwant=%#v\n", len(alert.Notifications), 1)
90+
}
91+
92+
if *alert.Notifications[0].DelayMin != delayMin {
93+
t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].DelayMin, delayMin)
94+
}
95+
96+
if alert.Notifications[0].TypeName != group {
97+
t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].TypeName, group)
98+
}
99+
100+
if alert.Notifications[0].IntervalMin != intervalMin {
101+
t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].IntervalMin, intervalMin)
102+
}
103+
104+
if *alert.Notifications[0].SMSEnabled != false {
105+
t.Errorf("got=%#v\nwant=%#v\n", alert.Notifications[0].SMSEnabled, false)
106+
}
107+
108+
alertID = alert.ID
109+
110+
})
100111

101112
t.Run("List", func(t *testing.T) {
102-
cmd := exec.Command(cliPath, atlasEntity, alertConfigEntity, "ls")
113+
cmd := exec.Command(cliPath, atlasEntity, alertsEntity, configEntity, "ls")
114+
cmd.Env = os.Environ()
115+
resp, err := cmd.CombinedOutput()
116+
117+
if err != nil {
118+
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
119+
}
120+
})
121+
122+
t.Run("Delete", func(t *testing.T) {
123+
cmd := exec.Command(cliPath, atlasEntity, alertsEntity, configEntity, "delete", alertID, "--force")
103124
cmd.Env = os.Environ()
104125
resp, err := cmd.CombinedOutput()
105126

internal/cli/atlas_alert_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func AtlasAlertConfigsBuilder() *cobra.Command {
2828

2929
cmd.AddCommand(AtlasAlertConfigCreateBuilder())
3030
cmd.AddCommand(AtlasAlertConfigListBuilder())
31+
cmd.AddCommand(AtlasAlertConfigDeleteBuilder())
3132

3233
return cmd
3334
}

internal/cli/atlas_alert_config_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func newMatcher(opts *atlasAlertConfigCreateOpts) *atlas.Matcher {
182182
}
183183
}
184184

185-
// mcli atlas alert-config(s) create -event event --enabled [--matcherField fieldName --matcherOperator operator --matcherValue value]
185+
// mcli atlas alerts config(s) create -event event --enabled [--matcherField fieldName --matcherOperator operator --matcherValue value]
186186
// [--notificationType type --notificationDelayMin min --notificationEmailEnabled --notificationSmsEnabled --notificationUsername username --notificationTeamID id
187187
// --notificationEmailAddress email --notificationMobileNumber number --notificationChannelName channel --notificationApiToken --notificationRegion region] [--projectId projectId]
188188
func AtlasAlertConfigCreateBuilder() *cobra.Command {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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/mcli/internal/flags"
19+
"github.com/mongodb/mcli/internal/store"
20+
"github.com/mongodb/mcli/internal/usage"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
type atlasAlertConfigDeleteOpts struct {
25+
*globalOpts
26+
*deleteOpts
27+
store store.AlertConfigurationDeleter
28+
}
29+
30+
func (opts *atlasAlertConfigDeleteOpts) init() error {
31+
if opts.ProjectID() == "" {
32+
return errMissingProjectID
33+
}
34+
35+
var err error
36+
opts.store, err = store.New()
37+
return err
38+
}
39+
40+
func (opts *atlasAlertConfigDeleteOpts) Run() error {
41+
return opts.DeleteFromProject(opts.store.DeleteAlertConfiguration, opts.ProjectID())
42+
}
43+
44+
// mcli atlas alerts config(s) delete id --projectId projectId [--confirm]
45+
func AtlasAlertConfigDeleteBuilder() *cobra.Command {
46+
opts := &atlasAlertConfigDeleteOpts{
47+
globalOpts: newGlobalOpts(),
48+
deleteOpts: &deleteOpts{
49+
successMessage: "Alert Config '%s' deleted\n",
50+
failMessage: "Alert Config not deleted",
51+
},
52+
}
53+
cmd := &cobra.Command{
54+
Use: "delete [id]",
55+
Short: "Delete an Atlas Alert Config.",
56+
Aliases: []string{"rm", "Delete", "Remove"},
57+
Args: cobra.ExactArgs(1),
58+
PreRunE: func(cmd *cobra.Command, args []string) error {
59+
opts.entry = args[0]
60+
return opts.init()
61+
},
62+
RunE: func(cmd *cobra.Command, args []string) error {
63+
return opts.Run()
64+
},
65+
}
66+
67+
cmd.Flags().BoolVar(&opts.confirm, flags.Force, false, usage.Force)
68+
69+
cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID)
70+
71+
return cmd
72+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
"github.com/mongodb/mcli/internal/mocks"
22+
)
23+
24+
func TestAtlasAlertConfigsDelete_Run(t *testing.T) {
25+
ctrl := gomock.NewController(t)
26+
mockStore := mocks.NewMockAlertConfigurationDeleter(ctrl)
27+
28+
defer ctrl.Finish()
29+
30+
deleteOpts := &atlasAlertConfigDeleteOpts{
31+
globalOpts: newGlobalOpts(),
32+
deleteOpts: &deleteOpts{
33+
confirm: true,
34+
entry: "test",
35+
},
36+
store: mockStore,
37+
}
38+
39+
mockStore.
40+
EXPECT().
41+
DeleteAlertConfiguration(deleteOpts.projectID, deleteOpts.entry).
42+
Return(nil).
43+
Times(1)
44+
45+
err := deleteOpts.Run()
46+
if err != nil {
47+
t.Fatalf("Run() unexpected error: %v", err)
48+
}
49+
}

internal/cli/atlas_alert_config_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (opts *atlasAlertConfigListOpts) newListOptions() *atlas.ListOptions {
5858
}
5959
}
6060

61-
// mcli atlas alert-config(s) list --projectId projectId [--page N] [--limit N]
61+
// mcli atlas alerts config(s) list --projectId projectId [--page N] [--limit N]
6262
func AtlasAlertConfigListBuilder() *cobra.Command {
6363
opts := &atlasAlertConfigListOpts{
6464
globalOpts: newGlobalOpts(),

internal/mocks/mock_alert_configuration.go

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

0 commit comments

Comments
 (0)