-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagerduty_provider.go
128 lines (113 loc) · 3.04 KB
/
pagerduty_provider.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
package tessen
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strings"
)
func GetFilteredListOfPagerDutyEvents(query Query, data *interface{}) []interface{} {
log.Debugf("GetFilteredListOfPagerDutyEvents: data: %q", *data)
results := make([]interface{}, 0)
if data == nil {
return results
}
templateName := "pagerduty_incident_list"
if query.Template != "" {
templateName = query.Template
}
template := GetTemplate(templateName)
if template == "" {
template = GetTemplate("event_list")
}
b, err := json.Marshal(*data)
if err != nil {
log.Errorf("%s", err)
return results
}
cmd := exec.Command("jq", fmt.Sprintf(".[] | select( %s ) | .id", query.Filter))
cmd.Stdin = bytes.NewReader(b)
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
log.Errorf("%s", err)
return results
}
for _, v := range strings.Split(out.String(), "\n") {
id := strings.Trim(v, "\"")
for _, e := range (*data).([]interface{}) {
event := e.(map[string]interface{})
if event["id"].(string) == id {
id := strings.Trim(v, "\"")
buf := new(bytes.Buffer)
RunTemplate(template, event, buf)
results = append(results, QueryResult{id, buf.String(), event})
continue
}
}
}
if len(results) == 0 {
results = append(results, QueryResult{"", "No results found", nil})
}
return results
}
func FetchPagerDutyEvent(id string, source *Source) interface{} {
eventData := source.CachedData.([]interface{})
log.Debugf("FetchPagerDutyEvent: eventData: %q", eventData)
for _, ev := range eventData {
log.Debugf("FetchPagerDutyEvent: ev: %q", ev)
event := ev.(map[string]interface{})
if event["id"].(string) == id {
return event
}
}
return nil
}
func FetchPagerDutyEvents(s *Source) ([]interface{}, error) {
var contents []byte
var err error
if s.Endpoint[0] == '/' {
contents, err = getPagerDutyResultsFromFile(s)
} else {
contents, err = getPagerDutyResultsFromPagerDuty(s)
}
log.Debugf("FetchPagerDutyEvents: contents: %q", contents)
if err != nil {
return nil, err
}
data := make(map[string]interface{})
dec := json.NewDecoder(strings.NewReader(string(contents)))
dec.Decode(&data)
incidents := data["incidents"].([]interface{})
log.Debugf("FetchPagerDutyEvents: incidents: %q", incidents)
return incidents, nil
}
func getPagerDutyResultsFromFile(s *Source) (contents []byte, err error) {
contents, err = ioutil.ReadFile(s.Endpoint)
if err != nil {
return nil, err
}
return contents, nil
}
func getPagerDutyResultsFromPagerDuty(s *Source) (contents []byte, err error) {
queryParams := "?status=triggered,acknowledged"
opts := s.Options
token := opts["token"]
client := &http.Client{}
url := fmt.Sprintf("%s/api/v1/incidents%s", s.Endpoint, queryParams)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", token))
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
contents, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return contents, nil
}