From 8be4e93a3c9a23067d937a651b059e5536df8ffa Mon Sep 17 00:00:00 2001
From: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com>
Date: Thu, 28 Mar 2024 09:29:08 -0700
Subject: [PATCH] Icu 12590 add filtering pagination support for roles (#2204)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat: 🎸 add search & pagination for roles
✅ Closes: https://hashicorp.atlassian.net/browse/ICU-12590
---
addons/api/addon/services/indexed-db.js | 1 +
addons/api/mirage/config.js | 4 +-
.../controllers/scopes/scope/roles/index.js | 33 ++++
ui/admin/app/routes/scopes/scope/roles.js | 17 +-
.../app/routes/scopes/scope/roles/index.js | 75 ++++++++
.../app/routes/scopes/scope/targets/index.js | 5 +-
.../app/routes/scopes/scope/users/index.js | 22 ++-
.../templates/scopes/scope/roles/index.hbs | 162 ++++++++++--------
.../templates/scopes/scope/targets/index.hbs | 1 +
.../templates/scopes/scope/users/index.hbs | 2 +
.../tests/acceptance/roles/create-test.js | 2 +
ui/admin/tests/acceptance/roles/list-test.js | 89 ++++++++--
ui/admin/tests/acceptance/roles/read-test.js | 10 +-
.../scopes/scope/roles/index-test.js | 22 +++
14 files changed, 328 insertions(+), 117 deletions(-)
create mode 100644 ui/admin/app/controllers/scopes/scope/roles/index.js
create mode 100644 ui/admin/tests/unit/controllers/scopes/scope/roles/index-test.js
diff --git a/addons/api/addon/services/indexed-db.js b/addons/api/addon/services/indexed-db.js
index 2040fe7fc..b56c43ca0 100644
--- a/addons/api/addon/services/indexed-db.js
+++ b/addons/api/addon/services/indexed-db.js
@@ -13,6 +13,7 @@ export const modelIndexes = {
session:
'&id, attributes.created_time, attributes.type, attributes.status, attributes.endpoint, attributes.target_id, attributes.user_id, attributes.scope.scope_id',
user: '&id, attributes.created_time, attributes.name, attributes.description, attributes.scope.scope_id',
+ role: '&id, attributes.created_time, attributes.name, attributes.description, attributes.scope.scope_id',
};
export const formatDbName = (userId, clusterUrl) =>
diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js
index 9121f0d0f..227948431 100644
--- a/addons/api/mirage/config.js
+++ b/addons/api/mirage/config.js
@@ -324,9 +324,7 @@ function routes() {
);
// IAM: Roles
- this.get('/roles', ({ roles }, { queryParams: { scope_id: scopeId } }) => {
- return roles.where({ scopeId });
- });
+ this.get('/roles');
this.post('/roles');
this.get('/roles/:id');
this.patch('/roles/:id');
diff --git a/ui/admin/app/controllers/scopes/scope/roles/index.js b/ui/admin/app/controllers/scopes/scope/roles/index.js
new file mode 100644
index 000000000..4af9b3428
--- /dev/null
+++ b/ui/admin/app/controllers/scopes/scope/roles/index.js
@@ -0,0 +1,33 @@
+import Controller from '@ember/controller';
+import { inject as service } from '@ember/service';
+import { action } from '@ember/object';
+import { tracked } from '@glimmer/tracking';
+import { debounce } from 'core/decorators/debounce';
+
+export default class ScopesScopeRolesIndexController extends Controller {
+ // =services
+
+ @service intl;
+
+ // =attributes
+
+ queryParams = ['search', 'page', 'pageSize'];
+
+ @tracked search = '';
+ @tracked page = 1;
+ @tracked pageSize = 10;
+
+ // =methods
+
+ /**
+ * Handles input on each keystroke and the search queryParam
+ * @param {object} event
+ */
+ @action
+ @debounce(250)
+ handleSearchInput(event) {
+ const { value } = event.target;
+ this.search = value;
+ this.page = 1;
+ }
+}
diff --git a/ui/admin/app/routes/scopes/scope/roles.js b/ui/admin/app/routes/scopes/scope/roles.js
index bb21ac163..4801d1410 100644
--- a/ui/admin/app/routes/scopes/scope/roles.js
+++ b/ui/admin/app/routes/scopes/scope/roles.js
@@ -13,8 +13,6 @@ import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeRolesRoute extends Route {
// =services
- @service store;
- @service intl;
@service session;
@service can;
@service router;
@@ -28,19 +26,8 @@ export default class ScopesScopeRolesRoute extends Route {
if (!this.session.isAuthenticated) this.router.transitionTo('index');
}
- /**
- * Load all roles under current scope.
- * @return {Promise{[RoleModel]}}
- */
- async model() {
- const scope = this.modelFor('scopes.scope');
- const { id: scope_id } = scope;
- if (this.can.can('list model', scope, { collection: 'roles' })) {
- return this.store.query('role', { scope_id });
- }
- }
-
// =actions
+
/**
* Rollback changes on an role.
* @param {RoleModel} role
@@ -83,7 +70,7 @@ export default class ScopesScopeRolesRoute extends Route {
@notifySuccess('notifications.delete-success')
async delete(role) {
await role.destroyRecord();
- await this.router.replaceWith('scopes.scope.roles');
+ this.router.replaceWith('scopes.scope.roles');
this.refresh();
}
}
diff --git a/ui/admin/app/routes/scopes/scope/roles/index.js b/ui/admin/app/routes/scopes/scope/roles/index.js
index 2992c0a6e..62bf916fe 100644
--- a/ui/admin/app/routes/scopes/scope/roles/index.js
+++ b/ui/admin/app/routes/scopes/scope/roles/index.js
@@ -4,9 +4,84 @@
*/
import Route from '@ember/routing/route';
+import { inject as service } from '@ember/service';
+
export default class ScopesScopeRolesIndexRoute extends Route {
+ // =attributes
+
+ queryParams = {
+ search: {
+ refreshModel: true,
+ replace: true,
+ },
+ page: {
+ refreshModel: true,
+ },
+ pageSize: {
+ refreshModel: true,
+ },
+ };
+
+ // =services
+
+ @service can;
+ @service store;
+
// =methods
+ /**
+ * Loads queried roles and the number of roles under current scope.
+ * @returns {Promise<{totalItems: number, users: [RoleModel], rolesExist: boolean }> }
+ */
+ async model({ search, page, pageSize }) {
+ const scope = this.modelFor('scopes.scope');
+ const { id: scope_id } = scope;
+ const filters = {
+ scope_id: [{ equals: scope_id }],
+ };
+
+ let roles;
+ let totalItems = 0;
+ if (this.can.can('list model', scope, { collection: 'roles' })) {
+ roles = await this.store.query('role', {
+ query: { search, filters },
+ page,
+ pageSize,
+ });
+ totalItems = roles.meta?.totalItems;
+ }
+ const rolesExist = await this.getRolesExist(scope_id, totalItems);
+
+ return { roles, rolesExist, totalItems };
+ }
+
+ /**
+ * Sets rolesExist to true if there exists any roles.
+ * @param {string} scope_id
+ * @param {number} totalItems
+ * @returns
+ */
+ async getRolesExist(scope_id, totalItems) {
+ if (totalItems > 0) {
+ return true;
+ }
+ const options = { pushToStore: false };
+ const role = await this.store.query(
+ 'role',
+ {
+ query: {
+ filters: {
+ scope_id: [{ equals: scope_id }],
+ },
+ },
+ page: 1,
+ pageSize: 1,
+ },
+ options,
+ );
+ return role.length > 0;
+ }
+
setupController(controller) {
const scope = this.modelFor('scopes.scope');
super.setupController(...arguments);
diff --git a/ui/admin/app/routes/scopes/scope/targets/index.js b/ui/admin/app/routes/scopes/scope/targets/index.js
index 9a8006f40..40e729861 100644
--- a/ui/admin/app/routes/scopes/scope/targets/index.js
+++ b/ui/admin/app/routes/scopes/scope/targets/index.js
@@ -43,8 +43,9 @@ export default class ScopesScopeTargetsIndexRoute extends Route {
// =methods
/**
- * Loads all targets under current scope.
- * @return {Promise{[TargetModel]}}
+ * Loads queried targets, the number of targets under current scope, and
+ * active sessions filtering options.
+ * @returns {Promise<{totalItems: number, targets: [TargetModel], allTargets: [TargetModel] }> }
*/
async model({ search, availableSessions, types, page, pageSize }) {
const scope = this.modelFor('scopes.scope');
diff --git a/ui/admin/app/routes/scopes/scope/users/index.js b/ui/admin/app/routes/scopes/scope/users/index.js
index e03323622..f41ad10b8 100644
--- a/ui/admin/app/routes/scopes/scope/users/index.js
+++ b/ui/admin/app/routes/scopes/scope/users/index.js
@@ -30,8 +30,8 @@ export default class ScopesScopeUsersIndexRoute extends Route {
// =methods
/**
- * Load all users under current scope.
- * @return {Promise{[UserModel]}}
+ * Loads queried users and the number of users under current scope.
+ * @returns {Promise<{totalItems: number, users: [UserModel], usersExist: boolean }> }
*/
async model({ search, page, pageSize }) {
const scope = this.modelFor('scopes.scope');
@@ -41,8 +41,6 @@ export default class ScopesScopeUsersIndexRoute extends Route {
scope_id: [{ equals: scope_id }],
};
- await this.getUsersExist(scope_id);
-
let users;
let totalItems = 0;
if (this.can.can('list model', scope, { collection: 'users' })) {
@@ -53,11 +51,21 @@ export default class ScopesScopeUsersIndexRoute extends Route {
});
totalItems = users.meta?.totalItems;
}
+ const usersExist = await this.getUsersExist(scope_id, totalItems);
- return { users, usersExist: this.usersExist, totalItems };
+ return { users, usersExist, totalItems };
}
- async getUsersExist(scopeId) {
+ /**
+ * Sets usersExist to true if there exists any users.
+ * @param {string} scopeId
+ * @param {number} totalItems
+ * @returns
+ */
+ async getUsersExist(scopeId, totalItems) {
+ if (totalItems > 0) {
+ return true;
+ }
const options = { pushToStore: false };
const user = await this.store.query(
'user',
@@ -72,7 +80,7 @@ export default class ScopesScopeUsersIndexRoute extends Route {
},
options,
);
- this.usersExist = user.length > 0;
+ return user.length > 0;
}
setupController(controller) {
diff --git a/ui/admin/app/templates/scopes/scope/roles/index.hbs b/ui/admin/app/templates/scopes/scope/roles/index.hbs
index b2b85ec67..2940994a9 100644
--- a/ui/admin/app/templates/scopes/scope/roles/index.hbs
+++ b/ui/admin/app/templates/scopes/scope/roles/index.hbs
@@ -14,89 +14,111 @@
{{t 'resources.role.title_plural'}}
{{t 'resources.role.description'}}
{{/if}}{{B.data.description}}
-{{B.data.description}}
+