13851: Reverted use of discoveryDoc() on findAPI()
[arvados.git] / apps / workbench / app / assets / javascripts / models / session_db.js
index fab8fe3925c00da42f5223a763a659ff18e70441..5d42fdf07f668c3f1f25ad26543a4ecc2b26f05c 100644 (file)
@@ -94,9 +94,9 @@ window.SessionDB = function() {
             }
             var session = db.loadLocal();
             var apiHostname = new URL(session.baseURL).hostname;
-            m.request(session.baseURL+'discovery/v1/apis/arvados/v1/rest').then(function(localDD) {
+            db.discoveryDoc(session).map(function(localDD) {
                 var uuidPrefix = localDD.uuidPrefix;
-                m.request(baseURL+'discovery/v1/apis/arvados/v1/rest').then(function(dd) {
+                db.discoveryDoc({baseURL: baseURL}).map(function(dd) {
                     if (uuidPrefix in dd.remoteHosts ||
                         (dd.remoteHostsViaDNS && apiHostname.endsWith('.arvadosapi.com'))) {
                         // Federated identity login via salted token
@@ -231,9 +231,22 @@ window.SessionDB = function() {
         // discovery doc from a session's API server.
         discoveryDoc: function(session) {
             var cache = db.discoveryCache[session.baseURL];
-            if (!cache) {
+            if (!cache && session) {
                 db.discoveryCache[session.baseURL] = cache = m.stream();
-                m.request(session.baseURL+'discovery/v1/apis/arvados/v1/rest').then(cache);
+                var baseURL = session.baseURL;
+                if (baseURL[baseURL.length - 1] !== '/') {
+                    baseURL += '/';
+                }
+                m.request(baseURL+'discovery/v1/apis/arvados/v1/rest')
+                    .then(function (dd) {
+                        // Just in case we're talking with an old API server.
+                        dd.remoteHosts = dd.remoteHosts || {};
+                        if (dd.remoteHostsViaDNS === undefined) {
+                            dd.remoteHostsViaDNS = false;
+                        }
+                        return dd;
+                    })
+                    .then(cache);
             }
             return cache;
         },
@@ -284,6 +297,7 @@ window.SessionDB = function() {
         autoLoadRemoteHosts: function() {
             var sessions = db.loadAll();
             var doc = db.discoveryDoc(db.loadLocal());
+            if (doc === undefined) { return; }
             doc.map(function(d) {
                 Object.keys(d.remoteHosts).map(function(uuidPrefix) {
                     if (!(sessions[uuidPrefix])) {
@@ -308,8 +322,7 @@ window.SessionDB = function() {
             if (userUUIDPrefix === session.user.owner_uuid.slice(0, 5)) {
                 return;
             }
-            var doc = db.discoveryDoc(session);
-            doc.map(function(d) {
+            db.discoveryDoc(session).map(function (d) {
                 // Guess the remote host from the local discovery doc settings
                 var rHost = null;
                 if (d.remoteHosts[userUUIDPrefix]) {
@@ -323,8 +336,7 @@ window.SessionDB = function() {
                 }
                 // Get the remote cluster workbench url & redirect there.
                 db.findAPI(rHost).then(function (apiUrl) {
-                    var doc = db.discoveryDoc({baseURL: apiUrl});
-                    doc.map(function (d) {
+                    db.discoveryDoc({baseURL: apiUrl}).map(function (d) {
                         document.location = d.workbenchUrl + path;
                     });
                 });