mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 01:17:37 -02:30
Add support for deleting templates on templates list (#266)
Adds support for deleting templates from the templates list
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { createMemoryHistory } from 'history';
|
import { mountWithContexts, waitForElement } from '../../../enzymeHelpers';
|
||||||
import { mountWithContexts } from '../../../enzymeHelpers';
|
|
||||||
import OrganizationsList, { _OrganizationsList } from '../../../../src/pages/Organizations/screens/OrganizationsList';
|
import OrganizationsList, { _OrganizationsList } from '../../../../src/pages/Organizations/screens/OrganizationsList';
|
||||||
import { OrganizationsAPI } from '../../../../src/api';
|
import { OrganizationsAPI } from '../../../../src/api';
|
||||||
|
|
||||||
@@ -122,25 +121,16 @@ describe('<OrganizationsList />', () => {
|
|||||||
expect(fetchOrgs).toBeCalled();
|
expect(fetchOrgs).toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('error is thrown when org not successfully deleted from api', async () => {
|
test('error is shown when org not successfully deleted from api', async () => {
|
||||||
const history = createMemoryHistory({
|
OrganizationsAPI.destroy = () => Promise.reject();
|
||||||
initialEntries: ['organizations?order_by=name&page=1&page_size=5'],
|
wrapper = mountWithContexts(<OrganizationsList />);
|
||||||
});
|
wrapper.find('OrganizationsList').setState({
|
||||||
wrapper = mountWithContexts(
|
|
||||||
<OrganizationsList />,
|
|
||||||
{ context: { router: { history } } }
|
|
||||||
);
|
|
||||||
await wrapper.setState({
|
|
||||||
organizations: mockAPIOrgsList.data.results,
|
organizations: mockAPIOrgsList.data.results,
|
||||||
itemCount: 3,
|
itemCount: 3,
|
||||||
isInitialized: true,
|
isInitialized: true,
|
||||||
selected: [...mockAPIOrgsList.data.results].push({
|
selected: mockAPIOrgsList.data.results.slice(0, 1)
|
||||||
name: 'Organization 6',
|
|
||||||
id: 'a',
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
wrapper.update();
|
wrapper.find('ToolbarDeleteButton').prop('onDelete')();
|
||||||
const component = wrapper.find('OrganizationsList');
|
await waitForElement(wrapper, 'Modal', (el) => el.props().isOpen === true && el.props().title === 'Error!');
|
||||||
component.instance().handleOrgDelete();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,38 +1,63 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mountWithContexts, waitForElement } from '../../enzymeHelpers';
|
import { mountWithContexts, waitForElement } from '../../enzymeHelpers';
|
||||||
import TemplatesList, { _TemplatesList } from '../../../src/pages/Templates/TemplatesList';
|
import TemplatesList, { _TemplatesList } from '../../../src/pages/Templates/TemplatesList';
|
||||||
import { UnifiedJobTemplatesAPI } from '../../../src/api';
|
import { JobTemplatesAPI, UnifiedJobTemplatesAPI, WorkflowJobTemplatesAPI } from '../../../src/api';
|
||||||
|
|
||||||
jest.mock('../../../src/api');
|
jest.mock('../../../src/api');
|
||||||
|
|
||||||
const mockTemplates = [{
|
const mockTemplates = [{
|
||||||
id: 1,
|
id: 1,
|
||||||
name: 'Template 1',
|
name: 'Job Template 1',
|
||||||
url: '/templates/job_template/1',
|
url: '/templates/job_template/1',
|
||||||
type: 'job_template',
|
type: 'job_template',
|
||||||
summary_fields: {
|
summary_fields: {
|
||||||
inventory: {},
|
user_capabilities: {
|
||||||
project: {},
|
delete: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2,
|
id: 2,
|
||||||
name: 'Template 2',
|
name: 'Job Template 2',
|
||||||
url: '/templates/job_template/2',
|
url: '/templates/job_template/2',
|
||||||
type: 'job_template',
|
type: 'job_template',
|
||||||
summary_fields: {
|
summary_fields: {
|
||||||
inventory: {},
|
user_capabilities: {
|
||||||
project: {},
|
delete: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 3,
|
id: 3,
|
||||||
name: 'Template 3',
|
name: 'Job Template 3',
|
||||||
url: '/templates/job_template/3',
|
url: '/templates/job_template/3',
|
||||||
type: 'job_template',
|
type: 'job_template',
|
||||||
summary_fields: {
|
summary_fields: {
|
||||||
inventory: {},
|
user_capabilities: {
|
||||||
project: {},
|
delete: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 4,
|
||||||
|
name: 'Workflow Job Template 1',
|
||||||
|
url: '/templates/workflow_job_template/4',
|
||||||
|
type: 'workflow_job_template',
|
||||||
|
summary_fields: {
|
||||||
|
user_capabilities: {
|
||||||
|
delete: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 5,
|
||||||
|
name: 'Workflow Job Template 2',
|
||||||
|
url: '/templates/workflow_job_template/5',
|
||||||
|
type: 'workflow_job_template',
|
||||||
|
summary_fields: {
|
||||||
|
user_capabilities: {
|
||||||
|
delete: false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
|
|
||||||
@@ -60,10 +85,10 @@ describe('<TemplatesList />', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Templates are retrieved from the api and the components finishes loading', async (done) => {
|
test('Templates are retrieved from the api and the components finishes loading', async (done) => {
|
||||||
const loadUnifiedJobTemplates = jest.spyOn(_TemplatesList.prototype, 'loadUnifiedJobTemplates');
|
const loadTemplates = jest.spyOn(_TemplatesList.prototype, 'loadTemplates');
|
||||||
const wrapper = mountWithContexts(<TemplatesList />);
|
const wrapper = mountWithContexts(<TemplatesList />);
|
||||||
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('contentLoading') === true);
|
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('contentLoading') === true);
|
||||||
expect(loadUnifiedJobTemplates).toHaveBeenCalled();
|
expect(loadTemplates).toHaveBeenCalled();
|
||||||
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('contentLoading') === false);
|
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('contentLoading') === false);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -84,7 +109,53 @@ describe('<TemplatesList />', () => {
|
|||||||
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('contentLoading') === false);
|
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('contentLoading') === false);
|
||||||
wrapper.find('Checkbox#select-all').props().onChange(true);
|
wrapper.find('Checkbox#select-all').props().onChange(true);
|
||||||
expect(handleSelectAll).toBeCalled();
|
expect(handleSelectAll).toBeCalled();
|
||||||
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('selected').length === 3);
|
await waitForElement(wrapper, 'TemplatesList', (el) => el.state('selected').length === 5);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('delete button is disabled if user does not have delete capabilities on a selected template', async (done) => {
|
||||||
|
const wrapper = mountWithContexts(<TemplatesList />);
|
||||||
|
wrapper.find('TemplatesList').setState({
|
||||||
|
templates: mockTemplates,
|
||||||
|
itemCount: 5,
|
||||||
|
isInitialized: true,
|
||||||
|
selected: mockTemplates.slice(0, 4)
|
||||||
|
});
|
||||||
|
await waitForElement(wrapper, 'ToolbarDeleteButton * button', (el) => el.getDOMNode().disabled === false);
|
||||||
|
wrapper.find('TemplatesList').setState({
|
||||||
|
selected: mockTemplates
|
||||||
|
});
|
||||||
|
await waitForElement(wrapper, 'ToolbarDeleteButton * button', (el) => el.getDOMNode().disabled === true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('api is called to delete templates for each selected template.', () => {
|
||||||
|
JobTemplatesAPI.destroy = jest.fn();
|
||||||
|
WorkflowJobTemplatesAPI.destroy = jest.fn();
|
||||||
|
const wrapper = mountWithContexts(<TemplatesList />);
|
||||||
|
wrapper.find('TemplatesList').setState({
|
||||||
|
templates: mockTemplates,
|
||||||
|
itemCount: 5,
|
||||||
|
isInitialized: true,
|
||||||
|
isModalOpen: true,
|
||||||
|
selected: mockTemplates.slice(0, 4)
|
||||||
|
});
|
||||||
|
wrapper.find('ToolbarDeleteButton').prop('onDelete')();
|
||||||
|
expect(JobTemplatesAPI.destroy).toHaveBeenCalledTimes(3);
|
||||||
|
expect(WorkflowJobTemplatesAPI.destroy).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('error is shown when template not successfully deleted from api', async () => {
|
||||||
|
JobTemplatesAPI.destroy = () => Promise.reject();
|
||||||
|
const wrapper = mountWithContexts(<TemplatesList />);
|
||||||
|
wrapper.find('TemplatesList').setState({
|
||||||
|
templates: mockTemplates,
|
||||||
|
itemCount: 1,
|
||||||
|
isInitialized: true,
|
||||||
|
isModalOpen: true,
|
||||||
|
selected: mockTemplates.slice(0, 1)
|
||||||
|
});
|
||||||
|
wrapper.find('ToolbarDeleteButton').prop('onDelete')();
|
||||||
|
await waitForElement(wrapper, 'Modal', (el) => el.props().isOpen === true && el.props().title === 'Error!');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Config from './models/Config';
|
import Config from './models/Config';
|
||||||
import InstanceGroups from './models/InstanceGroups';
|
import InstanceGroups from './models/InstanceGroups';
|
||||||
|
import JobTemplates from './models/JobTemplates';
|
||||||
import Jobs from './models/Jobs';
|
import Jobs from './models/Jobs';
|
||||||
import Me from './models/Me';
|
import Me from './models/Me';
|
||||||
import Organizations from './models/Organizations';
|
import Organizations from './models/Organizations';
|
||||||
@@ -7,9 +8,11 @@ import Root from './models/Root';
|
|||||||
import Teams from './models/Teams';
|
import Teams from './models/Teams';
|
||||||
import UnifiedJobTemplates from './models/UnifiedJobTemplates';
|
import UnifiedJobTemplates from './models/UnifiedJobTemplates';
|
||||||
import Users from './models/Users';
|
import Users from './models/Users';
|
||||||
|
import WorkflowJobTemplates from './models/WorkflowJobTemplates';
|
||||||
|
|
||||||
const ConfigAPI = new Config();
|
const ConfigAPI = new Config();
|
||||||
const InstanceGroupsAPI = new InstanceGroups();
|
const InstanceGroupsAPI = new InstanceGroups();
|
||||||
|
const JobTemplatesAPI = new JobTemplates();
|
||||||
const JobsAPI = new Jobs();
|
const JobsAPI = new Jobs();
|
||||||
const MeAPI = new Me();
|
const MeAPI = new Me();
|
||||||
const OrganizationsAPI = new Organizations();
|
const OrganizationsAPI = new Organizations();
|
||||||
@@ -17,15 +20,18 @@ const RootAPI = new Root();
|
|||||||
const TeamsAPI = new Teams();
|
const TeamsAPI = new Teams();
|
||||||
const UnifiedJobTemplatesAPI = new UnifiedJobTemplates();
|
const UnifiedJobTemplatesAPI = new UnifiedJobTemplates();
|
||||||
const UsersAPI = new Users();
|
const UsersAPI = new Users();
|
||||||
|
const WorkflowJobTemplatesAPI = new WorkflowJobTemplates();
|
||||||
|
|
||||||
export {
|
export {
|
||||||
ConfigAPI,
|
ConfigAPI,
|
||||||
InstanceGroupsAPI,
|
InstanceGroupsAPI,
|
||||||
|
JobTemplatesAPI,
|
||||||
JobsAPI,
|
JobsAPI,
|
||||||
MeAPI,
|
MeAPI,
|
||||||
OrganizationsAPI,
|
OrganizationsAPI,
|
||||||
RootAPI,
|
RootAPI,
|
||||||
TeamsAPI,
|
TeamsAPI,
|
||||||
UnifiedJobTemplatesAPI,
|
UnifiedJobTemplatesAPI,
|
||||||
UsersAPI
|
UsersAPI,
|
||||||
|
WorkflowJobTemplatesAPI
|
||||||
};
|
};
|
||||||
|
|||||||
10
src/api/models/JobTemplates.js
Normal file
10
src/api/models/JobTemplates.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import Base from '../Base';
|
||||||
|
|
||||||
|
class JobTemplates extends Base {
|
||||||
|
constructor (http) {
|
||||||
|
super(http);
|
||||||
|
this.baseUrl = '/api/v2/job_templates/';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default JobTemplates;
|
||||||
10
src/api/models/WorkflowJobTemplates.js
Normal file
10
src/api/models/WorkflowJobTemplates.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import Base from '../Base';
|
||||||
|
|
||||||
|
class WorkflowJobTemplates extends Base {
|
||||||
|
constructor (http) {
|
||||||
|
super(http);
|
||||||
|
this.baseUrl = '/api/v2/workflow_job_templates/';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default WorkflowJobTemplates;
|
||||||
@@ -83,7 +83,6 @@ class OrganizationsList extends Component {
|
|||||||
this.setState({ contentLoading: true, deletionError: false });
|
this.setState({ contentLoading: true, deletionError: false });
|
||||||
try {
|
try {
|
||||||
await Promise.all(selected.map((org) => OrganizationsAPI.destroy(org.id)));
|
await Promise.all(selected.map((org) => OrganizationsAPI.destroy(org.id)));
|
||||||
this.setState({ selected: [] });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.setState({ deletionError: true });
|
this.setState({ deletionError: true });
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -7,11 +7,14 @@ import {
|
|||||||
PageSection,
|
PageSection,
|
||||||
PageSectionVariants,
|
PageSectionVariants,
|
||||||
} from '@patternfly/react-core';
|
} from '@patternfly/react-core';
|
||||||
import { UnifiedJobTemplatesAPI } from '../../api';
|
import { JobTemplatesAPI, UnifiedJobTemplatesAPI, WorkflowJobTemplatesAPI } from '../../api';
|
||||||
|
|
||||||
import { getQSConfig, parseNamespacedQueryString } from '../../util/qs';
|
import { getQSConfig, parseNamespacedQueryString } from '../../util/qs';
|
||||||
|
import AlertModal from '../../components/AlertModal';
|
||||||
import DatalistToolbar from '../../components/DataListToolbar';
|
import DatalistToolbar from '../../components/DataListToolbar';
|
||||||
import PaginatedDataList from '../../components/PaginatedDataList';
|
import PaginatedDataList, {
|
||||||
|
ToolbarDeleteButton
|
||||||
|
} from '../../components/PaginatedDataList';
|
||||||
import TemplateListItem from './components/TemplateListItem';
|
import TemplateListItem from './components/TemplateListItem';
|
||||||
|
|
||||||
// The type value in const QS_CONFIG below does not have a space between job_template and
|
// The type value in const QS_CONFIG below does not have a space between job_template and
|
||||||
@@ -28,28 +31,35 @@ class TemplatesList extends Component {
|
|||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
contentError: false,
|
|
||||||
contentLoading: true,
|
contentLoading: true,
|
||||||
|
contentError: false,
|
||||||
|
deletionError: false,
|
||||||
selected: [],
|
selected: [],
|
||||||
templates: [],
|
templates: [],
|
||||||
itemCount: 0,
|
itemCount: 0,
|
||||||
};
|
};
|
||||||
this.loadUnifiedJobTemplates = this.loadUnifiedJobTemplates.bind(this);
|
this.loadTemplates = this.loadTemplates.bind(this);
|
||||||
this.handleSelectAll = this.handleSelectAll.bind(this);
|
this.handleSelectAll = this.handleSelectAll.bind(this);
|
||||||
this.handleSelect = this.handleSelect.bind(this);
|
this.handleSelect = this.handleSelect.bind(this);
|
||||||
|
this.handleTemplateDelete = this.handleTemplateDelete.bind(this);
|
||||||
|
this.handleDeleteErrorClose = this.handleDeleteErrorClose.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
this.loadUnifiedJobTemplates();
|
this.loadTemplates();
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate (prevProps) {
|
componentDidUpdate (prevProps) {
|
||||||
const { location } = this.props;
|
const { location } = this.props;
|
||||||
if (location !== prevProps.location) {
|
if (location !== prevProps.location) {
|
||||||
this.loadUnifiedJobTemplates();
|
this.loadTemplates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleDeleteErrorClose () {
|
||||||
|
this.setState({ deletionError: false });
|
||||||
|
}
|
||||||
|
|
||||||
handleSelectAll (isSelected) {
|
handleSelectAll (isSelected) {
|
||||||
const { templates } = this.state;
|
const { templates } = this.state;
|
||||||
const selected = isSelected ? [...templates] : [];
|
const selected = isSelected ? [...templates] : [];
|
||||||
@@ -65,7 +75,28 @@ class TemplatesList extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadUnifiedJobTemplates () {
|
async handleTemplateDelete () {
|
||||||
|
const { selected } = this.state;
|
||||||
|
|
||||||
|
this.setState({ contentLoading: true, deletionError: false });
|
||||||
|
try {
|
||||||
|
await Promise.all(selected.map(({ type, id }) => {
|
||||||
|
let deletePromise;
|
||||||
|
if (type === 'job_template') {
|
||||||
|
deletePromise = JobTemplatesAPI.destroy(id);
|
||||||
|
} else if (type === 'workflow_job_template') {
|
||||||
|
deletePromise = WorkflowJobTemplatesAPI.destroy(id);
|
||||||
|
}
|
||||||
|
return deletePromise;
|
||||||
|
}));
|
||||||
|
} catch (err) {
|
||||||
|
this.setState({ deletionError: true });
|
||||||
|
} finally {
|
||||||
|
await this.loadTemplates();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadTemplates () {
|
||||||
const { location } = this.props;
|
const { location } = this.props;
|
||||||
const params = parseNamespacedQueryString(QS_CONFIG, location.search);
|
const params = parseNamespacedQueryString(QS_CONFIG, location.search);
|
||||||
|
|
||||||
@@ -88,6 +119,7 @@ class TemplatesList extends Component {
|
|||||||
const {
|
const {
|
||||||
contentError,
|
contentError,
|
||||||
contentLoading,
|
contentLoading,
|
||||||
|
deletionError,
|
||||||
templates,
|
templates,
|
||||||
itemCount,
|
itemCount,
|
||||||
selected,
|
selected,
|
||||||
@@ -120,6 +152,14 @@ class TemplatesList extends Component {
|
|||||||
showExpandCollapse
|
showExpandCollapse
|
||||||
isAllSelected={isAllSelected}
|
isAllSelected={isAllSelected}
|
||||||
onSelectAll={this.handleSelectAll}
|
onSelectAll={this.handleSelectAll}
|
||||||
|
additionalControls={[
|
||||||
|
<ToolbarDeleteButton
|
||||||
|
key="delete"
|
||||||
|
onDelete={this.handleTemplateDelete}
|
||||||
|
itemsToDelete={selected}
|
||||||
|
itemName={i18n._(t`Template`)}
|
||||||
|
/>
|
||||||
|
]}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
renderItem={(template) => (
|
renderItem={(template) => (
|
||||||
@@ -134,6 +174,14 @@ class TemplatesList extends Component {
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
<AlertModal
|
||||||
|
isOpen={deletionError}
|
||||||
|
variant="danger"
|
||||||
|
title={i18n._(t`Error!`)}
|
||||||
|
onClose={this.handleDeleteErrorClose}
|
||||||
|
>
|
||||||
|
{i18n._(t`Failed to delete one or more template.`)}
|
||||||
|
</AlertModal>
|
||||||
</PageSection>
|
</PageSection>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user