-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
setup_mode.tsx
203 lines (176 loc) · 5.62 KB
/
setup_mode.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import { render } from 'react-dom';
import { get, includes } from 'lodash';
import { i18n } from '@kbn/i18n';
import { HttpStart, IHttpFetchError } from 'kibana/public';
import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public';
import { Legacy } from '../../legacy_shims';
import { SetupModeEnterButton } from '../../components/setup_mode/enter_button';
import { SetupModeFeature } from '../../../common/enums';
import { ISetupModeContext } from '../../components/setup_mode/setup_mode_context';
import { State as GlobalState } from '../contexts/global_state_context';
function isOnPage(hash: string) {
return includes(window.location.hash, hash);
}
let globalState: GlobalState;
let httpService: HttpStart;
let errorHandler: (error: IHttpFetchError) => void;
interface ISetupModeState {
enabled: boolean;
data: any;
callback?: (() => void) | null;
hideBottomBar: boolean;
}
const setupModeState: ISetupModeState = {
enabled: false,
data: null,
callback: null,
hideBottomBar: false,
};
export const getSetupModeState = () => setupModeState;
export const setNewlyDiscoveredClusterUuid = (clusterUuid: string) => {
globalState.cluster_uuid = clusterUuid;
globalState.save?.();
};
export const fetchCollectionData = async (uuid?: string, fetchWithoutClusterUuid = false) => {
const clusterUuid = globalState.cluster_uuid;
const ccs = globalState.ccs;
let url = '../api/monitoring/v1/setup/collection';
if (uuid) {
url += `/node/${uuid}`;
} else if (!fetchWithoutClusterUuid && clusterUuid) {
url += `/cluster/${clusterUuid}`;
} else {
url += '/cluster';
}
try {
const response = await httpService.post(url, {
body: JSON.stringify({
ccs,
}),
});
return response;
} catch (err) {
errorHandler(err);
throw err;
}
};
const notifySetupModeDataChange = () => setupModeState.callback && setupModeState.callback();
export const updateSetupModeData = async (uuid?: string, fetchWithoutClusterUuid = false) => {
const data = await fetchCollectionData(uuid, fetchWithoutClusterUuid);
setupModeState.data = data;
const hasPermissions = get(data, '_meta.hasPermissions', false);
if (!hasPermissions) {
let text: string = '';
if (!hasPermissions) {
text = i18n.translate('xpack.monitoring.setupMode.notAvailablePermissions', {
defaultMessage: 'You do not have the necessary permissions to do this.',
});
}
Legacy.shims.toastNotifications.addDanger({
title: i18n.translate('xpack.monitoring.setupMode.notAvailableTitle', {
defaultMessage: 'Setup mode is not available',
}),
text,
});
return toggleSetupMode(false);
}
notifySetupModeDataChange();
const clusterUuid = globalState.cluster_uuid;
if (!clusterUuid) {
const liveClusterUuid: string = get(data, '_meta.liveClusterUuid');
const migratedEsNodes = Object.values(get(data, 'elasticsearch.byUuid', {})).filter(
(node: any) => node.isPartiallyMigrated || node.isFullyMigrated
);
if (liveClusterUuid && migratedEsNodes.length > 0) {
setNewlyDiscoveredClusterUuid(liveClusterUuid);
}
}
};
export const hideBottomBar = () => {
setupModeState.hideBottomBar = true;
notifySetupModeDataChange();
};
export const showBottomBar = () => {
setupModeState.hideBottomBar = false;
notifySetupModeDataChange();
};
export const disableElasticsearchInternalCollection = async () => {
const clusterUuid = globalState.cluster_uuid;
const url = `../api/monitoring/v1/setup/collection/${clusterUuid}/disable_internal_collection`;
try {
const response = await httpService.post(url);
return response;
} catch (err) {
errorHandler(err);
throw err;
}
};
export const toggleSetupMode = (inSetupMode: boolean) => {
setupModeState.enabled = inSetupMode;
globalState.inSetupMode = inSetupMode;
globalState.save?.();
setSetupModeMenuItem();
notifySetupModeDataChange();
if (inSetupMode) {
// Intentionally do not await this so we don't block UI operations
updateSetupModeData();
}
};
export const setSetupModeMenuItem = () => {
if (isOnPage('no-data')) {
return;
}
const enabled = !globalState.inSetupMode;
const I18nContext = Legacy.shims.I18nContext;
render(
<KibanaContextProvider services={Legacy.shims.kibanaServices}>
<I18nContext>
<SetupModeEnterButton enabled={enabled} toggleSetupMode={toggleSetupMode} />
</I18nContext>
</KibanaContextProvider>,
document.getElementById('setupModeNav')
);
};
export const initSetupModeState = async (
state: GlobalState,
http: HttpStart,
handleErrors: (error: IHttpFetchError) => void,
callback?: () => void
) => {
globalState = state;
httpService = http;
errorHandler = handleErrors;
if (callback) {
setupModeState.callback = callback;
}
if (globalState.inSetupMode) {
toggleSetupMode(true);
}
};
export const isInSetupMode = (context?: ISetupModeContext, gState: GlobalState = globalState) => {
if (context?.setupModeSupported === false) {
return false;
}
if (setupModeState.enabled) {
return true;
}
return gState.inSetupMode;
};
export const isSetupModeFeatureEnabled = (feature: SetupModeFeature) => {
if (!setupModeState.enabled) {
return false;
}
if (feature === SetupModeFeature.MetricbeatMigration) {
if (Legacy.shims.isCloud) {
return false;
}
}
return true;
};