refactor: 💡 remove route-action usage from credential-stores (#2172)

* refactor: 💡 remove route-action usage from credential-stores

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12604
This commit is contained in:
Lisbet Alvarez authored and GitHub committed 2024-03-18 16:34:37 -07:00
1 parent 879c196887
commit d93986c4e2
32 files changed
+412 -250

No files matched your search

@@ -30,7 +30,7 @@
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'delete' @model)}}
{{on 'click' @delete}}
>
{{t 'resources.credential-store.actions.delete'}}
</dropdown.button>
@@ -0,0 +1,8 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrariesCredentialLibraryIndexController extends Controller {
@controller(
'scopes/scope/credential-stores/credential-store/credential-libraries/index',
)
credentialLibraries;
}
@@ -0,0 +1,99 @@
import Controller, { inject as controller } from '@ember/controller';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { inject as service } from '@ember/service';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrariesIndexController extends Controller {
@controller('scopes/scope/credential-stores/index') credentialStores;
// =services
@service can;
@service router;
// =actions
/**
* Rollback changes on a credential library.
* @param {CredentialLibraryModel} credentialLibrary
*/
@action
async cancel(credentialLibrary) {
const { isNew } = credentialLibrary;
credentialLibrary.rollbackAttributes();
if (isNew)
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credential-libraries',
);
}
/**
* Handle save of a credential library
* @param {CredentialLibraryModel} credentialLibrary
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(credentialLibrary) {
await credentialLibrary.save();
if (this.can.can('read model', credentialLibrary)) {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credential-libraries.credential-library',
credentialLibrary,
);
} else {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credential-libraries',
);
}
await this.router.refresh();
}
/**
* Handle delete of a credential library
* @param {CredentialLibraryModel} credentialLibrary
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(credentialLibrary) {
await credentialLibrary.destroyRecord();
await this.router.replaceWith(
'scopes.scope.credential-stores.credential-store.credential-libraries',
);
await this.router.refresh();
}
/**
* Copies the contents of array fields in order to force the instance
* into a dirty state. This ensures that `model.rollbackAttributes()` reverts
* to the original expected array.
*
* The deep copy implemented here is required to ensure that both the
* array itself and its members are all new.
*
* @param {credentialLibraryModel} credentialLibrary
*/
@action
edit(credentialLibrary) {
const { critical_options, extensions } = credentialLibrary;
credentialLibrary.critical_options = structuredClone(critical_options);
credentialLibrary.extensions = structuredClone(extensions);
}
/**
* Update type of credential library
* @param {string} type
*/
@action
async changeType(type) {
await this.router.replaceWith({ queryParams: { type } });
}
}
@@ -3,10 +3,13 @@
* SPDX-License-Identifier: MPL-2.0
*/
import Controller from '@ember/controller';
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrariesNewController extends Controller {
// =services
@controller(
'scopes/scope/credential-stores/credential-store/credential-libraries/index',
)
credentialLibraries;
// =attributes
queryParams = ['type'];
@@ -0,0 +1,8 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialsCredentialIndexController extends Controller {
@controller(
'scopes/scope/credential-stores/credential-store/credentials/index',
)
credentials;
}
@@ -0,0 +1,81 @@
import Controller, { inject as controller } from '@ember/controller';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { inject as service } from '@ember/service';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialsIndexController extends Controller {
@controller('scopes/scope/credential-stores/index') credentialStores;
// =services
@service can;
@service router;
// =actions
/**
* Rollback changes on a credential.
* @param {CredentialModel} credential
*/
@action
async cancel(credential) {
const { isNew } = credential;
credential.rollbackAttributes();
if (isNew)
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credentials',
);
}
/**
* Handle save of a credential
* @param {CredentialModel} credential
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(credential) {
await credential.save();
if (this.can.can('read credential', credential)) {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credentials.credential',
credential,
);
} else {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credentials',
);
}
await this.router.refresh();
}
/**
* Handle delete of a credential
* @param {Credential} credential
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(credential) {
await credential.destroyRecord();
await this.router.replaceWith(
'scopes.scope.credential-stores.credential-store.credentials',
);
await this.router.refresh();
}
/**
* Update type of credential
* @param {string} type
*/
@action
async changeType(type) {
await this.router.replaceWith({ queryParams: { type } });
}
}
@@ -3,10 +3,13 @@
* SPDX-License-Identifier: MPL-2.0
*/
import Controller from '@ember/controller';
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialsNewController extends Controller {
// =services
@controller(
'scopes/scope/credential-stores/credential-store/credentials/index',
)
credentials;
// =attributes
@@ -0,0 +1,5 @@
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeCredentialStoresCredentialStoreIndexController extends Controller {
@controller('scopes/scope/credential-stores/index') credentialStores;
}
@@ -0,0 +1,73 @@
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeCredentialStoresIndexController extends Controller {
// =services
@service can;
@service router;
// =actions
/**
* Handle save
* @param {CredentialStoreModel} credentialStore
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess('notifications.save-success')
async save(credentialStore) {
await credentialStore.save();
if (this.can.can('read model', credentialStore)) {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store',
credentialStore,
);
} else {
await this.router.transitionTo('scopes.scope.credential-stores');
}
await this.router.refresh();
}
/**
* Rollback changes on credential stores.
* @param {CredentialStoreModel} credentialStore
*/
@action
async cancel(credentialStore) {
const { isNew } = credentialStore;
credentialStore.rollbackAttributes();
if (isNew) {
await this.router.transitionTo('scopes.scope.credential-stores');
}
}
/**
* Deletes the credential store and redirects to index
* @param {CredentialStoreModel} credentialStore
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(credentialStore) {
await credentialStore.destroyRecord();
await this.router.replaceWith('scopes.scope.credential-stores');
await this.router.refresh();
}
/**
* Update type of credential store
* @param {string} type
*/
@action
async changeType(type) {
await this.router.replaceWith({ queryParams: { type } });
}
}
@@ -3,10 +3,10 @@
* SPDX-License-Identifier: MPL-2.0
*/
import Controller from '@ember/controller';
import Controller, { inject as controller } from '@ember/controller';
export default class ScopesScopeCredentialStoresNewController extends Controller {
// =services
@controller('scopes/scope/credential-stores/index') credentialStores;
// =attributes
queryParams = ['type'];
@@ -5,17 +5,12 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeCredentialStoresRoute extends Route {
// =services
@service store;
@service can;
@service router;
// =methods
@@ -34,49 +29,4 @@ export default class ScopesScopeCredentialStoresRoute extends Route {
return this.store.query('credential-store', { scope_id });
}
}
// =actions
/**
* Handle save
* @param {CredentialStoreModel} credentialStore
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess('notifications.save-success')
async save(credentialStore) {
await credentialStore.save();
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store',
credentialStore,
);
this.refresh();
}
/**
* Rollback changes on credential stores.
* @param {CredentialStoreModel} credentialStore
*/
@action
cancel(credentialStore) {
const { isNew } = credentialStore;
credentialStore.rollbackAttributes();
if (isNew) this.router.transitionTo('scopes.scope.credential-stores');
}
/**
* Deletes the credential store and redirects to index
* @param {CredentialStoreModel} credentialStore
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(credentialStore) {
await credentialStore.destroyRecord();
await this.router.replaceWith('scopes.scope.credential-stores');
this.refresh();
}
}
@@ -4,10 +4,6 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { inject as service } from '@ember/service';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrariesRoute extends Route {
@@ -15,7 +11,6 @@ export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrari
@service store;
@service can;
@service router;
// =methods
@@ -36,62 +31,4 @@ export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrari
return this.store.query('credential-library', { credential_store_id });
}
}
// =actions
/**
* Rollback changes on a credential library.
* @param {CredentialLibraryModel} credentialLibrary
*/
@action
cancel(credentialLibrary) {
const { isNew } = credentialLibrary;
credentialLibrary.rollbackAttributes();
if (isNew)
this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credential-libraries',
);
}
/**
* Handle save of a credential library
* @param {CredentialLibraryModel} credentialLibrary
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(credentialLibrary) {
await credentialLibrary.save();
if (this.can.can('read model', credentialLibrary)) {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credential-libraries.credential-library',
credentialLibrary,
);
} else {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credential-libraries',
);
}
this.refresh();
}
/**
* Handle delete of a credential library
* @param {CredentialLibraryModel} credentialLibrary
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async deleteCredentialLibrary(credentialLibrary) {
await credentialLibrary.destroyRecord();
await this.router.replaceWith(
'scopes.scope.credential-stores.credential-store.credential-libraries',
);
this.refresh();
}
}
@@ -5,13 +5,11 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrariesCredentialLibraryRoute extends Route {
// =services
@service store;
@service can;
@service router;
// =methods
@@ -45,21 +43,4 @@ export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrari
);
}
}
/**
* Copies the contents of array fields in order to force the instance
* into a dirty state. This ensures that `model.rollbackAttributes()` reverts
* to the original expected array.
*
* The deep copy implemented here is required to ensure that both the
* array itself and its members are all new.
*
* @param {credentialLibraryModel} credentialLibrary
*/
@action
edit(credentialLibrary) {
const { critical_options, extensions } = credentialLibrary;
credentialLibrary.critical_options = structuredClone(critical_options);
credentialLibrary.extensions = structuredClone(extensions);
}
}
@@ -4,7 +4,6 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { TYPE_CREDENTIAL_LIBRARY_VAULT_GENERIC } from 'api/models/credential-library';
@@ -64,13 +63,4 @@ export default class ScopesScopeCredentialStoresCredentialStoreCredentialLibrari
credential_store_id,
});
}
/**
* Update type of credential library
* @param {string} type
*/
@action
changeType(type) {
this.router.replaceWith({ queryParams: { type } });
}
}
@@ -4,10 +4,6 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
import { inject as service } from '@ember/service';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialsRoute extends Route {
@@ -15,7 +11,6 @@ export default class ScopesScopeCredentialStoresCredentialStoreCredentialsRoute
@service store;
@service can;
@service router;
// =methods
@@ -36,61 +31,4 @@ export default class ScopesScopeCredentialStoresCredentialStoreCredentialsRoute
return this.store.query('credential', { credential_store_id });
}
}
// =actions
/**
* Rollback changes on a credential.
* @param {CredentialModel} credential
*/
@action
cancel(credential) {
const { isNew } = credential;
credential.rollbackAttributes();
if (isNew)
this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credentials',
);
}
/**
* Handle save of a credential
* @param {CredentialModel} credential
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(credential) {
await credential.save();
if (this.can.can('read model', credential)) {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credentials.credential',
credential,
);
} else {
await this.router.transitionTo(
'scopes.scope.credential-stores.credential-store.credentials',
);
}
this.refresh();
}
/**
* Handle delete of a credential
* @param {Credential} credential
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async deleteCredential(credential) {
await credential.destroyRecord();
await this.router.replaceWith(
'scopes.scope.credential-stores.credential-store.credentials',
);
this.refresh();
}
}
@@ -4,7 +4,6 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class ScopesScopeCredentialStoresCredentialStoreCredentialsNewRoute extends Route {
@@ -77,13 +76,4 @@ export default class ScopesScopeCredentialStoresCredentialStoreCredentialsNewRou
...typeSpecificAttrs,
});
}
/**
* Update type of credential
* @param {string} type
*/
@action
changeType(type) {
this.router.replaceWith({ queryParams: { type } });
}
}
@@ -4,7 +4,6 @@
*/
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
export default class ScopesScopeCredentialStoresNewRoute extends Route {
@@ -63,13 +62,4 @@ export default class ScopesScopeCredentialStoresNewRoute extends Route {
record.scopeModel = scopeModel;
return record;
}
/**
* Update type of credential store
* @param {string} type
*/
@action
changeType(type) {
this.router.replaceWith({ queryParams: { type } });
}
}
@@ -38,7 +38,7 @@
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'deleteCredentialLibrary' @model)}}
{{on 'click' (fn this.credentialLibraries.delete @model)}}
>
{{t 'resources.credential-library.actions.delete'}}
</dropdown.button>
@@ -49,9 +49,9 @@
<page.body>
<Form::CredentialLibrary
@model={{@model}}
@edit={{route-action 'edit' @model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@edit={{fn this.credentialLibraries.edit @model}}
@submit={{fn this.credentialLibraries.save @model}}
@cancel={{fn this.credentialLibraries.cancel @model}}
/>
</page.body>
</Rose::Layout::Page>
@@ -22,6 +22,7 @@
<page.actions>
<CredentialStores::CredentialStore::Actions
@model={{this.credentialStore}}
@delete={{fn this.credentialStores.delete this.credentialStore}}
/>
</page.actions>
@@ -26,9 +26,9 @@
<page.body>
<Form::CredentialLibrary
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@changeType={{route-action 'changeType'}}
@submit={{fn this.credentialLibraries.save @model}}
@cancel={{fn this.credentialLibraries.cancel @model}}
@changeType={{this.credentialLibraries.changeType}}
/>
</page.body>
</Rose::Layout::Page>
@@ -38,7 +38,7 @@
<dropdown.button
@style='danger'
@disabled={{@model.canSave}}
{{on 'click' (route-action 'deleteCredential' @model)}}
{{on 'click' (fn this.credentials.delete @model)}}
>
{{t 'resources.credential.actions.delete'}}
</dropdown.button>
@@ -49,8 +49,8 @@
<page.body>
<Form::Credential
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@submit={{fn this.credentials.save @model}}
@cancel={{fn this.credentials.cancel @model}}
/>
</page.body>
</Rose::Layout::Page>
@@ -22,6 +22,7 @@
<page.actions>
<CredentialStores::CredentialStore::Actions
@model={{this.credentialStore}}
@delete={{fn this.credentialStores.delete this.credentialStore}}
/>
</page.actions>
@@ -26,9 +26,9 @@
<page.body>
<Form::Credential
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@changeType={{route-action 'changeType'}}
@submit={{fn this.credentials.save @model}}
@cancel={{fn this.credentials.cancel @model}}
@changeType={{this.credentials.changeType}}
/>
</page.body>
</Rose::Layout::Page>
@@ -18,14 +18,17 @@
</page.navigation>
<page.actions>
<CredentialStores::CredentialStore::Actions @model={{@model}} />
<CredentialStores::CredentialStore::Actions
@model={{@model}}
@delete={{fn this.credentialStores.delete @model}}
/>
</page.actions>
<page.body>
<Form::CredentialStore
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@submit={{fn this.credentialStores.save @model}}
@cancel={{fn this.credentialStores.cancel @model}}
/>
</page.body>
</Rose::Layout::Page>
@@ -25,9 +25,9 @@
<page.body>
<Form::CredentialStore
@model={{@model}}
@submit={{route-action 'save' @model}}
@cancel={{route-action 'cancel' @model}}
@changeType={{route-action 'changeType'}}
@submit={{fn this.credentialStores.save @model}}
@cancel={{fn this.credentialStores.cancel @model}}
@changeType={{this.credentialStores.changeType}}
/>
</page.body>
@@ -23,6 +23,8 @@ module(
setupApplicationTest(hooks);
setupMirage(hooks);
let featuresService;
const instances = {
scopes: {
global: null,
@@ -83,6 +85,8 @@ module(
urls.usernamePasswordCredential = `${urls.credentials}/${instances.usernamePasswordCredential.id}`;
urls.usernameKeyPairCredential = `${urls.credentials}/${instances.usernameKeyPairCredential.id}`;
urls.jsonCredential = `${urls.credentials}/${instances.jsonCredential.id}`;
featuresService = this.owner.lookup('service:features');
authenticateSession({});
});
@@ -117,6 +121,7 @@ module(
});
test('can save changes to existing JSON credential', async function (assert) {
featuresService.enable('json-credentials');
const mockInput = 'random string';
assert.notEqual(instances.jsonCredential.name, mockInput);
await visit(urls.jsonCredential);
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module(
'Unit | Controller | scopes/scope/credential-stores/credential-store/credential-libraries/credential-library/index',
function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/credential-stores/credential-store/credential-libraries/credential-library/index',
);
assert.ok(controller);
});
},
);
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module(
'Unit | Controller | scopes/scope/credential-stores/credential-store/credential-libraries/index',
function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/credential-stores/credential-store/credential-libraries/index',
);
assert.ok(controller);
});
},
);
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module(
'Unit | Controller | scopes/scope/credential-stores/credential-store/credentials/credential/index',
function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/credential-stores/credential-store/credentials/credential/index',
);
assert.ok(controller);
});
},
);
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module(
'Unit | Controller | scopes/scope/credential-stores/credential-store/credentials/index',
function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/credential-stores/credential-store/credentials/index',
);
assert.ok(controller);
});
},
);
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module(
'Unit | Controller | scopes/scope/credential-stores/credential-store/index',
function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/credential-stores/credential-store/index',
);
assert.ok(controller);
});
},
);
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module(
'Unit | Controller | scopes/scope/credential-stores/index',
function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let controller = this.owner.lookup(
'controller:scopes/scope/credential-stores/index',
);
assert.ok(controller);
});
},
);