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

67 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-01-20 08:32:13 -08:00
import ApplicationSerializer from './application';
import { copy } from 'ember-copy';
2022-01-20 08:32:13 -08:00
2022-01-20 16:47:46 -05:00
const fieldsByType = {
aws: [
'disable_credential_rotation',
'region',
'access_key_id',
'secret_access_key',
],
azure: [
'disable_credential_rotation',
'tenant_id',
'client_id',
'subscription_id',
'secret_id',
'secret_value',
],
};
2022-01-20 08:32:13 -08:00
export default class HostCatalogSerializer extends ApplicationSerializer {
// =methods
serialize(snapshot) {
const serialized = super.serialize(...arguments);
2022-01-20 16:47:46 -05:00
switch (snapshot.record.compositeType) {
2022-01-20 08:32:13 -08:00
case 'static':
return this.serializeStatic(...arguments);
default:
return serialized;
}
}
2022-01-20 16:47:46 -05:00
serializeAttribute(snapshot, json, key, attribute) {
const value = super.serializeAttribute(...arguments);
const { isPlugin, compositeType } = snapshot.record;
const { options } = attribute;
// Nested conditionals for readability
// This deletes any fields that don't belong to the record type
if (isPlugin && options.isNestedAttribute && json.attributes) {
// The key must be included in the fieldsByType list above
if (!fieldsByType[compositeType].includes(key))
delete json.attributes[key];
}
// This deletes any fields that don't belong to the record type
if (isPlugin && options.isNestedSecret && json.secrets) {
if (!fieldsByType[compositeType].includes(key)) delete json.secrets[key];
}
// new one for isNestedSecret
2022-01-20 16:47:46 -05:00
return value;
}
2022-01-20 08:32:13 -08:00
serializeStatic() {
const serialized = super.serialize(...arguments);
// Delete unnecessary fields for static host-catalog
delete serialized.attributes;
delete serialized.secrets;
2022-01-20 08:32:13 -08:00
return serialized;
}
normalize(typeClass, hash, ...rest) {
let normalizedHash = copy(hash, true);
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
delete normalized.data.attributes.attributes;
return normalized;
}
2022-01-20 08:32:13 -08:00
}