config: fix regression with Pager Duty (#1455)

The YAML strict mode doesn't allow mapping keys that are duplicates. If
someone wants to override one of the default keys in the Details hash,
the unmarshal function returns an error because the key is already
defined by DefaultPagerdutyConfig.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2018-07-05 09:54:28 +02:00 committed by stuart nelson
parent 2cd2bd3644
commit 2d3c4065e8
2 changed files with 73 additions and 6 deletions

View File

@ -42,6 +42,14 @@ var (
// DefaultEmailSubject defines the default Subject header of an Email.
DefaultEmailSubject = `{{ template "email.default.subject" . }}`
// DefaultPagerdutyDetails defines the default values for PagerDuty details.
DefaultPagerdutyDetails = map[string]string{
"firing": `{{ template "pagerduty.default.instances" .Alerts.Firing }}`,
"resolved": `{{ template "pagerduty.default.instances" .Alerts.Resolved }}`,
"num_firing": `{{ .Alerts.Firing | len }}`,
"num_resolved": `{{ .Alerts.Resolved | len }}`,
}
// DefaultPagerdutyConfig defines default values for PagerDuty configurations.
DefaultPagerdutyConfig = PagerdutyConfig{
NotifierConfig: NotifierConfig{
@ -50,12 +58,6 @@ var (
Description: `{{ template "pagerduty.default.description" .}}`,
Client: `{{ template "pagerduty.default.client" . }}`,
ClientURL: `{{ template "pagerduty.default.clientURL" . }}`,
Details: map[string]string{
"firing": `{{ template "pagerduty.default.instances" .Alerts.Firing }}`,
"resolved": `{{ template "pagerduty.default.instances" .Alerts.Resolved }}`,
"num_firing": `{{ .Alerts.Firing | len }}`,
"num_resolved": `{{ .Alerts.Resolved | len }}`,
},
}
// DefaultSlackConfig defines default values for Slack configurations.
@ -217,6 +219,14 @@ func (c *PagerdutyConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
if c.RoutingKey == "" && c.ServiceKey == "" {
return fmt.Errorf("missing service or routing key in PagerDuty config")
}
if c.Details == nil {
c.Details = make(map[string]string)
}
for k, v := range DefaultPagerdutyDetails {
if _, ok := c.Details[k]; !ok {
c.Details[k] = v
}
}
return nil
}

View File

@ -91,6 +91,63 @@ service_key: ''
}
}
func TestPagerdutyDetails(t *testing.T) {
var tests = []struct {
in string
checkFn func(map[string]string)
}{
{
in: `
routing_key: 'xyz'
`,
checkFn: func(d map[string]string) {
if len(d) != 4 {
t.Errorf("expected 4 items, got: %d", len(d))
}
},
},
{
in: `
routing_key: 'xyz'
details:
key1: val1
`,
checkFn: func(d map[string]string) {
if len(d) != 5 {
t.Errorf("expected 5 items, got: %d", len(d))
}
},
},
{
in: `
routing_key: 'xyz'
details:
key1: val1
key2: val2
firing: firing
`,
checkFn: func(d map[string]string) {
if len(d) != 6 {
t.Errorf("expected 6 items, got: %d", len(d))
}
},
},
}
for _, tc := range tests {
var cfg PagerdutyConfig
err := yaml.UnmarshalStrict([]byte(tc.in), &cfg)
if err != nil {
t.Errorf("expected no error, got:%v", err)
}
if tc.checkFn != nil {
tc.checkFn(cfg.Details)
}
}
}
func TestHipchatRoomIDIsPresent(t *testing.T) {
in := `
room_id: ''