From 766a0887497b001ff9477fd9a1ba00062fb2312d Mon Sep 17 00:00:00 2001 From: gconsidine Date: Tue, 10 Oct 2017 10:54:05 -0400 Subject: [PATCH] Use credential_type to fetch associated types in list view --- .../credentials/legacy.credentials.js | 5 +--- awx/ui/client/lib/models/Base.js | 17 +++++++---- .../list/credentials-list.controller.js | 29 +++++++++++++++---- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/awx/ui/client/features/credentials/legacy.credentials.js b/awx/ui/client/features/credentials/legacy.credentials.js index e271da0936..4c2e2d1d81 100644 --- a/awx/ui/client/features/credentials/legacy.credentials.js +++ b/awx/ui/client/features/credentials/legacy.credentials.js @@ -41,10 +41,7 @@ function LegacyCredentialsService (pathService) { return qs.search(path, $stateParams[`${list.iterator}_search`]); } - ], - credentialType: ['CredentialTypeModel', CredentialType => { - return new CredentialType('get'); - }] + ] } }; diff --git a/awx/ui/client/lib/models/Base.js b/awx/ui/client/lib/models/Base.js index a1c60e1bd9..a8e7b3fb0b 100644 --- a/awx/ui/client/lib/models/Base.js +++ b/awx/ui/client/lib/models/Base.js @@ -43,21 +43,28 @@ function requestWithCache (method, resource) { * supported by the API. * * @arg {Object} params - An object of keys and values to to format and - * to the URL as a query string. Refer to the API documentation for the + * to the URL as a query string. Refer to the API documentation for the * resource in use for specifics. * @arg {Object} config - Configuration specific to the UI to accommodate * common use cases. * - * @yields {boolean} - Indicating a match has been found. If so, the results + * @yields {boolean} - Indicating a match has been found. If so, the results * are set on the model. */ -function search (params, config) { +function search (params = {}, config = {}) { let req = { method: 'GET', - url: this.path, - params + url: this.path }; + if (typeof params === 'string') { + req.url = `?params`; + } else if (Array.isArray(params)) { + req.url += `?${params.join('&')}`; + } else { + req.params = params; + } + return $http(req) .then(({ data }) => { if (!data.count) { diff --git a/awx/ui/client/src/credentials/list/credentials-list.controller.js b/awx/ui/client/src/credentials/list/credentials-list.controller.js index 924f58e367..ee623d8e13 100644 --- a/awx/ui/client/src/credentials/list/credentials-list.controller.js +++ b/awx/ui/client/src/credentials/list/credentials-list.controller.js @@ -5,10 +5,13 @@ *************************************************/ export default ['$scope', 'Rest', 'CredentialList', 'Prompt', 'ProcessErrors', 'GetBasePath', - 'Wait', '$state', '$filter', 'rbacUiControlService', 'Dataset', 'credentialType', 'i18n', + 'Wait', '$state', '$filter', 'rbacUiControlService', 'Dataset', 'CredentialTypeModel', + 'i18n', function($scope, Rest, CredentialList, Prompt, ProcessErrors, GetBasePath, Wait, $state, $filter, rbacUiControlService, Dataset, - credentialType, i18n) { + CredentialType, i18n) { + + const credentialType = new CredentialType(); var list = CredentialList, defaultUrl = GetBasePath('credentials'); @@ -45,9 +48,25 @@ export default ['$scope', 'Rest', 'CredentialList', 'Prompt', 'ProcessErrors', ' return; } - $scope[list.name].forEach(credential => { - credential.kind = credentialType.match('id', credential.credential_type).name; - }); + const params = $scope[list.name] + .reduce((accumulator, credential) => { + accumulator.push(credential.credential_type); + + return accumulator; + }, []) + .filter((id, i, array) => array.indexOf(id) === i) + .map(id => `or__id=${id}`); + + credentialType.search(params) + .then(found => { + if (!found) { + return; + } + + $scope[list.name].forEach(credential => { + credential.kind = credentialType.match('id', credential.credential_type).name; + }); + }); } // iterate over the list and add fields like type label, after the