From a8e9f69c227ac94e2b79fe04de3de0696822c16c Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 16 Apr 2019 01:39:45 -0700 Subject: [PATCH] WTF-400 PagerDuty extracted to new config format --- main.go | 3 ++- modules/pagerduty/client.go | 17 ++++------------- modules/pagerduty/settings.go | 32 ++++++++++++++++++++++++++++++++ modules/pagerduty/widget.go | 19 +++++++++++-------- 4 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 modules/pagerduty/settings.go diff --git a/main.go b/main.go index 67e60119..7eb2fc04 100644 --- a/main.go +++ b/main.go @@ -259,7 +259,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w settings := opsgenie.NewSettingsFromYAML(wtf.Config) widget = opsgenie.NewWidget(app, settings) case "pagerduty": - widget = pagerduty.NewWidget(app) + settings := pagerduty.NewSettingsFromYAML(wtf.Config) + widget = pagerduty.NewWidget(app, settings) case "power": settings := power.NewSettingsFromYAML(wtf.Config) widget = power.NewWidget(app, settings) diff --git a/modules/pagerduty/client.go b/modules/pagerduty/client.go index d8f28421..aa8766b9 100644 --- a/modules/pagerduty/client.go +++ b/modules/pagerduty/client.go @@ -1,16 +1,14 @@ package pagerduty import ( - "os" "time" "github.com/PagerDuty/go-pagerduty" - "github.com/wtfutil/wtf/wtf" ) // GetOnCalls returns a list of people currently on call -func GetOnCalls() ([]pagerduty.OnCall, error) { - client := pagerduty.NewClient(apiKey()) +func GetOnCalls(apiKey string) ([]pagerduty.OnCall, error) { + client := pagerduty.NewClient(apiKey) var results []pagerduty.OnCall @@ -38,8 +36,8 @@ func GetOnCalls() ([]pagerduty.OnCall, error) { } // GetIncidents returns a list of people currently on call -func GetIncidents() ([]pagerduty.Incident, error) { - client := pagerduty.NewClient(apiKey()) +func GetIncidents(apiKey string) ([]pagerduty.Incident, error) { + client := pagerduty.NewClient(apiKey) var results []pagerduty.Incident @@ -64,10 +62,3 @@ func GetIncidents() ([]pagerduty.Incident, error) { return results, nil } - -func apiKey() string { - return wtf.Config.UString( - "wtf.mods.pagerduty.apiKey", - os.Getenv("WTF_PAGERDUTY_API_KEY"), - ) -} diff --git a/modules/pagerduty/settings.go b/modules/pagerduty/settings.go new file mode 100644 index 00000000..0e1ab511 --- /dev/null +++ b/modules/pagerduty/settings.go @@ -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 +} diff --git a/modules/pagerduty/widget.go b/modules/pagerduty/widget.go index 469abbdb..d95201f8 100644 --- a/modules/pagerduty/widget.go +++ b/modules/pagerduty/widget.go @@ -11,11 +11,15 @@ import ( type Widget struct { wtf.TextWidget + + settings *Settings } -func NewWidget(app *tview.Application) *Widget { +func NewWidget(app *tview.Application, settings *Settings) *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(app, "PagerDuty", "pagerduty", false), + + settings: settings, } return &widget @@ -30,12 +34,12 @@ func (widget *Widget) Refresh() { var err1 error var err2 error - if wtf.Config.UBool("wtf.mods.pagerduty.showSchedules", true) { - onCalls, err1 = GetOnCalls() + if widget.settings.showSchedules { + onCalls, err1 = GetOnCalls(widget.settings.apiKey) } - if wtf.Config.UBool("wtf.mods.pagerduty.showIncidents") { - incidents, err2 = GetIncidents() + if widget.settings.showIncidents { + incidents, err2 = GetIncidents(widget.settings.apiKey) } 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) - filtering := wtf.Config.UList("wtf.mods.pagerduty.escalationFilter") filter := make(map[string]bool) - for _, item := range filtering { + for _, item := range widget.settings.escalationFilter { filter[item.(string)] = true } for _, onCall := range onCalls { key := onCall.EscalationPolicy.Summary - if len(filtering) == 0 || filter[key] { + if len(widget.settings.escalationFilter) == 0 || filter[key] { tree[key] = append(tree[key], onCall) } }