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

72 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-08-19 18:38:18 -04:00
import ApplicationSerializer from './application';
const fieldByType = {
2022-02-04 09:26:26 -08:00
aws: ['preferred_endpoints', 'filters'],
azure: ['preferred_endpoints', 'filter'],
};
2020-08-19 18:38:18 -04:00
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;
return serialized;
}
serializeAttribute(snapshot, json, key, attribute) {
const value = super.serializeAttribute(...arguments);
const { isPlugin, compositeType } = snapshot.record;
const { options } = attribute;
if (isPlugin && options.isNestedAttribute) {
if (!fieldByType[compositeType].includes(key)) {
delete json.attributes[key];
}
}
return value;
}
2020-08-19 18:38:18 -04:00
}