· 4 years ago · Apr 21, 2021, 02:36 PM
1package main
2
3import (
4 "net/http"
5 "io/ioutil"
6 "encoding/json"
7 "time"
8 "fmt"
9 "flag"
10)
11
12type PDUser struct {
13 Id string
14 Summary string
15}
16
17type PDScheduleEntry struct {
18 Start string
19 End string
20 User PDUser
21}
22
23type PDFinalSchedule struct {
24 Name string
25 Rendered_Schedule_Entries []PDScheduleEntry
26}
27
28type PDTeams struct {
29 Id string
30 Summary string
31}
32
33type PDSchedule struct {
34 Id string
35 Summary string
36 Teams []PDTeams
37 Final_Schedule PDFinalSchedule
38}
39
40type PDScheduleResponse struct {
41 Schedule PDSchedule
42}
43
44func main() {
45 // PRIMARY_ERT_MANAGER_SCHEDULE_ID := "P6U5EG2"
46 // BACKSTOP_ERT_MANAGER_SCHEDULE_ID := "PQEEGZH"
47 // PRIMARY_ERT_ENGINEER_SCHEDULE_ID := "PO9HY09"
48 // BACKSTOP_ERT_ENGINEER_SCHEDULE_ID := "PWRDLJ4"
49
50 schedule_ids := []string{
51 "P6U5EG2",
52 "PQEEGZH",
53 "PO9HY09",
54 "PWRDLJ4",
55 }
56
57 tr := &http.Transport{
58 MaxIdleConns: 10,
59 IdleConnTimeout: 30 * time.Second,
60 DisableCompression: true,
61 }
62 client := &http.Client{Transport: tr}
63
64 since := flag.String("since", "2020-01-01", "a date since when you want to collect the data")
65 until := flag.String("until", "2020-12-31", "a date since when you want to collect the data")
66 time_zone := flag.String("tz", "UTC", "the timezone you want to use")
67 api_key := flag.String("token", "", "the API Key used to authenticate with PagerDuty and retrieve data")
68
69 flag.Parse()
70
71 fmt.Println("Retrieving data from PagerDuty since [" + *since + "] until [" + *until + "] with timezone [" + *time_zone + "].")
72
73 fmt.Println("----------------------------------------")
74 for j := 0; j < len(schedule_ids); j++ {
75
76 var sr PDScheduleResponse
77
78 req, err := http.NewRequest("GET", "https://api.pagerduty.com/schedules/" + schedule_ids[j] + "?overflow=true&since=" + *since + "&until=" + *until + "&time_zone=" + *time_zone, nil)
79 req.Header.Add("Accept", "application/vnd.pagerduty+json;version=2")
80 req.Header.Add("Authorization", "Token token=" + *api_key)
81
82 resp, err := client.Do(req)
83
84 defer resp.Body.Close()
85 body, err := ioutil.ReadAll(resp.Body)
86 if err != nil {
87 print(err)
88 }
89
90 json.Unmarshal([]byte(body), &sr)
91
92 fmt.Println("Schedule ID: [" + sr.Schedule.Id + "]")
93 fmt.Println("Schedule Name: [" + sr.Schedule.Summary + "]")
94 fmt.Println("On call rotations:")
95 for i := 0; i < len(sr.Schedule.Final_Schedule.Rendered_Schedule_Entries); i++ {
96 fmt.Println(" -> " + sr.Schedule.Final_Schedule.Rendered_Schedule_Entries[i].User.Summary + " \t\t Start date: [" + sr.Schedule.Final_Schedule.Rendered_Schedule_Entries[i].Start + "] \t End date: [" + sr.Schedule.Final_Schedule.Rendered_Schedule_Entries[i].End + "]")
97 }
98
99 fmt.Println("----------------------------------------")
100 }
101
102 fmt.Println("All data retrieved. Have a nice day.")
103
104}
105