From b603cdb0ea0ca1c3728665d18bfc99ac233fbb05 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Thu, 18 Feb 2021 23:17:51 -0500 Subject: [PATCH] Minor changes to the custom alert provider placeholders --- alerting/provider/custom/custom.go | 14 ++-- config/config_test.go | 106 ++++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 21 deletions(-) diff --git a/alerting/provider/custom/custom.go b/alerting/provider/custom/custom.go index d2c8e768..7c390506 100644 --- a/alerting/provider/custom/custom.go +++ b/alerting/provider/custom/custom.go @@ -34,24 +34,18 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler return provider } -// GetPlaceholderValue returns the Placeholder value for ALERT_TRIGGERED_OR_RESOLVED if configured +// GetAlertStatePlaceholderValue returns the Placeholder value for ALERT_TRIGGERED_OR_RESOLVED if configured func (provider *AlertProvider) GetAlertStatePlaceholderValue(resolved bool) string { - status := "triggered" + status := "TRIGGERED" if resolved { - status = "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 } } - - if resolved { - return "RESOLVED" - } - - return "TRIGGERED" + return status } func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request { diff --git a/config/config_test.go b/config/config_test.go index 538309fe..a53fd44d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -344,11 +344,6 @@ alerting: access-key: "1" originator: "31619191918" recipients: "31619191919" - custom: - placeholders: - ALERT_TRIGGERED_OR_RESOLVED: - triggered: "partial_outage" - resolved: "operational" services: - name: twinnation url: https://twinnation.org/health @@ -401,12 +396,6 @@ services: if config.Alerting.Messagebird.Recipients != "31619191919" { t.Errorf("Messagebird to recipients should've been %s, but was %s", "31619191919", config.Alerting.Messagebird.Recipients) } - if config.Alerting.Custom == nil { - t.Fatal("config.Alerting. Custom shouldn't have been nil") - } - if config.Alerting.Custom.Placeholders == nil { - t.Fatal("config.Alerting.Custom.Placeholders shouldn't have been nil") - } if len(config.Services) != 1 { t.Error("There should've been 1 service") } @@ -513,11 +502,106 @@ services: if !config.Alerting.Custom.IsValid() { t.Fatal("Custom alerting config should've been valid") } + if config.Alerting.Custom.GetAlertStatePlaceholderValue(true) != "RESOLVED" { + t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'RESOLVED', got", config.Alerting.Custom.GetAlertStatePlaceholderValue(true)) + } + if config.Alerting.Custom.GetAlertStatePlaceholderValue(false) != "TRIGGERED" { + t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'TRIGGERED', got", config.Alerting.Custom.GetAlertStatePlaceholderValue(false)) + } if config.Alerting.Custom.Insecure { t.Fatal("config.Alerting.Custom.Insecure shouldn't have been true") } } +func TestParseAndValidateConfigBytesWithCustomAlertingConfigAndCustomPlaceholderValues(t *testing.T) { + config, err := parseAndValidateConfigBytes([]byte(` +alerting: + custom: + placeholders: + ALERT_TRIGGERED_OR_RESOLVED: + TRIGGERED: "partial_outage" + RESOLVED: "operational" + url: "https://example.com" + insecure: true + body: "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]" +services: + - name: twinnation + url: https://twinnation.org/health + alerts: + - type: custom + conditions: + - "[STATUS] == 200" +`)) + if err != nil { + t.Error("expected no error, got", err.Error()) + } + if config == nil { + t.Fatal("Config shouldn't have been nil") + } + if config.Alerting == nil { + t.Fatal("config.Alerting shouldn't have been nil") + } + if config.Alerting.Custom == nil { + t.Fatal("PagerDuty alerting config shouldn't have been nil") + } + if !config.Alerting.Custom.IsValid() { + t.Fatal("Custom alerting config should've been valid") + } + if config.Alerting.Custom.GetAlertStatePlaceholderValue(true) != "operational" { + t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'operational'") + } + if config.Alerting.Custom.GetAlertStatePlaceholderValue(false) != "partial_outage" { + t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'partial_outage'") + } + if !config.Alerting.Custom.Insecure { + t.Fatal("config.Alerting.Custom.Insecure shouldn't have been true") + } +} + +func TestParseAndValidateConfigBytesWithCustomAlertingConfigAndOneCustomPlaceholderValue(t *testing.T) { + config, err := parseAndValidateConfigBytes([]byte(` +alerting: + custom: + placeholders: + ALERT_TRIGGERED_OR_RESOLVED: + TRIGGERED: "partial_outage" + url: "https://example.com" + insecure: true + body: "[ALERT_TRIGGERED_OR_RESOLVED]: [SERVICE_NAME] - [ALERT_DESCRIPTION]" +services: + - name: twinnation + url: https://twinnation.org/health + alerts: + - type: custom + conditions: + - "[STATUS] == 200" +`)) + if err != nil { + t.Error("expected no error, got", err.Error()) + } + if config == nil { + t.Fatal("Config shouldn't have been nil") + } + if config.Alerting == nil { + t.Fatal("config.Alerting shouldn't have been nil") + } + if config.Alerting.Custom == nil { + t.Fatal("PagerDuty alerting config shouldn't have been nil") + } + if !config.Alerting.Custom.IsValid() { + t.Fatal("Custom alerting config should've been valid") + } + if config.Alerting.Custom.GetAlertStatePlaceholderValue(true) != "RESOLVED" { + t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for RESOLVED should've been 'RESOLVED'") + } + if config.Alerting.Custom.GetAlertStatePlaceholderValue(false) != "partial_outage" { + t.Fatal("ALERT_TRIGGERED_OR_RESOLVED placeholder value for TRIGGERED should've been 'partial_outage'") + } + if !config.Alerting.Custom.Insecure { + t.Fatal("config.Alerting.Custom.Insecure shouldn't have been true") + } +} + func TestParseAndValidateConfigBytesWithCustomAlertingConfigThatHasInsecureSetToTrue(t *testing.T) { config, err := parseAndValidateConfigBytes([]byte(` alerting: