Files
threshold-ui/addons/api/addon/models/worker.js
T

93 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2022-07-28 14:33:23 -04:00
import GeneratedWorkerModel from '../generated/models/worker';
2022-08-09 15:34:42 -04:00
import { attr } from '@ember-data/model';
2022-07-28 14:33:23 -04:00
export default class WorkerModel extends GeneratedWorkerModel {
// =attributes
/**
2022-08-09 16:12:56 -04:00
* Names are optional on models in our API. Thus we need to fallback on ID
* for display purposes.
* @type {string}
*/
get displayName() {
return this.name || this.id;
}
/**
* Returns whether the worker is a pki worker.
* @type {boolean}
*/
get isPki() {
return this.type === 'pki';
}
/**
* Returns the number of config tags present on the worker.
* @type {number}
*/
get tagCount() {
if (!this.config_tags) {
return 0;
}
return Object.values(this.config_tags).reduce(
(previousCount, currentTags) => previousCount + currentTags.length,
0,
);
}
/**
* Returns the config tags as an array of key/value pair objects.
* @type {[object]}
*/
getConfigTagList() {
if (!this.config_tags) {
return null;
}
return Object.entries(this.config_tags).flatMap(([key, value]) =>
value.map((tag) => ({ key, value: tag })),
);
}
2022-08-09 15:34:42 -04:00
@attr({
2022-07-28 14:33:23 -04:00
description:
'The deduplicated union of the tags reported by the worker ' +
'from its configuration and any tags added through other means.\nOutput only.',
readOnly: true,
})
canonical_tags;
2022-08-09 15:34:42 -04:00
@attr({
2022-07-28 14:33:23 -04:00
description:
"The tags set in the worker's configuration file.\nOutput only.",
readOnly: true,
})
config_tags;
2022-08-09 15:34:42 -04:00
/**
2022-08-26 11:50:53 -04:00
* Method to modify the adapter to handle custom POST route for creating worker.
* @param {string} workerGeneratedAuthToken
2022-08-09 15:34:42 -04:00
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2022-08-26 11:50:53 -04:00
createWorkerLed(workerGeneratedAuthToken, options = { adapterOptions: {} }) {
2022-08-09 15:34:42 -04:00
const defaultAdapterOptions = {
method: 'create:worker-led',
2022-08-09 16:12:56 -04:00
workerGeneratedAuthToken,
2022-08-09 15:34:42 -04:00
};
return super.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
2022-07-28 14:33:23 -04:00
}