From 7422cc3822d5d040b0ce792cfa353570cc0ff6c7 Mon Sep 17 00:00:00 2001 From: Dmytro Prokhorenkov Date: Tue, 23 Jul 2019 17:23:53 +0200 Subject: [PATCH 1/2] Added to opsgenie module region selection According to OpsGenie API documentation: ``If using the EU instance of Opsgenie, the URL needs to be https://api.eu.opsgenie.com for requests to be successful.``` --- modules/opsgenie/client.go | 11 ++++++++++- modules/opsgenie/settings.go | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/modules/opsgenie/client.go b/modules/opsgenie/client.go index 928ad57e..91cb644a 100644 --- a/modules/opsgenie/client.go +++ b/modules/opsgenie/client.go @@ -24,13 +24,22 @@ type Parent struct { Enabled bool `json:"enabled"` } +var opsGenieAPIUrl = map[string]string{ + "default": "https://api.opsgenie.com", + "europe": "https://api.eu.opsgenie.com", +} + /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse, error) { agregatedResponses := []*OnCallResponse{} + region := "default" for _, sched := range schedules { - scheduleUrl := fmt.Sprintf("https://api.opsgenie.com/v2/schedules/%s/on-calls?scheduleIdentifierType=%s&flat=true", sched, scheduleIdentifierType) + if widget.settings.isEurope { + 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) agregatedResponses = append(agregatedResponses, response) if err != nil { diff --git a/modules/opsgenie/settings.go b/modules/opsgenie/settings.go index 99981313..70921fe2 100644 --- a/modules/opsgenie/settings.go +++ b/modules/opsgenie/settings.go @@ -13,6 +13,7 @@ type Settings struct { common *cfg.Common apiKey string `help:"Your OpsGenie API token."` + isEurope bool `help:"Defines if European servers should be used." 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."` scheduleIdentifierType string `help:"Type of the schedule identifier." values:"id or name" optional:"true"` @@ -24,6 +25,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig), apiKey: ymlConfig.UString("apiKey", os.Getenv("WTF_OPS_GENIE_API_KEY")), + isEurope: ymlConfig.UBool("isEurope", false), displayEmpty: ymlConfig.UBool("displayEmpty", true), scheduleIdentifierType: ymlConfig.UString("scheduleIdentifierType", "id"), } From 4b629db0d9240109169ce5d76dc0dda96023f814 Mon Sep 17 00:00:00 2001 From: Dmytro Prokhorenkov Date: Wed, 24 Jul 2019 11:54:43 +0200 Subject: [PATCH 2/2] Rewrote way how to define region Now region is defined with `region` variable and could be only `us` or `eu` --- modules/opsgenie/client.go | 26 +++++++++++++------------- modules/opsgenie/settings.go | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/modules/opsgenie/client.go b/modules/opsgenie/client.go index 91cb644a..9d4cb4de 100644 --- a/modules/opsgenie/client.go +++ b/modules/opsgenie/client.go @@ -25,28 +25,28 @@ type Parent struct { } var opsGenieAPIUrl = map[string]string{ - "default": "https://api.opsgenie.com", - "europe": "https://api.eu.opsgenie.com", + "us": "https://api.opsgenie.com", + "eu": "https://api.eu.opsgenie.com", } /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Fetch(scheduleIdentifierType string, schedules []string) ([]*OnCallResponse, error) { agregatedResponses := []*OnCallResponse{} - region := "default" - for _, sched := range schedules { - if widget.settings.isEurope { - 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) - agregatedResponses = append(agregatedResponses, response) - if err != nil { - return nil, err + if regionUrl, regionErr := opsGenieAPIUrl[widget.settings.region]; regionErr { + for _, sched := range schedules { + scheduleUrl := fmt.Sprintf("%s/v2/schedules/%s/on-calls?scheduleIdentifierType=%s&flat=true", regionUrl, sched, scheduleIdentifierType) + response, err := opsGenieRequest(scheduleUrl, widget.settings.apiKey) + agregatedResponses = append(agregatedResponses, response) + if err != nil { + return nil, err + } } + return agregatedResponses, nil + } else { + return nil, fmt.Errorf("You specified wrong region. Possible options are only 'us' and 'eu'.") } - return agregatedResponses, nil } /* -------------------- Unexported Functions -------------------- */ diff --git a/modules/opsgenie/settings.go b/modules/opsgenie/settings.go index 70921fe2..dbe4737f 100644 --- a/modules/opsgenie/settings.go +++ b/modules/opsgenie/settings.go @@ -13,7 +13,7 @@ type Settings struct { common *cfg.Common 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"` 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"` @@ -25,7 +25,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig), 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), scheduleIdentifierType: ymlConfig.UString("scheduleIdentifierType", "id"), }