Skip to content

Commit

Permalink
WTF-400 PagerDuty extracted to new config format
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Cummer committed Apr 16, 2019
1 parent 265149c commit a8e9f69
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 4 additions & 13 deletions modules/pagerduty/client.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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"),
)
}
32 changes: 32 additions & 0 deletions modules/pagerduty/settings.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 11 additions & 8 deletions modules/pagerduty/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())))
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit a8e9f69

Please sign in to comment.