Files
threshold-ui/addons/api/addon/serializers/host-catalog.js
T
Carlos Alcaide Corvo b33a4d0b1d refactor: 💡 Refactor app normalize and host catalog normalize
Refactor app normalize to deal with nestedAttributes and nestedSecrets
as expected, before it was not working as expected. Refactor the host
catalog normalize method. Add normalize test on host catalog to validate
changes on normalize methods.
2022-02-10 11:43:30 -08:00

68 lines
1.9 KiB
JavaScript

import ApplicationSerializer from './application';
import { copy } from 'ember-copy';
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',
],
};
export default class HostCatalogSerializer extends ApplicationSerializer {
// =methods
serialize(snapshot) {
const serialized = super.serialize(...arguments);
switch (snapshot.record.compositeType) {
case 'static':
return this.serializeStatic(...arguments);
default:
return serialized;
}
}
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
return value;
}
serializeStatic() {
const serialized = super.serialize(...arguments);
// Delete unnecessary fields for static host-catalog
delete serialized.attributes;
delete serialized.secrets;
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;
}
}