12515: Use workbenchUrl advertised by API server discovery doc.
authorTom Clegg <tclegg@veritasgenetics.com>
Wed, 8 Nov 2017 21:53:22 +0000 (16:53 -0500)
committerTom Clegg <tclegg@veritasgenetics.com>
Wed, 8 Nov 2017 21:53:22 +0000 (16:53 -0500)
Fall back to guessing "workbench."+apiHost (as before) if discovery
doc does not offer a workbenchUrl (e.g., API server is too old).

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg@veritasgenetics.com>

apps/workbench/app/assets/javascripts/components/collections.js
apps/workbench/app/assets/javascripts/models/session_db.js

index 33fca6c7b770bf35354244e1d783de77c571cb80..591bf38aa74b162e353c9705da1ecbcbba2d8cde 100644 (file)
@@ -48,13 +48,9 @@ window.CollectionsTable = {
                 loader.items().map(function(item) {
                     return m('tr', [
                         m('td', [
-                            // Guess workbench.{apihostport} is a
-                            // Workbench... unless the host part of
-                            // apihostport is an IPv4 or [IPv6]
-                            // address.
-                            item.session.baseURL.match('://(\\[|\\d+\\.\\d+\\.\\d+\\.\\d+[:/])') ? null :
+                            item.workbenchBaseURL() &&
                                 m('a.btn.btn-xs.btn-default', {
-                                    href: item.session.baseURL.replace('://', '://workbench.')+'collections/'+item.uuid,
+                                    href: item.workbenchBaseURL()+'collections/'+item.uuid,
                                 }, 'Show'),
                         ]),
                         m('td.arvados-uuid', item.uuid),
@@ -98,6 +94,9 @@ window.CollectionsSearch = {
             vnode.state.loader = new MergingLoader({
                 children: Object.keys(sessions).map(function(key) {
                     var session = sessions[key]
+                    var workbenchBaseURL = function() {
+                        return vnode.state.sessionDB.workbenchBaseURL(session)
+                    }
                     return new MultipageLoader({
                         sessionKey: key,
                         loadFunc: function(filters) {
@@ -113,7 +112,7 @@ window.CollectionsSearch = {
                                 },
                             }).then(function(resp) {
                                 resp.items.map(function(item) {
-                                    item.session = session
+                                    item.workbenchBaseURL = workbenchBaseURL
                                 })
                                 return resp
                             })
index 8330a68d188d9a4bd7d8947a23cbc83682dc17b5..01b0d72728c17d9531665058d397857ff3fa3b99 100644 (file)
@@ -5,6 +5,7 @@
 window.SessionDB = function() {
     var db = this
     Object.assign(db, {
+        discoveryCache: {},
         loadFromLocalStorage: function() {
             try {
                 return JSON.parse(window.localStorage.getItem('sessions')) || {}
@@ -120,6 +121,33 @@ window.SessionDB = function() {
                 })
             })
         },
+        // Return the Workbench base URL advertised by the session's
+        // API server, or a reasonable guess, or (if neither strategy
+        // works out) null.
+        workbenchBaseURL: function(session) {
+            var dd = db.discoveryDoc(session)()
+            if (!dd)
+                // Don't fall back to guessing until we receive the discovery doc
+                return null
+            if (dd.workbenchUrl)
+                return dd.workbenchUrl
+            // Guess workbench.{apihostport} is a Workbench... unless
+            // the host part of apihostport is an IPv4 or [IPv6]
+            // address.
+            if (!session.baseURL.match('://(\\[|\\d+\\.\\d+\\.\\d+\\.\\d+[:/])'))
+                return session.baseURL.replace('://', '://workbench.')
+            return null
+        },
+        // Return a m.stream that will get fulfilled with the
+        // discovery doc from a session's API server.
+        discoveryDoc: function(session) {
+            var cache = db.discoveryCache[session.baseURL]
+            if (!cache) {
+                db.discoveryCache[session.baseURL] = cache = m.stream()
+                m.request(session.baseURL+'discovery/v1/apis/arvados/v1/rest').then(cache)
+            }
+            return cache
+        },
         request: function(session, path, opts) {
             opts = opts || {}
             opts.headers = opts.headers || {}