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

141 lines
3.0 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2020-08-13 17:05:09 -04:00
import GeneratedHostSetModel from '../generated/models/host-set';
import { TYPES_HOST_CATALOG_PLUGIN } from './host-catalog';
2020-08-13 17:05:09 -04:00
2020-08-19 18:38:18 -04:00
export default class HostSetModel extends GeneratedHostSetModel {
// =attributes
2022-01-26 07:56:36 -08:00
/**
* Returns if the host-set is static or not.
* @type {boolean}
*/
get isStatic() {
return this.type === 'static';
}
/**
* Returns if the host-set is plugin or not.
* @type {boolean}
*/
get isPlugin() {
return this.type === 'plugin';
}
/**
* True if the host set is an unknown type.
* @type {boolean}
*/
get isUnknown() {
return (
this.isPlugin && !TYPES_HOST_CATALOG_PLUGIN.includes(this.plugin?.name)
);
}
2022-01-26 07:56:36 -08:00
/**
* Returns if a host-set plugin is AWS or not
* @type {boolean}
*/
get isAWS() {
return this.compositeType === 'aws';
}
/**
* Return if a host-set plugin is Azure or not.
*/
get isAzure() {
return this.compositeType === 'azure';
}
/**
* If a host-set is a plugin return its name,
* otherwise returns the host-set type
* @type {string}
*/
get compositeType() {
if (this.isUnknown) return 'unknown';
if (this.isPlugin) return this.plugin.name;
return 'static';
2022-01-26 07:56:36 -08:00
}
/**
* Sets type, if type is different than static, set plugin name to type
*/
set compositeType(type) {
if (type === 'static') {
this.type = 'static';
} else {
this.type = 'plugin';
this.plugin = { name: type };
}
}
2020-08-19 18:38:18 -04:00
// =methods
/**
* Adds hosts via the `add-hosts` method.
2020-08-19 18:38:18 -04:00
* See serializer and adapter for more information.
* @param {[string]} hostIDs
2020-08-20 12:04:54 -04:00
* @param {object} options
* @param {object} options.adapterOptions
2020-08-19 18:38:18 -04:00
* @return {Promise}
*/
2020-09-16 14:29:02 -04:00
addHosts(hostIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'add-hosts',
2020-09-16 14:29:02 -04:00
hostIDs,
};
return this.save({
...options,
2020-08-19 18:38:18 -04:00
adapterOptions: {
...defaultAdapterOptions,
2020-09-16 14:29:02 -04:00
...options.adapterOptions,
},
});
}
/**
* Add a single host via the `add-hosts` method.
* @param {string} hostID
* @param {object} options
* @return {Promise}
*/
addHost(hostID, options) {
return this.addHosts([hostID], options);
}
/**
* Delete hosts via the `remove-hosts` method.
* See serializer and adapter for more information.
* @param {[string]} hostIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2020-09-16 14:29:02 -04:00
removeHosts(hostIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'remove-hosts',
2020-09-16 14:29:02 -04:00
hostIDs,
2020-08-20 12:04:54 -04:00
};
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
2020-09-16 14:29:02 -04:00
...options.adapterOptions,
},
2020-08-19 18:38:18 -04:00
});
}
/**
* Delete a single host via the `remove-hosts` method.
* @param {string} hostID
* @param {object} options
* @return {Promise}
*/
removeHost(hostID, options) {
return this.removeHosts([hostID], options);
}
2020-08-19 18:38:18 -04:00
}