mirror of
https://github.com/ansible/awx.git
synced 2026-02-25 15:06:02 -03:30
Adds Inventory Source Sync Button
This commit is contained in:
@@ -88,6 +88,12 @@ class Inventories extends InstanceGroupsMixin(Base) {
|
|||||||
`How did you get here? Source not found for Inventory ID: ${inventoryId}`
|
`How did you get here? Source not found for Inventory ID: ${inventoryId}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncAllSources(inventoryId) {
|
||||||
|
return this.http.post(
|
||||||
|
`${this.baseUrl}${inventoryId}/update_inventory_sources/`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Inventories;
|
export default Inventories;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import React, { useCallback, useEffect } from 'react';
|
|||||||
import { useParams, useLocation } from 'react-router-dom';
|
import { useParams, useLocation } from 'react-router-dom';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
|
import { Button, Tooltip } from '@patternfly/react-core';
|
||||||
|
|
||||||
import useRequest, { useDeleteItems } from '../../../util/useRequest';
|
import useRequest, { useDeleteItems } from '../../../util/useRequest';
|
||||||
import { getQSConfig, parseQueryString } from '../../../util/qs';
|
import { getQSConfig, parseQueryString } from '../../../util/qs';
|
||||||
import { InventoriesAPI, InventorySourcesAPI } from '../../../api';
|
import { InventoriesAPI, InventorySourcesAPI } from '../../../api';
|
||||||
@@ -38,7 +40,6 @@ function InventorySourceList({ i18n }) {
|
|||||||
InventoriesAPI.readSources(id, params),
|
InventoriesAPI.readSources(id, params),
|
||||||
InventorySourcesAPI.readOptions(),
|
InventorySourcesAPI.readOptions(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sources: results[0].data.results,
|
sources: results[0].data.results,
|
||||||
sourceCount: results[0].data.count,
|
sourceCount: results[0].data.count,
|
||||||
@@ -49,8 +50,24 @@ function InventorySourceList({ i18n }) {
|
|||||||
{
|
{
|
||||||
sources: [],
|
sources: [],
|
||||||
sourceCount: 0,
|
sourceCount: 0,
|
||||||
|
sourceChoices: [],
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
const canSyncSources =
|
||||||
|
sources.length > 0 &&
|
||||||
|
sources.every(source => source.summary_fields.user_capabilities.start);
|
||||||
|
const {
|
||||||
|
isLoading: isSyncAllLoading,
|
||||||
|
error: syncAllError,
|
||||||
|
request: syncAll,
|
||||||
|
} = useRequest(
|
||||||
|
useCallback(async () => {
|
||||||
|
if (canSyncSources) {
|
||||||
|
await InventoriesAPI.syncAllSources(id);
|
||||||
|
fetchSources();
|
||||||
|
}
|
||||||
|
}, [id, fetchSources, canSyncSources])
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchSources();
|
fetchSources();
|
||||||
@@ -92,8 +109,8 @@ function InventorySourceList({ i18n }) {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<PaginatedDataList
|
<PaginatedDataList
|
||||||
contentError={error || deletionError}
|
contentError={error || deletionError || syncAllError}
|
||||||
hasContentLoading={isLoading || isDeleteLoading}
|
hasContentLoading={isLoading || isDeleteLoading || isSyncAllLoading}
|
||||||
items={sources}
|
items={sources}
|
||||||
itemCount={sourceCount}
|
itemCount={sourceCount}
|
||||||
pluralizedItemName={i18n._(t`Inventory Sources`)}
|
pluralizedItemName={i18n._(t`Inventory Sources`)}
|
||||||
@@ -117,6 +134,23 @@ function InventorySourceList({ i18n }) {
|
|||||||
itemsToDelete={selected}
|
itemsToDelete={selected}
|
||||||
pluralizedItemName={i18n._(t`Inventory Sources`)}
|
pluralizedItemName={i18n._(t`Inventory Sources`)}
|
||||||
/>,
|
/>,
|
||||||
|
...(canSyncSources
|
||||||
|
? [
|
||||||
|
<Tooltip
|
||||||
|
key="update"
|
||||||
|
content={i18n._(t`Sync All Sources`)}
|
||||||
|
position="top"
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
onClick={syncAll}
|
||||||
|
aria-label={i18n._(t`Sync All`)}
|
||||||
|
variant="secondary"
|
||||||
|
>
|
||||||
|
{i18n._(t`Sync All`)}
|
||||||
|
</Button>
|
||||||
|
</Tooltip>,
|
||||||
|
]
|
||||||
|
: []),
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -225,6 +225,47 @@ describe('<InventorySourceList />', () => {
|
|||||||
|
|
||||||
expect(wrapper.find('ContentError').length).toBe(1);
|
expect(wrapper.find('ContentError').length).toBe(1);
|
||||||
});
|
});
|
||||||
|
test('should render sync all button and make api call to start sync for all', async () => {
|
||||||
|
const readSourcesResponse = {
|
||||||
|
data: {
|
||||||
|
results: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'Source Foo',
|
||||||
|
status: '',
|
||||||
|
source: 'ec2',
|
||||||
|
url: '/api/v2/inventory_sources/56/',
|
||||||
|
summary_fields: {
|
||||||
|
user_capabilities: {
|
||||||
|
edit: true,
|
||||||
|
delete: true,
|
||||||
|
start: true,
|
||||||
|
schedule: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
count: 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
InventoriesAPI.readSources
|
||||||
|
.mockResolvedValue({ ...readSourcesResponse, status: 'pending' })
|
||||||
|
.mockResolvedValueOnce(readSourcesResponse);
|
||||||
|
InventorySourcesAPI.readOptions.mockResolvedValue({
|
||||||
|
data: {
|
||||||
|
actions: {
|
||||||
|
GET: { source: { choices: [['scm', 'SCM'], ['ec2', 'EC2']] } },
|
||||||
|
POST: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await waitForElement(wrapper, 'InventorySourceList', el => el.length > 0);
|
||||||
|
const syncAllButton = wrapper.find('Button[aria-label="Sync All"]');
|
||||||
|
expect(syncAllButton.length).toBe(1);
|
||||||
|
await act(async () => syncAllButton.prop('onClick')());
|
||||||
|
expect(InventoriesAPI.syncAllSources).toBeCalled();
|
||||||
|
expect(InventoriesAPI.readSources).toBeCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('<InventorySourceList /> RBAC testing', () => {
|
describe('<InventorySourceList /> RBAC testing', () => {
|
||||||
@@ -296,4 +337,80 @@ describe('<InventorySourceList /> RBAC testing', () => {
|
|||||||
newWrapper.unmount();
|
newWrapper.unmount();
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
test('should not render Sync All button', async () => {
|
||||||
|
InventoriesAPI.readSources.mockResolvedValue({
|
||||||
|
data: {
|
||||||
|
results: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'Source Foo',
|
||||||
|
status: '',
|
||||||
|
source: 'ec2',
|
||||||
|
url: '/api/v2/inventory_sources/56/',
|
||||||
|
summary_fields: {
|
||||||
|
user_capabilities: {
|
||||||
|
edit: true,
|
||||||
|
delete: true,
|
||||||
|
start: false,
|
||||||
|
schedule: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 2,
|
||||||
|
name: 'Source Bar',
|
||||||
|
status: '',
|
||||||
|
source: 'scm',
|
||||||
|
url: '/api/v2/inventory_sources/57/',
|
||||||
|
summary_fields: {
|
||||||
|
user_capabilities: {
|
||||||
|
edit: true,
|
||||||
|
delete: true,
|
||||||
|
start: true,
|
||||||
|
schedule: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
count: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
InventorySourcesAPI.readOptions.mockResolvedValue({
|
||||||
|
data: {
|
||||||
|
actions: {
|
||||||
|
GET: { source: { choices: [['scm', 'SCM'], ['ec2', 'EC2']] } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
let newWrapper;
|
||||||
|
const history = createMemoryHistory({
|
||||||
|
initialEntries: ['/inventories/inventory/2/sources'],
|
||||||
|
});
|
||||||
|
await act(async () => {
|
||||||
|
newWrapper = mountWithContexts(
|
||||||
|
<Route path="/inventories/:inventoryType/:id/sources">
|
||||||
|
<InventorySourceList />
|
||||||
|
</Route>,
|
||||||
|
{
|
||||||
|
context: {
|
||||||
|
router: {
|
||||||
|
history,
|
||||||
|
route: {
|
||||||
|
location: { search: '' },
|
||||||
|
match: { params: { id: 2 } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
await waitForElement(
|
||||||
|
newWrapper,
|
||||||
|
'InventorySourceList',
|
||||||
|
el => el.length > 0
|
||||||
|
);
|
||||||
|
expect(newWrapper.find('Button[aria-label="Sync All"]').length).toBe(0);
|
||||||
|
newWrapper.unmount();
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ function InventorySourceListItem({
|
|||||||
i18n,
|
i18n,
|
||||||
detailUrl,
|
detailUrl,
|
||||||
label,
|
label,
|
||||||
|
onFetchSources,
|
||||||
}) {
|
}) {
|
||||||
const [isSyncLoading, setIsSyncLoading] = useState(false);
|
const [isSyncLoading, setIsSyncLoading] = useState(false);
|
||||||
|
|
||||||
@@ -99,6 +100,7 @@ function InventorySourceListItem({
|
|||||||
onSyncLoading={isLoading => {
|
onSyncLoading={isLoading => {
|
||||||
setIsSyncLoading(isLoading);
|
setIsSyncLoading(isLoading);
|
||||||
}}
|
}}
|
||||||
|
onFetchSources={onFetchSources}
|
||||||
source={source}
|
source={source}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback, useState, useEffect } from 'react';
|
import React, { useCallback, useEffect } from 'react';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
@@ -9,9 +9,12 @@ import AlertModal from '../../../components/AlertModal/AlertModal';
|
|||||||
import ErrorDetail from '../../../components/ErrorDetail/ErrorDetail';
|
import ErrorDetail from '../../../components/ErrorDetail/ErrorDetail';
|
||||||
import { InventoryUpdatesAPI, InventorySourcesAPI } from '../../../api';
|
import { InventoryUpdatesAPI, InventorySourcesAPI } from '../../../api';
|
||||||
|
|
||||||
function InventorySourceSyncButton({ onSyncLoading, source, i18n }) {
|
function InventorySourceSyncButton({
|
||||||
const [updateStatus, setUpdateStatus] = useState(source.status);
|
onSyncLoading,
|
||||||
|
source,
|
||||||
|
i18n,
|
||||||
|
onFetchSources,
|
||||||
|
}) {
|
||||||
const {
|
const {
|
||||||
isLoading: startSyncLoading,
|
isLoading: startSyncLoading,
|
||||||
error: startSyncError,
|
error: startSyncError,
|
||||||
@@ -21,11 +24,10 @@ function InventorySourceSyncButton({ onSyncLoading, source, i18n }) {
|
|||||||
const {
|
const {
|
||||||
data: { status },
|
data: { status },
|
||||||
} = await InventorySourcesAPI.createSyncStart(source.id);
|
} = await InventorySourcesAPI.createSyncStart(source.id);
|
||||||
|
onFetchSources();
|
||||||
setUpdateStatus(status);
|
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}, [source.id]),
|
}, [source.id, onFetchSources]),
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -44,8 +46,8 @@ function InventorySourceSyncButton({ onSyncLoading, source, i18n }) {
|
|||||||
} = await InventorySourcesAPI.readDetail(source.id);
|
} = await InventorySourcesAPI.readDetail(source.id);
|
||||||
|
|
||||||
await InventoryUpdatesAPI.createSyncCancel(id);
|
await InventoryUpdatesAPI.createSyncCancel(id);
|
||||||
setUpdateStatus(null);
|
onFetchSources();
|
||||||
}, [source.id])
|
}, [source.id, onFetchSources])
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => onSyncLoading(startSyncLoading || cancelSyncLoading), [
|
useEffect(() => onSyncLoading(startSyncLoading || cancelSyncLoading), [
|
||||||
@@ -60,7 +62,7 @@ function InventorySourceSyncButton({ onSyncLoading, source, i18n }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{updateStatus === 'pending' ? (
|
{source.status === 'pending' ? (
|
||||||
<Tooltip content={i18n._(t`Cancel sync process`)} position="top">
|
<Tooltip content={i18n._(t`Cancel sync process`)} position="top">
|
||||||
<Button
|
<Button
|
||||||
isDisabled={cancelSyncLoading || startSyncLoading}
|
isDisabled={cancelSyncLoading || startSyncLoading}
|
||||||
@@ -107,6 +109,7 @@ InventorySourceSyncButton.defaultProps = {
|
|||||||
InventorySourceSyncButton.propTypes = {
|
InventorySourceSyncButton.propTypes = {
|
||||||
onSyncLoading: PropTypes.func.isRequired,
|
onSyncLoading: PropTypes.func.isRequired,
|
||||||
source: PropTypes.shape({}),
|
source: PropTypes.shape({}),
|
||||||
|
onFetchSources: PropTypes.func.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default withI18n()(InventorySourceSyncButton);
|
export default withI18n()(InventorySourceSyncButton);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ describe('<InventorySourceSyncButton />', () => {
|
|||||||
<InventorySourceSyncButton
|
<InventorySourceSyncButton
|
||||||
source={source}
|
source={source}
|
||||||
onSyncLoading={onSyncLoading}
|
onSyncLoading={onSyncLoading}
|
||||||
|
onFetchSources={() => {}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -40,6 +41,7 @@ describe('<InventorySourceSyncButton />', () => {
|
|||||||
<InventorySourceSyncButton
|
<InventorySourceSyncButton
|
||||||
source={{ status: 'pending', ...source }}
|
source={{ status: 'pending', ...source }}
|
||||||
onSyncLoading={onSyncLoading}
|
onSyncLoading={onSyncLoading}
|
||||||
|
onFetchSources={() => {}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
expect(wrapper.find('MinusCircleIcon').length).toBe(1);
|
expect(wrapper.find('MinusCircleIcon').length).toBe(1);
|
||||||
@@ -54,10 +56,6 @@ describe('<InventorySourceSyncButton />', () => {
|
|||||||
wrapper.find('Button[aria-label="Start sync source"]').simulate('click')
|
wrapper.find('Button[aria-label="Start sync source"]').simulate('click')
|
||||||
);
|
);
|
||||||
expect(InventorySourcesAPI.createSyncStart).toBeCalledWith(1);
|
expect(InventorySourcesAPI.createSyncStart).toBeCalledWith(1);
|
||||||
wrapper.update();
|
|
||||||
expect(wrapper.find('Button[aria-label="Cancel sync source"]').length).toBe(
|
|
||||||
1
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
test('should cancel sync properly', async () => {
|
test('should cancel sync properly', async () => {
|
||||||
InventorySourcesAPI.readDetail.mockResolvedValue({
|
InventorySourcesAPI.readDetail.mockResolvedValue({
|
||||||
@@ -71,6 +69,7 @@ describe('<InventorySourceSyncButton />', () => {
|
|||||||
<InventorySourceSyncButton
|
<InventorySourceSyncButton
|
||||||
source={{ status: 'pending', ...source }}
|
source={{ status: 'pending', ...source }}
|
||||||
onSyncLoading={onSyncLoading}
|
onSyncLoading={onSyncLoading}
|
||||||
|
onFetchSources={() => {}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
expect(wrapper.find('Button[aria-label="Cancel sync source"]').length).toBe(
|
expect(wrapper.find('Button[aria-label="Cancel sync source"]').length).toBe(
|
||||||
@@ -83,11 +82,5 @@ describe('<InventorySourceSyncButton />', () => {
|
|||||||
|
|
||||||
expect(InventorySourcesAPI.readDetail).toBeCalledWith(1);
|
expect(InventorySourcesAPI.readDetail).toBeCalledWith(1);
|
||||||
expect(InventoryUpdatesAPI.createSyncCancel).toBeCalledWith(120);
|
expect(InventoryUpdatesAPI.createSyncCancel).toBeCalledWith(120);
|
||||||
|
|
||||||
wrapper.update();
|
|
||||||
|
|
||||||
expect(wrapper.find('Button[aria-label="Start sync source"]').length).toBe(
|
|
||||||
1
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user