From: Lucas Di Pentima Date: Wed, 12 Feb 2020 22:53:20 +0000 (-0300) Subject: 15718: Adds API revision number from discovery doc to sessions. X-Git-Tag: 2.1.0~39^2~7 X-Git-Url: https://git.arvados.org/arvados-workbench2.git/commitdiff_plain/299d867b60f431b0dfa50f42630bca91ae53337d 15718: Adds API revision number from discovery doc to sessions. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- diff --git a/src/common/config.ts b/src/common/config.ts index 23faaf91..58fa13ae 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -78,6 +78,7 @@ export class Config { fileViewersConfigUrl: string; loginCluster: string; clusterConfig: ClusterConfigJSON; + apiRevision: number; } export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => { @@ -91,10 +92,21 @@ export const buildConfig = (clusterConfigJSON: ClusterConfigJSON): Config => { config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAVDownload.ExternalURL; config.loginCluster = clusterConfigJSON.Login.LoginCluster; config.clusterConfig = clusterConfigJSON; + config.apiRevision = 0; mapRemoteHosts(clusterConfigJSON, config); return config; }; +const getApiRevision = async (apiUrl: string) => { + try { + const dd = (await Axios.get(`${apiUrl}/${DISCOVERY_DOC_PATH}`)).data; + return parseInt(dd.revision, 10) || 0; + } catch { + console.warn("Unable to get API Revision number, defaulting to zero. Some features may not work properly."); + return 0; + } +}; + export const fetchConfig = () => { return Axios .get(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime()) @@ -107,9 +119,10 @@ export const fetchConfig = () => { if (workbenchConfig.API_HOST === undefined) { throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`); } - return Axios.get(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => { + return Axios.get(getClusterConfigURL(workbenchConfig.API_HOST)).then(async response => { const clusterConfigJSON = response.data; - const config = buildConfig(clusterConfigJSON); + const apiRevision = await getApiRevision(clusterConfigJSON.Services.Controller.ExternalURL); + const config = {...buildConfig(clusterConfigJSON), apiRevision}; const warnLocalConfig = (varName: string) => console.warn( `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \ remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`); @@ -192,6 +205,7 @@ export const mockConfig = (config: Partial): Config => ({ fileViewersConfigUrl: "", loginCluster: "", clusterConfig: mockClusterConfigJSON({}), + apiRevision: 0, ...config }); diff --git a/src/models/session.ts b/src/models/session.ts index 91a0d997..d388f599 100644 --- a/src/models/session.ts +++ b/src/models/session.ts @@ -19,4 +19,5 @@ export interface Session { loggedIn: boolean; status: SessionStatus; active: boolean; + apiRevision: number; } diff --git a/src/services/auth-service/auth-service.ts b/src/services/auth-service/auth-service.ts index c6e93a8f..690420e7 100644 --- a/src/services/auth-service/auth-service.ts +++ b/src/services/auth-service/auth-service.ts @@ -135,7 +135,8 @@ export class AuthService { loggedIn: true, active: true, uuid: user ? user.uuid : '', - status: SessionStatus.VALIDATED + status: SessionStatus.VALIDATED, + apiRevision: cfg.apiRevision, } as Session; const localSessions = this.getSessions().map(s => ({ ...s, @@ -155,7 +156,8 @@ export class AuthService { loggedIn: false, active: false, uuid: '', - status: SessionStatus.INVALIDATED + status: SessionStatus.INVALIDATED, + apiRevision: 0, } as Session; }); const sessions = [currentSession] diff --git a/src/store/auth/auth-action-session.ts b/src/store/auth/auth-action-session.ts index c1b97adc..a6387828 100644 --- a/src/store/auth/auth-action-session.ts +++ b/src/store/auth/auth-action-session.ts @@ -21,31 +21,37 @@ import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions import * as jsSHA from "jssha"; const getClusterConfig = async (origin: string): Promise => { - // Try the new public config endpoint + let configFromDD: Config | undefined; try { - const config = (await Axios.get(`${origin}/${CLUSTER_CONFIG_PATH}`)).data; - return buildConfig(config); - } catch { } - - // Fall back to discovery document - try { - const config = (await Axios.get(`${origin}/${DISCOVERY_DOC_PATH}`)).data; - return { - baseUrl: normalizeURLPath(config.baseUrl), - keepWebServiceUrl: config.keepWebServiceUrl, - remoteHosts: config.remoteHosts, - rootUrl: config.rootUrl, - uuidPrefix: config.uuidPrefix, - websocketUrl: config.websocketUrl, - workbenchUrl: config.workbenchUrl, - workbench2Url: config.workbench2Url, + const dd = (await Axios.get(`${origin}/${DISCOVERY_DOC_PATH}`)).data; + configFromDD = { + baseUrl: normalizeURLPath(dd.baseUrl), + keepWebServiceUrl: dd.keepWebServiceUrl, + remoteHosts: dd.remoteHosts, + rootUrl: dd.rootUrl, + uuidPrefix: dd.uuidPrefix, + websocketUrl: dd.websocketUrl, + workbenchUrl: dd.workbenchUrl, + workbench2Url: dd.workbench2Url, loginCluster: "", vocabularyUrl: "", fileViewersConfigUrl: "", - clusterConfig: mockClusterConfigJSON({}) + clusterConfig: mockClusterConfigJSON({}), + apiRevision: parseInt(dd.revision, 10), }; } catch { } + // Try the new public config endpoint + try { + const config = (await Axios.get(`${origin}/${CLUSTER_CONFIG_PATH}`)).data; + return {...buildConfig(config), apiRevision: configFromDD ? configFromDD.apiRevision : 0}; + } catch { } + + // Fall back to discovery document + if (configFromDD !== undefined) { + return configFromDD; + } + return null; }; @@ -120,13 +126,14 @@ export const validateSession = (session: Session, activeSession: Session) => dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED })); session.loggedIn = false; - const setupSession = (baseUrl: string, user: User, token: string) => { + const setupSession = (baseUrl: string, user: User, token: string, apiRevision: number) => { session.baseUrl = baseUrl; session.token = token; session.email = user.email; session.uuid = user.uuid; session.name = getUserFullname(user); session.loggedIn = true; + session.apiRevision = apiRevision; }; let fail: Error | null = null; @@ -135,12 +142,12 @@ export const validateSession = (session: Session, activeSession: Session) => dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config })); try { const { user, token } = await validateCluster(config, session.token); - setupSession(config.baseUrl, user, token); + setupSession(config.baseUrl, user, token, config.apiRevision); } catch (e) { fail = new Error(`Getting current user for ${session.remoteHost}: ${e.message}`); try { const { user, token } = await validateCluster(config, activeSession.token); - setupSession(config.baseUrl, user, token); + setupSession(config.baseUrl, user, token, config.apiRevision); fail = null; } catch (e2) { if (e.message === invalidV2Token) { @@ -227,7 +234,8 @@ export const addSession = (remoteHost: string, token?: string, sendToLogin?: boo baseUrl: config.baseUrl, clusterId: config.uuidPrefix, remoteHost, - token + token, + apiRevision: config.apiRevision, }; if (sessions.find(s => s.clusterId === config.uuidPrefix)) { diff --git a/src/store/auth/auth-action.test.ts b/src/store/auth/auth-action.test.ts index d126d9ca..8a17fe9f 100644 --- a/src/store/auth/auth-action.test.ts +++ b/src/store/auth/auth-action.test.ts @@ -67,6 +67,7 @@ describe('auth-actions', () => { rootUrl: "https://zzzzz.arvadosapi.com", uuidPrefix: "zzzzz", remoteHosts: { xc59z: "xc59z.arvadosapi.com" }, + apiRevision: 12345678, }; store.dispatch(initAuth(config)); @@ -82,6 +83,7 @@ describe('auth-actions', () => { expect(auth).toEqual({ apiToken: "token", config: { + apiRevision: 12345678, remoteHosts: { "xc59z": "xc59z.arvadosapi.com", }, @@ -94,6 +96,7 @@ describe('auth-actions', () => { loginCluster: undefined, remoteHostsConfig: { "zzzzz": { + "apiRevision": 12345678, "remoteHosts": { "xc59z": "xc59z.arvadosapi.com", }, @@ -114,8 +117,9 @@ describe('auth-actions', () => { "remoteHost": "https://zzzzz.arvadosapi.com", "status": 2, "token": "token", - "name": "John Doe" - "uuid": "zzzzz-tpzed-abcefg", + "name": "John Doe", + "apiRevision": 12345678, + "uuid": "zzzzz-tpzed-abcefg", }, { "active": false, "baseUrl": "", @@ -127,6 +131,7 @@ describe('auth-actions', () => { "token": "", "name": "", "uuid": "", + "apiRevision": 0, }], user: { email: "test@test.com",