2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
2022-06-23 13:16:26 +00:00
|
|
|
|
|
|
|
if (item.SourcePeer) {
|
|
|
|
item.ID = this
|
|
|
|
.uri`peer:${item.SourcePeer}:${item.SourceNS}:${item.SourceName}:${item.DestinationPartition}:${item.DestinationNS}:${item.DestinationName}`;
|
|
|
|
} else {
|
|
|
|
item.ID = this
|
|
|
|
.uri`${item.SourcePartition}:${item.SourceNS}:${item.SourceName}:${item.DestinationPartition}:${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(
|
2022-09-15 08:43:17 +00:00
|
|
|
(cb) =>
|
2020-09-18 10:14:06 +00:00
|
|
|
respond((headers, body) => {
|
|
|
|
return cb(
|
|
|
|
headers,
|
2022-09-15 08:43:17 +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(
|
2022-09-15 08:43:17 +00:00
|
|
|
(cb) =>
|
2020-09-18 10:14:06 +00:00
|
|
|
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
|
2021-09-24 16:31:58 +00:00
|
|
|
.uri`${serialized.SourcePartition}:${serialized.SourceNS}:${serialized.SourceName}:${serialized.DestinationPartition}:${serialized.DestinationNS}:${serialized.DestinationName}`;
|
2020-09-30 11:33:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|