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

WTF-400 OpsGenie extracted to new config format

This commit is contained in:
Chris Cummer 2019-04-15 21:16:18 -07:00
parent b6d845b734
commit ad15679588
4 changed files with 70 additions and 38 deletions

View File

@ -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":

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}