2020-08-26 14:24:30 +00:00
|
|
|
import Serializer from './application';
|
|
|
|
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/service-instance';
|
|
|
|
|
2020-11-09 17:29:12 +00:00
|
|
|
export default class ServiceInstanceSerializer extends Serializer {
|
|
|
|
primaryKey = PRIMARY_KEY;
|
|
|
|
slugKey = SLUG_KEY;
|
|
|
|
|
2020-12-09 13:08:30 +00:00
|
|
|
hash = JSON.stringify;
|
|
|
|
|
|
|
|
extractUid(item) {
|
|
|
|
return this.hash([
|
2021-09-15 18:50:11 +00:00
|
|
|
item.Partition || 'default',
|
2020-12-09 13:08:30 +00:00
|
|
|
item.Namespace || 'default',
|
|
|
|
item.Datacenter,
|
|
|
|
item.Node.Node,
|
|
|
|
item.Service.ID,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
transformHasManyResponseFromNode(node, item, checks) {
|
|
|
|
const serviceChecks = checks[item.ID] || [];
|
|
|
|
const statuses = serviceChecks.reduce(
|
|
|
|
(prev, item) => {
|
|
|
|
switch (item.Status) {
|
|
|
|
case 'passing':
|
|
|
|
prev.ChecksPassing.push(item);
|
|
|
|
break;
|
|
|
|
case 'warning':
|
|
|
|
prev.ChecksWarning.push(item);
|
|
|
|
break;
|
|
|
|
case 'critical':
|
|
|
|
prev.ChecksCritical.push(item);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ChecksPassing: [],
|
|
|
|
ChecksWarning: [],
|
|
|
|
ChecksCritical: [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const instance = {
|
|
|
|
...statuses,
|
|
|
|
Service: item,
|
|
|
|
Checks: serviceChecks,
|
|
|
|
Node: {
|
|
|
|
Datacenter: node.Datacenter,
|
|
|
|
Namespace: node.Namespace,
|
2021-09-15 18:50:11 +00:00
|
|
|
Partition: node.Partition,
|
2020-12-09 13:08:30 +00:00
|
|
|
ID: node.ID,
|
|
|
|
Node: node.Node,
|
|
|
|
Address: node.Address,
|
|
|
|
TaggedAddresses: node.TaggedAddresses,
|
|
|
|
Meta: node.Meta,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
instance.uid = this.extractUid(instance);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2020-11-09 17:29:12 +00:00
|
|
|
respondForQuery(respond, query) {
|
2020-12-09 13:08:30 +00:00
|
|
|
const body = super.respondForQuery(cb => {
|
|
|
|
return respond((headers, body) => {
|
2020-08-26 14:24:30 +00:00
|
|
|
if (body.length === 0) {
|
|
|
|
const e = new Error();
|
|
|
|
e.errors = [
|
|
|
|
{
|
|
|
|
status: '404',
|
|
|
|
title: 'Not found',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
throw e;
|
|
|
|
}
|
2020-12-09 13:08:30 +00:00
|
|
|
body.forEach(item => {
|
|
|
|
item.Datacenter = query.dc;
|
|
|
|
item.Namespace = query.ns || 'default';
|
2021-09-15 18:50:11 +00:00
|
|
|
item.Partition = query.partition || 'default';
|
2020-12-09 13:08:30 +00:00
|
|
|
item.uid = this.extractUid(item);
|
|
|
|
});
|
2020-08-26 14:24:30 +00:00
|
|
|
return cb(headers, body);
|
|
|
|
});
|
|
|
|
}, query);
|
2020-12-09 13:08:30 +00:00
|
|
|
return body;
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForQueryRecord(respond, query) {
|
2020-12-09 13:08:30 +00:00
|
|
|
return super.respondForQueryRecord(cb => {
|
|
|
|
return respond((headers, body) => {
|
|
|
|
body.forEach(item => {
|
|
|
|
item.Datacenter = query.dc;
|
|
|
|
item.Namespace = query.ns || 'default';
|
2021-09-15 18:50:11 +00:00
|
|
|
item.Partition = query.partition || 'default';
|
2020-12-09 13:08:30 +00:00
|
|
|
item.uid = this.extractUid(item);
|
|
|
|
});
|
2020-08-26 14:24:30 +00:00
|
|
|
body = body.find(function(item) {
|
|
|
|
return item.Node.Node === query.node && item.Service.ID === query.serviceId;
|
|
|
|
});
|
|
|
|
if (typeof body === 'undefined') {
|
|
|
|
const e = new Error();
|
|
|
|
e.errors = [
|
|
|
|
{
|
|
|
|
status: '404',
|
|
|
|
title: 'Not found',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
body.Namespace = body.Service.Namespace;
|
2021-09-15 18:50:11 +00:00
|
|
|
body.Partition = body.Service.Partition;
|
2020-08-26 14:24:30 +00:00
|
|
|
return cb(headers, body);
|
|
|
|
});
|
|
|
|
}, query);
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
}
|