Created
December 22, 2013 01:58
-
-
Save daxanya2/8077570 to your computer and use it in GitHub Desktop.
HubotからDigitalOceanAPIを叩いてみる ref: http://qiita.com/daxanya2/items/fc7ecc02b9eef52c3515
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: | |
: | |
"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