Skip to content

CLOUDP-327714: Add notification test API and webhook template fields #257

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions opsmngr/alert_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
const (
alertConfigurationPath = "api/public/v1.0/groups/%s/alertConfigs"
fieldNamesPath = "api/public/v1.0/alertConfigs/matchers/fieldNames"
testNotificationPath = alertConfigurationPath + "/%s/%s/test"
)

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

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

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

return root, resp, err
}

// TestAlertConfiguration fires a test notification to the alert configuration.
//
// TODO: Update this when documentation is published. See more: https://docs.opsmanager.mongodb.com/current/reference/api/TBD
func (s *AlertConfigurationsServiceOp) TestExistingNotification(ctx context.Context, groupID, alertConfigID, notificationID string) (*Response, error) {
if groupID == "" {
return nil, NewArgError("groupID", "must be set")
}

if alertConfigID == "" {
return nil, NewArgError("alertConfigID", "must be set")
}

if notificationID == "" {
return nil, NewArgError("notificationID", "must be set")
}

path := fmt.Sprintf(testNotificationPath, groupID, alertConfigID, notificationID)

req, err := s.Client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return nil, err
}

resp, err := s.Client.Do(ctx, req, nil)
if err != nil {
return resp, err
}

return resp, err
}
29 changes: 28 additions & 1 deletion opsmngr/alert_configurations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"github.com/go-test/deep"
)

const alertConfigID = "57b76ddc96e8215c017ceafb" // #nosec G101 // not a credential
const alertConfigID = "57b76ddc96e8215c017ceafb" // #nosec G101 // not a credential
const alertNotificationID = "685d2e7e10ea5a42f0fb9905" // #nosec G101 // not a credential

func TestAlertConfiguration_Create(t *testing.T) {
client, mux, teardown := setup()
Expand Down Expand Up @@ -657,3 +658,29 @@ func TestAlertConfiguration_ListMatcherFields(t *testing.T) {
t.Error(diff)
}
}

func TestAlertConfiguration_TestExistingNotification(t *testing.T) {
client, mux, teardown := setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/api/public/v1.0/groups/%s/alertConfigs/%s/%s/test", projectID, alertConfigID, alertNotificationID), func(_ http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
})

_, err := client.AlertConfigurations.TestExistingNotification(ctx, projectID, alertConfigID, alertNotificationID)
if err != nil {
t.Fatalf("AlertConfigurations.TestExistingNotification returned error: %v", err)
}

expectedError := NewArgError("alertConfigID", "must be set")
_, err = client.AlertConfigurations.TestExistingNotification(ctx, projectID, "", alertNotificationID)
if diff := deep.Equal(err, expectedError); diff != nil {
t.Error(diff)
}

expectedError = NewArgError("notificationID", "must be set")
_, err = client.AlertConfigurations.TestExistingNotification(ctx, projectID, alertConfigID, "")
if diff := deep.Equal(err, expectedError); diff != nil {
t.Error(diff)
}
}