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

Rewrote way how to define region

Now region is defined with `region` variable and could be only `us` or `eu`
This commit is contained in:
Dmytro Prokhorenkov 2019-07-24 11:54:43 +02:00
parent 7422cc3822
commit 4b629db0d9
2 changed files with 15 additions and 15 deletions

View File

@ -25,21 +25,18 @@ type Parent struct {
} }
var opsGenieAPIUrl = map[string]string{ var opsGenieAPIUrl = map[string]string{
"default": "https://api.opsgenie.com", "us": "https://api.opsgenie.com",
"europe": "https://api.eu.opsgenie.com", "eu": "https://api.eu.opsgenie.com",
} }
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
func (widget *Widget) Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse, error) { func (widget *Widget) Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse, error) {
agregatedResponses := []*OnCallResponse{} agregatedResponses := []*OnCallResponse{}
region := "default"
if regionUrl, regionErr := opsGenieAPIUrl[widget.settings.region]; regionErr {
for _, sched := range schedules { for _, sched := range schedules {
if widget.settings.isEurope { scheduleUrl := fmt.Sprintf("%s/v2/schedules/%s/on-calls?scheduleIdentifierType=%s&flat=true", regionUrl, sched, scheduleIdentifierType)
region = "europe"
}
scheduleUrl := fmt.Sprintf("%s/v2/schedules/%s/on-calls?scheduleIdentifierType=%s&flat=true", opsGenieAPIUrl[region], sched, scheduleIdentifierType)
response, err := opsGenieRequest(scheduleUrl, widget.settings.apiKey) response, err := opsGenieRequest(scheduleUrl, widget.settings.apiKey)
agregatedResponses = append(agregatedResponses, response) agregatedResponses = append(agregatedResponses, response)
if err != nil { if err != nil {
@ -47,6 +44,9 @@ func (widget *Widget) Fetch(scheduleIdentifierType string, schedules []string) (
} }
} }
return agregatedResponses, nil return agregatedResponses, nil
} else {
return nil, fmt.Errorf("You specified wrong region. Possible options are only 'us' and 'eu'.")
}
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -13,7 +13,7 @@ type Settings struct {
common *cfg.Common common *cfg.Common
apiKey string `help:"Your OpsGenie API token."` apiKey string `help:"Your OpsGenie API token."`
isEurope bool `help:"Defines if European servers should be used." optional:"true"` region string `help:"Defines region to use. Possible options: us (by default), eu." optional:"true"`
displayEmpty bool `help:"Whether schedules with no assigned person on-call should be displayed." optional:"true"` displayEmpty bool `help:"Whether schedules with no assigned person on-call should be displayed." optional:"true"`
schedule []string `help:"A list of names of the schedule(s) to retrieve."` schedule []string `help:"A list of names of the schedule(s) to retrieve."`
scheduleIdentifierType string `help:"Type of the schedule identifier." values:"id or name" optional:"true"` scheduleIdentifierType string `help:"Type of the schedule identifier." values:"id or name" optional:"true"`
@ -25,7 +25,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig), common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),
apiKey: ymlConfig.UString("apiKey", os.Getenv("WTF_OPS_GENIE_API_KEY")), apiKey: ymlConfig.UString("apiKey", os.Getenv("WTF_OPS_GENIE_API_KEY")),
isEurope: ymlConfig.UBool("isEurope", false), region: ymlConfig.UString("region", "us"),
displayEmpty: ymlConfig.UBool("displayEmpty", true), displayEmpty: ymlConfig.UBool("displayEmpty", true),
scheduleIdentifierType: ymlConfig.UString("scheduleIdentifierType", "id"), scheduleIdentifierType: ymlConfig.UString("scheduleIdentifierType", "id"),
} }