Skip to content

Commit f178c84

Browse files
authored
Fix instance peering pagination (#15108)
Currently the association box displays a list of available instances/addresses that can be peered to. The pagination issue arises as follows: - fetch 5 instances (based on page_size) - filter these instances down based on some criteria (like is_internal: false) - show results Filtering down the results inside of the fetch method results in pagnation errors (may return fewer than 5, for example) instead, do the filtering by API queries. That way the pagination count will be correct. Signed-off-by: Seth Foster <[email protected]>
1 parent c0f7180 commit f178c84

File tree

1 file changed

+25
-49
lines changed

1 file changed

+25
-49
lines changed

awx/ui/src/screens/Instances/InstancePeers/InstancePeerList.js

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import AssociateModal from 'components/AssociateModal';
1212
import ErrorDetail from 'components/ErrorDetail';
1313
import AlertModal from 'components/AlertModal';
1414
import useToast, { AlertVariant } from 'hooks/useToast';
15-
import { getQSConfig, parseQueryString, mergeParams } from 'util/qs';
15+
import { getQSConfig, parseQueryString } from 'util/qs';
1616
import { useLocation, useParams } from 'react-router-dom';
1717
import useRequest, { useDismissableError } from 'hooks/useRequest';
1818
import DataListToolbar from 'components/DataListToolbar';
@@ -106,72 +106,48 @@ function InstancePeerList({ setBreadcrumb }) {
106106
const { selected, isAllSelected, handleSelect, clearSelected, selectAll } =
107107
useSelected(peers);
108108

109-
const fetchInstancesToAssociate = useCallback(
109+
const fetchPeersToAssociate = useCallback(
110110
async (params) => {
111111
const address_list = [];
112112

113-
const instances = await InstancesAPI.read(
114-
mergeParams(params, {
115-
...{ not__node_type: ['control', 'hybrid'] },
116-
})
117-
);
118-
const receptors = (await ReceptorAPI.read()).data.results;
113+
// do not show this instance or instances that are already peered
114+
// to this instance (reverse_peers)
115+
const not_instances = instance.reverse_peers;
116+
not_instances.push(instance.id);
119117

120-
// get instance ids of the current peered receptor ids
121-
const already_peered_instance_ids = [];
122-
for (let h = 0; h < instance.peers.length; h++) {
123-
const matched = receptors.filter((obj) => obj.id === instance.peers[h]);
124-
matched.forEach((element) => {
125-
already_peered_instance_ids.push(element.instance);
126-
});
118+
params.not__instance = not_instances;
119+
params.is_internal = false;
120+
// do not show the current peers
121+
if (instance.peers.length > 0) {
122+
params.not__id__in = instance.peers.join(',');
127123
}
128124

129-
for (let q = 0; q < receptors.length; q++) {
130-
const receptor = receptors[q];
131-
132-
if (already_peered_instance_ids.includes(receptor.instance)) {
133-
// ignore reverse peers
134-
continue;
135-
}
125+
const receptoraddresses = await ReceptorAPI.read(params);
136126

137-
if (instance.peers.includes(receptor.id)) {
138-
// no links to existing links
139-
continue;
140-
}
141-
142-
if (instance.id === receptor.instance) {
143-
// no links to thy self
144-
continue;
145-
}
127+
// retrieve the instances that are associated with those receptor addresses
128+
const instance_ids = receptoraddresses.data.results.map(
129+
(obj) => obj.instance
130+
);
131+
const instance_ids_str = instance_ids.join(',');
132+
const instances = await InstancesAPI.read({ id__in: instance_ids_str });
146133

147-
if (instance.managed) {
148-
// no managed nodes
149-
continue;
150-
}
134+
for (let q = 0; q < receptoraddresses.data.results.length; q++) {
135+
const receptor = receptoraddresses.data.results[q];
151136

152137
const host = instances.data.results.filter(
153138
(obj) => obj.id === receptor.instance
154139
)[0];
155140

156-
if (host === undefined) {
157-
// no hosts
158-
continue;
159-
}
160-
161-
if (receptor.is_internal) {
162-
continue;
163-
}
164-
165141
const copy = receptor;
166142
copy.hostname = host.hostname;
167143
copy.node_type = host.node_type;
168144
copy.canonical = copy.canonical.toString();
169145
address_list.push(copy);
170146
}
171147

172-
instances.data.results = address_list;
148+
receptoraddresses.data.results = address_list;
173149

174-
return instances;
150+
return receptoraddresses;
175151
},
176152
[instance]
177153
);
@@ -191,7 +167,7 @@ function InstancePeerList({ setBreadcrumb }) {
191167
fetchPeers();
192168
addToast({
193169
id: instancesPeerToAssociate,
194-
title: t`Please be sure to run the install bundle for the selected instance(s) again in order to see changes take effect.`,
170+
title: t`Please be sure to run the install bundle for ${instance.hostname} again in order to see changes take effect.`,
195171
variant: AlertVariant.success,
196172
hasTimeout: true,
197173
});
@@ -315,13 +291,13 @@ function InstancePeerList({ setBreadcrumb }) {
315291
{isModalOpen && (
316292
<AssociateModal
317293
header={t`Instances`}
318-
fetchRequest={fetchInstancesToAssociate}
294+
fetchRequest={fetchPeersToAssociate}
319295
isModalOpen={isModalOpen}
320296
onAssociate={handlePeerAssociate}
321297
onClose={() => setIsModalOpen(false)}
322298
title={t`Select Peer Addresses`}
323299
optionsRequest={readInstancesOptions}
324-
displayKey="hostname"
300+
displayKey="address"
325301
columns={[
326302
{ key: 'hostname', name: t`Name` },
327303
{ key: 'address', name: t`Address` },

0 commit comments

Comments
 (0)