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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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