Created
June 10, 2011 02:14
-
-
Save smarx/1018127 to your computer and use it in GitHub Desktop.
Node.js, CoffeeScript, and the Windows Azure Service Management API
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
### | |
certificate generated via: | |
openssl req -x509 -nodes -days 365 -subj "/CN=test" -newkey rsa:1024 -keyout priv.pem -out pub.pem | |
openssl x509 -outform der -in pub.pem -out pub.cer | |
(pub.cer is what's uploaded to Windows Azure via the portal) | |
### | |
crypto = require 'crypto' | |
https = require 'https' | |
fs = require 'fs' | |
xml2js = require 'xml2js' | |
cli = require 'cli' | |
util = require 'util' | |
base64DecodeAll = (x) -> | |
return unless x instanceof Object | |
for name, value of x | |
if name == 'Label' | |
x[name] = (new Buffer value, 'base64').toString() | |
else | |
base64DecodeAll x[name] | |
return | |
class ServiceManagementApi | |
constructor: (@subscriptionId, @key, @cert) -> | |
getOperatingSystemFamilies: (callback) -> | |
this.call 'operatingsystemfamilies', 'GET', callback | |
call: (path, method, body, callback) -> | |
if not callback? | |
if not body? | |
callback = method | |
method = 'GET' | |
body = undefined | |
else | |
callback = body | |
body = undefined | |
options = { | |
host: 'management.core.windows.net', | |
port: 443, | |
key: @key, | |
cert: @cert, | |
method: method, | |
headers: { 'x-ms-version': '2010-10-28' }, | |
path: "/#{@subscriptionId}/#{path}" | |
} | |
req = https.request options, (res) -> | |
body = '' | |
res.on 'data', (chunk) -> body += chunk | |
res.on 'end', (result) -> | |
parser = new xml2js.Parser | |
parser.on 'end', (result) -> | |
base64DecodeAll result | |
for name, value of result | |
callback value unless name == '@' | |
return | |
parser.parseString body | |
return | |
req.write body if body? | |
req.end() | |
return | |
options = cli.parse { | |
subscriptionId: ['s', 'Subscription ID (found in the Windows Azure portal)', 'string'], | |
privateKey: ['p', 'Private key (.pem file)', 'file', 'priv.pem'], | |
certificate: ['c', 'Public certificate (.pem file)', 'file', 'pub.pem'] | |
} | |
new ServiceManagementApi options.subscriptionId, | |
fs.readFileSync(options.privateKey, 'ascii'), | |
fs.readFileSync(options.certificate, 'ascii') | |
.getOperatingSystemFamilies (result) -> | |
console.log util.inspect result, false, 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment