From 6df852f6fbdc5b033d0b8ac925b3e46253fea885 Mon Sep 17 00:00:00 2001 From: Dominic Evans Date: Tue, 28 Sep 2021 15:08:16 +0100 Subject: [PATCH] fix: support ListServices pagination and use query - support pagination (for when an apikey has visibility of more than 25 services) - if service names are specified in the configuration then pass those as a query string to the ListServices API --- src/pd.go | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/pd.go b/src/pd.go index 42eacac..51be3e7 100644 --- a/src/pd.go +++ b/src/pd.go @@ -79,20 +79,38 @@ func pdInit(cfg *ini.File) { } } - // get service ids - serviceList, err := pd.ListServices(pagerduty.ListServiceOptions{Query: ""}) - if err != nil { - log.Println("Error: Cannot retrieve service list from Pagerduty API") - os.Exit(1) - } serviceConf := make(map[string]bool) for _, v := range cfg.Section("pagerduty").Key("services").Strings(",") { + log.Printf("Service %s enabled in config", v) serviceConf[v] = true } - for _, s := range serviceList.Services { - if serviceConf[s.Name] { - log.Printf("Found service %s with id %s", s.Name, s.ID) - serviceIDs = append(serviceIDs, s.ID) + + ok := true + opts := pagerduty.ListServiceOptions{ + APIListObject: pagerduty.APIListObject{ + Limit: 25, + Offset: 0, + }, + } + + for k := range serviceConf { + for ok { + // get service ids that match given name + opts.Query = k + serviceList, err := pd.ListServices(opts) + if err != nil { + log.Println("Error: Cannot retrieve service list from Pagerduty API") + os.Exit(1) + } + for _, s := range serviceList.Services { + log.Printf("Checking service %s", s.Name) + if serviceConf[s.Name] { + log.Printf("Found service %s with id %s", s.Name, s.ID) + serviceIDs = append(serviceIDs, s.ID) + } + } + ok = serviceList.More + opts.Offset = opts.Offset + opts.Limit } }