Skip to content

Commit

Permalink
Title bar job start button now observes job submission variables data
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Nov 29, 2023
1 parent 26b778b commit 8fe7339
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
24 changes: 3 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 All @@ -37,11 +38,10 @@ export default class JobEditor extends Component {
}

if (this.args.variables) {
console.log('aye vars', 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 +258,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 8fe7339

Please sign in to comment.