2018-08-21 19:07:45 +00:00
|
|
|
import Component from '@ember/component';
|
2018-08-21 23:43:46 +00:00
|
|
|
import { assert } from '@ember/debug';
|
2018-08-21 19:07:45 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
|
|
|
|
import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
export default class JobEditor extends Component {
|
|
|
|
@service store;
|
|
|
|
@service config;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
'data-test-job-editor' = true;
|
2018-08-23 00:36:04 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
job = null;
|
|
|
|
onSubmit() {}
|
2018-08-21 23:43:46 +00:00
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('_context')
|
2020-06-10 13:49:16 +00:00
|
|
|
get context() {
|
|
|
|
return this._context;
|
|
|
|
}
|
2018-08-21 23:43:46 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
set context(value) {
|
|
|
|
const allowedValues = ['new', 'edit'];
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
assert(`context must be one of: ${allowedValues.join(', ')}`, allowedValues.includes(value));
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
this.set('_context', value);
|
|
|
|
}
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
_context = null;
|
|
|
|
parseError = null;
|
|
|
|
planError = null;
|
|
|
|
runError = null;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
planOutput = null;
|
|
|
|
|
|
|
|
@localStorageProperty('nomadMessageJobPlan', true) showPlanMessage;
|
|
|
|
@localStorageProperty('nomadMessageJobEditor', true) showEditorMessage;
|
|
|
|
|
|
|
|
@computed('planOutput')
|
|
|
|
get stage() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.planOutput ? 'plan' : 'editor';
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@(task(function*() {
|
2018-08-21 19:07:45 +00:00
|
|
|
this.reset();
|
|
|
|
|
|
|
|
try {
|
2019-03-26 07:46:44 +00:00
|
|
|
yield this.job.parse();
|
2018-08-21 19:07:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
const error = messageFromAdapterError(err) || 'Could not parse input';
|
|
|
|
this.set('parseError', error);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.scrollToError();
|
2018-08-21 19:07:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-03-26 07:46:44 +00:00
|
|
|
const plan = yield this.job.plan();
|
2018-08-21 19:07:45 +00:00
|
|
|
this.set('planOutput', plan);
|
|
|
|
} catch (err) {
|
|
|
|
const error = messageFromAdapterError(err) || 'Could not plan job';
|
|
|
|
this.set('planError', error);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.scrollToError();
|
2018-08-21 19:07:45 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}).drop())
|
|
|
|
plan;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@task(function*() {
|
2018-08-21 19:07:45 +00:00
|
|
|
try {
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.context === 'new') {
|
|
|
|
yield this.job.run();
|
2018-08-21 23:41:05 +00:00
|
|
|
} else {
|
2019-03-26 07:46:44 +00:00
|
|
|
yield this.job.update();
|
2018-08-21 23:41:05 +00:00
|
|
|
}
|
2018-08-21 19:07:45 +00:00
|
|
|
|
|
|
|
const id = this.get('job.plainId');
|
|
|
|
const namespace = this.get('job.namespace.name') || 'default';
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
// Treat the job as ephemeral and only provide ID parts.
|
2019-03-26 07:46:44 +00:00
|
|
|
this.onSubmit(id, namespace);
|
2018-08-21 19:07:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
const error = messageFromAdapterError(err) || 'Could not submit job';
|
|
|
|
this.set('runError', error);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.set('planOutput', null);
|
|
|
|
this.scrollToError();
|
2018-08-21 19:07:45 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
submit;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.set('planOutput', null);
|
|
|
|
this.set('planError', null);
|
|
|
|
this.set('parseError', null);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.set('runError', null);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-21 23:41:05 +00:00
|
|
|
|
|
|
|
scrollToError() {
|
|
|
|
if (!this.get('config.isTest')) {
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|