open-nomad/ui/app/models/task.js
hc-github-team-nomad-core e9b6be87e2
[ui] Job Variables page (#17964) (#18106)
* Bones of a component that has job variable awareness

* Got vars listed woo

* Variables as its own subnav and some pathLinkedVariable perf fixes

* Automatic Access to Variables alerter

* Helper and component to conditionally render the right link

* A bit of cleanup post-template stuff

* testfix for looping right-arrow keynav bc we have a new subnav section

* A very roundabout way of ensuring that, if a job exists when saving a variable with a pathLinkedEntity of that job, its saved right through to the job itself

* hacky but an async version of pathLinkedVariable

* model-driven and async fetcher driven with cleanup

* Only run the update-job func if jobname is detected in var path

* Test cases begun

* Management token for variables to appear in tests

* Its a management token so it gets to see the clients tab under system jobs

* Pre-review cleanup

* More tests

* Number of requests test and small fix to groups-by-way-or-resource-arrays elsewhere

* Variable intro text tests

* Variable name re-use

* Simplifying our wording a bit

* parse json vs plainId

* Addressed PR feedback, including de-waterfalling

Co-authored-by: Phil Renaud <phil.renaud@hashicorp.com>
2023-08-01 09:59:39 -04:00

106 lines
2.7 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { attr } from '@ember-data/model';
import Fragment from 'ember-data-model-fragments/fragment';
import {
fragment,
fragmentArray,
fragmentOwner,
} from 'ember-data-model-fragments/attributes';
import { computed } from '@ember/object';
export default class Task extends Fragment {
@fragmentOwner() taskGroup;
@attr('string') name;
@attr('string') driver;
@attr('string') kind;
@attr() meta;
@computed('taskGroup.mergedMeta', 'meta')
get mergedMeta() {
return {
...this.taskGroup.mergedMeta,
...this.meta,
};
}
@fragment('lifecycle') lifecycle;
@computed('lifecycle', 'lifecycle.sidecar')
get lifecycleName() {
if (this.lifecycle) {
const { hook, sidecar } = this.lifecycle;
if (hook === 'prestart') {
return sidecar ? 'prestart-sidecar' : 'prestart-ephemeral';
} else if (hook === 'poststart') {
return sidecar ? 'poststart-sidecar' : 'poststart-ephemeral';
} else if (hook === 'poststop') {
return 'poststop';
}
}
return 'main';
}
@attr('number') reservedMemory;
@attr('number') reservedMemoryMax;
@attr('number') reservedCPU;
@attr('number') reservedDisk;
@attr('number') reservedEphemeralDisk;
@fragmentArray('service-fragment') services;
@fragmentArray('volume-mount', { defaultValue: () => [] }) volumeMounts;
async _fetchParentJob() {
let job = this.store.peekRecord('job', this.taskGroup.job.id);
if (!job) {
job = await this.store.findRecord('job', this.taskGroup.job.id, {
reload: true,
});
}
this._job = job;
}
get pathLinkedVariable() {
if (!this._job) {
this._fetchParentJob();
return null;
} else {
let jobID = this._job.plainId;
if (this._job.parent.get('plainId')) {
jobID = this._job.parent.get('plainId');
}
return this._job.variables?.findBy(
'path',
`nomad/jobs/${jobID}/${this.taskGroup.name}/${this.name}`
);
}
}
// TODO: This async fetcher seems like a better fit for most of our use-cases than the above getter (which cannot do async/await)
async getPathLinkedVariable() {
if (!this._job) {
await this._fetchParentJob();
}
await this._job.variables;
let jobID = this._job.plainId;
// not getting plainID because we dont know the resolution status of the task's job's parent yet
let parentID = this._job.belongsTo('parent').id()
? JSON.parse(this._job.belongsTo('parent').id())[0]
: null;
if (parentID) {
jobID = parentID;
}
return await this._job.variables?.findBy(
'path',
`nomad/jobs/${jobID}/${this.taskGroup.name}/${this.name}`
);
}
}