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
|
// AlertProvider is the configuration necessary for sending an alert using Matrix
|
||||||
type AlertProvider struct {
|
type AlertProvider struct {
|
||||||
// HomeserverURL is the custom homeserver to use (optional)
|
MatrixProviderConfig `yaml:",inline"`
|
||||||
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"`
|
|
||||||
|
|
||||||
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
|
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
|
||||||
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
|
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
|
||||||
@ -34,16 +29,19 @@ type AlertProvider struct {
|
|||||||
type Override struct {
|
type Override struct {
|
||||||
Group string `yaml:"group"`
|
Group string `yaml:"group"`
|
||||||
|
|
||||||
HomeserverURL string `yaml:"homeserver-url"`
|
MatrixProviderConfig `yaml:",inline"`
|
||||||
AccessToken string `yaml:"access-token"`
|
|
||||||
InternalRoomID string `yaml:"internal-room-id"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultHomeserverURL = "https://matrix-client.matrix.org"
|
const defaultHomeserverURL = "https://matrix-client.matrix.org"
|
||||||
|
|
||||||
type matrixProviderConfig struct {
|
type MatrixProviderConfig struct {
|
||||||
HomeserverURL string `yaml:"homeserver-url"`
|
// ServerURL is the custom homeserver to use (optional)
|
||||||
AccessToken string `yaml:"access-token"`
|
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"`
|
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 {
|
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)))
|
buffer := bytes.NewBuffer([]byte(provider.buildRequestBody(endpoint, alert, result, resolved)))
|
||||||
config := provider.getConfigForGroup(endpoint.Group)
|
config := provider.getConfigForGroup(endpoint.Group)
|
||||||
if config.HomeserverURL == "" {
|
if config.ServerURL == "" {
|
||||||
config.HomeserverURL = defaultHomeserverURL
|
config.ServerURL = defaultHomeserverURL
|
||||||
}
|
}
|
||||||
txnId := randStringBytes(24)
|
txnId := randStringBytes(24)
|
||||||
request, err := http.NewRequest(
|
request, err := http.NewRequest(
|
||||||
http.MethodPut,
|
http.MethodPut,
|
||||||
fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s?access_token=%s",
|
fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message/%s?access_token=%s",
|
||||||
config.HomeserverURL,
|
config.ServerURL,
|
||||||
url.PathEscape(config.InternalRoomID),
|
url.PathEscape(config.InternalRoomID),
|
||||||
txnId,
|
txnId,
|
||||||
url.QueryEscape(config.AccessToken),
|
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
|
// 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 {
|
if provider.Overrides != nil {
|
||||||
for _, override := range provider.Overrides {
|
for _, override := range provider.Overrides {
|
||||||
if group == override.Group {
|
if group == override.Group {
|
||||||
return matrixProviderConfig{
|
return MatrixProviderConfig{
|
||||||
HomeserverURL: override.HomeserverURL,
|
ServerURL: override.ServerURL,
|
||||||
AccessToken: override.AccessToken,
|
AccessToken: override.AccessToken,
|
||||||
InternalRoomID: override.InternalRoomID,
|
InternalRoomID: override.InternalRoomID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matrixProviderConfig{
|
return MatrixProviderConfig{
|
||||||
HomeserverURL: provider.HomeserverURL,
|
ServerURL: provider.ServerURL,
|
||||||
AccessToken: provider.AccessToken,
|
AccessToken: provider.AccessToken,
|
||||||
InternalRoomID: provider.InternalRoomID,
|
InternalRoomID: provider.InternalRoomID,
|
||||||
}
|
}
|
||||||
|
@ -12,15 +12,31 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestAlertProvider_IsValid(t *testing.T) {
|
func TestAlertProvider_IsValid(t *testing.T) {
|
||||||
invalidProvider := AlertProvider{AccessToken: "", InternalRoomID: ""}
|
invalidProvider := AlertProvider{
|
||||||
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
|
AccessToken: "",
|
||||||
|
InternalRoomID: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
if invalidProvider.IsValid() {
|
if invalidProvider.IsValid() {
|
||||||
t.Error("provider shouldn't have been valid")
|
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() {
|
if !validProvider.IsValid() {
|
||||||
t.Error("provider should've been valid")
|
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() {
|
if !validProviderWithHomeserver.IsValid() {
|
||||||
t.Error("provider with homeserver should've been valid")
|
t.Error("provider with homeserver should've been valid")
|
||||||
}
|
}
|
||||||
@ -30,9 +46,11 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) {
|
|||||||
providerWithInvalidOverrideGroup := AlertProvider{
|
providerWithInvalidOverrideGroup := AlertProvider{
|
||||||
Overrides: []Override{
|
Overrides: []Override{
|
||||||
{
|
{
|
||||||
AccessToken: "",
|
Group: "",
|
||||||
InternalRoomID: "",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
Group: "",
|
AccessToken: "",
|
||||||
|
InternalRoomID: "",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -42,9 +60,11 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) {
|
|||||||
providerWithInvalidOverrideTo := AlertProvider{
|
providerWithInvalidOverrideTo := AlertProvider{
|
||||||
Overrides: []Override{
|
Overrides: []Override{
|
||||||
{
|
{
|
||||||
AccessToken: "",
|
Group: "group",
|
||||||
InternalRoomID: "",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
Group: "group",
|
AccessToken: "",
|
||||||
|
InternalRoomID: "",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -52,14 +72,18 @@ func TestAlertProvider_IsValidWithOverride(t *testing.T) {
|
|||||||
t.Error("provider integration key shouldn't have been valid")
|
t.Error("provider integration key shouldn't have been valid")
|
||||||
}
|
}
|
||||||
providerWithValidOverride := AlertProvider{
|
providerWithValidOverride := AlertProvider{
|
||||||
AccessToken: "1",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
InternalRoomID: "!a:example.com",
|
AccessToken: "1",
|
||||||
|
InternalRoomID: "!a:example.com",
|
||||||
|
},
|
||||||
Overrides: []Override{
|
Overrides: []Override{
|
||||||
{
|
{
|
||||||
HomeserverURL: "https://example.com",
|
Group: "group",
|
||||||
AccessToken: "1",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
InternalRoomID: "!a:example.com",
|
ServerURL: "https://example.com",
|
||||||
Group: "group",
|
AccessToken: "1",
|
||||||
|
InternalRoomID: "!a:example.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -208,19 +232,21 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
|||||||
Name string
|
Name string
|
||||||
Provider AlertProvider
|
Provider AlertProvider
|
||||||
InputGroup string
|
InputGroup string
|
||||||
ExpectedOutput matrixProviderConfig
|
ExpectedOutput MatrixProviderConfig
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Name: "provider-no-override-specify-no-group-should-default",
|
Name: "provider-no-override-specify-no-group-should-default",
|
||||||
Provider: AlertProvider{
|
Provider: AlertProvider{
|
||||||
HomeserverURL: "https://example.com",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
AccessToken: "1",
|
ServerURL: "https://example.com",
|
||||||
InternalRoomID: "!a:example.com",
|
AccessToken: "1",
|
||||||
Overrides: nil,
|
InternalRoomID: "!a:example.com",
|
||||||
|
},
|
||||||
|
Overrides: nil,
|
||||||
},
|
},
|
||||||
InputGroup: "",
|
InputGroup: "",
|
||||||
ExpectedOutput: matrixProviderConfig{
|
ExpectedOutput: MatrixProviderConfig{
|
||||||
HomeserverURL: "https://example.com",
|
ServerURL: "https://example.com",
|
||||||
AccessToken: "1",
|
AccessToken: "1",
|
||||||
InternalRoomID: "!a:example.com",
|
InternalRoomID: "!a:example.com",
|
||||||
},
|
},
|
||||||
@ -228,14 +254,16 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "provider-no-override-specify-group-should-default",
|
Name: "provider-no-override-specify-group-should-default",
|
||||||
Provider: AlertProvider{
|
Provider: AlertProvider{
|
||||||
HomeserverURL: "https://example.com",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
AccessToken: "1",
|
ServerURL: "https://example.com",
|
||||||
InternalRoomID: "!a:example.com",
|
AccessToken: "1",
|
||||||
Overrides: nil,
|
InternalRoomID: "!a:example.com",
|
||||||
|
},
|
||||||
|
Overrides: nil,
|
||||||
},
|
},
|
||||||
InputGroup: "group",
|
InputGroup: "group",
|
||||||
ExpectedOutput: matrixProviderConfig{
|
ExpectedOutput: MatrixProviderConfig{
|
||||||
HomeserverURL: "https://example.com",
|
ServerURL: "https://example.com",
|
||||||
AccessToken: "1",
|
AccessToken: "1",
|
||||||
InternalRoomID: "!a:example.com",
|
InternalRoomID: "!a:example.com",
|
||||||
},
|
},
|
||||||
@ -243,21 +271,25 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "provider-with-override-specify-no-group-should-default",
|
Name: "provider-with-override-specify-no-group-should-default",
|
||||||
Provider: AlertProvider{
|
Provider: AlertProvider{
|
||||||
HomeserverURL: "https://example.com",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
AccessToken: "1",
|
ServerURL: "https://example.com",
|
||||||
InternalRoomID: "!a:example.com",
|
AccessToken: "1",
|
||||||
|
InternalRoomID: "!a:example.com",
|
||||||
|
},
|
||||||
Overrides: []Override{
|
Overrides: []Override{
|
||||||
{
|
{
|
||||||
Group: "group",
|
Group: "group",
|
||||||
HomeserverURL: "https://example01.com",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
AccessToken: "12",
|
ServerURL: "https://example01.com",
|
||||||
InternalRoomID: "!a:example01.com",
|
AccessToken: "12",
|
||||||
|
InternalRoomID: "!a:example01.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
InputGroup: "",
|
InputGroup: "",
|
||||||
ExpectedOutput: matrixProviderConfig{
|
ExpectedOutput: MatrixProviderConfig{
|
||||||
HomeserverURL: "https://example.com",
|
ServerURL: "https://example.com",
|
||||||
AccessToken: "1",
|
AccessToken: "1",
|
||||||
InternalRoomID: "!a:example.com",
|
InternalRoomID: "!a:example.com",
|
||||||
},
|
},
|
||||||
@ -265,21 +297,25 @@ func TestAlertProvider_getConfigForGroup(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Name: "provider-with-override-specify-group-should-override",
|
Name: "provider-with-override-specify-group-should-override",
|
||||||
Provider: AlertProvider{
|
Provider: AlertProvider{
|
||||||
HomeserverURL: "https://example.com",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
AccessToken: "1",
|
ServerURL: "https://example.com",
|
||||||
InternalRoomID: "!a:example.com",
|
AccessToken: "1",
|
||||||
|
InternalRoomID: "!a:example.com",
|
||||||
|
},
|
||||||
Overrides: []Override{
|
Overrides: []Override{
|
||||||
{
|
{
|
||||||
Group: "group",
|
Group: "group",
|
||||||
HomeserverURL: "https://example01.com",
|
MatrixProviderConfig: MatrixProviderConfig{
|
||||||
AccessToken: "12",
|
ServerURL: "https://example01.com",
|
||||||
InternalRoomID: "!a:example01.com",
|
AccessToken: "12",
|
||||||
|
InternalRoomID: "!a:example01.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
InputGroup: "group",
|
InputGroup: "group",
|
||||||
ExpectedOutput: matrixProviderConfig{
|
ExpectedOutput: MatrixProviderConfig{
|
||||||
HomeserverURL: "https://example01.com",
|
ServerURL: "https://example01.com",
|
||||||
AccessToken: "12",
|
AccessToken: "12",
|
||||||
InternalRoomID: "!a:example01.com",
|
InternalRoomID: "!a:example01.com",
|
||||||
},
|
},
|
||||||
|
@ -318,9 +318,11 @@ func TestHandleAlertingWithProviderThatReturnsAnError(t *testing.T) {
|
|||||||
AlertType: alert.TypeMatrix,
|
AlertType: alert.TypeMatrix,
|
||||||
AlertingConfig: &alerting.Config{
|
AlertingConfig: &alerting.Config{
|
||||||
Matrix: &matrix.AlertProvider{
|
Matrix: &matrix.AlertProvider{
|
||||||
HomeserverURL: "https://example.com",
|
MatrixProviderConfig: matrix.MatrixProviderConfig{
|
||||||
AccessToken: "1",
|
ServerURL: "https://example.com",
|
||||||
InternalRoomID: "!a:example.com",
|
AccessToken: "1",
|
||||||
|
InternalRoomID: "!a:example.com",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user