2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2018-10-04 04:32:55 +00:00
|
|
|
import { get } from '@ember/object';
|
2019-04-10 22:18:39 +00:00
|
|
|
import { assign } from '@ember/polyfills';
|
2018-10-04 04:32:55 +00:00
|
|
|
import ApplicationSerializer from './application';
|
|
|
|
|
|
|
|
export default ApplicationSerializer.extend({
|
|
|
|
secretDataPath: 'data.data',
|
2018-10-06 03:02:23 +00:00
|
|
|
normalizeItems(payload) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const path = this.secretDataPath;
|
2018-10-04 04:32:55 +00:00
|
|
|
// move response that is the contents of the secret from the dataPath
|
|
|
|
// to `secret_data` so it will be `secretData` in the model
|
|
|
|
payload.secret_data = get(payload, path);
|
2019-04-10 22:18:39 +00:00
|
|
|
payload = assign({}, payload, payload.data.metadata);
|
2018-10-04 04:32:55 +00:00
|
|
|
delete payload.data;
|
2018-10-06 03:02:23 +00:00
|
|
|
payload.path = payload.id;
|
2018-10-04 04:32:55 +00:00
|
|
|
// return the payload if it's expecting a single object or wrap
|
|
|
|
// it as an array if not
|
|
|
|
return payload;
|
|
|
|
},
|
|
|
|
serialize(snapshot) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const secret = snapshot.belongsTo('secret');
|
2019-04-16 20:27:23 +00:00
|
|
|
// if both models failed to read from the server, we need to write without CAS
|
|
|
|
if (secret.record.failedServerRead && snapshot.record.failedServerRead) {
|
|
|
|
return {
|
|
|
|
data: snapshot.attr('secretData'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let version = secret.record.failedServerRead ? snapshot.attr('version') : secret.attr('currentVersion');
|
2018-12-03 14:22:13 +00:00
|
|
|
version = version || 0;
|
2018-10-15 14:40:18 +00:00
|
|
|
return {
|
2018-10-04 04:32:55 +00:00
|
|
|
data: snapshot.attr('secretData'),
|
2018-10-15 14:40:18 +00:00
|
|
|
options: {
|
|
|
|
cas: version,
|
|
|
|
},
|
2018-10-04 04:32:55 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|