Last active
August 29, 2015 14:05
-
-
Save ThisIsMissEm/1d0b7e474db1daa3e2d9 to your computer and use it in GitHub Desktop.
I wanted to use DigitalOcean with Ansible. The python scripts don't work, so, I wrote this 70 liner node.js script.
This file contains 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
#!/usr/bin/env node | |
var token = process.env.DIGITALOCEAN_TOKEN; | |
if(!token) { | |
console.error("Please specify a DIGITALOCEAN_TOKEN environment variable"); | |
process.exit(1); | |
} | |
var request = require('request'); | |
request.get({ | |
method: "GET", | |
uri: "https://api.digitalocean.com/v2/droplets", | |
json: true, | |
headers: { | |
"Authorization": "Bearer " + token | |
} | |
}, function(error, response, body) { | |
var inventory = {}; | |
body.droplets.forEach(function(droplet) { | |
findPublicNetwork(droplet.networks).forEach(function(network){ | |
var ip = network.ip_address; | |
safePush(inventory, droplet['name'], ip); | |
safePush(inventory, 'region_'+droplet['region'].slug, ip) | |
safePush(inventory, 'image_' +droplet['image'].slug, ip) | |
safePush(inventory, 'size_' +droplet['size'].slug, ip) | |
safePush(inventory, 'status_'+droplet['status'], ip) | |
}); | |
findPublicNetwork(droplet.networks, "v6").forEach(function(network){ | |
safePush(inventory, 'ipv6', network.ip_address); | |
}); | |
}); | |
console.log(JSON.stringify(inventory, null, "\t")); | |
}); | |
function safePush(obj, key, item){ | |
if(obj[key]){ | |
obj[key].push(item); | |
}else{ | |
obj[key] = [item]; | |
} | |
} | |
function findPublicNetwork(networks, type){ | |
if(!type || type !== 'v6') type = "v4"; | |
return networks[type].filter(function(network){ | |
return network.type === "public"; | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated it to be cleaner, support ipv6 on machines (see section "ipv6" in results), and to correctly fetch region/image/size/status groupings. Also dropped support for querying directly by droplet id, because, uh.. why would you want to do that?