github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/app/serializers/volume.js (about) 1 import { set, get } from '@ember/object'; 2 import ApplicationSerializer from './application'; 3 4 export default ApplicationSerializer.extend({ 5 attrs: { 6 externalId: 'ExternalID', 7 }, 8 9 embeddedRelationships: ['writeAllocations', 'readAllocations'], 10 11 // Volumes treat Allocations as embedded records. Ember has an 12 // EmbeddedRecords mixin, but it assumes an application is using 13 // the REST serializer and Nomad does not. 14 normalize(typeHash, hash) { 15 hash.NamespaceID = hash.Namespace; 16 17 hash.PlainId = hash.ID; 18 19 // TODO These shouldn't hardcode `csi/` as part of the IDs, 20 // but it is necessary to make the correct find requests and the 21 // payload does not contain the required information to derive 22 // this identifier. 23 hash.ID = JSON.stringify([`csi/${hash.ID}`, hash.NamespaceID || 'default']); 24 hash.PluginID = `csi/${hash.PluginID}`; 25 26 // Convert hash-based allocation embeds to lists 27 const readAllocs = hash.ReadAllocs || {}; 28 const writeAllocs = hash.WriteAllocs || {}; 29 const bindIDToAlloc = hash => id => { 30 const alloc = hash[id]; 31 alloc.ID = id; 32 return alloc; 33 }; 34 35 hash.ReadAllocations = Object.keys(readAllocs).map(bindIDToAlloc(readAllocs)); 36 hash.WriteAllocations = Object.keys(writeAllocs).map(bindIDToAlloc(writeAllocs)); 37 38 const normalizedHash = this._super(typeHash, hash); 39 return this.extractEmbeddedRecords(this, this.store, typeHash, normalizedHash); 40 }, 41 42 keyForRelationship(attr, relationshipType) { 43 //Embedded relationship attributes don't end in IDs 44 if (this.embeddedRelationships.includes(attr)) return attr.capitalize(); 45 return this._super(attr, relationshipType); 46 }, 47 48 // Convert the embedded relationship arrays into JSONAPI included records 49 extractEmbeddedRecords(serializer, store, typeHash, partial) { 50 partial.included = partial.included || []; 51 52 this.embeddedRelationships.forEach(embed => { 53 const relationshipMeta = typeHash.relationshipsByName.get(embed); 54 const relationship = get(partial, `data.relationships.${embed}.data`); 55 56 if (!relationship) return; 57 58 // Create a sidecar relationships array 59 const hasMany = new Array(relationship.length); 60 61 // For each embedded allocation, normalize the allocation JSON according 62 // to the allocation serializer. 63 relationship.forEach((alloc, idx) => { 64 const { data, included } = this.normalizeEmbeddedRelationship( 65 store, 66 relationshipMeta, 67 alloc 68 ); 69 70 // In JSONAPI, embedded records go in the included array. 71 partial.included.push(data); 72 if (included) { 73 partial.included.push(...included); 74 } 75 76 // In JSONAPI, the main payload value is an array of IDs that 77 // map onto the objects in the included array. 78 hasMany[idx] = { id: data.id, type: data.type }; 79 }); 80 81 // Set the JSONAPI relationship value to the sidecar. 82 const relationshipJson = { data: hasMany }; 83 set(partial, `data.relationships.${embed}`, relationshipJson); 84 }); 85 86 return partial; 87 }, 88 89 normalizeEmbeddedRelationship(store, relationshipMeta, relationshipHash) { 90 const modelName = relationshipMeta.type; 91 const modelClass = store.modelFor(modelName); 92 const serializer = store.serializerFor(modelName); 93 94 return serializer.normalize(modelClass, relationshipHash, null); 95 }, 96 });