2019-12-17 18:47:37 +00:00
|
|
|
import Serializer from './application';
|
|
|
|
import { get } from '@ember/object';
|
|
|
|
import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/nspace';
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
const normalizeACLs = (item) => {
|
2022-02-18 17:22:05 +00:00
|
|
|
if (get(item, 'ACLs.PolicyDefaults')) {
|
2022-09-15 08:43:17 +00:00
|
|
|
item.ACLs.PolicyDefaults = item.ACLs.PolicyDefaults.map(function (item) {
|
2022-02-18 17:22:05 +00:00
|
|
|
if (typeof item.template === 'undefined') {
|
|
|
|
item.template = '';
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Both of these might come though unset so we make sure we at least
|
|
|
|
// have an empty array here so we can add children to them if we
|
|
|
|
// need to whilst saving nspaces
|
2022-09-15 08:43:17 +00:00
|
|
|
['PolicyDefaults', 'RoleDefaults'].forEach(function (prop) {
|
2022-02-18 17:22:05 +00:00
|
|
|
if (typeof item.ACLs === 'undefined') {
|
|
|
|
item.ACLs = [];
|
|
|
|
}
|
|
|
|
if (typeof item.ACLs[prop] === 'undefined') {
|
|
|
|
item.ACLs[prop] = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
|
2020-11-09 17:29:12 +00:00
|
|
|
export default class NspaceSerializer extends Serializer {
|
|
|
|
primaryKey = PRIMARY_KEY;
|
|
|
|
slugKey = SLUG_KEY;
|
|
|
|
|
2021-09-15 18:50:11 +00:00
|
|
|
respondForQuery(respond, query, data, modelClass) {
|
|
|
|
return super.respondForQuery(
|
2022-09-15 08:43:17 +00:00
|
|
|
(cb) =>
|
2021-09-15 18:50:11 +00:00
|
|
|
respond((headers, body) =>
|
|
|
|
cb(
|
|
|
|
headers,
|
2022-09-15 08:43:17 +00:00
|
|
|
body.map(function (item) {
|
2021-12-01 11:04:02 +00:00
|
|
|
item.Namespace = '*';
|
2021-09-15 18:50:11 +00:00
|
|
|
item.Datacenter = query.dc;
|
2022-02-18 17:22:05 +00:00
|
|
|
return normalizeACLs(item);
|
2021-09-15 18:50:11 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
),
|
|
|
|
query
|
|
|
|
);
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForQueryRecord(respond, serialized, data) {
|
2021-09-15 18:50:11 +00:00
|
|
|
return super.respondForQuery(
|
2022-09-15 08:43:17 +00:00
|
|
|
(cb) =>
|
2021-09-15 18:50:11 +00:00
|
|
|
respond((headers, body) => {
|
|
|
|
body.Datacenter = serialized.dc;
|
2021-12-01 11:04:02 +00:00
|
|
|
body.Namespace = '*';
|
2022-02-18 17:22:05 +00:00
|
|
|
return cb(headers, normalizeACLs(body));
|
2021-09-15 18:50:11 +00:00
|
|
|
}),
|
|
|
|
serialized,
|
|
|
|
data
|
|
|
|
);
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForCreateRecord(respond, serialized, data) {
|
2021-09-15 18:50:11 +00:00
|
|
|
return super.respondForCreateRecord(
|
2022-09-15 08:43:17 +00:00
|
|
|
(cb) =>
|
2021-09-15 18:50:11 +00:00
|
|
|
respond((headers, body) => {
|
|
|
|
body.Datacenter = serialized.dc;
|
2021-12-01 11:04:02 +00:00
|
|
|
body.Namespace = '*';
|
2022-02-18 17:22:05 +00:00
|
|
|
return cb(headers, normalizeACLs(body));
|
2021-09-15 18:50:11 +00:00
|
|
|
}),
|
|
|
|
serialized,
|
|
|
|
data
|
|
|
|
);
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respondForUpdateRecord(respond, serialized, data) {
|
2019-12-17 18:47:37 +00:00
|
|
|
return respond((headers, body) => {
|
2022-02-18 17:22:05 +00:00
|
|
|
return normalizeACLs(body);
|
2019-12-17 18:47:37 +00:00
|
|
|
});
|
2020-11-09 17:29:12 +00:00
|
|
|
}
|
|
|
|
}
|