Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui] HCL-in-UI: Re-arrange buttons, add save-as-file #17752

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/17752.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: Adds a Download as .nomad.hcl button to jobspec editing in the UI
```
38 changes: 38 additions & 0 deletions ui/app/components/job-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { tracked } from '@glimmer/tracking';
export default class JobEditor extends Component {
@service config;
@service store;
@service notifications;

@tracked error = null;
@tracked planOutput = null;
Expand Down Expand Up @@ -199,6 +200,42 @@ export default class JobEditor extends Component {
reader.readAsText(file);
}

/**
* Download the job's definition or specification as .nomad.hcl file locally
*/
@action
async handleSaveAsFile() {
try {
const blob = new Blob([this.args.job._newDefinition], {
type: 'text/plain',
});
const url = window.URL.createObjectURL(blob);
const downloadAnchor = document.createElement('a');

downloadAnchor.href = url;
downloadAnchor.target = '_blank';
downloadAnchor.rel = 'noopener noreferrer';
downloadAnchor.download = 'jobspec.nomad.hcl';

downloadAnchor.click();
downloadAnchor.remove();

window.URL.revokeObjectURL(url);
this.notifications.add({
title: 'jobspec.nomad.hcl has been downloaded',
color: 'success',
icon: 'download',
});
} catch (err) {
this.notifications.add({
title: 'Error downloading file',
message: err.message,
color: 'critical',
sticky: true,
});
}
}

/**
* Get the definition or specification based on the view type.
*
Expand Down Expand Up @@ -253,6 +290,7 @@ export default class JobEditor extends Component {
onPlan: this.plan,
onReset: this.reset,
onSaveAs: this.args.handleSaveAsTemplate,
onSaveFile: this.handleSaveAsFile,
onSubmit: this.submit,
onSelect: this.args.onSelect,
onUpdate: this.updateCode,
Expand Down
5 changes: 5 additions & 0 deletions ui/app/styles/components/codemirror.scss
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ header.run-job-header {
grid-template-columns: 1fr auto;
margin-bottom: 2rem;
gap: 0 1rem;
align-items: end;
& > h1 {
grid-column: -1 / 1;
}
Expand All @@ -166,6 +167,10 @@ header.run-job-header {
bottom: 0;
background: white;
padding: 0.5rem 0;

&.pull-left {
justify-content: flex-start;
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions ui/app/templates/components/job-editor.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@
<p>
Paste or author HCL or JSON to submit to your cluster, or select from a list of templates. A plan will be requested before the job is submitted. You can also attach a job spec by uploading a job file or dragging &amp; dropping a file to the editor.
</p>

{{#if (can "write variable" path="*" namespace="*")}}
<Hds::ButtonSet>
<label
class="job-spec-upload hds-button hds-button--color-secondary hds-button--size-medium"
>
<div class="hds-button__text">Upload file</div>
<input
type="file"
onchange={{action this.uploadJobSpec}}
accept=".hcl,.json,.nomad"
/>
</label>
<Hds::Button
@text="Choose from template"
@color="secondary"
@route="jobs.run.templates"
data-test-choose-template
/>
</Hds::ButtonSet>
{{/if}}

</header>
{{/if}}
{{did-update this.setDefinitionOnModel this.definition}}
Expand Down
46 changes: 15 additions & 31 deletions ui/app/templates/components/job-editor/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,26 @@
</div>
{{/if}}
</div>
<div class="is-associative buttonset sticky">
<Hds::ButtonSet class="is-associative buttonset sticky pull-left">
<Hds::Button
{{on "click" (perform @fns.onPlan)}}
disabled={{or @fns.onPlan.isRunning (not @data.job._newDefinition)}}
data-test-plan
@text="Plan"
/>
{{#if @data.job.isNew}}
<Hds::ButtonSet>
<label
class="job-spec-upload hds-button hds-button--color-secondary hds-button--size-medium"
>
<div class="hds-button__text">Upload file</div>
<input
type="file"
onchange={{action @fns.onUpload}}
accept=".hcl,.json,.nomad"
/>
</label>
{{#if (can "write variable" path="*" namespace="*")}}
<Hds::Button
@icon="file-plus"
@text="Save as template"
@color="tertiary"
@route="jobs.run.templates.new"
{{on "click" @fns.onSaveAs}}
data-test-save-as-template
/>
<Hds::Button
@icon="file-text"
@text="Choose from a template"
@color="tertiary"
@route="jobs.run.templates"
data-test-choose-template
/>
{{/if}}
</Hds::ButtonSet>
<Hds::Button
@text="Save as template"
@color="secondary"
@route="jobs.run.templates.new"
{{on "click" @fns.onSaveAs}}
data-test-save-as-template
/>
{{/if}}
</div>
<Hds::Button
@text="Save as .nomad.hcl"
@color="secondary"
{{on "click" @fns.onSaveFile}}

/>
</Hds::ButtonSet>
2 changes: 2 additions & 0 deletions ui/tests/integration/components/job-editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ module('Integration | Component | job-editor', function (hooks) {
@job={{job}}
@context={{context}}
@onSubmit={{onSubmit}}
@handleSaveAsTemplate={{handleSaveAsTemplate}}
/>
`;

const renderNewJob = async (component, job) => {
component.setProperties({
job,
onSubmit: sinon.spy(),
handleSaveAsTemplate: sinon.spy(),
context: 'new',
});
await component.render(commonTemplate);
Expand Down