-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
222 lines (190 loc) · 6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"github.com/lmittmann/tint"
)
const (
endpoint_schedules = "https://app.datadoghq.eu/api/unstable/on-call/schedules/"
datadog_schedules_link = "https://app.datadoghq.eu/on-call/schedules/"
)
func get_on_call_schedule(
schedule_name string, dd_api_key string, dd_app_key string,
) (string, error) {
slog.Info("getting schedule for", "name", schedule_name)
headers := map[string]string{
"DD-API-KEY": dd_api_key,
"DD-APPLICATION-KEY": dd_app_key,
"Accept": "application/json",
}
// make the request
body, err := http_req("schedules", "GET", endpoint_schedules, headers, nil)
if err != nil {
slog.Error(err.Error())
return "", fmt.Errorf("error getting schedules http request")
}
// body is json so we need to parse it to get the schedule id
var schedules SchedulesBody
err = json.Unmarshal(body, &schedules)
if err != nil {
return "", fmt.Errorf("error unmarshalling schedules response body: %v", err)
}
// loop through the schedules to find the schedule id from the schedule name
for _, schedule := range schedules.Data {
if schedule.Type == "schedules" && schedule.Attributes.Name == schedule_name {
slog.Info("schedule found:", "id", schedule.Id)
return schedule.Id, nil
}
}
return "", fmt.Errorf("schedule not found")
}
func get_on_call_engineer(
schedule_id string, dd_api_key string, dd_app_key string,
) (string, error) {
slog.Info("getting on-call engineer for", "schedule_id", schedule_id)
headers := map[string]string{
"DD-API-KEY": dd_api_key,
"DD-APPLICATION-KEY": dd_app_key,
"Accept": "application/json",
}
// make the request
body, err := http_req("engineer", "GET", endpoint_schedules+schedule_id+"/on-call", headers, nil)
if err != nil {
slog.Error(err.Error())
return "", fmt.Errorf("error getting on-call engineer http request")
}
// body is json so we need to parse it to get the on-call engineer
var on_call OnCallBody
err = json.Unmarshal(body, &on_call)
if err != nil {
return "", fmt.Errorf("error unmarshalling engineer response body: %v", err)
}
// get the on-call engineer id
user_id := on_call.Data.Relationships.User.Data.Id
slog.Info("on-call engineer id found:", "id", user_id)
// loop through the included to find the on-call engineer name from the on-call engineer id
for _, included := range on_call.Included {
if included.Id == user_id && included.Type == "users" {
slog.Info("on-call engineer email found:", "email", included.Attributes.Email)
return included.Attributes.Email, nil
}
}
return "", fmt.Errorf("on-call engineer not found")
}
func send_slack_message(
on_call_engineer_email string,
schedule_id string,
endpoint_slack string,
schedule_name string,
) (string, error) {
slog.Info("sending slack message:", "on_call_engineer_email", on_call_engineer_email)
headers := map[string]string{
"Content-Type": "application/json",
}
// prepare body for the slack message
messageBody, err := json.Marshal(map[string]string{
"engineer_email": on_call_engineer_email,
"schedule_name": schedule_name,
"schedule_link": datadog_schedules_link + schedule_id,
})
if err != nil {
return "", fmt.Errorf("error marshalling slack message: %v", err)
}
// make the request
body, err := http_req("slack", "POST", endpoint_slack, headers, messageBody)
if err != nil {
slog.Error(err.Error())
return "", fmt.Errorf("error sending slack message with http request")
}
slog.Info("slack message sent:", "response", string(body))
return string(body), nil
}
func http_req(
kind string,
method string,
endpoint string,
headers map[string]string,
payload []byte,
) ([]byte, error) {
// create http request to the datadog to get the on-call schedule engineer
req, err := http.NewRequest(method, endpoint, bytes.NewBuffer(payload))
if err != nil {
return nil, fmt.Errorf("error creating %s request: %v", kind, err)
}
// set headers
for key, value := range headers {
req.Header.Set(key, value)
}
// make the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making %s request: %v", kind, err)
}
defer resp.Body.Close()
// read the body into a byte slice before unmarshalling it
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading %s response body: %v", kind, err)
}
// check if the response code is 200
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"received non-200 response code for %s request: code=%v body=%s",
kind,
resp.StatusCode,
body,
)
}
return body, nil
}
func get_vars() (string, string, string, string, error) {
schedule_name := os.Getenv("SCHEDULE_NAME")
dd_api_key := os.Getenv("DD_API_KEY")
dd_app_key := os.Getenv("DD_APP_KEY")
endpoint_slack := os.Getenv("ENDPOINT_SLACK")
var err error
switch {
case schedule_name == "":
err = fmt.Errorf("SCHEDULE_NAME environment variable not set")
case dd_api_key == "":
err = fmt.Errorf("DD_API_KEY environment variable not set")
case dd_app_key == "":
err = fmt.Errorf("DD_APP_KEY environment variable not set")
case endpoint_slack == "":
err = fmt.Errorf("ENDPOINT_SLACK environment variable not set")
}
return schedule_name, dd_api_key, dd_app_key, endpoint_slack, err
}
func main() {
var schedule_name, dd_api_key, dd_app_key, endpoint_slack string
var err error
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, nil)))
schedule_name, dd_api_key, dd_app_key, endpoint_slack, err = get_vars()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
schedule_id, err := get_on_call_schedule(schedule_name, dd_api_key, dd_app_key)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
on_call_engineer_email, err := get_on_call_engineer(schedule_id, dd_api_key, dd_app_key)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
_, err = send_slack_message(
on_call_engineer_email, schedule_id, endpoint_slack, schedule_name,
)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
}