From c7086d5a96c8f5cdec86ed8fa318957b6bcee101 Mon Sep 17 00:00:00 2001 From: hc-github-team-nomad-core <82989552+hc-github-team-nomad-core@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:23:47 -0600 Subject: [PATCH] backport of commit a35acdb84e2445930598663bb8cbc133e9832b4d (#19264) Co-authored-by: Phil Renaud --- .changelog/19220.txt | 3 +++ ui/app/components/job-editor.js | 23 ++-------------------- ui/app/components/job-page/parts/title.js | 11 +++++++++++ ui/app/utils/json-to-hcl.js | 24 +++++++++++++++++++++++ 4 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 .changelog/19220.txt create mode 100644 ui/app/utils/json-to-hcl.js diff --git a/.changelog/19220.txt b/.changelog/19220.txt new file mode 100644 index 000000000..9f42d4b6c --- /dev/null +++ b/.changelog/19220.txt @@ -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 +``` diff --git a/ui/app/components/job-editor.js b/ui/app/components/job-editor.js index fc91c2bf3..0b3deeb4b 100644 --- a/ui/app/components/job-editor.js +++ b/ui/app/components/job-editor.js @@ -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. @@ -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) ); } } @@ -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, diff --git a/ui/app/components/job-page/parts/title.js b/ui/app/components/job-page/parts/title.js index d9b68fe6d..843b43e99 100644 --- a/ui/app/components/job-page/parts/title.js +++ b/ui/app/components/job-page/parts/title.js @@ -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('') @@ -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(); diff --git a/ui/app/utils/json-to-hcl.js b/ui/app/utils/json-to-hcl.js new file mode 100644 index 000000000..ba49a9a53 --- /dev/null +++ b/ui/app/utils/json-to-hcl.js @@ -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'); +}