mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-24 17:04:42 +01:00
fix(alerting): Reuse MatrixProviderConfig struct
This commit is contained in:
parent
755c8bb43a
commit
09c3a6c72b
@ -16,12 +16,7 @@ import (
|
||||
|
||||
// AlertProvider is the configuration necessary for sending an alert using Matrix
|
||||
type AlertProvider struct {
|
||||
// HomeserverURL is the custom homeserver to use (optional)
|
||||
HomeserverURL string `yaml:"homeserver-url"`
|
||||
// AccessToken is the bot user's access token to send messages
|
||||
AccessToken string `yaml:"access-token"`
|
||||
// InternalRoomID is the room that the bot user has permissions to send messages to
|
||||
InternalRoomID string `yaml:"internal-room-id"`
|
||||
MatrixProviderConfig `yaml:",inline"`
|
||||
|
||||
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
|
||||
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
|
||||
@ -34,16 +29,19 @@ type AlertProvider struct {
|
||||
type Override struct {
|
||||
Group string `yaml:"group"`
|
||||
|
||||
HomeserverURL string `yaml:"homeserver-url"`
|
||||
AccessToken string `yaml:"access-token"`
|
||||
InternalRoomID string `yaml:"internal-room-id"`
|
||||
MatrixProviderConfig `yaml:",inline"`
|
||||
}
|
||||
|
||||
const defaultHomeserverURL = "https://matrix-client.matrix.org"
|
||||
|
||||
type matrixProviderConfig struct {
|
||||
HomeserverURL string `yaml:"homeserver-url"`
|
||||
type MatrixProviderConfig struct {
|
||||
// ServerURL is the custom homeserver to use (optional)
|
||||
ServerURL string `yaml:"server-url"`
|
||||
|
||||
// AccessToken is the bot user's access token to send messages
|
||||
AccessToken string `yaml:"access-token"`
|
||||
|
||||
// InternalRoomID is the room that the bot user has permissions to send messages to
|
||||
InternalRoomID string `yaml:"internal-room-id"`
|
||||
}
|
||||
|
||||
@ -65,14 +63,14 @@ func (provider *AlertProvider) IsValid() bool {
|
||||
func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) error {
|
||||
buffer := bytes.NewBuffer([]byte(provider.buildRequestBody(endpoint, alert, result, resolved)))
|
||||
config := provider.getConfigForGroup(endpoint.Group)
|
||||
if config.HomeserverURL == "" {
|
||||
config.HomeserverURL = defaultHomeserverURL
|
||||
if config.ServerURL == "" {
|
||||
config.ServerURL = defaultHomeserverURL
|
||||
}
|
||||
txnId := randStringBytes(24)
|
||||
request, err := http.NewRequest(
|
||||
http.MethodPut,
|
||||
fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s?access_token=%s",
|
||||
config.HomeserverURL,
|
||||
config.ServerURL,
|
||||
url.PathEscape(config.InternalRoomID),
|
||||
txnId,
|
||||
url.QueryEscape(config.AccessToken),
|
||||
@ -156,20 +154,20 @@ func buildHTMLMessageBody(endpoint *core.Endpoint, alert *alert.Alert, result *c
|
||||
}
|
||||
|
||||
// getConfigForGroup returns the appropriate configuration for a given group
|
||||
func (provider *AlertProvider) getConfigForGroup(group string) matrixProviderConfig {
|
||||
func (provider *AlertProvider) getConfigForGroup(group string) MatrixProviderConfig {
|
||||
if provider.Overrides != nil {
|
||||
for _, override := range provider.Overrides {
|
||||
if group == override.Group {
|
||||
return matrixProviderConfig{
|
||||
HomeserverURL: override.HomeserverURL,
|
||||
return MatrixProviderConfig{
|
||||
ServerURL: override.ServerURL,
|
||||
AccessToken: override.AccessToken,
|
||||
InternalRoomID: override.InternalRoomID,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return matrixProviderConfig{
|
||||
HomeserverURL: provider.HomeserverURL,
|
||||
return MatrixProviderConfig{
|
||||
ServerURL: provider.ServerURL,
|
||||
AccessToken: provider.AccessToken,
|
||||
InternalRoomID: provider.InternalRoomID,
|
||||
}
|
||||
|
@ -12,15 +12,31 @@ import (
|
||||
)
|
||||
|
||||
func TestAlertProvider_IsValid(t *testing.T) {
|
||||
invalidProvider := AlertProvider{AccessToken: "", InternalRoomID: ""}
|
||||
invalidProvider := AlertProvider{
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
AccessToken: "",
|
||||
InternalRoomID: "",
|
||||
},
|
||||
}
|
||||
if invalidProvider.IsValid() {
|
||||
t.Error("provider shouldn't have been valid")
|
||||
}
|
||||
validProvider := AlertProvider{AccessToken: "1", InternalRoomID: "!a:example.com"}
|
||||
validProvider := AlertProvider{
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
}
|
||||
if !validProvider.IsValid() {
|
||||
t.Error("provider should've been valid")
|
||||
}
|
||||
validProviderWithHomeserver := AlertProvider{HomeserverURL: "https://example.com", AccessToken: "1", InternalRoomID: "!a:example.com"}
|
||||
validProviderWithHomeserver := AlertProvider{
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
}
|
||||
if !validProviderWithHomeserver.IsValid() {
|
||||
t.Error("provider with homeserver should've been valid")
|
||||
}
|
||||
@ -30,9 +46,11 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) {
|
||||
providerWithInvalidOverrideGroup := AlertProvider{
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
AccessToken: "",
|
||||
InternalRoomID: "",
|
||||
Group: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -42,9 +60,11 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) {
|
||||
providerWithInvalidOverrideTo := AlertProvider{
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "group",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
AccessToken: "",
|
||||
InternalRoomID: "",
|
||||
Group: "group",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -52,14 +72,18 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) {
|
||||
t.Error("provider integration key shouldn't have been valid")
|
||||
}
|
||||
providerWithValidOverride := AlertProvider{
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
HomeserverURL: "https://example.com",
|
||||
Group: "group",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
Group: "group",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -208,19 +232,21 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
||||
Name string
|
||||
Provider AlertProvider
|
||||
InputGroup string
|
||||
ExpectedOutput matrixProviderConfig
|
||||
ExpectedOutput MatrixProviderConfig
|
||||
}{
|
||||
{
|
||||
Name: "provider-no-override-specify-no-group-should-default",
|
||||
Provider: AlertProvider{
|
||||
HomeserverURL: "https://example.com",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
Overrides: nil,
|
||||
},
|
||||
InputGroup: "",
|
||||
ExpectedOutput: matrixProviderConfig{
|
||||
HomeserverURL: "https://example.com",
|
||||
ExpectedOutput: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
@ -228,14 +254,16 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
||||
{
|
||||
Name: "provider-no-override-specify-group-should-default",
|
||||
Provider: AlertProvider{
|
||||
HomeserverURL: "https://example.com",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
Overrides: nil,
|
||||
},
|
||||
InputGroup: "group",
|
||||
ExpectedOutput: matrixProviderConfig{
|
||||
HomeserverURL: "https://example.com",
|
||||
ExpectedOutput: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
@ -243,21 +271,25 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
||||
{
|
||||
Name: "provider-with-override-specify-no-group-should-default",
|
||||
Provider: AlertProvider{
|
||||
HomeserverURL: "https://example.com",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "group",
|
||||
HomeserverURL: "https://example01.com",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example01.com",
|
||||
AccessToken: "12",
|
||||
InternalRoomID: "!a:example01.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
InputGroup: "",
|
||||
ExpectedOutput: matrixProviderConfig{
|
||||
HomeserverURL: "https://example.com",
|
||||
ExpectedOutput: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
@ -265,21 +297,25 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
||||
{
|
||||
Name: "provider-with-override-specify-group-should-override",
|
||||
Provider: AlertProvider{
|
||||
HomeserverURL: "https://example.com",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
Overrides: []Override{
|
||||
{
|
||||
Group: "group",
|
||||
HomeserverURL: "https://example01.com",
|
||||
MatrixProviderConfig: MatrixProviderConfig{
|
||||
ServerURL: "https://example01.com",
|
||||
AccessToken: "12",
|
||||
InternalRoomID: "!a:example01.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
InputGroup: "group",
|
||||
ExpectedOutput: matrixProviderConfig{
|
||||
HomeserverURL: "https://example01.com",
|
||||
ExpectedOutput: MatrixProviderConfig{
|
||||
ServerURL: "https://example01.com",
|
||||
AccessToken: "12",
|
||||
InternalRoomID: "!a:example01.com",
|
||||
},
|
||||
|
@ -318,12 +318,14 @@ func TestHandleAlertingWithProviderThatReturnsAnError(t *testing.T) {
|
||||
AlertType: alert.TypeMatrix,
|
||||
AlertingConfig: &alerting.Config{
|
||||
Matrix: &matrix.AlertProvider{
|
||||
HomeserverURL: "https://example.com",
|
||||
MatrixProviderConfig: matrix.MatrixProviderConfig{
|
||||
ServerURL: "https://example.com",
|
||||
AccessToken: "1",
|
||||
InternalRoomID: "!a:example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, scenario := range scenarios {
|
||||
|
Loading…
Reference in New Issue
Block a user