Fix TypeError when running a command on a host in a smart inventory (#11768)

Fix TypeError when running a command on a host in a smart inventory

See: https://github.com/ansible/awx/issues/11611
This commit is contained in:
Kersom
2022-02-21 16:34:31 -05:00
committed by GitHub
parent 7cf0523561
commit eb859b9812
4 changed files with 35 additions and 4 deletions

View File

@@ -59,6 +59,7 @@ function AdHocCommands({
useEffect(() => { useEffect(() => {
fetchData(); fetchData();
}, [fetchData]); }, [fetchData]);
const { const {
isLoading: isLaunchLoading, isLoading: isLaunchLoading,
error: launchError, error: launchError,
@@ -172,6 +173,8 @@ function AdHocCommands({
AdHocCommands.propTypes = { AdHocCommands.propTypes = {
adHocItems: PropTypes.arrayOf(PropTypes.object).isRequired, adHocItems: PropTypes.arrayOf(PropTypes.object).isRequired,
hasListItems: PropTypes.bool.isRequired, hasListItems: PropTypes.bool.isRequired,
onLaunchLoading: PropTypes.func.isRequired,
moduleOptions: PropTypes.arrayOf(PropTypes.array).isRequired,
}; };
export default AdHocCommands; export default AdHocCommands;

View File

@@ -73,6 +73,10 @@ describe('<AdHocCommands />', () => {
adHocItems={adHocItems} adHocItems={adHocItems}
hasListItems hasListItems
onLaunchLoading={() => jest.fn()} onLaunchLoading={() => jest.fn()}
moduleOptions={[
['command', 'command'],
['shell', 'shell'],
]}
/> />
); );
}); });

View File

@@ -25,25 +25,33 @@ function SmartInventoryHostList({ inventory }) {
const location = useLocation(); const location = useLocation();
const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false); const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false);
const { const {
result: { hosts, count }, result: { hosts, count, moduleOptions },
error: contentError, error: contentError,
isLoading, isLoading,
request: fetchHosts, request: fetchHosts,
} = useRequest( } = useRequest(
useCallback(async () => { useCallback(async () => {
const params = parseQueryString(QS_CONFIG, location.search); const params = parseQueryString(QS_CONFIG, location.search);
const { const [
data: { results, count: hostCount }, {
} = await InventoriesAPI.readHosts(inventory.id, params); data: { results, count: hostCount },
},
adHocOptions,
] = await Promise.all([
InventoriesAPI.readHosts(inventory.id, params),
InventoriesAPI.readAdHocOptions(inventory.id),
]);
return { return {
hosts: results, hosts: results,
count: hostCount, count: hostCount,
moduleOptions: adHocOptions.data.actions.GET.module_name.choices,
}; };
}, [location.search, inventory.id]), }, [location.search, inventory.id]),
{ {
hosts: [], hosts: [],
count: 0, count: 0,
moduleOptions: [],
} }
); );
@@ -91,6 +99,7 @@ function SmartInventoryHostList({ inventory }) {
adHocItems={selected} adHocItems={selected}
hasListItems={count > 0} hasListItems={count > 0}
onLaunchLoading={setIsAdHocLaunchLoading} onLaunchLoading={setIsAdHocLaunchLoading}
moduleOptions={moduleOptions}
/>, />,
] ]
: [] : []

View File

@@ -27,6 +27,21 @@ describe('<SmartInventoryHostList />', () => {
InventoriesAPI.readHosts.mockResolvedValue({ InventoriesAPI.readHosts.mockResolvedValue({
data: mockHosts, data: mockHosts,
}); });
InventoriesAPI.readAdHocOptions.mockResolvedValue({
data: {
actions: {
GET: {
module_name: {
choices: [
['command', 'command'],
['shell', 'shell'],
],
},
},
POST: {},
},
},
});
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHostList inventory={clonedInventory} /> <SmartInventoryHostList inventory={clonedInventory} />