Skip to content

Commit

Permalink
backport of commit a35acdb (#19265)
Browse files Browse the repository at this point in the history
Co-authored-by: Phil Renaud <phil.renaud@hashicorp.com>
  • Loading branch information
hc-github-team-nomad-core and philrenaud authored Dec 1, 2023
1 parent 764f95e commit 20e7434
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changelog/19220.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: fix an issue where starting a stopped job with default-less variables would not retain those variables when done via the job page start button in the web ui
```
23 changes: 2 additions & 21 deletions ui/app/components/job-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { task } from 'ember-concurrency';
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
import { tracked } from '@glimmer/tracking';
import jsonToHcl from 'nomad-ui/utils/json-to-hcl';

/**
* JobEditor component that provides an interface for editing and managing Nomad jobs.
Expand Down Expand Up @@ -39,9 +40,7 @@ export default class JobEditor extends Component {
if (this.args.variables) {
this.args.job.set(
'_newDefinitionVariables',
this.jsonToHcl(this.args.variables.flags).concat(
this.args.variables.literal
)
jsonToHcl(this.args.variables.flags).concat(this.args.variables.literal)
);
}
}
Expand Down Expand Up @@ -258,24 +257,6 @@ export default class JobEditor extends Component {
}
}

/**
* Convert a JSON object to an HCL string.
*
* @param {Object} obj - The JSON object to convert.
* @returns {string} The HCL string representation of the JSON object.
*/
jsonToHcl(obj) {
const hclLines = [];

for (const key in obj) {
const value = obj[key];
const hclValue = typeof value === 'string' ? `"${value}"` : value;
hclLines.push(`${key}=${hclValue}\n`);
}

return hclLines.join('\n');
}

get data() {
return {
cancelable: this.args.cancelable,
Expand Down
11 changes: 11 additions & 0 deletions ui/app/components/job-page/parts/title.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { inject as service } from '@ember/service';
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
import { tagName } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
import jsonToHcl from 'nomad-ui/utils/json-to-hcl';

@classic
@tagName('')
Expand Down Expand Up @@ -76,6 +77,16 @@ export default class Title extends Component {
// In the event that this fails, fall back to the raw definition.
try {
const specification = yield job.fetchRawSpecification();

let _newDefinitionVariables = job.get('_newDefinitionVariables') || '';
if (specification.VariableFlags) {
_newDefinitionVariables += jsonToHcl(specification.VariableFlags);
}
if (specification.Variables) {
_newDefinitionVariables += specification.Variables;
}
job.set('_newDefinitionVariables', _newDefinitionVariables);

job.set('_newDefinition', specification.Source);
} catch {
const definition = yield job.fetchRawDefinition();
Expand Down
24 changes: 24 additions & 0 deletions ui/app/utils/json-to-hcl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

// @ts-check

/**
* Convert a JSON object to an HCL string.
*
* @param {Object} obj - The JSON object to convert.
* @returns {string} The HCL string representation of the JSON object.
*/
export default function jsonToHcl(obj) {
const hclLines = [];

for (const key in obj) {
const value = obj[key];
const hclValue = typeof value === 'string' ? `"${value}"` : value;
hclLines.push(`${key}=${hclValue}\n`);
}

return hclLines.join('\n');
}

0 comments on commit 20e7434

Please sign in to comment.