Skip to content
Permalink
Browse files

[minor] Set the correct API endpoint before we set a new datacenter l…

…ocation
  • Loading branch information...
3rd-Eden committed Jan 16, 2013
1 parent 7af5f2b commit 20cffae45aabdc0cfad6eab6c9128547cf7ab048
Showing with 22 additions and 10 deletions.
  1. +15 −8 node.js/lib/client/apps.js
  2. +7 −2 node.js/lib/client/client.js
@@ -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;
}
});
}
@@ -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;
}
});
@@ -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);
});
};

@@ -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);
@@ -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'];

@@ -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) {
@@ -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);
});
};

0 comments on commit 20cffae

Please sign in to comment.
You can’t perform that action at this time.