Skip to content

Commit a386b3a

Browse files
CLOUDP-327714: Add notification test API and webhook template fields
1 parent 343f164 commit a386b3a

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

opsmngr/alert_configurations.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
const (
2424
alertConfigurationPath = "api/public/v1.0/groups/%s/alertConfigs"
2525
fieldNamesPath = "api/public/v1.0/alertConfigs/matchers/fieldNames"
26+
testNotificationPath = alertConfigurationPath + "/%s/%s/test"
2627
)
2728

2829
// AlertConfigurationsService provides access to the alert configuration related functions in the Ops Manager API.
@@ -35,6 +36,7 @@ type AlertConfigurationsService interface {
3536
ListMatcherFields(ctx context.Context) ([]string, *Response, error)
3637
Update(context.Context, string, string, *AlertConfiguration) (*AlertConfiguration, *Response, error)
3738
Delete(context.Context, string, string) (*Response, error)
39+
TestExistingNotification(context.Context, string, string, string) (*Response, error)
3840
}
3941

4042
// AlertConfigurationsServiceOp handles communication with the AlertConfiguration related methods.
@@ -124,6 +126,8 @@ type Notification struct {
124126
MicrosoftTeamsWebhookURL string `json:"microsoftTeamsWebhookUrl,omitempty"` // Microsoft Teams Wewbhook URL
125127
WebhookSecret string `json:"webhookSecret,omitempty"` // Webhook Secret
126128
WebhookURL string `json:"webhookUrl,omitempty"` // Webhook URL
129+
WebhookHeadersTemplate string `json:"webhookHeadersTemplate,omitempty"` // Freemarker-syntax template to customize webhook request headers.
130+
WebhookBodyTemplate string `json:"webhookBodyTemplate,omitempty"` // Freemarker-syntax template to customize webhook request body.
127131
}
128132

129133
// AlertConfigurationsResponse is the response from the AlertConfigurationsService.List.
@@ -359,3 +363,34 @@ func (s *AlertConfigurationsServiceOp) ListMatcherFields(ctx context.Context) ([
359363

360364
return root, resp, err
361365
}
366+
367+
// TestAlertConfiguration fires a test notification to the alert configuration.
368+
//
369+
// TODO: Update this when documentation is published. See more: https://docs.opsmanager.mongodb.com/current/reference/api/TBD
370+
func (s *AlertConfigurationsServiceOp) TestExistingNotification(ctx context.Context, groupID string, alertConfigID string, notificationID string) (*Response, error) {
371+
if groupID == "" {
372+
return nil, NewArgError("groupID", "must be set")
373+
}
374+
375+
if alertConfigID == "" {
376+
return nil, NewArgError("alertConfigID", "must be set")
377+
}
378+
379+
if notificationID == "" {
380+
return nil, NewArgError("notificationID", "must be set")
381+
}
382+
383+
path := fmt.Sprintf(testNotificationPath, groupID, alertConfigID, notificationID)
384+
385+
req, err := s.Client.NewRequest(ctx, http.MethodPost, path, nil)
386+
if err != nil {
387+
return nil, err
388+
}
389+
390+
resp, err := s.Client.Do(ctx, req, nil)
391+
if err != nil {
392+
return resp, err
393+
}
394+
395+
return resp, err
396+
}

opsmngr/alert_configurations_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"github.com/go-test/deep"
2424
)
2525

26-
const alertConfigID = "57b76ddc96e8215c017ceafb" // #nosec G101 // not a credential
26+
const alertConfigID = "57b76ddc96e8215c017ceafb" // #nosec G101 // not a credential
27+
const alertNotificationID = "685d2e7e10ea5a42f0fb9905" // #nosec G101 // not a credential
2728

2829
func TestAlertConfiguration_Create(t *testing.T) {
2930
client, mux, teardown := setup()
@@ -657,3 +658,29 @@ func TestAlertConfiguration_ListMatcherFields(t *testing.T) {
657658
t.Error(diff)
658659
}
659660
}
661+
662+
func TestAlertConfiguration_TestExistingNotification(t *testing.T) {
663+
client, mux, teardown := setup()
664+
defer teardown()
665+
666+
mux.HandleFunc(fmt.Sprintf("/api/public/v1.0/groups/%s/alertConfigs/%s/%s/test", projectID, alertConfigID, alertNotificationID), func(_ http.ResponseWriter, r *http.Request) {
667+
testMethod(t, r, http.MethodPost)
668+
})
669+
670+
_, err := client.AlertConfigurations.TestExistingNotification(ctx, projectID, alertConfigID, alertNotificationID)
671+
if err != nil {
672+
t.Fatalf("AlertConfigurations.TestExistingNotification returned error: %v", err)
673+
}
674+
675+
expectedError := NewArgError("alertConfigID", "must be set")
676+
_, err = client.AlertConfigurations.TestExistingNotification(ctx, projectID, "", alertNotificationID)
677+
if diff := deep.Equal(err, expectedError); diff != nil {
678+
t.Error(diff)
679+
}
680+
681+
expectedError = NewArgError("notificationID", "must be set")
682+
_, err = client.AlertConfigurations.TestExistingNotification(ctx, projectID, alertConfigID, "")
683+
if diff := deep.Equal(err, expectedError); diff != nil {
684+
t.Error(diff)
685+
}
686+
}

0 commit comments

Comments
 (0)