allow global opsgenie api key (#1208)

* allow global opsgenie api key

* added missing files

* removed test
This commit is contained in:
Carlos Alexandro Becker 2018-01-29 13:05:17 -02:00 committed by stuart nelson
parent 23f31d7d5a
commit c5ea346d06
6 changed files with 71 additions and 21 deletions

View File

@ -235,6 +235,12 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
if !strings.HasSuffix(ogc.APIURL, "/") { if !strings.HasSuffix(ogc.APIURL, "/") {
ogc.APIURL += "/" ogc.APIURL += "/"
} }
if ogc.APIKey == "" {
if c.Global.OpsGenieAPIKey == "" {
return fmt.Errorf("no global OpsGenie API Key set")
}
ogc.APIKey = c.Global.OpsGenieAPIKey
}
} }
for _, wcc := range rcv.WechatConfigs { for _, wcc := range rcv.WechatConfigs {
wcc.APIURL = c.Global.WeChatAPIURL wcc.APIURL = c.Global.WeChatAPIURL
@ -348,6 +354,7 @@ type GlobalConfig struct {
HipchatAPIURL string `yaml:"hipchat_api_url,omitempty" json:"hipchat_api_url,omitempty"` HipchatAPIURL string `yaml:"hipchat_api_url,omitempty" json:"hipchat_api_url,omitempty"`
HipchatAuthToken Secret `yaml:"hipchat_auth_token,omitempty" json:"hipchat_auth_token,omitempty"` HipchatAuthToken Secret `yaml:"hipchat_auth_token,omitempty" json:"hipchat_auth_token,omitempty"`
OpsGenieAPIURL string `yaml:"opsgenie_api_url,omitempty" json:"opsgenie_api_url,omitempty"` OpsGenieAPIURL string `yaml:"opsgenie_api_url,omitempty" json:"opsgenie_api_url,omitempty"`
OpsGenieAPIKey Secret `yaml:"opsgenie_api_key,omitempty" json:"opsgenie_api_key,omitempty"`
WeChatAPIURL string `yaml:"wechat_api_url,omitempty" json:"wechat_api_url,omitempty"` WeChatAPIURL string `yaml:"wechat_api_url,omitempty" json:"wechat_api_url,omitempty"`
WeChatAPISecret string `yaml:"wechat_api_secret,omitempty" json:"wechat_api_secret,omitempty"` WeChatAPISecret string `yaml:"wechat_api_secret,omitempty" json:"wechat_api_secret,omitempty"`
WeChatAPICorpID string `yaml:"wechat_api_corp_id,omitempty" json:"wechat_api_corp_id,omitempty"` WeChatAPICorpID string `yaml:"wechat_api_corp_id,omitempty" json:"wechat_api_corp_id,omitempty"`

View File

@ -381,3 +381,28 @@ func TestVictorOpsNoAPIKey(t *testing.T) {
t.Errorf("Expected: %s\nGot: %s", "no global VictorOps API Key set", err.Error()) t.Errorf("Expected: %s\nGot: %s", "no global VictorOps API Key set", err.Error())
} }
} }
func TestOpsGenieDefaultAPIKey(t *testing.T) {
conf, _, err := LoadFile("testdata/conf.opsgenie-default-apikey.yml")
if err != nil {
t.Errorf("Error parsing %s: %s", "testdata/conf.opsgenie-default-apikey.yml", err)
}
var defaultKey = conf.Global.OpsGenieAPIKey
if defaultKey != conf.Receivers[0].OpsGenieConfigs[0].APIKey {
t.Errorf("Invalid OpsGenie key: %s\nExpected: %s", conf.Receivers[0].OpsGenieConfigs[0].APIKey, defaultKey)
}
if defaultKey == conf.Receivers[1].OpsGenieConfigs[0].APIKey {
t.Errorf("Invalid OpsGenie key: %s\nExpected: %s", conf.Receivers[0].OpsGenieConfigs[0].APIKey, "qwe456")
}
}
func TestOpsGenieNoAPIKey(t *testing.T) {
_, _, err := LoadFile("testdata/conf.opsgenie-no-apikey.yml")
if err == nil {
t.Errorf("Expected an error parsing %s: %s", "testdata/conf.opsgenie-no-apikey.yml", err)
}
if err.Error() != "no global OpsGenie API Key set" {
t.Errorf("Expected: %s\nGot: %s", "no global OpsGenie API Key set", err.Error())
}
}

View File

@ -382,9 +382,6 @@ func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
if err := unmarshal((*plain)(c)); err != nil { if err := unmarshal((*plain)(c)); err != nil {
return err return err
} }
if c.APIKey == "" {
return fmt.Errorf("missing API key in OpsGenie config")
}
return checkOverflow(c.XXX, "opsgenie config") return checkOverflow(c.XXX, "opsgenie config")
} }

View File

@ -1,8 +1,9 @@
package config package config
import ( import (
"gopkg.in/yaml.v2"
"testing" "testing"
"gopkg.in/yaml.v2"
) )
func TestEmailToIsPresent(t *testing.T) { func TestEmailToIsPresent(t *testing.T) {
@ -143,23 +144,6 @@ corp_id: ''
} }
} }
func TestOpsGenieAPIKeyIsPresent(t *testing.T) {
in := `
api_key: ''
`
var cfg OpsGenieConfig
err := yaml.Unmarshal([]byte(in), &cfg)
expected := "missing API key in OpsGenie config"
if err == nil {
t.Fatalf("no error returned, expected:\n%v", expected)
}
if err.Error() != expected {
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
}
}
func TestVictorOpsRoutingKeyIsPresent(t *testing.T) { func TestVictorOpsRoutingKeyIsPresent(t *testing.T) {
in := ` in := `
routing_key: '' routing_key: ''

View File

@ -0,0 +1,22 @@
global:
opsgenie_api_key: asd132
route:
group_by: ['alertname', 'cluster', 'service']
group_wait: 30s
group_interval: 5m
repeat_interval: 3h
receiver: team-Y-opsgenie
routes:
- match:
service: foo
receiver: team-X-opsgenie
receivers:
- name: 'team-X-opsgenie'
opsgenie_configs:
- teams: 'team-X'
- name: 'team-Y-opsgenie'
opsgenie_configs:
- teams: '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-opsgenie
routes:
- match:
service: foo
receiver: team-X-opsgenie
receivers:
- name: 'team-X-opsgenie'
opsgenie_configs:
- teams: 'team-X'