diff --git a/awx/ui/client/src/inventories-hosts/inventories/related/sources/add/sources-add.controller.js b/awx/ui/client/src/inventories-hosts/inventories/related/sources/add/sources-add.controller.js index 10865e4823..4415dc386d 100644 --- a/awx/ui/client/src/inventories-hosts/inventories/related/sources/add/sources-add.controller.js +++ b/awx/ui/client/src/inventories-hosts/inventories/related/sources/add/sources-add.controller.js @@ -9,24 +9,83 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition', 'GetChoices', 'GetBasePath', 'CreateSelect2', 'GetSourceTypeOptions', 'rbacUiControlService', 'ToJSON', 'SourcesService', 'Empty', 'Wait', 'Rest', 'Alert', 'ProcessErrors', 'inventorySourcesOptions', - '$rootScope', 'i18n', + '$rootScope', 'i18n', 'InventorySourceModel', 'InventoryHostsStrings', function($state, $stateParams, $scope, SourcesFormDefinition, ParseTypeChange, GenerateForm, inventoryData, GroupsService, GetChoices, GetBasePath, CreateSelect2, GetSourceTypeOptions, rbacUiControlService, ToJSON, SourcesService, Empty, Wait, Rest, Alert, ProcessErrors, - inventorySourcesOptions,$rootScope, i18n) { + inventorySourcesOptions,$rootScope, i18n, InventorySource, InventoryHostsStrings) { let form = SourcesFormDefinition; - init(); + $scope.mode = 'add'; + // apply form definition's default field values + GenerateForm.applyDefaults(form, $scope, true); + $scope.canAdd = inventorySourcesOptions.actions.POST; + $scope.envParseType = 'yaml'; - function init() { - $scope.mode = 'add'; - // apply form definition's default field values - GenerateForm.applyDefaults(form, $scope, true); - $scope.canAdd = inventorySourcesOptions.actions.POST; - $scope.envParseType = 'yaml'; - initSources(); - } + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'rax_regions', + choice_name: 'rax_region_choices', + options: inventorySourcesOptions + }); + + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'ec2_regions', + choice_name: 'ec2_region_choices', + options: inventorySourcesOptions + }); + + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'gce_regions', + choice_name: 'gce_region_choices', + options: inventorySourcesOptions + }); + + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'azure_regions', + choice_name: 'azure_rm_region_choices', + options: inventorySourcesOptions + }); + + // Load options for group_by + GetChoices({ + scope: $scope, + field: 'group_by', + variable: 'ec2_group_by', + choice_name: 'ec2_group_by_choices', + options: inventorySourcesOptions + }); + + initRegionSelect(); + + GetChoices({ + scope: $scope, + field: 'verbosity', + variable: 'verbosity_options', + options: inventorySourcesOptions + }); + + CreateSelect2({ + element: '#inventory_source_verbosity', + multiple: false + }); + + $scope.verbosity = $scope.verbosity_options[1]; + + GetSourceTypeOptions({ + scope: $scope, + variable: 'source_type_options' + }); + + const inventorySource = new InventorySource(); var getInventoryFiles = function (project) { var url; @@ -39,7 +98,14 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition', .then(({data}) => { $scope.inventory_files = data; $scope.inventory_files.push("/ (project root)"); - sync_inventory_file_select2(); + CreateSelect2({ + element:'#inventory-file-select', + addNew: true, + multiple: false, + scope: $scope, + options: 'inventory_files', + model: 'inventory_file' + }); Wait('stop'); }) .catch(() => { @@ -59,17 +125,6 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition', } }); - function sync_inventory_file_select2() { - CreateSelect2({ - element:'#inventory-file-select', - addNew: true, - multiple: false, - scope: $scope, - options: 'inventory_files', - model: 'inventory_file' - }); - } - $scope.lookupCredential = function(){ if($scope.source.value !== "scm" && $scope.source.value !== "custom") { let kind = ($scope.source.value === "ec2") ? "aws" : $scope.source.value; @@ -151,7 +206,10 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition', // region / source options callback $scope.$on('sourceTypeOptionsReady', function() { - initSourceSelect(); + CreateSelect2({ + element: '#inventory_source_source', + multiple: false + }); }); function initRegionSelect(){ @@ -160,10 +218,6 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition', multiple: true }); - initGroupBySelect(); - } - - function initGroupBySelect(){ let add_new = false; if( _.get($scope, 'source') === 'ec2' || _.get($scope.source, 'value') === 'ec2') { $scope.group_by_choices = $scope.ec2_group_by; @@ -209,81 +263,6 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition', }); } - function initSourceSelect(){ - CreateSelect2({ - element: '#inventory_source_source', - multiple: false - }); - } - - function initVerbositySelect(){ - CreateSelect2({ - element: '#inventory_source_verbosity', - multiple: false - }); - - $scope.verbosity = $scope.verbosity_options[1]; - } - - function initSources(){ - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'rax_regions', - choice_name: 'rax_region_choices', - options: inventorySourcesOptions - }); - - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'ec2_regions', - choice_name: 'ec2_region_choices', - options: inventorySourcesOptions - }); - - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'gce_regions', - choice_name: 'gce_region_choices', - options: inventorySourcesOptions - }); - - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'azure_regions', - choice_name: 'azure_rm_region_choices', - options: inventorySourcesOptions - }); - - // Load options for group_by - GetChoices({ - scope: $scope, - field: 'group_by', - variable: 'ec2_group_by', - choice_name: 'ec2_group_by_choices', - options: inventorySourcesOptions - }); - - initRegionSelect(); - - GetChoices({ - scope: $scope, - field: 'verbosity', - variable: 'verbosity_options', - options: inventorySourcesOptions - }); - - initVerbositySelect(); - - GetSourceTypeOptions({ - scope: $scope, - variable: 'source_type_options' - }); - } - $scope.formCancel = function() { $state.go('^'); }; @@ -325,9 +304,17 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition', } else { params.source = null; } - SourcesService.post(params).then((response) => { + + inventorySource.request('post', { + data: params + }).then((response) => { let inventory_source_id = response.data.id; $state.go('^.edit', {inventory_source_id: inventory_source_id}, {reload: true}); + }).catch(({ data, status, config }) => { + ProcessErrors($scope, data, status, null, { + hdr: 'Error!', + msg: InventoryHostsStrings.get('error.CALL', { path: `${config.url}`, status }) + }); }); }; } diff --git a/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.controller.js b/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.controller.js index 8218c35fbb..104ff2574a 100644 --- a/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.controller.js +++ b/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.controller.js @@ -7,85 +7,169 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString', 'rbacUiControlService', 'ToJSON', 'ParseTypeChange', 'GroupsService', 'GetChoices', 'GetBasePath', 'CreateSelect2', 'GetSourceTypeOptions', - 'inventorySourceData', 'SourcesService', 'inventoryData', 'inventorySourcesOptions', 'Empty', - 'Wait', 'Rest', 'Alert', '$rootScope', 'i18n', + 'SourcesService', 'inventoryData', 'inventorySourcesOptions', 'Empty', + 'Wait', 'Rest', 'Alert', '$rootScope', 'i18n', 'InventoryHostsStrings', + 'ProcessErrors', 'inventorySource', function($state, $stateParams, $scope, ParseVariableString, rbacUiControlService, ToJSON,ParseTypeChange, GroupsService, GetChoices, GetBasePath, CreateSelect2, GetSourceTypeOptions, - inventorySourceData, SourcesService, inventoryData, inventorySourcesOptions, Empty, - Wait, Rest, Alert, $rootScope, i18n) { + SourcesService, inventoryData, inventorySourcesOptions, Empty, + Wait, Rest, Alert, $rootScope, i18n, InventoryHostsStrings, + ProcessErrors, inventorySource) { - function init() { - $scope.projectBasePath = GetBasePath('projects') + '?not__status=never updated'; - $scope.canAdd = inventorySourcesOptions.actions.POST; - // instantiate expected $scope values from inventorySourceData - _.assign($scope, - {credential: inventorySourceData.credential}, - {overwrite: inventorySourceData.overwrite}, - {overwrite_vars: inventorySourceData.overwrite_vars}, - {update_on_launch: inventorySourceData.update_on_launch}, - {update_cache_timeout: inventorySourceData.update_cache_timeout}, - {instance_filters: inventorySourceData.instance_filters}, - {inventory_script: inventorySourceData.source_script}, - {verbosity: inventorySourceData.verbosity}); + const inventorySourceData = inventorySource.get(); - $scope.inventory_source_obj = inventorySourceData; - if (inventorySourceData.credential) { - $scope.credential_name = inventorySourceData.summary_fields.credential.name; + $scope.projectBasePath = GetBasePath('projects') + '?not__status=never updated'; + $scope.canAdd = inventorySourcesOptions.actions.POST; + // instantiate expected $scope values from inventorySourceData + _.assign($scope, + {credential: inventorySourceData.credential}, + {overwrite: inventorySourceData.overwrite}, + {overwrite_vars: inventorySourceData.overwrite_vars}, + {update_on_launch: inventorySourceData.update_on_launch}, + {update_cache_timeout: inventorySourceData.update_cache_timeout}, + {instance_filters: inventorySourceData.instance_filters}, + {inventory_script: inventorySourceData.source_script}, + {verbosity: inventorySourceData.verbosity}); + + $scope.inventory_source_obj = inventorySourceData; + if (inventorySourceData.credential) { + $scope.credential_name = inventorySourceData.summary_fields.credential.name; + } + + if(inventorySourceData.source === 'scm') { + $scope.project = inventorySourceData.source_project; + $scope.project_name = inventorySourceData.summary_fields.source_project.name; + updateSCMProject(); + } + + // display custom inventory_script name + if (inventorySourceData.source === 'custom' && inventorySourceData.summary_fields.source_script) { + $scope.inventory_script_name = inventorySourceData.summary_fields.source_script.name; + } + $scope = angular.extend($scope, inventorySourceData); + + $scope.$watch('summary_fields.user_capabilities.edit', function(val) { + $scope.canAdd = val; + }); + + $scope.$on('sourceTypeOptionsReady', function() { + $scope.source = _.find($scope.source_type_options, { value: inventorySourceData.source }); + var source = $scope.source && $scope.source.value ? $scope.source.value : null; + $scope.cloudCredentialRequired = source !== '' && source !== 'scm' && source !== 'custom' && source !== 'ec2' ? true : false; + CreateSelect2({ + element: '#inventory_source_source', + multiple: false + }); + + if (source === 'ec2' || source === 'custom' || + source === 'vmware' || source === 'openstack' || + source === 'scm' || source === 'cloudforms' || + source === 'satellite6') { + + var varName; + if (source === 'scm') { + varName = 'custom_variables'; + } else { + varName = source + '_variables'; + } + + $scope[varName] = ParseVariableString(inventorySourceData + .source_vars); + + ParseTypeChange({ + scope: $scope, + field_id: varName, + variable: varName, + parse_variable: 'envParseType', + }); } + }); - if(inventorySourceData.source === 'scm') { - $scope.project = inventorySourceData.source_project; - $scope.project_name = inventorySourceData.summary_fields.source_project.name; + $scope.envParseType = 'yaml'; + + GetSourceTypeOptions({ + scope: $scope, + variable: 'source_type_options' + }); + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'rax_regions', + choice_name: 'rax_region_choices', + options: inventorySourcesOptions + }); + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'ec2_regions', + choice_name: 'ec2_region_choices', + options: inventorySourcesOptions + }); + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'gce_regions', + choice_name: 'gce_region_choices', + options: inventorySourcesOptions + }); + GetChoices({ + scope: $scope, + field: 'source_regions', + variable: 'azure_regions', + choice_name: 'azure_rm_region_choices', + options: inventorySourcesOptions + }); + GetChoices({ + scope: $scope, + field: 'group_by', + variable: 'ec2_group_by', + choice_name: 'ec2_group_by_choices', + options: inventorySourcesOptions + }); + + var source = $scope.source === 'azure_rm' ? 'azure' : $scope.source; + var regions = inventorySourceData.source_regions.split(','); + // azure_rm regions choices are keyed as "azure" in an OPTIONS request to the inventory_sources endpoint + $scope.source_region_choices = $scope[source + '_regions']; + + // the API stores azure regions as all-lowercase strings - but the azure regions received from OPTIONS are Snake_Cased + if (source === 'azure') { + $scope.source_regions = _.map(regions, (region) => _.find($scope[source + '_regions'], (o) => o.value.toLowerCase() === region)); + } + // all other regions are 1-1 + else { + $scope.source_regions = _.map(regions, (region) => _.find($scope[source + '_regions'], (o) => o.value === region)); + } + initRegionSelect(); + + GetChoices({ + scope: $scope, + field: 'verbosity', + variable: 'verbosity_options', + options: inventorySourcesOptions + }); + + var i; + for (i = 0; i < $scope.verbosity_options.length; i++) { + if ($scope.verbosity_options[i].value === $scope.verbosity) { + $scope.verbosity = $scope.verbosity_options[i]; + } + } + + initVerbositySelect(); + + $scope.$watch('verbosity', initVerbositySelect); + + // Register a watcher on project_name + if ($scope.getInventoryFilesUnregister) { + $scope.getInventoryFilesUnregister(); + } + $scope.getInventoryFilesUnregister = $scope.$watch('project', function (newValue, oldValue) { + if (newValue !== oldValue) { updateSCMProject(); } - - // display custom inventory_script name - if (inventorySourceData.source === 'custom' && inventorySourceData.summary_fields.source_script) { - $scope.inventory_script_name = inventorySourceData.summary_fields.source_script.name; - } - $scope = angular.extend($scope, inventorySourceData); - - $scope.$watch('summary_fields.user_capabilities.edit', function(val) { - $scope.canAdd = val; - }); - - $scope.$on('sourceTypeOptionsReady', function() { - initSourceSelect(); - }); - - $scope.envParseType = 'yaml'; - - initSources(); - - GetChoices({ - scope: $scope, - field: 'verbosity', - variable: 'verbosity_options', - options: inventorySourcesOptions - }); - - var i; - for (i = 0; i < $scope.verbosity_options.length; i++) { - if ($scope.verbosity_options[i].value === $scope.verbosity) { - $scope.verbosity = $scope.verbosity_options[i]; - } - } - - initVerbositySelect(); - - $scope.$watch('verbosity', initVerbositySelect); - - // Register a watcher on project_name - if ($scope.getInventoryFilesUnregister) { - $scope.getInventoryFilesUnregister(); - } - $scope.getInventoryFilesUnregister = $scope.$watch('project', function (newValue, oldValue) { - if (newValue !== oldValue) { - updateSCMProject(); - } - }); - } + }); function initVerbositySelect(){ CreateSelect2({ @@ -138,110 +222,12 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString', } } - function initSourceSelect() { - $scope.source = _.find($scope.source_type_options, { value: inventorySourceData.source }); - var source = $scope.source && $scope.source.value ? $scope.source.value : null; - $scope.cloudCredentialRequired = source !== '' && source !== 'scm' && source !== 'custom' && source !== 'ec2' ? true : false; - CreateSelect2({ - element: '#inventory_source_source', - multiple: false - }); - - if (source === 'ec2' || source === 'custom' || - source === 'vmware' || source === 'openstack' || - source === 'scm' || source === 'cloudforms' || - source === 'satellite6') { - - var varName; - if (source === 'scm') { - varName = 'custom_variables'; - } else { - varName = source + '_variables'; - } - - $scope[varName] = ParseVariableString(inventorySourceData - .source_vars); - - ParseTypeChange({ - scope: $scope, - field_id: varName, - variable: varName, - parse_variable: 'envParseType', - }); - } - } - - function initRegionData() { - var source = $scope.source === 'azure_rm' ? 'azure' : $scope.source; - var regions = inventorySourceData.source_regions.split(','); - // azure_rm regions choices are keyed as "azure" in an OPTIONS request to the inventory_sources endpoint - $scope.source_region_choices = $scope[source + '_regions']; - - // the API stores azure regions as all-lowercase strings - but the azure regions received from OPTIONS are Snake_Cased - if (source === 'azure') { - $scope.source_regions = _.map(regions, (region) => _.find($scope[source + '_regions'], (o) => o.value.toLowerCase() === region)); - } - // all other regions are 1-1 - else { - $scope.source_regions = _.map(regions, (region) => _.find($scope[source + '_regions'], (o) => o.value === region)); - } - initRegionSelect(); - } - - function initSources() { - GetSourceTypeOptions({ - scope: $scope, - variable: 'source_type_options' - }); - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'rax_regions', - choice_name: 'rax_region_choices', - options: inventorySourcesOptions - }); - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'ec2_regions', - choice_name: 'ec2_region_choices', - options: inventorySourcesOptions - }); - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'gce_regions', - choice_name: 'gce_region_choices', - options: inventorySourcesOptions - }); - GetChoices({ - scope: $scope, - field: 'source_regions', - variable: 'azure_regions', - choice_name: 'azure_rm_region_choices', - options: inventorySourcesOptions - }); - GetChoices({ - scope: $scope, - field: 'group_by', - variable: 'ec2_group_by', - choice_name: 'ec2_group_by_choices', - options: inventorySourcesOptions - }); - - initRegionData(); - } - function initRegionSelect() { CreateSelect2({ element: '#inventory_source_source_regions', multiple: true }); - initGroupBySelect(); - } - - function initGroupBySelect(){ let add_new = false; if( _.get($scope, 'source') === 'ec2' || _.get($scope.source, 'value') === 'ec2') { $scope.group_by_choices = $scope.ec2_group_by; @@ -366,9 +352,16 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString', params.source = null; } - SourcesService - .put(params) - .then(() => $state.go('.', null, { reload: true })); + inventorySource.request('put', { + data: params + }).then(() => { + $state.go('.', null, { reload: true }); + }).catch(({ data, status, config }) => { + ProcessErrors($scope, data, status, null, { + hdr: 'Error!', + msg: InventoryHostsStrings.get('error.CALL', { path: `${config.url}`, status }) + }); + }); }; $scope.sourceChange = function(source) { @@ -419,7 +412,5 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString', initRegionSelect(); }; - - init(); } ]; diff --git a/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.route.js b/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.route.js index c73c7840b4..2402b9fb08 100644 --- a/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.route.js +++ b/awx/ui/client/src/inventories-hosts/inventories/related/sources/edit/sources-edit.route.js @@ -20,8 +20,8 @@ export default { } }, resolve: { - inventorySourceData: ['$stateParams', 'SourcesService', function($stateParams, SourcesService) { - return SourcesService.get({id: $stateParams.inventory_source_id }).then(response => response.data.results[0]); + inventorySource: ['InventorySourceModel', '$stateParams', (InventorySource, $stateParams) => { + return new InventorySource('get', $stateParams.inventory_source_id); }], inventorySourcesOptions: ['InventoriesService', '$stateParams', function(InventoriesService, $stateParams) { return InventoriesService.inventorySourcesOptions($stateParams.inventory_id)