Move away from generic solution to a fixed one
This commit is contained in:
parent
d7d904ae5f
commit
1d21f5889d
@ -24,13 +24,6 @@ type AlertProvider struct {
|
|||||||
Placeholders map[string]map[string]string `yaml:"placeholders,omitempty"`
|
Placeholders map[string]map[string]string `yaml:"placeholders,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultPlaceholderValues = map[string]map[string]string{
|
|
||||||
"ALERT_TRIGGERED_OR_RESOLVED": map[string]string{
|
|
||||||
"triggered": "TRIGGERED",
|
|
||||||
"resolved": "RESOLVED",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValid returns whether the provider's configuration is valid
|
// IsValid returns whether the provider's configuration is valid
|
||||||
func (provider *AlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.URL) > 0
|
return len(provider.URL) > 0
|
||||||
@ -41,21 +34,24 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler
|
|||||||
return provider
|
return provider
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPlaceholderValue returns the Placeholder value
|
// GetPlaceholderValue returns the Placeholder value for ALERT_TRIGGERED_OR_RESOLVED if configured
|
||||||
func (provider *AlertProvider) GetPlaceholderValue(name, status string) string {
|
func (provider *AlertProvider) GetAlertStatePlaceholderValue(resolved bool) string {
|
||||||
if _, ok := provider.Placeholders[name]; ok {
|
status := "triggered"
|
||||||
if val, ok := provider.Placeholders[name][status]; ok {
|
if resolved {
|
||||||
|
status = "resolved"
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := provider.Placeholders["ALERT_TRIGGERED_OR_RESOLVED"]; ok {
|
||||||
|
if val, ok := provider.Placeholders["ALERT_TRIGGERED_OR_RESOLVED"][status]; ok {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if _, ok := DefaultPlaceholderValues[name]; ok {
|
|
||||||
if val, ok := DefaultPlaceholderValues[name][status]; ok {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
if resolved {
|
||||||
|
return "RESOLVED"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "TRIGGERED"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request {
|
func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request {
|
||||||
@ -71,9 +67,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
|
|||||||
}
|
}
|
||||||
if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
||||||
if resolved {
|
if resolved {
|
||||||
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "resolved"))
|
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(true))
|
||||||
} else {
|
} else {
|
||||||
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "triggered"))
|
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") {
|
if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") {
|
||||||
@ -84,9 +80,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
|
|||||||
}
|
}
|
||||||
if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
||||||
if resolved {
|
if resolved {
|
||||||
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "resolved"))
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(true))
|
||||||
} else {
|
} else {
|
||||||
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "triggered"))
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(method) == 0 {
|
if len(method) == 0 {
|
||||||
|
@ -94,7 +94,7 @@ func TestAlertProvider_buildHTTPRequestWithCustomPlaceholder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertProvider_GetPlaceholderValue(t *testing.T) {
|
func TestAlertProvider_GetAlertStatePlaceholderValueDefaults(t *testing.T) {
|
||||||
customAlertProvider := &AlertProvider{
|
customAlertProvider := &AlertProvider{
|
||||||
URL: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
URL: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
|
||||||
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
||||||
@ -102,15 +102,11 @@ func TestAlertProvider_GetPlaceholderValue(t *testing.T) {
|
|||||||
Placeholders: nil,
|
Placeholders: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
if customAlertProvider.GetPlaceholderValue("I_DO_NOT_EXIST", "i_do_also_no_exist") != "" {
|
if customAlertProvider.GetAlertStatePlaceholderValue(true) != "RESOLVED" {
|
||||||
t.Error("expected empty response from a non existing placeholder")
|
t.Error("expected here actually RESOLVED")
|
||||||
}
|
}
|
||||||
|
|
||||||
if customAlertProvider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "I_DO_NOT_EXIST") != "" {
|
if customAlertProvider.GetAlertStatePlaceholderValue(false) != "TRIGGERED" {
|
||||||
t.Error("expected empty response from a non existing subvalue")
|
t.Error("expected here actually TRIGGERED")
|
||||||
}
|
|
||||||
|
|
||||||
if customAlertProvider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "triggered") != "TRIGGERED" {
|
|
||||||
t.Error("expected 'triggered' as a result")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user