-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsecret-v2-version.js
41 lines (39 loc) · 1.3 KB
/
secret-v2-version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { get } from '@ember/object';
import { assign } from '@ember/polyfills';
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
secretDataPath: 'data.data',
normalizeItems(payload) {
const path = this.secretDataPath;
// 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);
payload = assign({}, payload, payload.data.metadata);
delete payload.data;
payload.path = payload.id;
// return the payload if it's expecting a single object or wrap
// it as an array if not
return payload;
},
serialize(snapshot) {
const secret = snapshot.belongsTo('secret');
// 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');
version = version || 0;
return {
data: snapshot.attr('secretData'),
options: {
cas: version,
},
};
},
});