2023-03-01 09:39:41 -06:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-24 18:03:45 -04:00
|
|
|
import ApplicationSerializer from './application';
|
2023-06-02 16:03:17 -07:00
|
|
|
import { TYPE_TARGET_SSH } from '../models/target';
|
2020-08-24 18:03:45 -04:00
|
|
|
|
|
|
|
|
export default class TargetSerializer extends ApplicationSerializer {
|
|
|
|
|
// =methods
|
|
|
|
|
|
|
|
|
|
/**
|
2020-08-26 13:52:54 -04:00
|
|
|
* If `adapterOptions.hostSets` is set to an array of host set models,
|
|
|
|
|
* the resulting target serialization should include **only host sets**
|
2021-07-20 16:09:59 -04:00
|
|
|
* and the version.
|
2022-08-08 09:21:07 -07:00
|
|
|
* If `adapterOptions.credentialSources` is set to an array of
|
|
|
|
|
* credential library and credential models, the resulting target serialization should
|
|
|
|
|
* include **only credential libraries and crendentials** and the version.
|
|
|
|
|
* Normally, neither host sets or credential libraries or credentials are not serialized.
|
2020-08-24 18:03:45 -04:00
|
|
|
* @override
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
|
|
|
|
serialize(snapshot) {
|
|
|
|
|
let serialized = super.serialize(...arguments);
|
2023-06-02 16:03:17 -07:00
|
|
|
const { type } = snapshot.record;
|
2021-09-22 08:49:38 -04:00
|
|
|
const hostSourceIDs = snapshot?.adapterOptions?.hostSetIDs;
|
|
|
|
|
if (hostSourceIDs) {
|
|
|
|
|
serialized = this.serializeWithHostSources(snapshot, hostSourceIDs);
|
|
|
|
|
}
|
2022-09-01 10:07:36 -07:00
|
|
|
const brokeredCredentialSourceIDs =
|
|
|
|
|
snapshot?.adapterOptions?.brokeredCredentialSourceIDs;
|
|
|
|
|
const injectedApplicationCredentialSourceIDs =
|
|
|
|
|
snapshot?.adapterOptions?.injectedApplicationCredentialSourceIDs;
|
|
|
|
|
|
|
|
|
|
if (brokeredCredentialSourceIDs)
|
|
|
|
|
serialized = this.serializeWithBrokeredCredentialSources(
|
|
|
|
|
snapshot,
|
2023-11-22 14:07:41 -05:00
|
|
|
brokeredCredentialSourceIDs,
|
2022-09-01 10:07:36 -07:00
|
|
|
);
|
|
|
|
|
if (injectedApplicationCredentialSourceIDs)
|
|
|
|
|
serialized = this.serializeWithInjectedApplicationCredentialSources(
|
2021-07-20 16:09:59 -04:00
|
|
|
snapshot,
|
2023-11-22 14:07:41 -05:00
|
|
|
injectedApplicationCredentialSourceIDs,
|
2021-07-20 16:09:59 -04:00
|
|
|
);
|
2023-03-22 10:52:33 -04:00
|
|
|
|
2024-04-05 16:35:23 -07:00
|
|
|
if (serialized?.with_aliases) {
|
|
|
|
|
// API expects scope id along with every alias value
|
|
|
|
|
serialized.with_aliases = serialized.with_aliases.map((item) => ({
|
|
|
|
|
...item,
|
|
|
|
|
scope_id: 'global',
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 16:03:17 -07:00
|
|
|
// Delete session recording related fields from non-SSH targets
|
|
|
|
|
if (type !== TYPE_TARGET_SSH) {
|
|
|
|
|
delete serialized?.attributes?.storage_bucket_id;
|
|
|
|
|
delete serialized?.attributes?.enable_session_recording;
|
2023-03-22 10:52:33 -04:00
|
|
|
}
|
2020-08-24 18:03:45 -04:00
|
|
|
return serialized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-23 10:04:23 -04:00
|
|
|
* Returns a payload containing only version and an array of passed IDs,
|
|
|
|
|
* rather than existing instances on the model.
|
2020-08-24 18:03:45 -04:00
|
|
|
* @param {Snapshot} snapshot
|
2020-08-26 14:03:43 -04:00
|
|
|
* @param {[string]} hostSetIDs
|
2020-08-24 18:03:45 -04:00
|
|
|
* @return {object}
|
|
|
|
|
*/
|
2021-09-22 08:49:38 -04:00
|
|
|
serializeWithHostSources(snapshot, hostSourceIDs) {
|
2020-08-24 18:03:45 -04:00
|
|
|
return {
|
|
|
|
|
version: snapshot.attr('version'),
|
2021-09-22 19:15:09 -04:00
|
|
|
host_source_ids: hostSourceIDs,
|
2020-08-24 18:03:45 -04:00
|
|
|
};
|
|
|
|
|
}
|
2021-07-20 16:09:59 -04:00
|
|
|
|
|
|
|
|
/**
|
2021-09-23 10:04:23 -04:00
|
|
|
* Returns a payload containing only version and an array of passed IDs,
|
|
|
|
|
* rather than existing instances on the model.
|
2021-07-20 16:09:59 -04:00
|
|
|
* @param {Snapshot} snapshot
|
2022-09-01 10:07:36 -07:00
|
|
|
* @param {[string]} brokered_credential_source_ids
|
|
|
|
|
* @return {object}
|
|
|
|
|
*/
|
|
|
|
|
serializeWithBrokeredCredentialSources(
|
|
|
|
|
snapshot,
|
2023-11-22 14:07:41 -05:00
|
|
|
brokered_credential_source_ids,
|
2022-09-01 10:07:36 -07:00
|
|
|
) {
|
|
|
|
|
return {
|
|
|
|
|
version: snapshot.attr('version'),
|
|
|
|
|
brokered_credential_source_ids,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a payload containing only version and an array of passed IDs,
|
|
|
|
|
* rather than existing instances on the model.
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @param {[string]} injected_application_credential_source_ids
|
2021-07-20 16:09:59 -04:00
|
|
|
* @return {object}
|
|
|
|
|
*/
|
2022-09-01 10:07:36 -07:00
|
|
|
serializeWithInjectedApplicationCredentialSources(
|
|
|
|
|
snapshot,
|
2023-11-22 14:07:41 -05:00
|
|
|
injected_application_credential_source_ids,
|
2022-09-01 10:07:36 -07:00
|
|
|
) {
|
2021-07-20 16:09:59 -04:00
|
|
|
return {
|
|
|
|
|
version: snapshot.attr('version'),
|
2022-09-01 10:07:36 -07:00
|
|
|
injected_application_credential_source_ids,
|
2021-07-20 16:09:59 -04:00
|
|
|
};
|
|
|
|
|
}
|
2023-03-22 10:52:33 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a payload containing only version and storage bucket id,
|
|
|
|
|
* rather than existing instances on the model.
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @returns {object}
|
|
|
|
|
*/
|
|
|
|
|
serializeWithStorageBucket(snapshot) {
|
|
|
|
|
return {
|
|
|
|
|
version: snapshot.attr('version'),
|
|
|
|
|
storage_bucket_id: snapshot?.attr('storage_bucket_id'),
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-04-12 16:36:17 -07:00
|
|
|
|
|
|
|
|
normalize(typeClass, hash, ...rest) {
|
|
|
|
|
const normalizedHash = structuredClone(hash);
|
|
|
|
|
const normalized = super.normalize(typeClass, normalizedHash, ...rest);
|
|
|
|
|
// Ember data retains the previous entry in the array attr when the updated attr becomes undefined/empty.
|
|
|
|
|
// So, we explicitly set the attr to an empty array if the updated attribute is undefined from the API
|
|
|
|
|
|
|
|
|
|
if (!normalized.data.attributes.aliases) {
|
|
|
|
|
normalized.data.attributes.aliases = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return normalized;
|
|
|
|
|
}
|
2020-08-24 18:03:45 -04:00
|
|
|
}
|