2018-10-06 03:02:23 +00:00
|
|
|
/* eslint-disable */
|
2018-10-04 04:32:55 +00:00
|
|
|
import { isEmpty } from '@ember/utils';
|
2018-10-10 04:44:08 +00:00
|
|
|
import { get } from '@ember/object';
|
2018-10-04 04:32:55 +00:00
|
|
|
import ApplicationAdapter from './application';
|
2018-10-10 04:44:08 +00:00
|
|
|
import DS from 'ember-data';
|
2019-03-01 16:08:30 +00:00
|
|
|
import { encodePath } from 'vault/utils/path-encoding-helpers';
|
2018-10-04 04:32:55 +00:00
|
|
|
|
|
|
|
export default ApplicationAdapter.extend({
|
|
|
|
namespace: 'v1',
|
2018-10-06 03:02:23 +00:00
|
|
|
_url(backend, id, infix = 'data') {
|
2019-03-01 16:08:30 +00:00
|
|
|
let url = `${this.buildURL()}/${encodePath(backend)}/${infix}/`;
|
2018-10-04 04:32:55 +00:00
|
|
|
if (!isEmpty(id)) {
|
2019-03-01 16:08:30 +00:00
|
|
|
url = url + encodePath(id);
|
2018-10-04 04:32:55 +00:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
|
|
|
urlForFindRecord(id) {
|
|
|
|
let [backend, path, version] = JSON.parse(id);
|
2018-12-03 14:22:13 +00:00
|
|
|
let base = this._url(backend, path);
|
|
|
|
return version ? base + `?version=${version}` : base;
|
2018-10-04 04:32:55 +00:00
|
|
|
},
|
|
|
|
|
2018-11-12 16:27:06 +00:00
|
|
|
urlForQueryRecord(id) {
|
2018-12-03 14:22:13 +00:00
|
|
|
return this.urlForFindRecord(id);
|
2018-11-12 16:27:06 +00:00
|
|
|
},
|
|
|
|
|
2018-10-10 04:44:08 +00:00
|
|
|
findRecord() {
|
|
|
|
return this._super(...arguments).catch(errorOrModel => {
|
|
|
|
// if it's a real 404, this will be an error, if not
|
|
|
|
// it will be the body of a deleted / destroyed version
|
|
|
|
if (errorOrModel instanceof DS.AdapterError) {
|
|
|
|
throw errorOrModel;
|
|
|
|
}
|
|
|
|
return errorOrModel;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-11-12 16:27:06 +00:00
|
|
|
queryRecord(id, options) {
|
|
|
|
return this.ajax(this.urlForQueryRecord(id), 'GET', options).then(resp => {
|
|
|
|
if (options.wrapTTL) {
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
resp.id = id;
|
|
|
|
resp.backend = backend;
|
|
|
|
return resp;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-10-06 03:02:23 +00:00
|
|
|
urlForCreateRecord(modelName, snapshot) {
|
|
|
|
let backend = snapshot.belongsTo('secret').belongsTo('engine').id;
|
|
|
|
let path = snapshot.attr('path');
|
|
|
|
return this._url(backend, path);
|
|
|
|
},
|
|
|
|
|
|
|
|
createRecord(store, modelName, snapshot) {
|
|
|
|
let backend = snapshot.belongsTo('secret').belongsTo('engine').id;
|
|
|
|
let path = snapshot.attr('path');
|
|
|
|
return this._super(...arguments).then(resp => {
|
|
|
|
resp.id = JSON.stringify([backend, path, resp.version]);
|
|
|
|
return resp;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
urlForUpdateRecord(id) {
|
|
|
|
let [backend, path] = JSON.parse(id);
|
|
|
|
return this._url(backend, path);
|
|
|
|
},
|
|
|
|
|
2018-10-10 04:44:08 +00:00
|
|
|
v2DeleteOperation(store, id, deleteType = 'delete') {
|
|
|
|
let [backend, path, version] = JSON.parse(id);
|
2018-10-06 03:02:23 +00:00
|
|
|
|
2018-10-10 04:44:08 +00:00
|
|
|
// deleteType should be 'delete', 'destroy', 'undelete'
|
|
|
|
return this.ajax(this._url(backend, path, deleteType), 'POST', { data: { versions: [version] } }).then(
|
|
|
|
() => {
|
|
|
|
let model = store.peekRecord('secret-v2-version', id);
|
2019-04-16 20:27:23 +00:00
|
|
|
return model && model.rollbackAttributes() && model.reload();
|
2018-10-10 04:44:08 +00:00
|
|
|
}
|
|
|
|
);
|
2018-10-06 03:02:23 +00:00
|
|
|
},
|
|
|
|
|
2018-10-10 04:44:08 +00:00
|
|
|
handleResponse(status, headers, payload, requestData) {
|
2018-10-06 03:02:23 +00:00
|
|
|
// the body of the 404 will have some relevant information
|
2018-10-10 04:44:08 +00:00
|
|
|
if (status === 404 && get(payload, 'data.metadata')) {
|
|
|
|
return this._super(200, headers, payload, requestData);
|
|
|
|
}
|
2018-10-04 04:32:55 +00:00
|
|
|
return this._super(...arguments);
|
|
|
|
},
|
|
|
|
});
|