Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Database API integration #45

Closed
wants to merge 9 commits into from

2 participants

Nico Reed Charlie Robbins
Nico Reed

This is in a pretty stable state.

something as a note, jitsu databases get <database name> only returns the id of the database and the rest of the metadata is ignored (its a huge object with tons of fields, 15+ in most cases, and which are useful varies on database and use case), this may or may not be the desired behavior.

Charlie Robbins
Owner

Merged this commits in using cherry-pick

Charlie Robbins indexzero closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7 lib/jitsu.js
View
@@ -17,6 +17,7 @@ var failCodes = jitsu.failCodes = {
400: 'Bad Request',
403: 'Not Authorized',
404: 'Item not found',
+ 409: 'Conflict',
500: 'Internal Server Error'
};
@@ -33,6 +34,8 @@ jitsu.log = require('cliff');
jitsu.api = {};
jitsu.api.Client = require('jitsu/api/client').Client;
jitsu.api.Apps = require('jitsu/api/apps').Apps;
+jitsu.api.Databases = require('jitsu/api/databases').Databases;
+jitsu.api.Logs = require('jitsu/api/logs').Logs;
jitsu.api.Snapshots = require('jitsu/api/snapshots').Snapshots;
jitsu.api.Users = require('jitsu/api/users').Users;
jitsu.prompt = require('prompt');
@@ -148,6 +151,8 @@ jitsu.setup = function (callback) {
jitsu.users = new jitsu.api.Users(jitsu.config);
jitsu.apps = new jitsu.api.Apps(jitsu.config);
jitsu.snapshots = new jitsu.api.Snapshots(jitsu.config);
+ jitsu.databases = new jitsu.api.Databases(jitsu.config);
+ jitsu.logs = new jitsu.api.Logs(jitsu.config);
started = true;
callback();
};
@@ -262,4 +267,4 @@ jitsu.showError = function (command, err, shallow, skip) {
}
winston.info('Nodejitsu '.grey + 'not ok'.red.bold);
-};
+};
71 lib/jitsu/api/databases.js
View
@@ -0,0 +1,71 @@
+/*
+ * databases.js: Client for the Nodejitsu users API.
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var util = require('util'),
+ winston = require('winston'),
+ jitsu = require('jitsu');
+
+//
+// ### function Databases (options)
+// #### @options {Object} Options for this instance
+// Constructor function for the Databases resource responsible
+// with Nodejitsu's Databases API
+//
+var Databases = exports.Databases = function (options) {
+ jitsu.api.Client.call(this, options);
+};
+
+// Inherit from Client base object
+util.inherits(Databases, jitsu.api.Client);
+
+//
+// ### function create (databaseType, databaseName, callback)
+// #### @databaseType {string} Type of database to create, valid values: redis, couch, mongo
+// #### @databaseName {string} Name of the database to create
+// #### @callback {function} Continuation to pass control to when complete
+// Provisions a database for the user
+//
+Databases.prototype.create = function (databaseType, databaseName, callback) {
+ this._request('POST', ['databases', this.options.get('username'), databaseName], {type:databaseType}, callback, function (res, result) {
+ callback(null, result);
+ });
+};
+
+//
+// ### function get (databaseName, callback)
+// #### @databaseName {string} Name of the database to get
+// #### @callback {function} Continuation to pass control to when complete
+// Gets the metadata for the specified database
+//
+Databases.prototype.get = function (databaseName, callback) {
+ this._request('GET', ['databases', this.options.get('username'), databaseName], callback, function (res, result) {
+ callback(null, result);
+ });
+};
+
+//
+// ### function list (callback)
+// #### @callback {function} Continuation to pass control to when complete
+// Gets the list of databases assigned to the user
+//
+Databases.prototype.list = function (callback) {
+ this._request('GET', ['databases', this.options.get('username')], callback, function (res, result) {
+ callback(null, result);
+ });
+};
+
+//
+// ### function destroy (databaseName, callback)
+// #### @databaseName {string} Name of the database to delete
+// #### @callback {function} Continuation to pass control to when complete
+// Deprovisions specified database
+//
+Databases.prototype.destroy = function (databaseName, callback) {
+ this._request('DELETE', ['databases', this.options.get('username'), databaseName], callback, function (res, result) {
+ callback(null, result);
+ });
+}
35 lib/jitsu/api/logs.js
View
@@ -0,0 +1,35 @@
+/*
+ * logs.js: Client for the Nodejitsu logs API.
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var util = require('util'),
+ winston = require('winston'),
+ jitsu = require('jitsu');
+
+//
+// ### function Logs (options)
+// #### @options {Object} Options for this instance
+// Constructor function for the Logs resource
+// with Nodejitsu's Logs API
+//
+var Logs = exports.Logs = function (options) {
+ jitsu.api.Client.call(this, options);
+};
+
+// Inherit from Client base object
+util.inherits(Logs, jitsu.api.Client);
+
+Logs.prototype.byApp = function (appId, callback) {
+ this._request('POST', ['logs', this.options.get('username') , appId], {}, callback, function (res, result) {
+ callback(null, result);
+ });
+};
+
+Logs.prototype.byUser = function (callback) {
+ this._request('POST', ['logs', this.options.get('username')], {}, callback, function (res, result) {
+ callback(null, result);
+ });
+};
128 lib/jitsu/commands/databases.js
View
@@ -0,0 +1,128 @@
+/*
+ * databases.js: Commands related to user resources
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var winston = require('winston'),
+ jitsu = require('jitsu');
+
+var databases = exports;
+
+databases.usage = [
+ '`jitsu databases *` commands allow you to work with the database api',
+ '',
+ 'jitsu databases create <database type> <database name>',
+ 'jitsu databases list',
+ 'jitsu databases get <database name>',
+ 'jitsu databases destroy <database name>'
+];
+
+databases.create = function (databaseType, databaseName, callback) {
+ // Make sure that the user is passing a valid database type
+ if ( ['couch', 'redis', 'mongo'].indexOf(databaseType) === -1 ) {
+ winston.error('Invalid database type ' + databaseType.red);
+ winston.info('Expected: `couch`, `redis` or `mongo`');
+ return callback();
+ }
+
+ jitsu.databases.create(databaseType, databaseName, function (err) {
+ if (err) {
+ if (err.statusCode === '409' ) {
+ winston.error('You already created a database with that name.');
+ }
+ else {
+ winston.error(err);
+ return callback(err);
+ }
+ }
+ else {
+ winston.info('Database ' + databaseName + ' was created.');
+ }
+ databases.get(databaseName, callback);
+ });
+};
+
+databases.create.usage = [
+ 'Spins up a database for the user',
+ '',
+ 'Example usage:',
+ 'jitsu databases create couch <database name>',
+ 'jitsu databases create mongo <database name>',
+ 'jitsu databases create redis <database name>'
+]
+
+databases.get = function (databaseName, callback) {
+ jitsu.databases.get(databaseName, function (err, results) {
+ if (err) {
+ if (err.statusCode === '404') {
+ winston.error('Database does not exist');
+ }
+ else {
+ winston.error('Unexpected Error: ' + err);
+ }
+ }
+ else {
+ winston.info('Database name: ' + results.name);
+ winston.info('Database type: ' + results.type);
+ winston.info('Id: ' + results.metadata.id);
+ }
+ callback();
+ });
+}
+
+databases.get.usage = [
+ 'Gets the metadata of a database',
+ '',
+ 'Example usage:',
+ 'jitsu databases get <database name>'
+]
+
+databases.list = function (callback) {
+ jitsu.databases.list(function (err, results) {
+ if (err) {
+ winston.error('Unexpected Error: ' + err);
+ }
+ else {
+ results.forEach(function (database) {
+ winston.info('');
+ winston.info('Database name: ' + database.name);
+ winston.info('Database type: ' + database.type);
+ winston.info('Id: ' + database.metadata.id);
+ });
+
+ if (results.length === 0) {
+ winston.info('You have no databases.');
+ }
+ }
+ callback();
+ });
+}
+
+databases.list.usage = [
+ 'Lists the dabases you currently have running',
+ '',
+ 'Example usage:',
+ 'jitsu databases list'
+]
+
+databases.destroy = function (databaseName, callback) {
+ jitsu.databases.destroy(databaseName, function (err) {
+ if (err) {
+ winston.error('Database could not be destroyed.');
+ }
+ else {
+ winston.info('Database was deleted.');
+ }
+ callback();
+ });
+}
+
+databases.destroy.usage = [
+ 'Deprovisions a database',
+ 'WARNING: this action is not reversible',
+ '',
+ 'Example usage:',
+ 'jitsu databases destroy <database name>'
+]
5 lib/jitsu/commands/help.js
View
@@ -50,6 +50,7 @@ help.usage = [
' jitsu apps',
' jitsu snapshots',
' jitsu users',
+ ' jitsu databases',
' jitsu conf',
' jitsu logout',
'',
@@ -103,7 +104,7 @@ help.show = function (name, action) {
// Setup exports for all relevant resources:
// `apps`, `snapshots`, `config`.
//
-['apps', 'snapshots', 'users', 'config', 'list', 'deploy', 'create'].forEach(function (resource) {
+['apps', 'databases', 'snapshots', 'users', 'config', 'list', 'deploy', 'create'].forEach(function (resource) {
help[resource] = function (action, callback) {
if (!callback) {
callback = action;
@@ -113,4 +114,4 @@ help.show = function (name, action) {
help.show(resource, action);
callback();
};
-});
+});
78 lib/jitsu/commands/logs.js
View
@@ -0,0 +1,78 @@
+/*
+ * logs.js: Commands related to user resources
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var winston = require('winston'),
+ jitsu = require('jitsu');
+
+var logs = exports;
+
+logs.usage = [
+ '`jitsu logs *` commands allow you to work with the log api',
+ '',
+ 'jitsu logs all',
+ 'jitsu logs app <app name>',
+];
+
+var parse = function (string) {
+ var pairs = string.split(',');
+ var obj = {}
+ for (var i = 0; i < pairs.length; ++i) {
+ var pair_split = pairs[i].split('=');
+ obj[pair_split[0]] = pair_split[1];
+ }
+ return obj;
+}
+
+logs.all = function (databaseType, databaseName, callback) {
+ jitsu.logs.byUser( function (err, results) {
+ if (err) {
+ callback(err);
+ }
+ else {
+ for (key in results) {
+ for (var i = 0; i < results[key].data.length; ++i) {
+ var data = parse(results[key].data[i].text);
+ winston.info(results[key].data[i].timestamp.blue +
+ ' ' + data['app'].yellow +
+ ' ' + data['message']);
+ }
+ }
+ callback();
+ }
+ });
+};
+
+logs.all.usage = [
+ 'Print the logs from all applications',
+ '',
+ 'Example usage:',
+ 'jitsu logs all',
+]
+
+logs.app = function (databaseName, callback) {
+ jitsu.logs.byApp(databaseName, function (err, results) {
+ if (err) {
+ callback(err);
+ }
+ else {
+ for (var i = 0; i < results.data.length; ++i) {
+ var data = parse(results.data[i].text);
+ winston.info(results.data[i].timestamp.blue +
+ ' ' + data['app'].yellow +
+ ' ' + data['message']);
+ }
+ callback();
+ }
+ });
+}
+
+logs.app.usage = [
+ 'Print the logs from specified application',
+ '',
+ 'Example usage:',
+ 'jitsu logs app <database name>'
+]
5 package.json
View
@@ -19,7 +19,9 @@
"cliff": "0.1.x",
"colors": "0.x.x",
"eyes": "0.1.x",
+ "findit": "0.0.x",
"nconf": "0.1.x",
+ "loggly": "0.3.x",
"npm": "1.0.x & > 1.0.6",
"optimist": "0.2.x",
"pkginfo": "0.2.x",
@@ -28,7 +30,8 @@
"request": "1.9.x",
"require-analyzer": "0.2.x",
"winston": "0.3.x",
- "semver": "1.0.x"
+ "semver": "1.0.x",
+ "wordwrap": "0.0.x"
},
"devDependencies": {
"vows": "0.5.x"
Something went wrong with that request. Please try again.