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

98 lines
2.5 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
2020-08-19 18:38:18 -04:00
import ApplicationSerializer from './application';
export default class HostSetSerializer extends ApplicationSerializer {
// =properties
/**
* @type {boolean}
*/
serializeScopeID = false;
2020-08-19 18:38:18 -04:00
// =methods
/**
* If `adapterOptions.serializeHostIDs` is true, the serialization should
* include **only host_ids** and the version. Normally, these are not
* serialized.
* @override
* @param {Snapshot} snapshot
* @return {object}
*/
serialize(snapshot) {
const hostIDs = snapshot?.adapterOptions?.hostIDs;
if (hostIDs) {
return this.serializeWithHostIDs(snapshot, hostIDs);
} else {
switch (snapshot.record.compositeType) {
case 'static':
return this.serializeStatic(...arguments);
default:
return super.serialize(...arguments);
}
}
2020-08-19 18:38:18 -04:00
}
/**
* Returns a payload containing only the host_ids array.
* @param {Snapshot} snapshot
* @param {[string]} hostIDs
2020-08-19 18:38:18 -04:00
* @return {object}
*/
serializeWithHostIDs(snapshot, hostIDs) {
2020-08-19 18:38:18 -04:00
return {
version: snapshot.attr('version'),
host_ids: hostIDs,
2020-08-19 18:38:18 -04:00
};
}
serializeStatic() {
let serialized = super.serialize(...arguments);
// Delete unnecessary fields for static type
delete serialized.attributes;
2022-02-04 09:26:26 -08:00
delete serialized.preferred_endpoints;
delete serialized.sync_interval_seconds;
return serialized;
}
2025-01-10 14:06:31 -05:00
serializeAttribute(snapshot, json, key) {
const value = super.serializeAttribute(...arguments);
2022-12-02 13:44:11 -05:00
if (key === 'filter_string') {
const { filter_string } = json.attributes;
if (filter_string) {
json.attributes.filter = json.attributes.filter_string;
}
delete json.attributes.filter_string;
}
return value;
}
/**
* Temporarily converts `filters` to an array if it is a string. This is a
* quirk of the API.
*
* TODO: remove once API consistently returns arrays.
*/
normalize(typeClass, hash, ...rest) {
const normalizedHash = structuredClone(hash);
2022-12-02 13:44:11 -05:00
if (typeof normalizedHash?.attributes?.filters === 'string') {
normalizedHash.attributes.filters = [normalizedHash.attributes.filters];
}
2022-12-02 13:44:11 -05:00
// Change name from filter to filter_string
if (normalizedHash?.attributes?.filter) {
normalizedHash.attributes.filter_string =
normalizedHash.attributes.filter;
delete normalizedHash.attributes.filter;
}
return super.normalize(typeClass, normalizedHash, ...rest);
}
2020-08-19 18:38:18 -04:00
}