open-vault/ui/app/adapters/secret-v2-version.js
Jordan Reimer 5c2a08de6d
Ember Upgrade to 3.24 (#13443)
* Update browserslist

* Add browserslistrc

* ember-cli-update --to 3.26, fix conflicts

* Run codemodes that start with ember-*

* More codemods - before cp*

* More codemods (curly data-test-*)

* WIP ember-basic-dropdown template errors

* updates ember-basic-dropdown and related deps to fix build issues

* updates basic dropdown instances to new version API

* updates more deps -- ember-template-lint is working again

* runs no-implicit-this codemod

* creates and runs no-quoteless-attributes codemod

* runs angle brackets codemod

* updates lint:hbs globs to only touch hbs files

* removes yield only templates

* creates and runs deprecated args transform

* supresses lint error for invokeAction on LinkTo component

* resolves remaining ambiguous path lint errors

* resolves simple-unless lint errors

* adds warnings for deprecated tagName arg on LinkTo components

* adds warnings for remaining curly component invocation

* updates global template lint rules

* resolves remaining template lint errors

* disables some ember specfic lint rules that target pre octane patterns

* js lint fix run

* resolves remaining js lint errors

* fixes test run

* adds npm-run-all dep

* fixes test attribute issues

* fixes console acceptance tests

* fixes tests

* adds yield only wizard/tutorial-active template

* fixes more tests

* attempts to fix more flaky tests

* removes commented out settled in transit test

* updates deprecations workflow and adds initializer to filter by version

* updates flaky policies acl old test

* updates to flaky transit test

* bumps ember deps down to LTS version

* runs linters after main merge

* fixes client count tests after bad merge conflict fixes

* fixes client count history test

* more updates to lint config

* another round of hbs lint fixes after extending stylistic rule

* updates lint-staged commands

* removes indent eslint rule since it seems to break things

* fixes bad attribute in transform-edit-form template

* test fixes

* fixes enterprise tests

* adds changelog

* removes deprecated ember-concurrency-test-waiters dep and adds @ember/test-waiters

* flaky test fix

Co-authored-by: hashishaw <cshaw@hashicorp.com>
2021-12-16 20:44:29 -07:00

164 lines
5.1 KiB
JavaScript

/* eslint-disable */
import AdapterError from '@ember-data/adapter/error';
import { isEmpty } from '@ember/utils';
import { get } from '@ember/object';
import ApplicationAdapter from './application';
import { encodePath } from 'vault/utils/path-encoding-helpers';
export default ApplicationAdapter.extend({
namespace: 'v1',
_url(backend, id, infix = 'data') {
let url = `${this.buildURL()}/${encodePath(backend)}/${infix}/`;
if (!isEmpty(id)) {
url = url + encodePath(id);
}
return url;
},
urlForFindRecord(id) {
let [backend, path, version] = JSON.parse(id);
let base = this._url(backend, path);
return version ? base + `?version=${version}` : base;
},
urlForQueryRecord(id) {
return this.urlForFindRecord(id);
},
findRecord() {
return this._super(...arguments).catch((errorOrModel) => {
// if the response is a real 404 or if the secret is gated by a control group this will be an error,
// otherwise the response will be the body of a deleted / destroyed version
if (errorOrModel instanceof AdapterError) {
throw errorOrModel;
}
return errorOrModel;
});
},
async getSecretDataVersion(backend, id) {
// used in secret-edit route when you don't have current version and you need it for pulling the correct secret-v2-version record
let url = this._url(backend, id);
let response = await this.ajax(this._url(backend, id), 'GET');
return response.data.metadata.version;
},
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;
});
},
querySecretDataByVersion(id) {
return this.ajax(this.urlForQueryRecord(id), 'GET')
.then((resp) => {
return resp.data;
})
.catch((error) => {
return error.data;
});
},
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);
},
async deleteLatestVersion(backend, path) {
try {
await this.ajax(this._url(backend, path, 'data'), 'DELETE');
let model = this.store.peekRecord('secret-v2-version', path);
await model.reload();
return model && model.rollbackAttributes();
} catch (e) {
return e;
}
},
async undeleteVersion(backend, path, currentVersionForNoReadMetadata) {
try {
await this.ajax(this._url(backend, path, 'undelete'), 'POST', {
data: { versions: [currentVersionForNoReadMetadata] },
});
let model = this.store.peekRecord('secret-v2-version', path);
await model.reload();
return model && model.rollbackAttributes();
} catch (e) {
return e;
}
},
async softDelete(backend, path, version) {
try {
await this.ajax(this._url(backend, path, 'delete'), 'POST', {
data: { versions: [version] },
});
let model = this.store.peekRecord('secret-v2-version', path);
await model.reload();
return model && model.rollbackAttributes();
} catch (e) {
return e;
}
},
async deleteByDeleteType(backend, path, deleteType, version) {
try {
await this.ajax(this._url(backend, path, deleteType), 'POST', {
data: { versions: [version] },
});
let model = this.store.peekRecord('secret-v2-version', path);
await model.reload();
return model && model.rollbackAttributes();
} catch (e) {
return e;
}
},
v2DeleteOperation(store, id, deleteType = 'delete', currentVersionForNoReadMetadata) {
let [backend, path, version] = JSON.parse(id);
// deleteType should be 'delete', 'destroy', 'undelete', 'delete-latest-version', 'destroy-version'
if (
(currentVersionForNoReadMetadata && deleteType === 'delete') ||
deleteType === 'delete-latest-version'
) {
// moved to async to away model reload which is a promise
return this.deleteLatestVersion(backend, path);
} else if (deleteType === 'undelete' && !version) {
// happens when no read access to metadata
return this.undeleteVersion(backend, path, currentVersionForNoReadMetadata);
} else if (deleteType === 'soft-delete') {
return this.softDelete(backend, path, version);
} else {
return this.deleteByDeleteType(backend, path, deleteType, version);
}
},
handleResponse(status, headers, payload, requestData) {
// the body of the 404 will have some relevant information
if (status === 404 && get(payload, 'data.metadata')) {
return this._super(200, headers, payload, requestData);
}
return this._super(...arguments);
},
});