-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcluster.js
57 lines (48 loc) · 1.51 KB
/
cluster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import RESTSerializer, { EmbeddedRecordsMixin } from '@ember-data/serializer/rest';
import { assign } from '@ember/polyfills';
import { decamelize } from '@ember/string';
import IdentityManager from '../utils/identity-manager';
const uuids = new IdentityManager();
export default RESTSerializer.extend(EmbeddedRecordsMixin, {
keyForAttribute: function (attr) {
return decamelize(attr);
},
attrs: {
nodes: { embedded: 'always' },
dr: { embedded: 'always' },
performance: { embedded: 'always' },
},
setReplicationId(data) {
if (data) {
data.id = data.cluster_id || uuids.fetch();
}
},
normalize(modelClass, data) {
// embedded records need a unique value to be stored
// set id for dr and performance to cluster_id or random unique id
this.setReplicationId(data.dr);
this.setReplicationId(data.performance);
return this._super(modelClass, data);
},
pushPayload(store, payload) {
const transformedPayload = this.normalizeResponse(
store,
store.modelFor('cluster'),
payload,
null,
'findAll'
);
return store.push(transformedPayload);
},
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
// FIXME when multiple clusters lands
const transformedPayload = {
clusters: assign({ id: '1' }, payload.data || payload),
};
return this._super(store, primaryModelClass, transformedPayload, id, requestType);
},
});