From 74bc9f689982a4c0d5c174c81984e37bef5afc3e Mon Sep 17 00:00:00 2001 From: lgalis Date: Wed, 28 Feb 2018 16:01:14 -0500 Subject: [PATCH] Refactor getting the credential list --- .../playbook-reusable-code-mixin.js | 43 ++++++++----------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/controllers/playbook-reusable-code-mixin.js b/app/assets/javascripts/controllers/playbook-reusable-code-mixin.js index 9070b91d1668..e500ac98ad97 100644 --- a/app/assets/javascripts/controllers/playbook-reusable-code-mixin.js +++ b/app/assets/javascripts/controllers/playbook-reusable-code-mixin.js @@ -98,6 +98,20 @@ function playbookReusableCodeMixin(API, $q, miqService) { ); }; + var getCredentialsForType = function(type, credentialUrl, vm) { + var prefixes = ['retirement', 'provisioning']; + var prefixLen = prefixes.length; + allApiPromises.push(API.get(credentialUrl + sortOptions) + .then(function(data) { + vm[type + '_credentials'] = data.resources; + for (var i = 0; i < prefixLen; i++) { + vm['_' + prefixes[i] + type + '_credential'] = _.find(vm[type + '_credentials'], {id: vm[vm.model][prefixes[i] + '_' + type + '_credential_id']}); + } + }) + .catch(miqService.handleFailure) + ); + }; + // list of service catalogs var formOptions = function(vm) { miqService.sparkleOn(); @@ -134,34 +148,13 @@ function playbookReusableCodeMixin(API, $q, miqService) { ); // list of machine credentials - allApiPromises.push(API.get('/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential&expand=resources&attributes=id,name' + sortOptions) - .then(function(data) { - vm.machine_credentials = data.resources; - vm._retirement_machine_credential = _.find(vm.machine_credentials, {id: vm[vm.model].retirement_machine_credential_id}); - vm._provisioning_machine_credential = _.find(vm.machine_credentials, {id: vm[vm.model].provisioning_machine_credential_id}); - }) - .catch(miqService.handleFailure) - ); + getCredentialsForType('machine', '/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential&expand=resources&attributes=id,name', vm); - // list of machine credentials - allApiPromises.push(API.get('/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::VaultCredential&expand=resources&attributes=id,name' + sortOptions) - .then(function(data) { - vm.vault_credentials = data.resources; - vm._vault_credential = _.find(vm.vault_credentials, {id: vm[vm.model].retirement_vault_credential_id}); - vm._provisioning_vault_credential = _.find(vm.vault_credentials, {id: vm[vm.model].provisioning_vault_credential_id}); - }) - .catch(miqService.handleFailure) - ); + // list of vault credentials + getCredentialsForType('vault', '/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::VaultCredential&expand=resources&attributes=id,name', vm); // list of network credentials - allApiPromises.push(API.get('/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::NetworkCredential&expand=resources&attributes=id,name' + sortOptions) - .then(function(data) { - vm.network_credentials = data.resources; - vm._retirement_network_credential = _.find(vm.network_credentials, {id: vm[vm.model].retirement_network_credential_id}); - vm._provisioning_network_credential = _.find(vm.network_credentials, {id: vm[vm.model].provisioning_network_credential_id}); - }) - .catch(miqService.handleFailure) - ); + getCredentialsForType('network', '/api/authentications?collection_class=ManageIQ::Providers::EmbeddedAnsible::AutomationManager::NetworkCredential&expand=resources&attributes=id,name', vm); }; function retrievedFormData(vm) {