Start working on implementing common provider interface
This commit is contained in:
108
config/config.go
108
config/config.go
@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"github.com/TwinProduction/gatus/alerting"
|
||||
"github.com/TwinProduction/gatus/alerting/provider"
|
||||
"github.com/TwinProduction/gatus/core"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
@ -99,26 +100,95 @@ func validateAlertingConfig(config *Config) {
|
||||
log.Printf("[config][validateAlertingConfig] Alerting is not configured")
|
||||
return
|
||||
}
|
||||
alertTypes := []core.AlertType{
|
||||
core.SlackAlert,
|
||||
core.TwilioAlert,
|
||||
core.PagerDutyAlert,
|
||||
core.CustomAlert,
|
||||
}
|
||||
var validProviders, invalidProviders []core.AlertType
|
||||
if config.Alerting.Slack != nil && config.Alerting.Slack.IsValid() {
|
||||
validProviders = append(validProviders, core.SlackAlert)
|
||||
} else {
|
||||
invalidProviders = append(invalidProviders, core.SlackAlert)
|
||||
}
|
||||
if config.Alerting.Twilio != nil && config.Alerting.Twilio.IsValid() {
|
||||
validProviders = append(validProviders, core.TwilioAlert)
|
||||
} else {
|
||||
invalidProviders = append(invalidProviders, core.TwilioAlert)
|
||||
}
|
||||
if config.Alerting.PagerDuty != nil && config.Alerting.PagerDuty.IsValid() {
|
||||
validProviders = append(validProviders, core.PagerDutyAlert)
|
||||
} else {
|
||||
invalidProviders = append(invalidProviders, core.PagerDutyAlert)
|
||||
}
|
||||
if config.Alerting.Custom != nil && config.Alerting.Custom.IsValid() {
|
||||
validProviders = append(validProviders, core.CustomAlert)
|
||||
} else {
|
||||
invalidProviders = append(invalidProviders, core.CustomAlert)
|
||||
for _, alertType := range alertTypes {
|
||||
alertProvider := GetAlertingProviderByAlertType(config, alertType)
|
||||
if alertProvider != nil {
|
||||
if alertProvider.IsValid() {
|
||||
validProviders = append(validProviders, alertType)
|
||||
} else {
|
||||
log.Printf("[config][validateAlertingConfig] Ignoring provider=%s because configuration is invalid", alertType)
|
||||
invalidProviders = append(invalidProviders, alertType)
|
||||
}
|
||||
} else {
|
||||
invalidProviders = append(invalidProviders, alertType)
|
||||
}
|
||||
}
|
||||
//if config.Alerting.Slack != nil {
|
||||
// if config.Alerting.Slack.IsValid() {
|
||||
// validProviders = append(validProviders, core.SlackAlert)
|
||||
// } else {
|
||||
// log.Printf("[config][validateAlertingConfig] Ignoring provider=%s because configuration is invalid", core.SlackAlert)
|
||||
// invalidProviders = append(invalidProviders, core.SlackAlert)
|
||||
// }
|
||||
//} else {
|
||||
// invalidProviders = append(invalidProviders, core.SlackAlert)
|
||||
//}
|
||||
//if config.Alerting.Twilio != nil {
|
||||
// if config.Alerting.Twilio.IsValid() {
|
||||
// validProviders = append(validProviders, core.TwilioAlert)
|
||||
// } else {
|
||||
// log.Printf("[config][validateAlertingConfig] Ignoring provider=%s because configuration is invalid", core.TwilioAlert)
|
||||
// invalidProviders = append(invalidProviders, core.TwilioAlert)
|
||||
// }
|
||||
//} else {
|
||||
// invalidProviders = append(invalidProviders, core.TwilioAlert)
|
||||
//}
|
||||
//if config.Alerting.PagerDuty != nil {
|
||||
// if config.Alerting.PagerDuty.IsValid() {
|
||||
// validProviders = append(validProviders, core.PagerDutyAlert)
|
||||
// } else {
|
||||
// log.Printf("[config][validateAlertingConfig] Ignoring provider=%s because configuration is invalid", core.PagerDutyAlert)
|
||||
// invalidProviders = append(invalidProviders, core.PagerDutyAlert)
|
||||
// }
|
||||
//} else {
|
||||
// invalidProviders = append(invalidProviders, core.PagerDutyAlert)
|
||||
//}
|
||||
//if config.Alerting.Custom != nil {
|
||||
// if config.Alerting.Custom.IsValid() {
|
||||
// validProviders = append(validProviders, core.CustomAlert)
|
||||
// } else {
|
||||
// log.Printf("[config][validateAlertingConfig] Ignoring provider=%s because configuration is invalid", core.CustomAlert)
|
||||
// invalidProviders = append(invalidProviders, core.CustomAlert)
|
||||
// }
|
||||
//} else {
|
||||
// invalidProviders = append(invalidProviders, core.CustomAlert)
|
||||
//}
|
||||
log.Printf("[config][validateAlertingConfig] configuredProviders=%s; ignoredProviders=%s", validProviders, invalidProviders)
|
||||
}
|
||||
|
||||
func GetAlertingProviderByAlertType(config *Config, alertType core.AlertType) provider.AlertProvider {
|
||||
switch alertType {
|
||||
case core.SlackAlert:
|
||||
if config.Alerting.Slack == nil {
|
||||
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||
return nil
|
||||
}
|
||||
return config.Alerting.Slack
|
||||
case core.TwilioAlert:
|
||||
if config.Alerting.Twilio == nil {
|
||||
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||
return nil
|
||||
}
|
||||
return config.Alerting.Twilio
|
||||
case core.PagerDutyAlert:
|
||||
if config.Alerting.PagerDuty == nil {
|
||||
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||
return nil
|
||||
}
|
||||
return config.Alerting.PagerDuty
|
||||
case core.CustomAlert:
|
||||
if config.Alerting.Custom == nil {
|
||||
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||
return nil
|
||||
}
|
||||
return config.Alerting.Custom
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TwinProduction/gatus/core"
|
||||
"testing"
|
||||
"time"
|
||||
@ -36,7 +35,6 @@ services:
|
||||
if config.Services[1].Url != "https://api.github.com/healthz" {
|
||||
t.Errorf("URL should have been %s", "https://api.github.com/healthz")
|
||||
}
|
||||
fmt.Println(config.Services[0].Interval)
|
||||
if config.Services[0].Interval != 15*time.Second {
|
||||
t.Errorf("Interval should have been %s", 15*time.Second)
|
||||
}
|
||||
@ -123,6 +121,8 @@ func TestParseAndValidateConfigBytesWithAlerting(t *testing.T) {
|
||||
alerting:
|
||||
slack:
|
||||
webhook-url: "http://example.com"
|
||||
pagerduty:
|
||||
integration-key: "00000000000000000000000000000000"
|
||||
services:
|
||||
- name: twinnation
|
||||
url: https://twinnation.org/actuator/health
|
||||
@ -144,10 +144,19 @@ services:
|
||||
t.Error("Metrics should've been false by default")
|
||||
}
|
||||
if config.Alerting == nil {
|
||||
t.Fatal("config.AlertingConfig shouldn't have been nil")
|
||||
t.Fatal("config.Alerting shouldn't have been nil")
|
||||
}
|
||||
if config.Alerting.Slack == nil || !config.Alerting.Slack.IsValid() {
|
||||
t.Fatal("Slack alerting config should've been valid")
|
||||
}
|
||||
if config.Alerting.Slack.WebhookUrl != "http://example.com" {
|
||||
t.Errorf("Slack webhook should've been %s, but was %s", "http://example.com", config.Alerting.Slack)
|
||||
t.Errorf("Slack webhook should've been %s, but was %s", "http://example.com", config.Alerting.Slack.WebhookUrl)
|
||||
}
|
||||
if config.Alerting.PagerDuty == nil || !config.Alerting.PagerDuty.IsValid() {
|
||||
t.Fatal("PagerDuty alerting config should've been valid")
|
||||
}
|
||||
if config.Alerting.PagerDuty.IntegrationKey != "00000000000000000000000000000000" {
|
||||
t.Errorf("PagerDuty integration key should've been %s, but was %s", "00000000000000000000000000000000", config.Alerting.PagerDuty.IntegrationKey)
|
||||
}
|
||||
if len(config.Services) != 1 {
|
||||
t.Error("There should've been 1 service")
|
||||
@ -180,3 +189,31 @@ services:
|
||||
t.Errorf("The type of the alert should've been %s, but it was %s", "Healthcheck failed 7 times in a row", config.Services[0].Alerts[0].Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAndValidateConfigBytesWithInvalidPagerDutyAlertingConfig(t *testing.T) {
|
||||
config, err := parseAndValidateConfigBytes([]byte(`
|
||||
alerting:
|
||||
pagerduty:
|
||||
integration-key: "INVALID_KEY"
|
||||
services:
|
||||
- name: twinnation
|
||||
url: https://twinnation.org/actuator/health
|
||||
conditions:
|
||||
- "[STATUS] == 200"
|
||||
`))
|
||||
if err != nil {
|
||||
t.Error("No error should've been returned")
|
||||
}
|
||||
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.PagerDuty == nil {
|
||||
t.Fatal("PagerDuty alerting config shouldn't have been nil")
|
||||
}
|
||||
if config.Alerting.PagerDuty.IsValid() {
|
||||
t.Fatal("PagerDuty alerting config should've been invalid")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user