1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

WTF-400 PagerDuty extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-16 01:39:45 -07:00
parent 265149ca11
commit a8e9f69c22
4 changed files with 49 additions and 22 deletions

View File

@ -259,7 +259,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
settings := opsgenie.NewSettingsFromYAML(wtf.Config) settings := opsgenie.NewSettingsFromYAML(wtf.Config)
widget = opsgenie.NewWidget(app, settings) widget = opsgenie.NewWidget(app, settings)
case "pagerduty": case "pagerduty":
widget = pagerduty.NewWidget(app) settings := pagerduty.NewSettingsFromYAML(wtf.Config)
widget = pagerduty.NewWidget(app, settings)
case "power": case "power":
settings := power.NewSettingsFromYAML(wtf.Config) settings := power.NewSettingsFromYAML(wtf.Config)
widget = power.NewWidget(app, settings) widget = power.NewWidget(app, settings)

View File

@ -1,16 +1,14 @@
package pagerduty package pagerduty
import ( import (
"os"
"time" "time"
"github.com/PagerDuty/go-pagerduty" "github.com/PagerDuty/go-pagerduty"
"github.com/wtfutil/wtf/wtf"
) )
// GetOnCalls returns a list of people currently on call // GetOnCalls returns a list of people currently on call
func GetOnCalls() ([]pagerduty.OnCall, error) { func GetOnCalls(apiKey string) ([]pagerduty.OnCall, error) {
client := pagerduty.NewClient(apiKey()) client := pagerduty.NewClient(apiKey)
var results []pagerduty.OnCall var results []pagerduty.OnCall
@ -38,8 +36,8 @@ func GetOnCalls() ([]pagerduty.OnCall, error) {
} }
// GetIncidents returns a list of people currently on call // GetIncidents returns a list of people currently on call
func GetIncidents() ([]pagerduty.Incident, error) { func GetIncidents(apiKey string) ([]pagerduty.Incident, error) {
client := pagerduty.NewClient(apiKey()) client := pagerduty.NewClient(apiKey)
var results []pagerduty.Incident var results []pagerduty.Incident
@ -64,10 +62,3 @@ func GetIncidents() ([]pagerduty.Incident, error) {
return results, nil return results, nil
} }
func apiKey() string {
return wtf.Config.UString(
"wtf.mods.pagerduty.apiKey",
os.Getenv("WTF_PAGERDUTY_API_KEY"),
)
}

View File

@ -0,0 +1,32 @@
package pagerduty
import (
"os"
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
type Settings struct {
common *cfg.Common
apiKey string
escalationFilter []interface{}
showIncidents bool
showSchedules bool
}
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
localConfig, _ := ymlConfig.Get("wtf.mods.pagerduty")
settings := Settings{
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
apiKey: localConfig.UString("apiKey", os.Getenv("WTF_PAGERDUTY_API_KEY")),
escalationFilter: localConfig.UList("escalationFilter"),
showIncidents: localConfig.UBool("showIncidents", true),
showSchedules: localConfig.UBool("showSchedules", true),
}
return &settings
}

View File

@ -11,11 +11,15 @@ import (
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
settings *Settings
} }
func NewWidget(app *tview.Application) *Widget { func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{ widget := Widget{
TextWidget: wtf.NewTextWidget(app, "PagerDuty", "pagerduty", false), TextWidget: wtf.NewTextWidget(app, "PagerDuty", "pagerduty", false),
settings: settings,
} }
return &widget return &widget
@ -30,12 +34,12 @@ func (widget *Widget) Refresh() {
var err1 error var err1 error
var err2 error var err2 error
if wtf.Config.UBool("wtf.mods.pagerduty.showSchedules", true) { if widget.settings.showSchedules {
onCalls, err1 = GetOnCalls() onCalls, err1 = GetOnCalls(widget.settings.apiKey)
} }
if wtf.Config.UBool("wtf.mods.pagerduty.showIncidents") { if widget.settings.showIncidents {
incidents, err2 = GetIncidents() incidents, err2 = GetIncidents(widget.settings.apiKey)
} }
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s", widget.Name()))) widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s", widget.Name())))
@ -75,15 +79,14 @@ func (widget *Widget) contentFrom(onCalls []pagerduty.OnCall, incidents []pagerd
tree := make(map[string][]pagerduty.OnCall) tree := make(map[string][]pagerduty.OnCall)
filtering := wtf.Config.UList("wtf.mods.pagerduty.escalationFilter")
filter := make(map[string]bool) filter := make(map[string]bool)
for _, item := range filtering { for _, item := range widget.settings.escalationFilter {
filter[item.(string)] = true filter[item.(string)] = true
} }
for _, onCall := range onCalls { for _, onCall := range onCalls {
key := onCall.EscalationPolicy.Summary key := onCall.EscalationPolicy.Summary
if len(filtering) == 0 || filter[key] { if len(widget.settings.escalationFilter) == 0 || filter[key] {
tree[key] = append(tree[key], onCall) tree[key] = append(tree[key], onCall)
} }
} }