14813: Fix to use cluster config when local config values aren't defined
[arvados-workbench2.git] / src / common / config.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6
7 export const WORKBENCH_CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
8
9 interface WorkbenchConfig {
10     API_HOST: string;
11     VOCABULARY_URL?: string;
12     FILE_VIEWERS_CONFIG_URL?: string;
13 }
14
15 export interface ClusterConfigJSON {
16     ClusterID: string;
17     RemoteClusters: {
18         [key: string]: {
19             ActivateUsers: boolean
20             Host: string
21             Insecure: boolean
22             Proxy: boolean
23             Scheme: string
24         }
25     };
26     Services: {
27         Controller: {
28             ExternalURL: string
29         }
30         Workbench1: {
31             ExternalURL: string
32         }
33         Workbench2: {
34             ExternalURL: string
35         }
36         Websocket: {
37             ExternalURL: string
38         }
39         WebDAV: {
40             ExternalURL: string
41         }
42     };
43     Workbench: {
44         ArvadosDocsite: string;
45         VocabularyURL: string;
46         FileViewersConfigURL: string;
47     };
48 }
49
50 export class Config {
51     baseUrl: string;
52     keepWebServiceUrl: string;
53     remoteHosts: {
54         [key: string]: string
55     };
56     rootUrl: string;
57     uuidPrefix: string;
58     websocketUrl: string;
59     workbenchUrl: string;
60     workbench2Url: string;
61     vocabularyUrl: string;
62     fileViewersConfigUrl: string;
63 }
64
65 export const fetchConfig = () => {
66     return Axios
67         .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
68         .then(response => response.data)
69         .catch(() => Promise.resolve(getDefaultConfig()))
70         .then(workbenchConfig => Axios
71             .get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST))
72             .then(response => {
73
74                 const config = new Config();
75                 const clusterConfigJSON = response.data;
76                 const docsite = clusterConfigJSON.Workbench.ArvadosDocsite;
77                 const warnLocalConfig = (varName: string) => console.warn(
78                     `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
79 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
80
81                 // Check if the workbench config has an entry for vocabulary and file viewer URLs
82                 // If so, use these values (even if it is an empty string), but print a console warning.
83                 // Otherwise, use the cluster config.
84                 let fileViewerConfigUrl;
85                 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
86                     warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
87                     fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
88                 }
89                 else {
90                     fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
91                 }
92                 config.fileViewersConfigUrl = fileViewerConfigUrl;
93
94                 let vocabularyUrl;
95                 if (workbenchConfig.VOCABULARY_URL !== undefined) {
96                     warnLocalConfig("VOCABULARY_URL");
97                     vocabularyUrl = workbenchConfig.VOCABULARY_URL;
98                 }
99                 else {
100                     vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
101                 }
102                 config.vocabularyUrl = vocabularyUrl;
103
104                 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
105                 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
106                 config.uuidPrefix = clusterConfigJSON.ClusterID;
107                 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
108                 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
109                 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
110                 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
111                 mapRemoteHosts(clusterConfigJSON, config);
112
113                 return { config, apiHost: workbenchConfig.API_HOST };
114             })
115         );
116 };
117
118 // Maps remote cluster hosts and removes the default RemoteCluster entry
119 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
120     config.remoteHosts = {};
121     Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
122     delete config.remoteHosts["*"];
123 };
124
125 export const mockConfig = (config: Partial<Config>): Config => ({
126     baseUrl: "",
127     keepWebServiceUrl: "",
128     remoteHosts: {},
129     rootUrl: "",
130     uuidPrefix: "",
131     websocketUrl: "",
132     workbenchUrl: "",
133     workbench2Url: "",
134     vocabularyUrl: "",
135     fileViewersConfigUrl: ""
136 });
137
138 const getDefaultConfig = (): WorkbenchConfig => ({
139     API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || "",
140     VOCABULARY_URL: undefined,
141     FILE_VIEWERS_CONFIG_URL: undefined,
142 });
143
144 export const ARVADOS_API_PATH = "arvados/v1";
145 export const CLUSTER_CONFIG_URL = "arvados/v1/config";
146 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_URL}?nocache=${(new Date()).getTime()}`;