open-consul/ui/packages/consul-ui/app/serializers/service-instance.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

import Serializer from './application';
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/service-instance';
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([
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,
ID: node.ID,
Node: node.Node,
Address: node.Address,
TaggedAddresses: node.TaggedAddresses,
Meta: node.Meta,
},
};
instance.uid = this.extractUid(instance);
return instance;
}
respondForQuery(respond, query) {
2020-12-09 13:08:30 +00:00
const body = super.respondForQuery(cb => {
return respond((headers, body) => {
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';
item.uid = this.extractUid(item);
});
return cb(headers, body);
});
}, query);
2020-12-09 13:08:30 +00:00
return body;
}
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';
item.uid = this.extractUid(item);
});
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;
return cb(headers, body);
});
}, query);
}
}