2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-01 09:39:41 -06:00
|
|
|
*/
|
|
|
|
|
|
2022-07-28 14:33:23 -04:00
|
|
|
import GeneratedWorkerModel from '../generated/models/worker';
|
|
|
|
|
|
2024-07-09 15:10:30 -05:00
|
|
|
export const TYPE_WORKER_PKI = 'pki';
|
|
|
|
|
|
2024-07-03 12:02:36 -05:00
|
|
|
export const TAG_TYPE_CONFIG = 'config';
|
|
|
|
|
export const TAG_TYPE_API = 'api';
|
|
|
|
|
|
2024-07-16 12:54:44 -05:00
|
|
|
export const HCP_MANAGED_KEY = 'boundary.cloud.hashicorp.com:managed';
|
|
|
|
|
|
2022-07-28 14:33:23 -04:00
|
|
|
export default class WorkerModel extends GeneratedWorkerModel {
|
|
|
|
|
// =attributes
|
|
|
|
|
|
2022-08-30 12:01:59 -07:00
|
|
|
/**
|
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() {
|
2024-07-09 15:10:30 -05:00
|
|
|
return this.type === TYPE_WORKER_PKI;
|
2022-08-09 16:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-16 14:53:29 -04:00
|
|
|
/**
|
2024-07-25 12:40:36 -07:00
|
|
|
* Returns the number of tags present on the worker.
|
2022-09-16 14:53:29 -04:00
|
|
|
* @type {number}
|
|
|
|
|
*/
|
|
|
|
|
get tagCount() {
|
2024-07-25 12:40:36 -07:00
|
|
|
const allTags = [
|
|
|
|
|
...Object.values(this.config_tags ?? {}),
|
|
|
|
|
...Object.values(this.api_tags ?? {}),
|
|
|
|
|
];
|
|
|
|
|
return allTags.reduce(
|
2022-09-16 14:53:29 -04:00
|
|
|
(previousCount, currentTags) => previousCount + currentTags.length,
|
2023-11-22 14:07:41 -05:00
|
|
|
0,
|
2022-09-16 14:53:29 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 20:51:06 -04:00
|
|
|
/**
|
|
|
|
|
* Returns the config tags as an array of key/value pair objects.
|
|
|
|
|
* @type {[object]}
|
|
|
|
|
*/
|
2024-06-17 14:38:19 -05:00
|
|
|
get configTagList() {
|
2022-09-20 20:51:06 -04:00
|
|
|
if (!this.config_tags) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.entries(this.config_tags).flatMap(([key, value]) =>
|
2024-07-03 12:02:36 -05:00
|
|
|
value.map((tag) => ({ key, value: tag, type: TAG_TYPE_CONFIG })),
|
2024-06-17 14:38:19 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the api tags as an array of key/value pair objects.
|
|
|
|
|
* @type {[object]}
|
|
|
|
|
*/
|
|
|
|
|
get apiTagList() {
|
|
|
|
|
if (!this.api_tags) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.entries(this.api_tags).flatMap(([key, value]) =>
|
2024-07-03 12:02:36 -05:00
|
|
|
value.map((tag) => ({ key, value: tag, type: TAG_TYPE_API })),
|
2022-09-20 20:51:06 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 14:38:19 -05:00
|
|
|
/**
|
|
|
|
|
* Returns all tags as an array of key/value pair objects with tag type.
|
|
|
|
|
* @type {[object]}
|
|
|
|
|
*/
|
|
|
|
|
get allTags() {
|
|
|
|
|
return [...(this.configTagList ?? []), ...(this.apiTagList ?? [])];
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-07-09 15:10:30 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method to set api tags on the worker.
|
|
|
|
|
* @param {object} apiTags
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
|
|
|
|
setApiTags(apiTags) {
|
|
|
|
|
return this.save({
|
|
|
|
|
adapterOptions: { method: 'set-worker-tags', apiTags },
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-07-12 13:31:42 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method to remove api tags on the worker.
|
|
|
|
|
* @param {object} apiTags
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
|
|
|
|
removeApiTags(apiTags) {
|
|
|
|
|
return this.save({
|
|
|
|
|
adapterOptions: { method: 'remove-worker-tags', apiTags },
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-07-28 14:33:23 -04:00
|
|
|
}
|