Skip to content

Commit

Permalink
numerous changes related to unified cli work
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Haynie committed Nov 16, 2014
1 parent 3ecb66e commit 1f3c8c9
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 34 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 1.0.4 (11-16-2014)

- Added support for creating new application from tiapp.xml
- Added support for creating session from session id
- Added support for retrieving cloud environments
- Added support for both production and development logins
- User.find will now use current user if not user id passed

# 1.0.1 (09-29-2014)

- Added support for creating an ACS application
Expand Down
51 changes: 41 additions & 10 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,47 @@ if (args.length!==2) {
process.exit(1);
}

function Decycler() {
var cache = [];
return function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
};
}

function die() {
console.error.apply(console.err,arguments);
process.exit(1);
}

sdk.setDevelopment();

sdk.Auth.login(args[0], args[1], function(err,session){
if (err) {
console.err('Error=',err);
process.exit(1);
if (err) { die('Error=',err); }
console.log(JSON.stringify(session,Decycler(),2));

/*
sdk.App.create(session, '/Users/jhaynie/tmp/foo/app/tiapp.xml', function(err,result){
console.log(arguments);
});*/

/*try {
sdk.Auth.createSessionFromID(session.id, function(err,session_){
if (err) { die('Error=',err); }
sdk.App.findAll(session_, function(err, apps){
if (err) { die('Error=',err); }
console.log(apps);
});
});
}
sdk.Cloud.createApp(session, "foo", function(err,app){
if (err) {
console.err('Error=',err);
process.exit(1);
}
console.log('Application created: ',app);
});
catch (E) {
console.error(E.stack);
}*/
});
44 changes: 42 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
var request = require('request'),
urllib = require('url'),
fs = require('fs'),
AppC = require('./index');

function App() {
Expand All @@ -11,31 +12,70 @@ function App() {

/**
* find the apps that the logged in has access to
*
* @param {Object} session
* @param {String} org id
* @param {Function} callback
*/
App.findAll = function(session, org_id, callback) {
if (org_id && typeof(org_id)=='function') {
callback = org_id;
org_id = null;
}
var url = urllib.resolve(AppC.baseurl, '/api/v1/app' + (org_id?('?org_id'+org_id):''));
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback));
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback));
}

/**
* find a specific app by id
*
* @param {Object} session
* @param {String} app id
* @param {Function} callback
*/
App.find = function(session, app_id, callback) {
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+app_id);
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback));
}

/**
* update an app
*
* @param {Object} session
* @param {Object} app object to update
* @param {Function} callback
*/
App.update = function(session, app, callback) {
var guid = app.app_guid;
if (!guid) throw new Error("no app_guid property found");
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+guid),
obj = AppC.createRequestObject(session,url);
obj.json = app;
request.put(obj,AppC.createAPIResponseHandler(callback));
request.put(obj,AppC.createAPIResponseHandler(callback));
}

/**
* create or update an application from tiapp.xml file
*
* @param {Object} session
* @param {String} file location (path) to tiapp.xml
* @param {Function} callback
*/
App.create = function(session, tiappxml, callback) {
if (!fs.existsSync(tiappxml)) {
return callback(new Error("tiapp.xml file doesn't not exist"));
}
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/saveFromTiApp?org_id='+session.user.org_id);
console.log(url);
fs.readFile(tiappxml, function(err, data) {
if (err) { return callback(err); }

var obj = AppC.createRequestObject(session,url),
req = request.post(obj, AppC.createAPIResponseHandler(callback)),
form = req.form();

form.append('tiapp',data, {filename:'tiapp.xml'});
});
}

module.exports = App;
70 changes: 57 additions & 13 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
var request = require('request'),
urllib = require('url'),
AppC = require('./index'),
Session = require('./session');
User = require('./user'),
Session = require('./session'),
tough = require('tough-cookie');

function Auth() {
}
Expand All @@ -22,6 +24,21 @@ Auth.logout = function(session, callback) {
request.post(AppC.createRequestObject(session,url), next);
}

function resolveUserOrg(session, next) {
// find our orgs
AppC.Org.find(session,function(err,orgs){
if (err) return next(err);
session.orgs = {};
// map in our orgs
orgs && orgs.forEach(function(org){
session.orgs[org.org_id] = org;
});
// set our org to the logged in org
session.user.org_id && (session.user.org = session.orgs[session.user.org_id]);
next(null,session);
});
}

/**
* login
*/
Expand All @@ -30,18 +47,7 @@ Auth.login = function(username, password, callback) {
url = urllib.resolve(AppC.baseurl, '/api/v1/auth/login'),
next = AppC.createAPIResponseHandler(callback, function mapper(obj, next) {
session._set(obj);
// find our orgs
AppC.Org.find(session,function(err,orgs){
if (err) return callback(err);
session.orgs = {};
// map in our orgs
orgs && orgs.forEach(function(org){
session.orgs[org.org_id] = org;
});
// set our org to the logged in org
session.user.org_id && (session.user.org = session.orgs[session.user.org_id]);
next(null,session);
});
resolveUserOrg(session,next);
}),
r = request.post(AppC.createRequestObject(session,url), next),
form = r.form();
Expand All @@ -52,4 +58,42 @@ Auth.login = function(username, password, callback) {

}

/**
* from an existing authenticated session, create a new Session object
*/
Auth.createSessionFromID = function createSessionFromID(id, callback) {
var session = new Session(),
url = require('url'),
parse = url.parse(AppC.baseurl),
host = parse.host,
path = parse.path,
cookie = new tough.Cookie();

cookie.key = 'connect.sid';
cookie.value = id;
cookie.secure = true;
cookie.httpOnly = true;
cookie.path = path;
cookie.domain = host;
cookie.expires = Infinity;
cookie.hostOnly = false
cookie.creation = new Date;
cookie.lastAccessed = new Date;

session.id = id;
session.jar = new tough.CookieJar();

session.jar.setCookie(cookie, AppC.baseurl, function(err, cookie){
if (err) { return callback(err); }
// fetch the current user and set it on the session
User.find(session,function(err,user){
if (err) { return callback(err); }
session.user = user;
resolveUserOrg(session,function(err){
callback(err,session);
});
});
});
};

module.exports = Auth;
31 changes: 26 additions & 5 deletions lib/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ var async = require('async'),
request = require('request'),
AppC = require('./index');

Cloud.baseurl = 'https://api.cloud.appcelerator.com';

function Cloud() {
}


function createCloudResponseHandler(callback) {
return function(err,response,body) {
if (err) { return callback(err); }
Expand All @@ -32,7 +29,7 @@ function createCloudResponseHandler(callback) {
*/
function login(session, callback) {

var url = Cloud.baseurl + "/v1/admins/login360.json?ct=enterprise&connect.sid="+session.id;
var url = Cloud.getEnvironment(session) + '/v1/admins/login360.json?ct=enterprise&connect.sid='+session.id;

request(AppC.createRequestObject(session,url), createCloudResponseHandler(function(err,body){
if (!err) {
Expand All @@ -55,7 +52,7 @@ Cloud.createApp = function createApp(session, name, callback) {
}

tasks.push(function(next){
var url = Cloud.baseurl + '/v1/apps/create.json?_session_id='+session.acs_session+'&ct=enterprise';
var url = Cloud.getEnvironment(session) + '/v1/apps/create.json?_session_id='+session.acs_session+'&ct=enterprise';
var r = request.post(AppC.createRequestObject(session,url), createCloudResponseHandler(function(err,body){
if (err) { return next(err); }
callback(null, body.response.apps[0]);
Expand All @@ -70,4 +67,28 @@ Cloud.createApp = function createApp(session, name, callback) {
});
};

Cloud.NODE_ACS = 'nodeACSEndpoint';
Cloud.ACS_BASE = 'acsBaseUrl';
Cloud.AUTH_BASE = 'acsAuthBaseUrl';

/**
* return the appropriate environment url.
*
* @param {String} type is one of Cloud.NODE_ACS, Cloud.ACS_BASE or Cloud.AUTH_BASE
* @param {String} name is one of production or development or another environment name
*/
Cloud.getEnvironment = function(session, type, name) {
if (!session.user) {
throw new Error("session is not valid. missing user");
}
if (!session.org) {
throw new Error("session is not valid. missing org");
}
type = type || Cloud.ACS_BASE;
name = name || AppC.isProduction ? 'production' : 'development';
return session.org.envs.filter(function(env){
return env.name===name;
})[0][type];
};

module.exports = Cloud;
22 changes: 20 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ function AppC() {

exports = module.exports = AppC;

AppC.baseurl = 'https://dashboard.appcelerator.com';

AppC.Auth = require('./auth');
AppC.Org = require('./org');
AppC.User = require('./user');
Expand All @@ -21,6 +19,26 @@ AppC.userAgent = pkg.name+'/'+pkg.version;
AppC.createAPIResponseHandler = createAPIResponseHandler;
AppC.createRequestObject = createRequestObject;

/**
* set the base url to use production
*/
AppC.setProduction = function setProduction() {
AppC.baseurl = 'https://dashboard.appcelerator.com';
AppC.isProduction = true;
};

/**
* set the base url to use development
*/
AppC.setDevelopment = function setDevelopment() {
AppC.baseurl = 'https://360-preprod.appcelerator.com';
AppC.isProduction = false;
};


// set DEFAULT to production host
AppC.setProduction();

function createRequestObject(session,url) {
if (!session || !session.jar) {
throw new Error("session invalid");
Expand Down
7 changes: 6 additions & 1 deletion lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ function User() {
User.find = function(session, user_id, callback) {
if (typeof(user_id)==='function') {
callback = user_id;
user_id = session && session.user && session.user.guid;
if (session && session.user && session.user.guid) {
user_id = session.user.guid;
}
else {
user_id = 'current';
}
}
var url = urllib.resolve(AppC.baseurl, '/api/v1/user/'+user_id);
request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(callback));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "appc-platform-sdk",
"version": "1.0.3",
"version": "1.0.4",
"description": "Appcelerator Platform SDK for node.js",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 1f3c8c9

Please sign in to comment.