2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* 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';
|
2024-04-04 16:23:48 -04:00
|
|
|
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';
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 18:06:12 -05:00
|
|
|
/**
|
|
|
|
|
* True if the host set is an unknown type.
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
get isUnknown() {
|
2024-04-04 16:23:48 -04:00
|
|
|
return (
|
|
|
|
|
this.isPlugin && !TYPES_HOST_CATALOG_PLUGIN.includes(this.plugin?.name)
|
|
|
|
|
);
|
2022-02-14 18:06:12 -05:00
|
|
|
}
|
|
|
|
|
|
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() {
|
2022-02-14 18:06:12 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-10 13:47:51 -04:00
|
|
|
* Adds hosts via the `add-hosts` method.
|
2020-08-19 18:38:18 -04:00
|
|
|
* See serializer and adapter for more information.
|
2020-09-10 13:47:51 -04:00
|
|
|
* @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: {} }) {
|
2020-09-10 13:47:51 -04:00
|
|
|
const defaultAdapterOptions = {
|
|
|
|
|
method: 'add-hosts',
|
2020-09-16 14:29:02 -04:00
|
|
|
hostIDs,
|
2020-09-10 13:47:51 -04:00
|
|
|
};
|
|
|
|
|
return this.save({
|
|
|
|
|
...options,
|
2020-08-19 18:38:18 -04:00
|
|
|
adapterOptions: {
|
2020-09-10 13:47:51 -04:00
|
|
|
...defaultAdapterOptions,
|
2020-09-16 14:29:02 -04:00
|
|
|
...options.adapterOptions,
|
|
|
|
|
},
|
2020-09-10 13:47:51 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 16:20:34 -04:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 13:47:51 -04:00
|
|
|
/**
|
|
|
|
|
* 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: {} }) {
|
2020-09-10 13:47:51 -04:00
|
|
|
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: {
|
2020-09-10 13:47:51 -04:00
|
|
|
...defaultAdapterOptions,
|
2020-09-16 14:29:02 -04:00
|
|
|
...options.adapterOptions,
|
|
|
|
|
},
|
2020-08-19 18:38:18 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 13:47:51 -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
|
|
|
}
|