2018-05-22 15:03:45 +00:00
|
|
|
import Serializer from './application';
|
2020-09-18 10:14:06 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-09-24 15:07:13 +00:00
|
|
|
import { get } from '@ember/object';
|
2019-12-17 18:47:37 +00:00
|
|
|
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/intention';
|
2018-05-22 15:03:45 +00:00
|
|
|
|
2020-11-09 17:29:12 +00:00
|
|
|
export default class IntentionSerializer extends Serializer {
|
|
|
|
@service('encoder') encoder;
|
|
|
|
|
|
|
|
primaryKey = PRIMARY_KEY;
|
|
|
|
slugKey = SLUG_KEY;
|
|
|
|
|
|
|
|
init() {
|
|
|
|
super.init(...arguments);
|
2020-09-18 10:14:06 +00:00
|
|
|
this.uri = this.encoder.uriTag();
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ensureID(item) {
|
2020-09-24 15:07:13 +00:00
|
|
|
if (!get(item, 'ID.length')) {
|
2020-09-18 10:14:06 +00:00
|
|
|
item.Legacy = false;
|
|
|
|
} else {
|
|
|
|
item.Legacy = true;
|
2020-09-24 15:07:13 +00:00
|
|
|
item.LegacyID = item.ID;
|
2020-09-18 10:14:06 +00:00
|
|
|
}
|
2020-09-24 15:07:13 +00:00
|
|
|
item.ID = this
|
|
|
|
.uri`${item.SourceNS}:${item.SourceName}:${item.DestinationNS}:${item.DestinationName}`;
|
2020-09-18 10:14:06 +00:00
|
|
|
return item;
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForQuery(respond, query) {
|
|
|
|
return super.respondForQuery(
|
2020-09-18 10:14:06 +00:00
|
|
|
cb =>
|
|
|
|
respond((headers, body) => {
|
|
|
|
return cb(
|
|
|
|
headers,
|
2021-01-27 10:41:24 +00:00
|
|
|
body.map(item => this.ensureID(item))
|
2020-09-18 10:14:06 +00:00
|
|
|
);
|
|
|
|
}),
|
|
|
|
query
|
|
|
|
);
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForQueryRecord(respond, query) {
|
|
|
|
return super.respondForQueryRecord(
|
2020-09-18 10:14:06 +00:00
|
|
|
cb =>
|
|
|
|
respond((headers, body) => {
|
2021-01-27 10:41:24 +00:00
|
|
|
body = this.ensureID(body);
|
2020-09-18 10:14:06 +00:00
|
|
|
return cb(headers, body);
|
|
|
|
}),
|
|
|
|
query
|
|
|
|
);
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForCreateRecord(respond, serialized, data) {
|
2020-09-30 11:33:01 +00:00
|
|
|
const slugKey = this.slugKey;
|
|
|
|
const primaryKey = this.primaryKey;
|
|
|
|
return respond((headers, body) => {
|
|
|
|
body = data;
|
|
|
|
body.ID = this
|
|
|
|
.uri`${serialized.SourceNS}:${serialized.SourceName}:${serialized.DestinationNS}:${serialized.DestinationName}`;
|
|
|
|
return this.fingerprint(primaryKey, slugKey, body.Datacenter)(body);
|
|
|
|
});
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForUpdateRecord(respond, serialized, data) {
|
2020-09-30 11:33:01 +00:00
|
|
|
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);
|
|
|
|
});
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
}
|