-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
InstancePeerList.js
327 lines (298 loc) · 10.1 KB
/
InstancePeerList.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import React, { useCallback, useEffect, useState } from 'react';
import { t } from '@lingui/macro';
import { CardBody } from 'components/Card';
import PaginatedTable, {
getSearchableKeys,
HeaderCell,
HeaderRow,
ToolbarAddButton,
} from 'components/PaginatedTable';
import DisassociateButton from 'components/DisassociateButton';
import AssociateModal from 'components/AssociateModal';
import ErrorDetail from 'components/ErrorDetail';
import AlertModal from 'components/AlertModal';
import useToast, { AlertVariant } from 'hooks/useToast';
import { getQSConfig, parseQueryString } from 'util/qs';
import { useLocation, useParams } from 'react-router-dom';
import useRequest, { useDismissableError } from 'hooks/useRequest';
import DataListToolbar from 'components/DataListToolbar';
import { InstancesAPI, ReceptorAPI } from 'api';
import useExpanded from 'hooks/useExpanded';
import useSelected from 'hooks/useSelected';
import InstancePeerListItem from './InstancePeerListItem';
const QS_CONFIG = getQSConfig('peer', {
page: 1,
page_size: 20,
order_by: 'pk',
});
function InstancePeerList({ setBreadcrumb }) {
const location = useLocation();
const { id } = useParams();
const [isModalOpen, setIsModalOpen] = useState(false);
const { addToast, Toast, toastProps } = useToast();
const readInstancesOptions = useCallback(
() => InstancesAPI.readOptions(id),
[id]
);
const {
isLoading,
error: contentError,
request: fetchPeers,
result: { instance, peers, count, relatedSearchableKeys, searchableKeys },
} = useRequest(
useCallback(async () => {
const params = parseQueryString(QS_CONFIG, location.search);
const [
{ data: detail },
{
data: { results },
},
actions,
instances,
] = await Promise.all([
InstancesAPI.readDetail(id),
InstancesAPI.readPeers(id, params),
InstancesAPI.readOptions(),
InstancesAPI.read(),
]);
const address_list = [];
for (let q = 0; q < results.length; q++) {
const receptor = results[q];
if (receptor.managed === true) continue;
const host = instances.data.results.filter(
(obj) => obj.id === receptor.instance
)[0];
const copy = receptor;
copy.hostname = host.hostname;
copy.node_type = host.node_type;
address_list.push(copy);
}
return {
instance: detail,
peers: address_list,
count: address_list.length,
relatedSearchableKeys: (actions?.data?.related_search_fields || []).map(
(val) => val.slice(0, -8)
),
searchableKeys: getSearchableKeys(actions.data.actions?.GET),
};
}, [id, location]),
{
instance: {},
peers: [],
count: 0,
relatedSearchableKeys: [],
searchableKeys: [],
}
);
useEffect(() => {
fetchPeers();
}, [fetchPeers]);
useEffect(() => {
if (instance) {
setBreadcrumb(instance);
}
}, [instance, setBreadcrumb]);
const { expanded, isAllExpanded, handleExpand, expandAll } =
useExpanded(peers);
const { selected, isAllSelected, handleSelect, clearSelected, selectAll } =
useSelected(peers);
const fetchPeersToAssociate = useCallback(
async (params) => {
const address_list = [];
// do not show this instance or instances that are already peered
// to this instance (reverse_peers)
const not_instances = instance.reverse_peers;
not_instances.push(instance.id);
params.not__instance = not_instances;
params.is_internal = false;
// do not show the current peers
if (instance.peers.length > 0) {
params.not__id__in = instance.peers.join(',');
}
const receptoraddresses = await ReceptorAPI.read(params);
// retrieve the instances that are associated with those receptor addresses
const instance_ids = receptoraddresses.data.results.map(
(obj) => obj.instance
);
const instance_ids_str = instance_ids.join(',');
const instances = await InstancesAPI.read({ id__in: instance_ids_str });
for (let q = 0; q < receptoraddresses.data.results.length; q++) {
const receptor = receptoraddresses.data.results[q];
const host = instances.data.results.filter(
(obj) => obj.id === receptor.instance
)[0];
const copy = receptor;
copy.hostname = host.hostname;
copy.node_type = host.node_type;
copy.canonical = copy.canonical.toString();
address_list.push(copy);
}
receptoraddresses.data.results = address_list;
return receptoraddresses;
},
[instance]
);
const {
isLoading: isAssociateLoading,
request: handlePeerAssociate,
error: associateError,
} = useRequest(
useCallback(
async (instancesPeerToAssociate) => {
const selected_peers = instancesPeerToAssociate.map((obj) => obj.id);
const new_peers = [...new Set([...instance.peers, ...selected_peers])];
await InstancesAPI.update(instance.id, { peers: new_peers });
fetchPeers();
addToast({
id: instancesPeerToAssociate,
title: t`Please be sure to run the install bundle for ${instance.hostname} again in order to see changes take effect.`,
variant: AlertVariant.success,
hasTimeout: true,
});
},
[instance, fetchPeers, addToast]
)
);
const {
isLoading: isDisassociateLoading,
request: handlePeersDiassociate,
error: disassociateError,
} = useRequest(
useCallback(async () => {
let new_peers = instance.peers;
const selected_ids = selected.map((obj) => obj.id);
for (let i = 0; i < selected_ids.length; i++) {
new_peers = new_peers.filter((s_id) => s_id !== selected_ids[i]);
}
await InstancesAPI.update(instance.id, { peers: new_peers });
fetchPeers();
addToast({
title: t`Peer removed. Please be sure to run the install bundle for ${instance.hostname} again in order to see changes take effect.`,
variant: AlertVariant.success,
hasTimeout: true,
});
}, [instance, selected, fetchPeers, addToast])
);
const { error, dismissError } = useDismissableError(
associateError || disassociateError
);
const isHopNode = instance.node_type === 'hop';
const isExecutionNode = instance.node_type === 'execution';
return (
<CardBody>
<PaginatedTable
contentError={contentError}
hasContentLoading={
isLoading || isDisassociateLoading || isAssociateLoading
}
items={peers}
itemCount={count}
pluralizedItemName={t`Peers`}
qsConfig={QS_CONFIG}
onRowClick={handleSelect}
clearSelected={clearSelected}
toolbarSearchableKeys={searchableKeys}
toolbarRelatedSearchableKeys={relatedSearchableKeys}
toolbarSearchColumns={[
{
name: t`Name`,
key: 'hostname__icontains',
isDefault: true,
},
]}
toolbarSortColumns={[
{
name: t`Name`,
key: 'hostname',
},
]}
headerRow={
<HeaderRow qsConfig={QS_CONFIG} isExpandable>
<HeaderCell
tooltip={t`Cannot run health check on hop nodes.`}
sortKey="hostname"
>{t`Instance Name`}</HeaderCell>
<HeaderCell sortKey="address">{t`Address`}</HeaderCell>
<HeaderCell sortKey="port">{t`Port`}</HeaderCell>
<HeaderCell sortKey="node_type">{t`Node Type`}</HeaderCell>
<HeaderCell sortKey="canonical">{t`Canonical`}</HeaderCell>
</HeaderRow>
}
renderToolbar={(props) => (
<DataListToolbar
{...props}
isAllSelected={isAllSelected}
onSelectAll={selectAll}
isAllExpanded={isAllExpanded}
onExpandAll={expandAll}
qsConfig={QS_CONFIG}
additionalControls={[
(isExecutionNode || isHopNode) && (
<ToolbarAddButton
ouiaId="add-instance-peers-button"
key="associate"
defaultLabel={t`Associate`}
onClick={() => setIsModalOpen(true)}
/>
),
(isExecutionNode || isHopNode) && (
<DisassociateButton
verifyCannotDisassociate={false}
key="disassociate"
onDisassociate={handlePeersDiassociate}
itemsToDisassociate={selected}
modalTitle={t`Remove peers?`}
/>
),
]}
/>
)}
renderRow={(peer, index) => (
<InstancePeerListItem
isSelected={selected.some((row) => row.id === peer.id)}
onSelect={() => handleSelect(peer)}
isExpanded={expanded.some((row) => row.id === peer.id)}
onExpand={() => handleExpand(peer)}
key={peer.id}
peerInstance={peer}
rowIndex={index}
/>
)}
/>
{isModalOpen && (
<AssociateModal
header={t`Instances`}
fetchRequest={fetchPeersToAssociate}
isModalOpen={isModalOpen}
onAssociate={handlePeerAssociate}
onClose={() => setIsModalOpen(false)}
title={t`Select Peer Addresses`}
optionsRequest={readInstancesOptions}
displayKey="address"
columns={[
{ key: 'hostname', name: t`Name` },
{ key: 'address', name: t`Address` },
{ key: 'port', name: t`Port` },
{ key: 'node_type', name: t`Node Type` },
{ key: 'protocol', name: t`Protocol` },
]}
/>
)}
<Toast {...toastProps} />
{error && (
<AlertModal
isOpen={error}
onClose={dismissError}
title={t`Error!`}
variant="error"
>
{associateError && t`Failed to associate peer.`}
{disassociateError && t`Failed to remove peers.`}
<ErrorDetail error={error} />
</AlertModal>
)}
</CardBody>
);
}
export default InstancePeerList;