Added possibility to have a global victorops api_key (#897)

* Added possibility to have a global victorops api_key

* adding tests for victorops default key
This commit is contained in:
Carlos Alexandro Becker 2017-07-12 04:25:47 -03:00 committed by stuart nelson
parent f4c11751a9
commit ca8d73a59b
5 changed files with 70 additions and 6 deletions

View File

@ -14,6 +14,7 @@
package config
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
@ -22,8 +23,6 @@ import (
"strings"
"time"
"encoding/json"
"github.com/prometheus/common/model"
"gopkg.in/yaml.v2"
)
@ -244,6 +243,12 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
if !strings.HasSuffix(voc.APIURL, "/") {
voc.APIURL += "/"
}
if voc.APIKey == "" {
if c.Global.VictorOpsAPIKey == "" {
return fmt.Errorf("no global VictorOps API Key set")
}
voc.APIKey = c.Global.VictorOpsAPIKey
}
}
names[rcv.Name] = struct{}{}
}
@ -316,6 +321,7 @@ type GlobalConfig struct {
HipchatAuthToken Secret `yaml:"hipchat_auth_token,omitempty" json:"hipchat_auth_token,omitempty"`
OpsGenieAPIHost string `yaml:"opsgenie_api_host,omitempty" json:"opsgenie_api_host,omitempty"`
VictorOpsAPIURL string `yaml:"victorops_api_url,omitempty" json:"victorops_api_url,omitempty"`
VictorOpsAPIKey Secret `yaml:"victorops_api_key,omitempty" json:"victorops_api_key,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`

View File

@ -131,7 +131,6 @@ func TestJSONUnmarshalMarshaled(t *testing.T) {
}
func TestEmptyFieldsAndRegex(t *testing.T) {
boolFoo := true
var regexpFoo Regexp
regexpFoo.Regexp, _ = regexp.Compile("^(?:^(foo1|foo2|baz)$)$")
@ -205,3 +204,28 @@ func TestEmptyFieldsAndRegex(t *testing.T) {
t.Fatalf("%s: unexpected config result: \n\n%s\n expected\n\n%s", "testdata/conf.empty-fields.yml", configGot, configExp)
}
}
func TestVictorOpsDefaultAPIKey(t *testing.T) {
conf, _, err := LoadFile("testdata/conf.victorops-default-apikey.yml")
if err != nil {
t.Errorf("Error parsing %s: %s", "testdata/conf.victorops-default-apikey.yml", err)
}
var defaultKey = conf.Global.VictorOpsAPIKey
if defaultKey != conf.Receivers[0].VictorOpsConfigs[0].APIKey {
t.Errorf("Invalid victorops key: %s\nExpected: %s", conf.Receivers[0].VictorOpsConfigs[0].APIKey, defaultKey)
}
if defaultKey == conf.Receivers[1].VictorOpsConfigs[0].APIKey {
t.Errorf("Invalid victorops key: %s\nExpected: %s", conf.Receivers[0].VictorOpsConfigs[0].APIKey, "qwe456")
}
}
func TestVictorOpsNoAPIKey(t *testing.T) {
_, _, err := LoadFile("testdata/conf.victorops-no-apikey.yml")
if err == nil {
t.Errorf("Expected an error parsing %s: %s", "testdata/conf.victorops-no-apikey.yml", err)
}
if err.Error() != "no global VictorOps API Key set" {
t.Errorf("Expected: %s\nGot: %s", "no global VictorOps API Key set", err.Error())
}
}

View File

@ -340,9 +340,6 @@ func (c *VictorOpsConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if c.APIKey == "" {
return fmt.Errorf("missing API key in VictorOps config")
}
if c.RoutingKey == "" {
return fmt.Errorf("missing Routing key in VictorOps config")
}

View File

@ -0,0 +1,22 @@
global:
victorops_api_key: asd132
route:
group_by: ['alertname', 'cluster', 'service']
group_wait: 30s
group_interval: 5m
repeat_interval: 3h
receiver: team-Y-victorops
routes:
- match:
service: foo
receiver: team-X-victorops
receivers:
- name: 'team-X-victorops'
victorops_configs:
- routing_key: 'team-X'
- name: 'team-Y-victorops'
victorops_configs:
- routing_key: 'team-Y'
api_key: qwe456

View File

@ -0,0 +1,15 @@
route:
group_by: ['alertname', 'cluster', 'service']
group_wait: 30s
group_interval: 5m
repeat_interval: 3h
receiver: team-X-victorops
routes:
- match:
service: foo
receiver: team-X-victorops
receivers:
- name: 'team-X-victorops'
victorops_configs:
- routing_key: 'team-X'