Skip to content

Commit

Permalink
[minor] Set the correct API endpoint before we set a new datacenter l…
Browse files Browse the repository at this point in the history
…ocation
  • Loading branch information
3rd-Eden committed Jan 16, 2013
1 parent 7af5f2b commit 20cffae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
23 changes: 15 additions & 8 deletions node.js/lib/client/apps.js
Expand Up @@ -47,8 +47,8 @@ Apps.prototype.list = function (username, callback) {
//
if (username === self.options.get('username')) {
result.apps.forEach(function reduce(memo, app) {
if (app.config) {
self.clouds[app._id] = app.config.cloud;
if (app.config && app.config.cloud) {
self.clouds[app._id] = app.cloud;
}
});
}
Expand Down Expand Up @@ -87,7 +87,7 @@ Apps.prototype.view = function (appName, callback) {
//
// Update the cloud cache.
//
if (app.config) {
if (app.config && app.config.cloud) {
self.clouds[appName] = app.config.cloud;
}
});
Expand Down Expand Up @@ -202,15 +202,22 @@ Apps.prototype.datacenter = function (appName, cloud, callback) {

if (!Array.isArray(cloud)) cloud = [cloud];

this.cloud({ method: 'POST', uri: argv, body: cloud, appName: appName }, this.request, function (err, result) {
if (err) return callback(err);

callback(err, result);
//
// As this API request needs to go to the correct datacenter, add the current
// cloud call to our internal datacenter cache so it will target the correct
// API
//
this.clouds[appName] = cloud;

this.cloud({ method: 'POST', uri: argv, body: cloud, appName: appName }, this.request, function (err, result) {
//
// Assume that this call invalidates our cached datacenter endpoint
// Assume that this call invalidates our cached datacenter endpoint, so
// remove it, and it will be fetched again on the next call
//
delete self.clouds[appName];

if (err) return callback(err);
callback(err, result);
});
};

Expand Down
9 changes: 7 additions & 2 deletions node.js/lib/client/client.js
Expand Up @@ -51,6 +51,9 @@ Client.prototype.cloud = function (options, api, callback) {
return api.call(this, options, callback);
}

//
// Fetches the API endpoints
//
function endpoints(done) {
self.request({ uri: ['endpoints'] }, function endpoints(err, datacenters) {
if (err) return done(err);
Expand All @@ -60,6 +63,9 @@ Client.prototype.cloud = function (options, api, callback) {
});
}

//
// Fetches the datacenter locations for the app
//
function locations(done) {
var argv = ['apps', options.appName, 'cloud'];

Expand Down Expand Up @@ -100,7 +106,7 @@ Client.prototype.cloud = function (options, api, callback) {
}, {});

opts.remoteUri = self.datacenters[cloud.provider][cloud.datacenter];
if (!~opts.remoteUri.indexOf('http')) opts.remoteUri = 'http://'+ opts.remoteUri;
if (!~opts.remoteUri.indexOf('http')) opts.remoteUri = 'https://'+ opts.remoteUri;

api.call(self, opts, done);
}, function ready(err, results) {
Expand Down Expand Up @@ -188,7 +194,6 @@ Client.prototype.request = function (options, callback) {

// Only add the response argument when people ask for it
if (callback.length === 3) return callback(error, result, res);

callback(error, result);
});
};
Expand Down

0 comments on commit 20cffae

Please sign in to comment.