This module implements a few common operations for the OpsGenie API in Elvish.
This file is written in literate programming style, to make it easy to explain. See $name.elv for the generated file.
Install the elvish-modules
package using epm:
use epm
epm:install github.com/zzamboni/elvish-modules
In your rc.elv
, load this module:
use github.com/zzamboni/elvish-modules/opsgenie
You need to specify your API key so that it can be used in the requests:
opsgenie:api-key = 'xxxx00x0-0xxx-0000-x00x-00x0000xx0xx'
opsgenie:admins
returns a map containing the users which have admin
role in their teams as keys, with the value being the team in which they have that role.
opsgenie:admins
Load modules used in the code.
use str
First, we declare the variable that will contain the API key.
var api-key = ''
For write operations, we can set a secondary key which has the necessary privileges.
var write-api-key = ''
We initialize the API URL.
var api-url = 'https://api.eu.opsgenie.com/v2'
The opsgenie:request
function is the lowest-level function - it simply queries an API URL, parses and returns the result.
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
}
The opsgenie:post-request
function is similar but receives a $data
payload which should be a map, and is posted as JSON together with the request.
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
}
The opsgenie:request-data
returns the data
field in the response.
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
}