Skip to content

Commit

Permalink
allow global opsgenie api key (prometheus#1208)
Browse files Browse the repository at this point in the history
* allow global opsgenie api key

* added missing files

* removed test
  • Loading branch information
caarlos0 authored and stuartnelson3 committed Jan 29, 2018
1 parent 23f31d7 commit c5ea346
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
if !strings.HasSuffix(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 {
wcc.APIURL = c.Global.WeChatAPIURL
Expand Down Expand Up @@ -348,6 +354,7 @@ type GlobalConfig struct {
HipchatAPIURL string `yaml:"hipchat_api_url,omitempty" json:"hipchat_api_url,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"`
OpsGenieAPIKey Secret `yaml:"opsgenie_api_key,omitempty" json:"opsgenie_api_key,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"`
WeChatAPICorpID string `yaml:"wechat_api_corp_id,omitempty" json:"wechat_api_corp_id,omitempty"`
Expand Down
25 changes: 25 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,28 @@ func TestVictorOpsNoAPIKey(t *testing.T) {
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())
}
}
3 changes: 0 additions & 3 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,6 @@ func (c *OpsGenieConfig) 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 OpsGenie config")
}
return checkOverflow(c.XXX, "opsgenie config")
}

Expand Down
20 changes: 2 additions & 18 deletions config/notifiers_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"gopkg.in/yaml.v2"
"testing"

"gopkg.in/yaml.v2"
)

func TestEmailToIsPresent(t *testing.T) {
Expand Down Expand Up @@ -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) {
in := `
routing_key: ''
Expand Down
22 changes: 22 additions & 0 deletions config/testdata/conf.opsgenie-default-apikey.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions config/testdata/conf.opsgenie-no-apikey.yml
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit c5ea346

Please sign in to comment.