Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
193 lines (156 sloc) 5.19 KB

OpsGenie API functions for Elvish

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.

Table of Contents

Usage

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

Implementation

First, we declare the variable that will contain the API key.

api-key = ''

For write operations, we can set a secondary key which has the necessary privileges.

write-api-key = ''

We initialize the API URL.

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 &params=[&]]{
  auth-hdr = 'Authorization: GenieKey '$api-key
  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]{
  auth-hdr = 'Authorization: GenieKey '$write-api-key
  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]{
  response = (request $url)
  data = $response[data]
  if $paged {
    while (and (has-key $response paging) (has-key $response[paging] next)) {
      response = (request $response[paging][next])
      newdata = $response[data]
      data = [ $@data $@newdata ]
    }
  }
  put $data
}
fn admins {
  admins = [&]
  url = $api-url'/teams'
  put (explode (request-data $url))[name] | each [id]{
    #put $id
    try {
      put (explode (request-data $url'/'$id'?identifierType=name')[members]) | each [user]{
        #put $user
        if (eq $user[role] admin) {
          admins[$user[user][username]] = $id
        }
      }
    } except e {
      # This is here to skip teams without members
    }
  }
  put $admins
}

fn url-for [what &params=[&]]{
  params-str = (keys $params | each [k]{ put $k"="$params[$k] } | joins "&")
  put $api-url'/'$what'?'$params-str
}

fn list [what &keys=[name] &params=[&]]{
  each [r]{
    res = [&]
    if (eq $keys []) {
      res = $r
    } else {
      each [k]{
        res[$k] = $r[$k]
      } $keys
    }
    put $res
  } (request-data (url-for $what &params=$params))
}

fn get [what &params=[&]]{
  request (url-for $what &params=$params)
}

fn get-data [what &params=[&]]{
  request-data (url-for $what &params=$params)
}

fn create-user [username fullname role otherfields &team=""]{
  payload = $otherfields
  payload[username] = $username
  payload[fullName] = $fullname
  payload[role] = [&name= $role]
  post-request (url-for users) $payload
  echo ""
  if (not-eq $team "") {
    data = [ &user= [ &username= (echo $username | tr '[A-Z]' '[a-z]') ] ]
    post-request (url-for "teams/"$team"/members" &params=[ &teamIdentifierType= name ]) $data
    echo ""
  }
}
fn add-users-to-team [team @users]{
  each [username]{
    data = [ &user= [ &username= (echo $username | tr '[A-Z]' '[a-z]') ] ]
    post-request (url-for "teams/"$team"/members" &params=[ &teamIdentifierType= name ]) $data
    echo ""
  } $users
}
fn post-api [path data &params=[&] ]{
  url = (url-for $path &params=$params)
  post-request $url $data
}
fn api [path &params=[&] ]{
  url = (url-for $path)
  request $url &params=$params
}