open-consul/ui/packages/consul-ui/app/serializers/service.js
John Cowen 5c18dc099f
ui: Move ember-data classes to use native JS classes/decorators (#9136)
* ui: Upgrade ember-data models to use native classes/decorators

* ui: Update remaining ember-data imports

* ui: Move ember-data Adapters to use native classes

* ui: Upgrade serializers to native classes/decorators

* ui: remove meta from roles, they never had it to start with
2020-11-09 17:29:12 +00:00

61 lines
1.8 KiB
JavaScript

import Serializer from './application';
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/service';
import { get } from '@ember/object';
export default class ServiceSerializer extends Serializer {
primaryKey = PRIMARY_KEY;
slugKey = SLUG_KEY;
respondForQuery(respond, query) {
return super.respondForQuery(
cb =>
respond((headers, body) => {
// Services and proxies all come together in the same list. Here we
// map the proxies to their related services on a Service.Proxy
// property for easy access later on
const services = {};
body
.filter(function(item) {
return item.Kind !== 'connect-proxy';
})
.forEach(item => {
services[item.Name] = item;
});
body
.filter(function(item) {
return item.Kind === 'connect-proxy';
})
.forEach(item => {
// Iterating to cover the usecase of a proxy being used by more
// than one service
if (item.ProxyFor) {
item.ProxyFor.forEach(service => {
if (typeof services[service] !== 'undefined') {
services[service].Proxy = item;
}
});
}
});
return cb(headers, body);
}),
query
);
}
respondForQueryRecord(respond, query) {
// Name is added here from the query, which is used to make the uid
// Datacenter gets added in the ApplicationSerializer
return super.respondForQueryRecord(
cb =>
respond((headers, body) => {
return cb(headers, {
Name: query.id,
Namespace: get(body, 'firstObject.Service.Namespace'),
Nodes: body,
});
}),
query
);
}
}