open-consul/ui/packages/consul-ui/app/serializers/intention.js
John Cowen b19e14e8a8
ui: Add initial partition support to intentions (#11129)
* ui: Add initial partition support to intentions
2021-09-24 17:31:58 +01:00

75 lines
2 KiB
JavaScript

import Serializer from './application';
import { inject as service } from '@ember/service';
import { get } from '@ember/object';
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/intention';
export default class IntentionSerializer extends Serializer {
@service('encoder') encoder;
primaryKey = PRIMARY_KEY;
slugKey = SLUG_KEY;
init() {
super.init(...arguments);
this.uri = this.encoder.uriTag();
}
ensureID(item) {
if (!get(item, 'ID.length')) {
item.Legacy = false;
} else {
item.Legacy = true;
item.LegacyID = item.ID;
}
item.ID = this
.uri`${item.SourcePartition}:${item.SourceNS}:${item.SourceName}:${item.DestinationPartition}:${item.DestinationNS}:${item.DestinationName}`;
return item;
}
respondForQuery(respond, query) {
return super.respondForQuery(
cb =>
respond((headers, body) => {
return cb(
headers,
body.map(item => this.ensureID(item))
);
}),
query
);
}
respondForQueryRecord(respond, query) {
return super.respondForQueryRecord(
cb =>
respond((headers, body) => {
body = this.ensureID(body);
return cb(headers, body);
}),
query
);
}
respondForCreateRecord(respond, serialized, data) {
const slugKey = this.slugKey;
const primaryKey = this.primaryKey;
return respond((headers, body) => {
body = data;
body.ID = this
.uri`${serialized.SourcePartition}:${serialized.SourceNS}:${serialized.SourceName}:${serialized.DestinationPartition}:${serialized.DestinationNS}:${serialized.DestinationName}`;
return this.fingerprint(primaryKey, slugKey, body.Datacenter)(body);
});
}
respondForUpdateRecord(respond, serialized, data) {
const slugKey = this.slugKey;
const primaryKey = this.primaryKey;
return respond((headers, body) => {
body = data;
body.LegacyID = body.ID;
body.ID = serialized.ID;
return this.fingerprint(primaryKey, slugKey, body.Datacenter)(body);
});
}
}