2020-03-25 12:51:26 +00:00
|
|
|
import { set, get } from '@ember/object';
|
|
|
|
import ApplicationSerializer from './application';
|
2022-03-08 17:28:36 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2022-04-07 19:51:41 +00:00
|
|
|
import { capitalize } from '@ember/string';
|
2022-03-08 17:28:36 +00:00
|
|
|
@classic
|
2020-06-11 21:23:00 +00:00
|
|
|
export default class VolumeSerializer extends ApplicationSerializer {
|
|
|
|
attrs = {
|
2020-03-25 12:51:26 +00:00
|
|
|
externalId: 'ExternalID',
|
2020-06-11 21:23:00 +00:00
|
|
|
};
|
2020-03-25 12:51:26 +00:00
|
|
|
|
2020-06-12 20:48:52 +00:00
|
|
|
embeddedRelationships = ['writeAllocations', 'readAllocations'];
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
// Volumes treat Allocations as embedded records. Ember has an
|
|
|
|
// EmbeddedRecords mixin, but it assumes an application is using
|
|
|
|
// the REST serializer and Nomad does not.
|
|
|
|
normalize(typeHash, hash) {
|
|
|
|
hash.NamespaceID = hash.Namespace;
|
|
|
|
|
|
|
|
hash.PlainId = hash.ID;
|
|
|
|
|
|
|
|
// TODO These shouldn't hardcode `csi/` as part of the IDs,
|
|
|
|
// but it is necessary to make the correct find requests and the
|
|
|
|
// payload does not contain the required information to derive
|
|
|
|
// this identifier.
|
|
|
|
hash.ID = JSON.stringify([`csi/${hash.ID}`, hash.NamespaceID || 'default']);
|
|
|
|
hash.PluginID = `csi/${hash.PluginID}`;
|
|
|
|
|
2020-11-25 21:44:06 +00:00
|
|
|
// Populate read/write allocation lists from aggregate allocation list
|
2020-03-25 12:51:26 +00:00
|
|
|
const readAllocs = hash.ReadAllocs || {};
|
|
|
|
const writeAllocs = hash.WriteAllocs || {};
|
2020-11-25 21:44:06 +00:00
|
|
|
|
|
|
|
hash.ReadAllocations = [];
|
|
|
|
hash.WriteAllocations = [];
|
|
|
|
|
|
|
|
if (hash.Allocations) {
|
2021-12-28 14:45:20 +00:00
|
|
|
hash.Allocations.forEach(function (alloc) {
|
2020-11-25 21:44:06 +00:00
|
|
|
const id = alloc.ID;
|
|
|
|
if (id in readAllocs) {
|
|
|
|
hash.ReadAllocations.push(alloc);
|
|
|
|
}
|
|
|
|
if (id in writeAllocs) {
|
|
|
|
hash.WriteAllocations.push(alloc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
delete hash.Allocations;
|
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
const normalizedHash = super.normalize(typeHash, hash);
|
2021-12-28 16:08:12 +00:00
|
|
|
return this.extractEmbeddedRecords(
|
|
|
|
this,
|
|
|
|
this.store,
|
|
|
|
typeHash,
|
|
|
|
normalizedHash
|
|
|
|
);
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
keyForRelationship(attr, relationshipType) {
|
|
|
|
//Embedded relationship attributes don't end in IDs
|
2022-04-07 19:51:41 +00:00
|
|
|
if (this.embeddedRelationships.includes(attr)) return capitalize(attr);
|
2020-06-11 21:23:00 +00:00
|
|
|
return super.keyForRelationship(attr, relationshipType);
|
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
// Convert the embedded relationship arrays into JSONAPI included records
|
|
|
|
extractEmbeddedRecords(serializer, store, typeHash, partial) {
|
|
|
|
partial.included = partial.included || [];
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
this.embeddedRelationships.forEach((embed) => {
|
2020-03-25 12:51:26 +00:00
|
|
|
const relationshipMeta = typeHash.relationshipsByName.get(embed);
|
|
|
|
const relationship = get(partial, `data.relationships.${embed}.data`);
|
|
|
|
|
|
|
|
if (!relationship) return;
|
|
|
|
|
|
|
|
// Create a sidecar relationships array
|
|
|
|
const hasMany = new Array(relationship.length);
|
|
|
|
|
|
|
|
// For each embedded allocation, normalize the allocation JSON according
|
|
|
|
// to the allocation serializer.
|
|
|
|
relationship.forEach((alloc, idx) => {
|
|
|
|
const { data, included } = this.normalizeEmbeddedRelationship(
|
|
|
|
store,
|
|
|
|
relationshipMeta,
|
|
|
|
alloc
|
|
|
|
);
|
|
|
|
|
|
|
|
// In JSONAPI, embedded records go in the included array.
|
|
|
|
partial.included.push(data);
|
|
|
|
if (included) {
|
|
|
|
partial.included.push(...included);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In JSONAPI, the main payload value is an array of IDs that
|
|
|
|
// map onto the objects in the included array.
|
|
|
|
hasMany[idx] = { id: data.id, type: data.type };
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set the JSONAPI relationship value to the sidecar.
|
|
|
|
const relationshipJson = { data: hasMany };
|
|
|
|
set(partial, `data.relationships.${embed}`, relationshipJson);
|
|
|
|
});
|
|
|
|
|
|
|
|
return partial;
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
2020-03-25 12:51:26 +00:00
|
|
|
|
|
|
|
normalizeEmbeddedRelationship(store, relationshipMeta, relationshipHash) {
|
|
|
|
const modelName = relationshipMeta.type;
|
|
|
|
const modelClass = store.modelFor(modelName);
|
|
|
|
const serializer = store.serializerFor(modelName);
|
|
|
|
|
|
|
|
return serializer.normalize(modelClass, relationshipHash, null);
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
|
|
|
}
|