grafana/pkg/services/alerting/notifiers/opsgenie_test.go

229 lines
7.4 KiB
Go
Raw Normal View History

2016-12-06 14:48:13 -06:00
package notifiers
import (
"context"
"reflect"
"strings"
"testing"
2016-12-06 14:48:13 -06:00
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
encryptionservice "github.com/grafana/grafana/pkg/services/encryption/service"
"github.com/grafana/grafana/pkg/services/notifications"
"github.com/grafana/grafana/pkg/services/tag"
Encryption: Refactor securejsondata.SecureJsonData to stop relying on global functions (#38865) * Encryption: Add support to encrypt/decrypt sjd * Add datasources.Service as a proxy to datasources db operations * Encrypt ds.SecureJsonData before calling SQLStore * Move ds cache code into ds service * Fix tlsmanager tests * Fix pluginproxy tests * Remove some securejsondata.GetEncryptedJsonData usages * Add pluginsettings.Service as a proxy for plugin settings db operations * Add AlertNotificationService as a proxy for alert notification db operations * Remove some securejsondata.GetEncryptedJsonData usages * Remove more securejsondata.GetEncryptedJsonData usages * Fix lint errors * Minor fixes * Remove encryption global functions usages from ngalert * Fix lint errors * Minor fixes * Minor fixes * Remove securejsondata.DecryptedValue usage * Refactor the refactor * Remove securejsondata.DecryptedValue usage * Move securejsondata to migrations package * Move securejsondata to migrations package * Minor fix * Fix integration test * Fix integration tests * Undo undesired changes * Fix tests * Add context.Context into encryption methods * Fix tests * Fix tests * Fix tests * Trigger CI * Fix test * Add names to params of encryption service interface * Remove bus from CacheServiceImpl * Add logging * Add keys to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Add missing key to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Undo changes in markdown files * Fix formatting * Add context to secrets service * Rename decryptSecureJsonData to decryptSecureJsonDataFn * Name args in GetDecryptedValueFn * Add template back to NewAlertmanagerNotifier * Copy GetDecryptedValueFn to ngalert * Add logging to pluginsettings * Fix pluginsettings test Co-authored-by: Tania B <yalyna.ts@gmail.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
2021-10-07 09:33:50 -05:00
"github.com/grafana/grafana/pkg/services/validations"
"github.com/stretchr/testify/require"
2016-12-06 14:48:13 -06:00
)
func TestOpsGenieNotifier(t *testing.T) {
encryptionService := encryptionservice.SetupTestService(t)
t.Run("Parsing alert notification from settings", func(t *testing.T) {
t.Run("empty settings should return error", func(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
}
_, err := NewOpsGenieNotifier(model, encryptionService.GetDecryptedValue, nil)
require.Error(t, err)
})
2016-12-06 14:48:13 -06:00
t.Run("settings should trigger incident", func(t *testing.T) {
json := `
2016-12-06 14:48:13 -06:00
{
"apiKey": "abcdefgh0123456789"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
}
2016-12-06 14:48:13 -06:00
not, err := NewOpsGenieNotifier(model, encryptionService.GetDecryptedValue, nil)
opsgenieNotifier := not.(*OpsGenieNotifier)
2016-12-06 14:48:13 -06:00
require.Nil(t, err)
require.Equal(t, "opsgenie_testing", opsgenieNotifier.Name)
require.Equal(t, "opsgenie", opsgenieNotifier.Type)
require.Equal(t, "abcdefgh0123456789", opsgenieNotifier.APIKey)
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
})
})
t.Run("Handling notification tags", func(t *testing.T) {
t.Run("invalid sendTagsAs value should return error", func(t *testing.T) {
json := `{
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
"apiKey": "abcdefgh0123456789",
"sendTagsAs": "not_a_valid_value"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
}
_, err := NewOpsGenieNotifier(model, encryptionService.GetDecryptedValue, nil)
require.Error(t, err)
require.Equal(t, reflect.TypeOf(err), reflect.TypeOf(alerting.ValidationError{}))
require.True(t, strings.HasSuffix(err.Error(), "Invalid value for sendTagsAs: \"not_a_valid_value\""))
})
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
t.Run("alert payload should include tag pairs only as an array in the tags key when sendAsTags is not set", func(t *testing.T) {
json := `{
"apiKey": "abcdefgh0123456789"
}`
tagPairs := []*tag.Tag{
{Key: "keyOnly"},
{Key: "aKey", Value: "aValue"},
}
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
}
notificationService := notifications.MockNotificationService()
notifier, notifierErr := NewOpsGenieNotifier(model, encryptionService.GetDecryptedValue, notificationService) // unhandled error
opsgenieNotifier := notifier.(*OpsGenieNotifier)
evalContext := alerting.NewEvalContext(context.Background(), &alerting.Rule{
ID: 0,
Name: "someRule",
Message: "someMessage",
State: models.AlertStateAlerting,
AlertRuleTags: tagPairs,
}, &validations.OSSPluginRequestValidator{}, nil, nil, nil, annotationstest.NewFakeAnnotationsRepo())
evalContext.IsTestRun = true
tags := make([]string, 0)
details := make(map[string]interface{})
alertErr := opsgenieNotifier.createAlert(evalContext)
bodyJSON, err := simplejson.NewJson([]byte(notificationService.Webhook.Body))
if err == nil {
tags = bodyJSON.Get("tags").MustStringArray([]string{})
details = bodyJSON.Get("details").MustMap(map[string]interface{}{})
}
require.Nil(t, notifierErr)
require.Nil(t, alertErr)
require.Equal(t, tags, []string{"keyOnly", "aKey:aValue"})
require.Equal(t, details, map[string]interface{}{"url": ""})
})
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
t.Run("alert payload should include tag pairs only as a map in the details key when sendAsTags=details", func(t *testing.T) {
json := `{
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
"apiKey": "abcdefgh0123456789",
"sendTagsAs": "details"
}`
tagPairs := []*tag.Tag{
{Key: "keyOnly"},
{Key: "aKey", Value: "aValue"},
}
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
}
notificationService := notifications.MockNotificationService()
notifier, notifierErr := NewOpsGenieNotifier(model, encryptionService.GetDecryptedValue, notificationService) // unhandled error
opsgenieNotifier := notifier.(*OpsGenieNotifier)
evalContext := alerting.NewEvalContext(context.Background(), &alerting.Rule{
ID: 0,
Name: "someRule",
Message: "someMessage",
State: models.AlertStateAlerting,
AlertRuleTags: tagPairs,
}, nil, nil, nil, nil, annotationstest.NewFakeAnnotationsRepo())
evalContext.IsTestRun = true
tags := make([]string, 0)
details := make(map[string]interface{})
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
alertErr := opsgenieNotifier.createAlert(evalContext)
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
bodyJSON, err := simplejson.NewJson([]byte(notificationService.Webhook.Body))
if err == nil {
tags = bodyJSON.Get("tags").MustStringArray([]string{})
details = bodyJSON.Get("details").MustMap(map[string]interface{}{})
}
require.Nil(t, notifierErr)
require.Nil(t, alertErr)
require.Equal(t, tags, []string{})
require.Equal(t, details, map[string]interface{}{"keyOnly": "", "aKey": "aValue", "url": ""})
})
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
t.Run("alert payload should include tag pairs as both a map in the details key and an array in the tags key when sendAsTags=both", func(t *testing.T) {
json := `{
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
"apiKey": "abcdefgh0123456789",
"sendTagsAs": "both"
}`
tagPairs := []*tag.Tag{
{Key: "keyOnly"},
{Key: "aKey", Value: "aValue"},
}
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "opsgenie_testing",
Type: "opsgenie",
Settings: settingsJSON,
}
notificationService := notifications.MockNotificationService()
notifier, notifierErr := NewOpsGenieNotifier(model, encryptionService.GetDecryptedValue, notificationService) // unhandled error
opsgenieNotifier := notifier.(*OpsGenieNotifier)
evalContext := alerting.NewEvalContext(context.Background(), &alerting.Rule{
ID: 0,
Name: "someRule",
Message: "someMessage",
State: models.AlertStateAlerting,
AlertRuleTags: tagPairs,
}, nil, nil, nil, nil, annotationstest.NewFakeAnnotationsRepo())
evalContext.IsTestRun = true
tags := make([]string, 0)
details := make(map[string]interface{})
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
alertErr := opsgenieNotifier.createAlert(evalContext)
Alerting: Allow sending notification tags to Opsgenie as extra properties (#30332) * Alerting: Opsgenie send tags as extra properties Allow users to select where to send notification tags when alerting via OpsGenie. Supports sending tags as key/value details, concatenated strings in tags or both. Users will be able to see their tags as key/values under extra properties in an alert on the Opsgenie UI. These key/values can then be used in the platform for routing, templating etc. * Configurable delivery to either tags, extra properties or both * Default to current behaviour (tags only) * Support both so users can transition from tags to details Add docs and clean up references * Alerting: Add additional arg for Opsgenie tests The NewEvalContext function now requires a 'PluginRequestValidator' argument. As our test does not use the validator we can specify 'nil' to satisfy the function and allow our test to pass as expected. * Alerting: Opsgenie doc fixes Accept suggested changes for Opsgenie docs Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Alerting: Opsgenie provisioning settings docs Add the new setting to the provisioning docs * Alerting: Opsgenie doc typo correction Move the comma (,) out of the preformatting tags for the setting value * Alerting: Opsgenie refactor send switches Refactor the send switches to be methods on the OpsgenieNotiefier itself. This also cleans up the method names so that the code reads a bit more logically as: if we should send details: send details if we should send tags: send tags This avoids the calling code needing to care about passing the state and allows an engineer working in the `createAlert` function to focus on the results of applying the logic instead. * Alerting: Opsgenie docs rename note Rename the note heading to match the number to more clearly link them. * Alerting: Opsgenie use standard reference to note Refer to the note below as per recommendation and standards. Fixes #30331 Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2021-03-24 11:07:26 -05:00
bodyJSON, err := simplejson.NewJson([]byte(notificationService.Webhook.Body))
if err == nil {
tags = bodyJSON.Get("tags").MustStringArray([]string{})
details = bodyJSON.Get("details").MustMap(map[string]interface{}{})
}
require.Nil(t, notifierErr)
require.Nil(t, alertErr)
require.Equal(t, tags, []string{"keyOnly", "aKey:aValue"})
require.Equal(t, details, map[string]interface{}{"keyOnly": "", "aKey": "aValue", "url": ""})
2016-12-06 14:48:13 -06:00
})
})
}