Fixed inventory source form error message. Leveraged inv src model for adding/editing existing object

This commit is contained in:
mabashian 2018-04-09 15:01:16 -04:00
parent 5b619ff0c1
commit 23d1454646
3 changed files with 158 additions and 148 deletions

View File

@ -9,24 +9,78 @@ 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
});
initVerbositySelect();
GetSourceTypeOptions({
scope: $scope,
variable: 'source_type_options'
});
const inventorySource = new InventorySource();
var getInventoryFiles = function (project) {
var url;
@ -225,65 +279,6 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition',
$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 +320,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 })
});
});
};
}

View File

@ -7,85 +7,87 @@
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() {
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];
}
}
if(inventorySourceData.source === 'scm') {
$scope.project = inventorySourceData.source_project;
$scope.project_name = inventorySourceData.summary_fields.source_project.name;
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({
@ -366,9 +368,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 +428,5 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString',
initRegionSelect();
};
init();
}
];

View File

@ -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)