-
Notifications
You must be signed in to change notification settings - Fork 20
/
opsgenie.elv
110 lines (95 loc) · 2.74 KB
/
opsgenie.elv
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
use str
var api-key = ''
var write-api-key = ''
var api-url = 'https://api.eu.opsgenie.com/v2'
fn request {|url ¶ms=[&]|
var auth-hdr = 'Authorization: GenieKey '$api-key
set params = [(keys $params | each {|k| put "--data-urlencode" $k"="$params[$k] })]
curl -G -s $@params -H $auth-hdr $url | from-json
}
fn post-request {|url data|
var auth-hdr = 'Authorization: GenieKey '$write-api-key
var json-data = (put $data | to-json)
curl -X POST -H $auth-hdr -H 'Content-Type: application/json' --data $json-data $url
}
fn request-data {|url &paged=$true|
var response = (request $url)
var data = $response[data]
if $paged {
while (and (has-key $response paging) (has-key $response[paging] next)) {
set response = (request $response[paging][next])
var newdata = $response[data]
set data = [ $@data $@newdata ]
}
}
put $data
}
fn admins {
var admins = [&]
var url = $api-url'/teams'
put (all (request-data $url))[name] | each {|id|
#put $id
try {
put (all (request-data $url'/'$id'?identifierType=name')[members]) | each {|user|
#put $user
if (eq $user[role] admin) {
set admins[$user[user][username]] = $id
}
}
} catch e {
# This is here to skip teams without members
}
}
put $admins
}
fn url-for {|what ¶ms=[&]|
var params-str = (keys $params | each {|k| put $k"="$params[$k] } | str:join "&")
put $api-url'/'$what'?'$params-str
}
fn list {|what &keys=[name] ¶ms=[&]|
each {|r|
var res = [&]
if (eq $keys []) {
set res = $r
} else {
each {|k|
set res[$k] = $r[$k]
} $keys
}
put $res
} (request-data (url-for $what ¶ms=$params))
}
fn get {|what ¶ms=[&]|
request (url-for $what ¶ms=$params)
}
fn get-data {|what ¶ms=[&]|
request-data (url-for $what ¶ms=$params)
}
fn create-user {|username fullname role otherfields &team=""|
var payload = $otherfields
set payload[username] = $username
set payload[fullName] = $fullname
set payload[role] = [&name= $role]
post-request (url-for users) $payload
echo ""
if (not-eq $team "") {
var data = [ &user= [ &username= (echo $username | tr '[A-Z]' '[a-z]') ] ]
post-request (url-for "teams/"$team"/members" ¶ms=[ &teamIdentifierType= name ]) $data
echo ""
}
}
fn add-users-to-team {|team @users|
each {|username|
var data = [ &user= [ &username= (echo $username | tr '[A-Z]' '[a-z]') ] ]
post-request (url-for "teams/"$team"/members" ¶ms=[ &teamIdentifierType= name ]) $data
echo ""
} $users
}
fn post-api {|path data ¶ms=[&] |
var url = (url-for $path ¶ms=$params)
post-request $url $data
}
fn api {|path ¶ms=[&] |
var url = (url-for $path)
request $url ¶ms=$params
}