2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-08 11:38:41 -04:00
|
|
|
import ApplicationSerializer from './application';
|
2023-02-24 13:11:17 -05:00
|
|
|
import {
|
|
|
|
|
TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC,
|
|
|
|
|
TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE,
|
|
|
|
|
} from '../models/credential-library';
|
2021-07-08 11:38:41 -04:00
|
|
|
|
2022-06-28 19:30:26 -04:00
|
|
|
export default class CredentialLibrarySerializer extends ApplicationSerializer {
|
2022-09-20 18:24:18 -04:00
|
|
|
// =properties
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
serializeScopeID = false;
|
|
|
|
|
|
2022-06-28 19:30:26 -04:00
|
|
|
/**
|
|
|
|
|
* @override
|
|
|
|
|
* @method serialize
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
*/
|
2023-02-24 13:11:17 -05:00
|
|
|
serialize(snapshot) {
|
|
|
|
|
switch (snapshot.record.type) {
|
|
|
|
|
case TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC:
|
|
|
|
|
return this.serializeVaultGeneric(...arguments);
|
|
|
|
|
case TYPE_CREDENTIAL_LIBRARY_VAULT_SSH_CERTIFICATE:
|
|
|
|
|
default:
|
|
|
|
|
return super.serialize(...arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serializeAttribute(snapshot, json, key, attribute) {
|
|
|
|
|
super.serializeAttribute(...arguments);
|
|
|
|
|
const { options } = attribute;
|
|
|
|
|
const { type } = snapshot.record;
|
|
|
|
|
|
|
|
|
|
// If an attribute has a `for` option, it must match the
|
|
|
|
|
// record's `type`, else the attribute is excluded
|
|
|
|
|
// from serialization.
|
|
|
|
|
if (options?.for && options.for !== type) {
|
|
|
|
|
if (options.isNestedAttribute) {
|
|
|
|
|
delete json.attributes[key];
|
|
|
|
|
} else {
|
|
|
|
|
delete json[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serializeVaultGeneric() {
|
2022-06-28 19:30:26 -04:00
|
|
|
const serialized = super.serialize(...arguments);
|
|
|
|
|
if (serialized.attributes) {
|
2023-02-24 13:11:17 -05:00
|
|
|
// Set `http_request_body` only if `http_method` is POST
|
|
|
|
|
// otherwise set to null to have the field removed
|
|
|
|
|
if (!serialized.attributes?.http_method?.match(/post/i)) {
|
|
|
|
|
serialized.attributes.http_request_body = null;
|
2023-02-01 13:46:23 -05:00
|
|
|
}
|
2022-06-28 19:30:26 -04:00
|
|
|
}
|
|
|
|
|
return serialized;
|
|
|
|
|
}
|
|
|
|
|
}
|