Files
threshold-ui/addons/api/addon/models/worker.js
T
Cameron Perera 8cb16dc46c Icu 13894 create edit tags modal (#2384)
* feat: 🎸 add "no tags" state for worker tags

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13894

* feat: 🎸 add edit workflow for worker tags

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13894

* refactor: 💡 update tag action permissions logic

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13894

* refactor: 💡 small change to updateApiTags

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13894

* feat: 🎸 show different tooltip for hcp managed worker tags

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13894

* refactor: 💡 address PR feedback

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13894

* refactor: 💡 address more PR feedback

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-13894
2024-07-16 15:49:30 -05:00

152 lines
3.5 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import GeneratedWorkerModel from '../generated/models/worker';
import { attr } from '@ember-data/model';
export const TYPE_WORKER_PKI = 'pki';
export const TAG_TYPE_CONFIG = 'config';
export const TAG_TYPE_API = 'api';
export const HCP_MANAGED_KEY = 'boundary.cloud.hashicorp.com:managed';
export default class WorkerModel extends GeneratedWorkerModel {
// =attributes
/**
* 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 === TYPE_WORKER_PKI;
}
/**
* Returns the number of canonical tags present on the worker.
* @type {number}
*/
get tagCount() {
if (!this.canonical_tags) {
return 0;
}
return Object.values(this.canonical_tags).reduce(
(previousCount, currentTags) => previousCount + currentTags.length,
0,
);
}
/**
* Returns the config tags as an array of key/value pair objects.
* @type {[object]}
*/
get configTagList() {
if (!this.config_tags) {
return null;
}
return Object.entries(this.config_tags).flatMap(([key, value]) =>
value.map((tag) => ({ key, value: tag, type: TAG_TYPE_CONFIG })),
);
}
/**
* 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]) =>
value.map((tag) => ({ key, value: tag, type: TAG_TYPE_API })),
);
}
/**
* Returns all tags as an array of key/value pair objects with tag type.
* @type {[object]}
*/
get allTags() {
return [...(this.configTagList ?? []), ...(this.apiTagList ?? [])];
}
@attr({
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,
emptyObjectIfMissing: true,
})
canonical_tags;
@attr({
description:
"The tags set in the worker's configuration file.\nOutput only.",
readOnly: true,
emptyObjectIfMissing: true,
})
config_tags;
@attr({
description: 'The api tags set for the worker.\nOutput only.',
emptyObjectIfMissing: true,
})
api_tags;
/**
* Method to modify the adapter to handle custom POST route for creating worker.
* @param {string} workerGeneratedAuthToken
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
createWorkerLed(workerGeneratedAuthToken, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'create:worker-led',
workerGeneratedAuthToken,
};
return super.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Method to set api tags on the worker.
* @param {object} apiTags
* @return {Promise}
*/
setApiTags(apiTags) {
return this.save({
adapterOptions: { method: 'set-worker-tags', apiTags },
});
}
/**
* Method to remove api tags on the worker.
* @param {object} apiTags
* @return {Promise}
*/
removeApiTags(apiTags) {
return this.save({
adapterOptions: { method: 'remove-worker-tags', apiTags },
});
}
}