2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-30 17:10:44 +00:00
|
|
|
// @ts-check
|
|
|
|
import Model from '@ember-data/model';
|
2022-06-10 14:05:34 +00:00
|
|
|
import { computed } from '@ember/object';
|
2022-05-30 17:10:44 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
import MutableArray from '@ember/array/mutable';
|
2022-06-07 01:42:23 +00:00
|
|
|
import { trimPath } from '../helpers/trim-path';
|
2022-07-13 15:40:13 +00:00
|
|
|
import { attr } from '@ember-data/model';
|
2022-05-30 17:10:44 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-07 01:42:23 +00:00
|
|
|
* @typedef KeyValue
|
2022-05-30 17:10:44 +00:00
|
|
|
* @type {object}
|
|
|
|
* @property {string} key
|
|
|
|
* @property {string} value
|
|
|
|
*/
|
|
|
|
|
2022-06-07 01:42:23 +00:00
|
|
|
/**
|
2022-08-29 18:41:58 +00:00
|
|
|
* @typedef Variable
|
2022-06-07 01:42:23 +00:00
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-08-29 18:41:58 +00:00
|
|
|
* A Variable has a path, namespace, and an array of key-value pairs within the client.
|
2022-06-07 01:42:23 +00:00
|
|
|
* On the server, these key-value pairs are serialized into object structure.
|
|
|
|
* @class
|
|
|
|
* @extends Model
|
|
|
|
*/
|
2022-05-30 17:10:44 +00:00
|
|
|
@classic
|
|
|
|
export default class VariableModel extends Model {
|
2022-06-07 01:42:23 +00:00
|
|
|
/**
|
|
|
|
* Can be any arbitrary string, but behaves best when used as a slash-delimited file path.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2022-07-08 14:08:02 +00:00
|
|
|
@attr('string', { defaultValue: '' }) path;
|
2022-05-30 17:10:44 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-07 01:42:23 +00:00
|
|
|
* @type {MutableArray<KeyValue>}
|
2022-05-30 17:10:44 +00:00
|
|
|
*/
|
|
|
|
@attr({
|
|
|
|
defaultValue() {
|
|
|
|
return [{ key: '', value: '' }];
|
|
|
|
},
|
|
|
|
})
|
|
|
|
keyValues;
|
2022-06-07 01:42:23 +00:00
|
|
|
|
|
|
|
/** @type {number} */
|
|
|
|
@attr('number') createIndex;
|
|
|
|
/** @type {number} */
|
|
|
|
@attr('number') modifyIndex;
|
2022-07-06 18:17:10 +00:00
|
|
|
/** @type {Date} */
|
|
|
|
@attr('date') createTime;
|
|
|
|
/** @type {Date} */
|
|
|
|
@attr('date') modifyTime;
|
2022-06-07 01:42:23 +00:00
|
|
|
/** @type {string} */
|
2022-07-08 14:08:02 +00:00
|
|
|
@attr('string', { defaultValue: 'default' }) namespace;
|
2022-06-07 01:42:23 +00:00
|
|
|
|
2022-06-10 14:05:34 +00:00
|
|
|
@computed('path')
|
2022-06-17 18:03:43 +00:00
|
|
|
get parentFolderPath() {
|
|
|
|
const split = this.path.split('/');
|
|
|
|
const [, ...folderPath] = [split.pop(), ...split];
|
|
|
|
return folderPath.join('/');
|
2022-06-10 14:05:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 01:42:23 +00:00
|
|
|
/**
|
2022-07-08 14:08:02 +00:00
|
|
|
* Removes starting and trailing slashes, pathLinkedEntitiesand sets the ID property
|
2022-06-07 01:42:23 +00:00
|
|
|
*/
|
|
|
|
setAndTrimPath() {
|
2022-06-10 14:05:34 +00:00
|
|
|
this.set('path', trimPath([this.path]));
|
2022-08-15 15:56:09 +00:00
|
|
|
if (!this.get('id')) {
|
|
|
|
this.set('id', `${this.get('path')}@${this.get('namespace')}`);
|
|
|
|
}
|
2022-06-07 01:42:23 +00:00
|
|
|
}
|
2022-06-21 19:49:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates the key-value pairs into an object structure.
|
|
|
|
*/
|
|
|
|
@computed('keyValues')
|
|
|
|
get items() {
|
|
|
|
return this.keyValues.reduce((acc, { key, value }) => {
|
|
|
|
acc[key] = value;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
2022-07-08 14:08:02 +00:00
|
|
|
|
|
|
|
// Gets the path of the variable, and if it starts with jobs/, delimits on / and returns each part separately in an array
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef pathLinkedEntities
|
|
|
|
* @type {Object}
|
|
|
|
* @property {string} job
|
|
|
|
* @property {string} [group]
|
|
|
|
* @property {string} [task]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {pathLinkedEntities}
|
|
|
|
*/
|
|
|
|
get pathLinkedEntities() {
|
|
|
|
const entityTypes = ['job', 'group', 'task'];
|
|
|
|
const emptyEntities = { job: '', group: '', task: '' };
|
2022-07-20 16:19:01 +00:00
|
|
|
if (
|
|
|
|
this.path?.startsWith('nomad/jobs/') &&
|
|
|
|
this.path?.split('/').length <= 5
|
|
|
|
) {
|
2022-07-08 14:08:02 +00:00
|
|
|
return this.path
|
|
|
|
.split('/')
|
2022-07-20 16:19:01 +00:00
|
|
|
.slice(2, 5)
|
2022-07-08 14:08:02 +00:00
|
|
|
.reduce((acc, pathPart, index) => {
|
|
|
|
acc[entityTypes[index]] = pathPart;
|
|
|
|
return acc;
|
|
|
|
}, emptyEntities);
|
|
|
|
} else {
|
|
|
|
return emptyEntities;
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 17:10:44 +00:00
|
|
|
}
|