Skip to content

Commit 6b0006e

Browse files
committed
config: add global mattermost_api_url and mattermost_api_url_file
Default Mattermost receiver webhook_url/webhook_url_file from global settings Signed-off-by: Kliachin Aleksei <madest92@gmail.com>
1 parent b5df726 commit 6b0006e

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ func (c *Config) UnmarshalYAML(unmarshal func(any) error) error {
479479
}
480480
}
481481

482+
if c.Global.MattermostAPIURL != nil && len(c.Global.MattermostAPIURLFile) > 0 {
483+
return errors.New("at most one of mattermost_api_url & mattermost_api_url_file must be configured")
484+
}
485+
482486
if c.Global.OpsGenieAPIKey != "" && len(c.Global.OpsGenieAPIKeyFile) > 0 {
483487
return errors.New("at most one of opsgenie_api_key & opsgenie_api_key_file must be configured")
484488
}
@@ -773,6 +777,10 @@ func (c *Config) UnmarshalYAML(unmarshal func(any) error) error {
773777
if mattermost.HTTPConfig == nil {
774778
mattermost.HTTPConfig = c.Global.HTTPConfig
775779
}
780+
if mattermost.WebhookURL == nil && len(mattermost.WebhookURLFile) == 0 {
781+
mattermost.WebhookURL = c.Global.MattermostAPIURL
782+
mattermost.WebhookURLFile = c.Global.MattermostAPIURLFile
783+
}
776784
}
777785

778786
names[rcv.Name] = struct{}{}
@@ -994,6 +1002,8 @@ type GlobalConfig struct {
9941002
SMTPTLSConfig *commoncfg.TLSConfig `yaml:"smtp_tls_config,omitempty" json:"smtp_tls_config,omitempty"`
9951003
SlackAPIURL *SecretURL `yaml:"slack_api_url,omitempty" json:"slack_api_url,omitempty"`
9961004
SlackAPIURLFile string `yaml:"slack_api_url_file,omitempty" json:"slack_api_url_file,omitempty"`
1005+
MattermostAPIURL *SecretURL `yaml:"mattermost_api_url,omitempty" json:"mattermost_api_url,omitempty"`
1006+
MattermostAPIURLFile string `yaml:"mattermost_api_url_file,omitempty" json:"mattermost_api_url_file,omitempty"`
9971007
SlackAppToken Secret `yaml:"slack_app_token,omitempty" json:"slack_app_token,omitempty"`
9981008
SlackAppTokenFile string `yaml:"slack_app_token_file,omitempty" json:"slack_app_token_file,omitempty"`
9991009
SlackAppURL *URL `yaml:"slack_app_url,omitempty" json:"slack_app_url,omitempty"`

config/config_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,31 @@ func TestEmptyFieldsAndRegex(t *testing.T) {
10161016
}
10171017
}
10181018

1019+
func TestMattermostGlobalAPIURLDefaulting(t *testing.T) {
1020+
conf, err := LoadFile("testdata/conf.mattermost-default-api-url-file.yml")
1021+
if err != nil {
1022+
t.Fatalf("Error parsing %s: %s", "testdata/conf.mattermost-default-api-url-file.yml", err)
1023+
}
1024+
1025+
// no override
1026+
firstConfig := conf.Receivers[0].MattermostConfigs[0]
1027+
if firstConfig.WebhookURLFile != "/global_mm_file" || firstConfig.WebhookURL != nil {
1028+
t.Fatalf("Invalid Mattermost URL file: %s\nExpected: %s", firstConfig.WebhookURLFile, "/global_mm_file")
1029+
}
1030+
1031+
// override the file
1032+
secondConfig := conf.Receivers[0].MattermostConfigs[1]
1033+
if secondConfig.WebhookURLFile != "/override_mm_file" || secondConfig.WebhookURL != nil {
1034+
t.Fatalf("Invalid Mattermost URL file: %s\nExpected: %s", secondConfig.WebhookURLFile, "/override_mm_file")
1035+
}
1036+
1037+
// override the global file with an inline URL
1038+
thirdConfig := conf.Receivers[0].MattermostConfigs[2]
1039+
if thirdConfig.WebhookURL.String() != "http://mysecret.example.com/" || thirdConfig.WebhookURLFile != "" {
1040+
t.Fatalf("Invalid Mattermost URL: %s\nExpected: %s", thirdConfig.WebhookURL.String(), "http://mysecret.example.com/")
1041+
}
1042+
}
1043+
10191044
func TestGlobalAndLocalHTTPConfig(t *testing.T) {
10201045
config, err := LoadFile("testdata/conf.http-config.good.yml")
10211046
if err != nil {

config/notifiers.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,10 +1230,6 @@ func (c *MattermostConfig) UnmarshalYAML(unmarshal func(any) error) error {
12301230
return err
12311231
}
12321232

1233-
if c.WebhookURL == nil && c.WebhookURLFile == "" {
1234-
return errors.New("one of webhook_url or webhook_url_file must be configured")
1235-
}
1236-
12371233
if c.WebhookURL != nil && len(c.WebhookURLFile) > 0 {
12381234
return errors.New("at most one of webhook_url & webhook_url_file must be configured")
12391235
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
global:
2+
mattermost_api_url_file: '/global_mm_file'
3+
route:
4+
receiver: 'mm-notifications'
5+
receivers:
6+
- name: 'mm-notifications'
7+
mattermost_configs:
8+
# Use global
9+
- channel: 'alerts1'
10+
text: 'test'
11+
# Override global with other file
12+
- channel: 'alerts2'
13+
text: 'test'
14+
webhook_url_file: '/override_mm_file'
15+
# Override global with inline URL
16+
- channel: 'alerts3'
17+
text: 'test'
18+
webhook_url: 'http://mysecret.example.com/'

docs/configuration.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ global:
102102
# The API URL to use for Slack notifications.
103103
[ slack_api_url: <secret> ]
104104
[ slack_api_url_file: <filepath> ]
105+
[ mattermost_api_url: <secret> ]
106+
[ mattermost_api_url_file: <filepath> ]
105107
[ victorops_api_key: <secret> ]
106108
[ victorops_api_key_file: <filepath> ]
107109
[ victorops_api_url: <string> | default = "https://alert.victorops.com/integrations/generic/20131114/alert/" ]
@@ -1019,10 +1021,10 @@ Mattermost notifications are sent via the [Mattermost webhook API](https://devel
10191021
# Whether to notify about resolved alerts.
10201022
[ send_resolved: <boolean> | default = true ]
10211023
1022-
# The Mattermost webhook URL.
1023-
# webhook_url and webhook_url_file are mutually exclusive.
1024-
webhook_url: <secret>
1025-
webhook_url_file: <filepath>
1024+
# The Mattermost webhook URL. Either `webhook_url` or `webhook_url_file` should be set.
1025+
# Defaults to global settings if none are set here.
1026+
[ webhook_url: <secret> | default = global.mattermost_api_url ]
1027+
[ webhook_url_file: <filepath> | default = global.mattermost_api_url_file ]
10261028

10271029
# Overrides the channel the message posts in. Use the channel’s name and not the display name, e.g. use town-square, not Town Square.
10281030
[ channel: <string> | default = '' ]

0 commit comments

Comments
 (0)