Skip to content

Instantly share code, notes, and snippets.

@daxanya2
Created December 22, 2013 01:58
Show Gist options
  • Save daxanya2/8077570 to your computer and use it in GitHub Desktop.
Save daxanya2/8077570 to your computer and use it in GitHub Desktop.
HubotからDigitalOceanAPIを叩いてみる ref: http://qiita.com/daxanya2/items/fc7ecc02b9eef52c3515
HttpClient = require 'scoped-http-client'
async = require 'async'
CLIENT_ID = '__YOUR_CLIENT_ID__'
API_KEY = '__YOUR_API__KEY__'
class DigitalOcean
constructor: (debug = false) ->
get : (path, params, callback) ->
params.client_id=CLIENT_ID
params.api_key=API_KEY
url = "https://api.digitalocean.com/#{path}/?"+("#{k}=#{v}" for k,v of params).join('&')
console.log(url) if debug
HttpClient.create(url)
.header("User-Agent", "Hubot/#{@version}")
.get() (err,res,body) =>
try
callback(JSON.parse(body))
catch e
console.log(e)
console.log("can't load")
check_api_status : (json) ->
json.status == 'OK'
show_droplets : (droplets) ->
("#{droplet.name}(#{droplet.id})" for droplet in droplets).join('\n')
show_images : (images) ->
("#{image.name}(#{image.id})" for image in images).join('\n')
module.exports = (robot) ->
api = new DigitalOcean(debug=false)
robot.respond /digitalocean\s*(リスト|list)/i, (msg) ->
async.waterfall [
(callback)->
api.get("droplets", {}, (json) ->
if api.check_api_status(json)
if (json.droplets.length > 0)
msg.reply '今動いているdropletはこれだけありますー。\n' + api.show_droplets(json.droplets)
else
msg.reply '今はdropletは一つも立ち上がってないですー。'
callback null
else
msg.reply "digitaloceanのdroplets_apiが正常に動いていないようですー。"
)
(callback)->
api.get("images", {filter:"my_images"}, (json)->
if api.check_api_status(json)
if (json.images.length > 0)
msg.reply '今動いている自前管理のimageはこれだけありますー。\n' + api.show_images(json.images)
else
msg.reply '今は自前管理のimageは一つもないですー。'
callback null
else
msg.reply "digitaloceanのimages_apiが正常に動いていないようですー。"
)
], (err) ->
:
:
"dependencies": {
:
"scoped-http-client": "0.9.8",
"async": "0.2.9",
:
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment