1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00

use getAddedAndRemoved for saving instance groups

This commit is contained in:
Keith Grant 2019-10-01 14:37:42 -07:00
parent ba4e79fd3a
commit 77b68e0eb7
3 changed files with 40 additions and 63 deletions

View File

@ -20,8 +20,8 @@ function JobTemplateAdd({ history, i18n }) {
const { const {
labels, labels,
organizationId, organizationId,
addedInstanceGroups, instanceGroups,
removedInstanceGroups, initialInstanceGroups,
...remainingValues ...remainingValues
} = values; } = values;
@ -32,7 +32,7 @@ function JobTemplateAdd({ history, i18n }) {
} = await JobTemplatesAPI.create(remainingValues); } = await JobTemplatesAPI.create(remainingValues);
await Promise.all([ await Promise.all([
submitLabels(id, labels, organizationId), submitLabels(id, labels, organizationId),
submitInstanceGroups(id, addedInstanceGroups, removedInstanceGroups), submitInstanceGroups(id, instanceGroups),
]); ]);
history.push(`/templates/${type}/${id}/details`); history.push(`/templates/${type}/${id}/details`);
} catch (error) { } catch (error) {

View File

@ -107,8 +107,8 @@ class JobTemplateEdit extends Component {
const { const {
labels, labels,
organizationId, organizationId,
addedInstanceGroups, instanceGroups,
removedInstanceGroups, initialInstanceGroups,
...remainingValues ...remainingValues
} = values; } = values;
@ -117,7 +117,7 @@ class JobTemplateEdit extends Component {
await JobTemplatesAPI.update(template.id, remainingValues); await JobTemplatesAPI.update(template.id, remainingValues);
await Promise.all([ await Promise.all([
this.submitLabels(labels, organizationId), this.submitLabels(labels, organizationId),
this.submitInstanceGroups(addedInstanceGroups, removedInstanceGroups), this.submitInstanceGroups(instanceGroups, initialInstanceGroups),
]); ]);
history.push(this.detailsUrl); history.push(this.detailsUrl);
} catch (formSubmitError) { } catch (formSubmitError) {
@ -151,12 +151,13 @@ class JobTemplateEdit extends Component {
return results; return results;
} }
async submitInstanceGroups(addedGroups, removedGroups) { async submitInstanceGroups(groups, initialGroups) {
const { template } = this.props; const { template } = this.props;
const associatePromises = addedGroups.map(group => const { added, removed } = getAddedAndRemoved(initialGroups, groups);
const associatePromises = added.map(group =>
JobTemplatesAPI.associateInstanceGroup(template.id, group.id) JobTemplatesAPI.associateInstanceGroup(template.id, group.id)
); );
const disassociatePromises = removedGroups.map(group => const disassociatePromises = removed.map(group =>
JobTemplatesAPI.disassociateInstanceGroup(template.id, group.id) JobTemplatesAPI.disassociateInstanceGroup(template.id, group.id)
); );
return Promise.all([...associatePromises, ...disassociatePromises]); return Promise.all([...associatePromises, ...disassociatePromises]);

View File

@ -74,20 +74,16 @@ class JobTemplateForm extends Component {
contentError: false, contentError: false,
project: props.template.summary_fields.project, project: props.template.summary_fields.project,
inventory: props.template.summary_fields.inventory, inventory: props.template.summary_fields.inventory,
relatedInstanceGroups: [],
allowCallbacks: !!props.template.host_config_key, allowCallbacks: !!props.template.host_config_key,
}; };
this.handleProjectValidation = this.handleProjectValidation.bind(this); this.handleProjectValidation = this.handleProjectValidation.bind(this);
this.loadRelatedInstanceGroups = this.loadRelatedInstanceGroups.bind(this); this.loadRelatedInstanceGroups = this.loadRelatedInstanceGroups.bind(this);
this.handleInstanceGroupsChange = this.handleInstanceGroupsChange.bind(
this
);
} }
componentDidMount() { componentDidMount() {
const { validateField } = this.props; const { validateField } = this.props;
this.setState({ contentError: null, hasContentLoading: true }); this.setState({ contentError: null, hasContentLoading: true });
// TODO: determine whene LabelSelect has finished loading labels // TODO: determine when LabelSelect has finished loading labels
Promise.all([this.loadRelatedInstanceGroups()]).then(() => { Promise.all([this.loadRelatedInstanceGroups()]).then(() => {
this.setState({ hasContentLoading: false }); this.setState({ hasContentLoading: false });
validateField('project'); validateField('project');
@ -95,16 +91,14 @@ class JobTemplateForm extends Component {
} }
async loadRelatedInstanceGroups() { async loadRelatedInstanceGroups() {
const { template } = this.props; const { setFieldValue, template } = this.props;
if (!template.id) { if (!template.id) {
return; return;
} }
try { try {
const { data } = await JobTemplatesAPI.readInstanceGroups(template.id); const { data } = await JobTemplatesAPI.readInstanceGroups(template.id);
this.setState({ setFieldValue('initialInstanceGroups', data.results);
initialInstanceGroups: data.results, setFieldValue('instanceGroups', [...data.results]);
relatedInstanceGroups: [...data.results],
});
} catch (err) { } catch (err) {
this.setState({ contentError: err }); this.setState({ contentError: err });
} }
@ -124,37 +118,12 @@ class JobTemplateForm extends Component {
}; };
} }
handleInstanceGroupsChange(relatedInstanceGroups) {
const { setFieldValue } = this.props;
const { initialInstanceGroups } = this.state;
let added = [];
const removed = [];
if (initialInstanceGroups) {
initialInstanceGroups.forEach(group => {
if (!relatedInstanceGroups.find(g => g.id === group.id)) {
removed.push(group);
}
});
relatedInstanceGroups.forEach(group => {
if (!initialInstanceGroups.find(g => g.id === group.id)) {
added.push(group);
}
});
} else {
added = relatedInstanceGroups;
}
setFieldValue('addedInstanceGroups', added);
setFieldValue('removedInstanceGroups', removed);
this.setState({ relatedInstanceGroups });
}
render() { render() {
const { const {
contentError, contentError,
hasContentLoading, hasContentLoading,
inventory, inventory,
project, project,
relatedInstanceGroups,
allowCallbacks, allowCallbacks,
} = this.state; } = this.state;
const { const {
@ -334,13 +303,13 @@ class JobTemplateForm extends Component {
content={i18n._(t`Optional labels that describe this job template, content={i18n._(t`Optional labels that describe this job template,
such as 'dev' or 'test'. Labels can be used to group and filter such as 'dev' or 'test'. Labels can be used to group and filter
job templates and completed jobs.`)} job templates and completed jobs.`)}
/> />
<LabelSelect <LabelSelect
value={field.value} value={field.value}
onChange={labels => setFieldValue('labels', labels)} onChange={labels => setFieldValue('labels', labels)}
onError={err => this.setState({ contentError: err })} onError={err => this.setState({ contentError: err })}
/> />
</FormGroup> </FormGroup>
)} )}
/> />
</FormRow> </FormRow>
@ -440,12 +409,17 @@ class JobTemplateForm extends Component {
)} )}
/> />
</FormRow> </FormRow>
<InstanceGroupsLookup <Field
css="margin-top: 20px" name="instanceGroups"
value={relatedInstanceGroups} render={({ field, form }) => (
onChange={this.handleInstanceGroupsChange} <InstanceGroupsLookup
tooltip={i18n._(t`Select the Instance Groups for this Organization css="margin-top: 20px"
to run on.`)} value={field.value}
onChange={value => form.setFieldValue(field.name, value)}
tooltip={i18n._(t`Select the Instance Groups for this Organization
to run on.`)}
/>
)}
/> />
<Field <Field
name="job_tags" name="job_tags"
@ -576,10 +550,12 @@ class JobTemplateForm extends Component {
const FormikApp = withFormik({ const FormikApp = withFormik({
mapPropsToValues(props) { mapPropsToValues(props) {
const { template = {} } = props; const { template = {} } = props;
const { summary_fields = { const {
labels: { results: [] }, summary_fields = {
inventory: { organization: null }, labels: { results: [] },
} } = template; inventory: { organization: null },
},
} = template;
return { return {
name: template.name || '', name: template.name || '',
@ -603,8 +579,8 @@ const FormikApp = withFormik({
use_fact_cache: template.use_fact_cache || false, use_fact_cache: template.use_fact_cache || false,
host_config_key: template.host_config_key || '', host_config_key: template.host_config_key || '',
organizationId: summary_fields.inventory.organization_id || null, organizationId: summary_fields.inventory.organization_id || null,
addedInstanceGroups: [], initialInstanceGroups: [],
removedInstanceGroups: [], instanceGroups: [],
}; };
}, },
handleSubmit: (values, { props }) => props.handleSubmit(values), handleSubmit: (values, { props }) => props.handleSubmit(values),