diff --git a/awx/ui/client/src/forms/Groups.js b/awx/ui/client/src/forms/Groups.js index c5bf595ca2..5b6cb20b31 100644 --- a/awx/ui/client/src/forms/Groups.js +++ b/awx/ui/client/src/forms/Groups.js @@ -86,7 +86,8 @@ export default reqExpression: "cloudCredentialRequired", init: "false" }, - ngDisabled: '!(group_obj.summary_fields.user_capabilities.edit || canAdd)' + ngDisabled: '!(group_obj.summary_fields.user_capabilities.edit || canAdd)', + watchBasePath: "credentialBasePath" }, source_regions: { label: 'Regions', diff --git a/awx/ui/client/src/forms/JobTemplates.js b/awx/ui/client/src/forms/JobTemplates.js index 07f7ddf7c2..28462577ae 100644 --- a/awx/ui/client/src/forms/JobTemplates.js +++ b/awx/ui/client/src/forms/JobTemplates.js @@ -76,6 +76,7 @@ export default list: 'InventoryList', sourceModel: 'inventory', sourceField: 'name', + autopopulateLookup: false, awRequiredWhen: { reqExpression: '!ask_inventory_on_launch', alwaysShowAsterisk: true @@ -138,6 +139,7 @@ export default type: 'lookup', list: 'CredentialList', basePath: 'credentials', + autopopulateLookup: false, search: { kind: 'ssh' }, diff --git a/awx/ui/client/src/inventories/manage/groups/groups-add.controller.js b/awx/ui/client/src/inventories/manage/groups/groups-add.controller.js index 9229595d96..cc1283c582 100644 --- a/awx/ui/client/src/inventories/manage/groups/groups-add.controller.js +++ b/awx/ui/client/src/inventories/manage/groups/groups-add.controller.js @@ -112,6 +112,13 @@ export default ['$state', '$stateParams', '$scope', 'GroupForm', 'CredentialList }; $scope.sourceChange = function(source) { source = source.value; + if (source === 'custom'){ + $scope.credentialBasePath = GetBasePath('inventory_script'); + } + // equal to case 'ec2' || 'rax' || 'azure' || 'azure_rm' || 'vmware' || 'satellite6' || 'cloudforms' || 'openstack' + else{ + $scope.credentialBasePath = (source === 'ec2') ? GetBasePath('credentials') + '?kind=aws' : GetBasePath('credentials') + (source === '' ? '' : '?kind=' + (source)); + } if (source === 'ec2' || source === 'custom' || source === 'vmware' || source === 'openstack') { ParseTypeChange({ scope: $scope, @@ -120,6 +127,7 @@ export default ['$state', '$stateParams', '$scope', 'GroupForm', 'CredentialList parse_variable: 'envParseType' }); } + // reset fields $scope.group_by_choices = source === 'ec2' ? $scope.ec2_group_by : null; // azure_rm regions choices are keyed as "azure" in an OPTIONS request to the inventory_sources endpoint diff --git a/awx/ui/client/src/shared/directives.js b/awx/ui/client/src/shared/directives.js index ade342c479..be7f14e5e5 100644 --- a/awx/ui/client/src/shared/directives.js +++ b/awx/ui/client/src/shared/directives.js @@ -469,34 +469,59 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper']) autopopulateLookup, modelKey = attrs.ngModel, modelName = attrs.source, - watcher = attrs.awRequiredWhen || undefined; + watcher = attrs.awRequiredWhen || undefined, + watchBasePath; - if (attrs.autopopulateLookup !== undefined) { - autopopulateLookup = attrs.autopopulateLookup; + if (attrs.autopopulatelookup !== undefined) { + autopopulateLookup = JSON.parse(attrs.autopopulatelookup); } else { autopopulateLookup = true; } + + // The following block of code is for instances where the + // lookup field is reused by varying sub-forms. Example: The groups + // form will change it's credential lookup based on the + // source type. The basepath the lookup should utilize is dynamic + // in this case. You'd configure the "watchBasePath" key on the + // field's configuration in the form configuration field. + if (attrs.watchbasepath !== undefined) { + watchBasePath = attrs.watchbasepath; + scope.$watch(watchBasePath, (newValue) => { + if(newValue !== undefined && fieldIsAutopopulatable()){ + _doAutoPopulate(); + } + }); + } + function _doAutoPopulate() { let query = ''; - basePath = GetBasePath(elm.attr('data-basePath')) || elm.attr('data-basePath'); + if (attrs.watchbasepath !== undefined && scope[attrs.watchbasepath] !== undefined) { + basePath = scope[attrs.watchbasepath]; + } + else { + basePath = GetBasePath(elm.attr('data-basePath')) || elm.attr('data-basePath'); + switch(modelName) { + case 'credential': + query = '?kind=ssh'; + break; + case 'network_credential': + query = '?kind=net'; + break; + } - switch(modelName) { - case 'credential': - query = '?kind=ssh'; - break; - case 'network_credential': - query = '?kind=net'; - break; } Rest.setUrl(`${basePath}` + query); Rest.get() .success(function (data) { if (data.count === 1) { - scope[modelKey] = data.results[0].name; - scope[modelName] = data.results[0].id; + if(data.results[0].summary_fields.user_capabilities.edit === true){ + scope[modelKey] = data.results[0].name; + scope[modelName] = data.results[0].id; + } + } }); } @@ -518,9 +543,7 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper']) } if (scope.mode === "add") { if(watcher){ - scope.$watch(watcher, (newValue, oldValue, scope2) => { - console.log('success', watcher, newValue , oldValue, scope2); - + scope.$watch(watcher, () => { if(Boolean(scope.$eval(watcher)) === true){ // if we get here then the field is required diff --git a/awx/ui/client/src/shared/form-generator.js b/awx/ui/client/src/shared/form-generator.js index 52e0eccfc4..9b59e9db81 100644 --- a/awx/ui/client/src/shared/form-generator.js +++ b/awx/ui/client/src/shared/form-generator.js @@ -1368,7 +1368,8 @@ angular.module('FormGenerator', [GeneratorHelpers.name, 'Utilities', listGenerat html += `data-basePath="${field.basePath}"`; html += `data-source="${field.sourceModel}"`; html += `data-query="?${field.sourceField}__iexact=:value"`; - html += (field.autopopulateLookup) ? `autopopulateLookup=${field.autopopulateLookup}` : ""; + html += (field.autopopulateLookup !== undefined) ? ` autopopulateLookup=${field.autopopulateLookup} ` : ""; + html += (field.watchBasePath !== undefined) ? ` watchBasePath=${field.watchBasePath} ` : ""; html += `ng-model-options="{ updateOn: 'default blur', debounce: { 'default': 300, 'blur': 0 } }"`; html += " awlookup >\n"; html += "\n";