From ad156795886a86013a628bde9d2a6507043af327 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Mon, 15 Apr 2019 21:16:18 -0700 Subject: [PATCH] WTF-400 OpsGenie extracted to new config format --- main.go | 3 +- modules/opsgenie/client.go | 15 ++-------- modules/opsgenie/settings.go | 55 ++++++++++++++++++++++++++++++++++++ modules/opsgenie/widget.go | 35 +++++++---------------- 4 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 modules/opsgenie/settings.go diff --git a/main.go b/main.go index 370624ec..93fb1510 100644 --- a/main.go +++ b/main.go @@ -256,7 +256,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w settings := newrelic.NewSettingsFromYAML(wtf.Config) widget = newrelic.NewWidget(app, settings) case "opsgenie": - widget = opsgenie.NewWidget(app) + settings := opsgenie.NewSettingsFromYAML(wtf.Config) + widget = opsgenie.NewWidget(app, settings) case "pagerduty": widget = pagerduty.NewWidget(app) case "power": diff --git a/modules/opsgenie/client.go b/modules/opsgenie/client.go index 4e56bec9..928ad57e 100644 --- a/modules/opsgenie/client.go +++ b/modules/opsgenie/client.go @@ -4,9 +4,6 @@ import ( "encoding/json" "fmt" "net/http" - "os" - - "github.com/wtfutil/wtf/wtf" ) type OnCallResponse struct { @@ -29,11 +26,12 @@ type Parent struct { /* -------------------- Exported Functions -------------------- */ -func Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse, error) { +func (widget *Widget) Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse, error) { agregatedResponses := []*OnCallResponse{} + for _, sched := range schedules { scheduleUrl := fmt.Sprintf("https://api.opsgenie.com/v2/schedules/%s/on-calls?scheduleIdentifierType=%s&flat=true", sched, scheduleIdentifierType) - response, err := opsGenieRequest(scheduleUrl, apiKey()) + response, err := opsGenieRequest(scheduleUrl, widget.settings.apiKey) agregatedResponses = append(agregatedResponses, response) if err != nil { return nil, err @@ -44,13 +42,6 @@ func Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse /* -------------------- Unexported Functions -------------------- */ -func apiKey() string { - return wtf.Config.UString( - "wtf.mods.opsgenie.apiKey", - os.Getenv("WTF_OPS_GENIE_API_KEY"), - ) -} - func opsGenieRequest(url string, apiKey string) (*OnCallResponse, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { diff --git a/modules/opsgenie/settings.go b/modules/opsgenie/settings.go new file mode 100644 index 00000000..07ebc512 --- /dev/null +++ b/modules/opsgenie/settings.go @@ -0,0 +1,55 @@ +package opsgenie + +import ( + "os" + + "github.com/olebedev/config" + "github.com/wtfutil/wtf/cfg" +) + +type Settings struct { + common *cfg.Common + + apiKey string + displayEmpty bool + schedule []string + scheduleIdentifierType string +} + +func NewSettingsFromYAML(ymlConfig *config.Config) *Settings { + localConfig, _ := ymlConfig.Get("wtf.mods.opsgenie") + + settings := Settings{ + common: cfg.NewCommonSettingsFromYAML(ymlConfig), + + apiKey: localConfig.UString("apiKey", os.Getenv("WTF_OPS_GENIE_API_KEY")), + displayEmpty: localConfig.UBool("displayEmpty", true), + scheduleIdentifierType: localConfig.UString("scheduleIdentifierType", "id"), + } + + settings.schedule = settings.arrayifySchedules(localConfig) + + return &settings +} + +// arrayifySchedules figures out if we're dealing with a single project or an array of projects +func (settings *Settings) arrayifySchedules(localConfig *config.Config) []string { + schedules := []string{} + + // Single schedule + schedule, err := localConfig.String("schedule") + if err == nil { + schedules = append(schedules, schedule) + return schedules + } + + // Array of schedules + scheduleList := localConfig.UList("schedule") + for _, scheduleName := range scheduleList { + if schedule, ok := scheduleName.(string); ok { + schedules = append(schedules, schedule) + } + } + + return schedules +} diff --git a/modules/opsgenie/widget.go b/modules/opsgenie/widget.go index 717c5f34..26c051b8 100644 --- a/modules/opsgenie/widget.go +++ b/modules/opsgenie/widget.go @@ -10,11 +10,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, "OpsGenie", "opsgenie", false), + + settings: settings, } return &widget @@ -23,10 +27,11 @@ func NewWidget(app *tview.Application) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - data, err := Fetch( - wtf.Config.UString("wtf.mods.opsgenie.scheduleIdentifierType"), - getSchedules(), + data, err := widget.Fetch( + widget.settings.scheduleIdentifierType, + widget.settings.schedule, ) + widget.View.SetTitle(widget.ContextualTitle(widget.Name())) var content string @@ -43,31 +48,11 @@ func (widget *Widget) Refresh() { /* -------------------- Unexported Functions -------------------- */ -func getSchedules() []string { - // see if schedule is set to a single string - configPath := "wtf.mods.opsgenie.schedule" - singleSchedule, err := wtf.Config.String(configPath) - if err == nil { - return []string{singleSchedule} - } - // else, assume list - scheduleList := wtf.Config.UList(configPath) - var ret []string - for _, schedule := range scheduleList { - if str, ok := schedule.(string); ok { - ret = append(ret, str) - } - } - return ret -} - func (widget *Widget) contentFrom(onCallResponses []*OnCallResponse) string { str := "" - displayEmpty := wtf.Config.UBool("wtf.mods.opsgenie.displayEmpty", true) - for _, data := range onCallResponses { - if (len(data.OnCallData.Recipients) == 0) && (displayEmpty == false) { + if (len(data.OnCallData.Recipients) == 0) && (widget.settings.displayEmpty == false) { continue }