2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-04-20 18:40:27 +00:00
|
|
|
import ApplicationSerializer from '../application';
|
|
|
|
|
|
|
|
export default class KeymgmtKeySerializer extends ApplicationSerializer {
|
|
|
|
normalizeItems(payload) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const normalized = super.normalizeItems(payload);
|
2022-04-20 18:40:27 +00:00
|
|
|
// Transform versions from object with number keys to array with key ids
|
|
|
|
if (normalized.versions) {
|
|
|
|
let lastRotated;
|
|
|
|
let created;
|
2022-11-09 23:15:31 +00:00
|
|
|
const versions = [];
|
2022-04-20 18:40:27 +00:00
|
|
|
Object.keys(normalized.versions).forEach((key, i, arr) => {
|
|
|
|
versions.push({
|
|
|
|
id: parseInt(key, 10),
|
|
|
|
...normalized.versions[key],
|
|
|
|
});
|
|
|
|
if (i === 0) {
|
|
|
|
created = normalized.versions[key].creation_time;
|
|
|
|
} else if (arr.length - 1 === i) {
|
|
|
|
// Set lastRotated to the last key
|
|
|
|
lastRotated = normalized.versions[key].creation_time;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
normalized.versions = versions;
|
|
|
|
return { ...normalized, last_rotated: lastRotated, created };
|
|
|
|
} else if (Array.isArray(normalized)) {
|
|
|
|
return normalized.map((key) => ({
|
|
|
|
id: key.id,
|
|
|
|
name: key.id,
|
|
|
|
backend: payload.backend,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return normalized;
|
|
|
|
}
|
|
|
|
}
|