af
Version:
a command line interface for interacting with AppFog API. This should eventually be able to replace the af gem.
186 lines (172 loc) • 9.37 kB
JavaScript
/**
* This is the main module for appfog-api
*
* provides functions for all api calls to appfog/cloudfoundry
*/
var http = require("http"),
util = require("util"),
request = require('request'),
querystring= require("querystring");
module.exports.settings = {
token: "",
endpoint: "https://api.appfog.com"
};
/**
* https://github.com/lucperkins/appfog-api-docs
*
*/
/**
* Endpoint : /info
* Method : GET
* Description : Returns a variety of information, including currently-supported runtimes, user payment tier, and so on.
* af cli cmd : af info
*/
module.exports.info = function info(opts, cb) {
request({
url: module.exports.settings.endpoint + '/info',
json: true,
headers: {
'Authorization': module.exports.settings.token
}
}, function (error, response, body) {
cb(error, body);
});
};
/**
* REQUEST: post https://api.appfog.com/users/chris%40matheson.it/tokens
* RESPONSE_HEADERS:
* accept_ranges : bytes
* age : 0
* cache_control : no-cache
* content_type : application/json; charset=utf-8
* date : Wed, 01 May 2013 09:26:44 GMT
* server : nginx
* via : 1.1 varnish
* x_powered_by : Express
* x_varnish : 674631566
* content_length : 128
* connection : keep-alive
* REQUEST_BODY: {"password":"imhim4032"}
* RESPONSE: [200]
* {
* "token": "04085b084922166368726973406d61746865736f6e2e6974063a0645466c2b07541a8a5122193777feea5ff8c71d1e1a92a85105b338600e43d7"
* }
* <<<
*/
/**
* Endpoint : /users
* Method : GET
* Description :
* af cli cmd : af login
*/
module.exports.login = function login(opts, cb) {
var tokenPatern = /[0-9A-F]{116}/i;
request.post({
url: module.exports.settings.endpoint+'/users/'+querystring.escape(opts.email)+'/tokens',
json: true,
body: {"password":opts.password}
}, function (error, response, body) {
console.info('got response from login');
module.exports.settings.token = body.token;
if(!tokenPatern.test(module.exports.settings.token)){
//invalid token
error = error || new Error('Invalid token revieved');
}
cb(error, module.exports.settings.token);
});
};
/**
* Endpoint : /apps
* Method : GET
* Description : Lists all apps for a user
* af cli cmd : af apps
*/
module.exports.apps = function apps(opts, cb) {
request({
url: module.exports.settings.endpoint+'/apps',
json: true,
headers:{
'Authorization': module.exports.settings.token
}
}, function (error, response, body) {
cb(error, body);
});
};
/**
* Endpoint : /apps
* Method : GET
* Description : Lists all apps for a user
* af cli cmd : N/A
*/
module.exports.app = function app(opts, cb) {
request({
url: module.exports.settings.endpoint+'/apps/'+opts.appname,
json: true,
headers:{
'Authorization': module.exports.settings.token
}
}, function (error, response, body) {
cb(error, body);
});
};
/**
* Endpoint : /apps/app-name
* Method : PUT
* Description : Updates an application.
* af cli cmd : af update
*/
module.exports.update = function update (opts, cb) {
console.log('Returns stats for a specific application, including URI(s), IP address, uptime, memory quota, HTTP port, and so on.');
};
/**
* Endpoint : /services
* Method : GET
* Description : Lists all the services associated with a user, including MySQL, PostgresQL, MongoDB, and Redis databases,
* as well as RabbitMQ messaging systems.
* af cli cmd : af services
*/
module.exports.services = function services(opts, cb) {
request({
url: module.exports.settings.endpoint+'/services',
json: true,
headers:{
'Authorization': module.exports.settings.token
}
}, function (error, response, body) {
cb(error, body);
});
};
/**
* Endpoint : /stats
* Method : GET
* Description : Returns stats for a specific application, including URI(s), IP address, uptime, memory quota, HTTP port, and so on.
* af cli cmd : af stats
*/
module.exports.stats = function stats (opts, cb) {
request({
url: module.exports.settings.endpoint+'/apps/'+ opts.appname +'/stats',
json: true,
headers:{
'Authorization': module.exports.settings.token
}
}, function (error, response, body) {
console.log(util.inspect(body, {colors:true, depth:null}));
});
};
/**
* Endpoint : /infras
* Method : GET
* Description : Lists currently available infrastructures, as well as their associated names (for example eu-aws for AWS-Europe (Ireland).
* af cli cmd : af infras
*/
module.exports.infras = function infras (opts, cb) {
request({
url: module.exports.settings.endpoint+'/infras',
json: true,
headers:{
'Authorization': module.exports.settings.token
}
}, function (error, response, body) {
console.log(util.format('%j', body));
});
};