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', 'GetChoices', 'GetBasePath', 'CreateSelect2', 'GetSourceTypeOptions',
'rbacUiControlService', 'ToJSON', 'SourcesService', 'Empty', 'rbacUiControlService', 'ToJSON', 'SourcesService', 'Empty',
'Wait', 'Rest', 'Alert', 'ProcessErrors', 'inventorySourcesOptions', 'Wait', 'Rest', 'Alert', 'ProcessErrors', 'inventorySourcesOptions',
'$rootScope', 'i18n', '$rootScope', 'i18n', 'InventorySourceModel', 'InventoryHostsStrings',
function($state, $stateParams, $scope, SourcesFormDefinition, ParseTypeChange, function($state, $stateParams, $scope, SourcesFormDefinition, ParseTypeChange,
GenerateForm, inventoryData, GroupsService, GetChoices, GenerateForm, inventoryData, GroupsService, GetChoices,
GetBasePath, CreateSelect2, GetSourceTypeOptions, rbacUiControlService, GetBasePath, CreateSelect2, GetSourceTypeOptions, rbacUiControlService,
ToJSON, SourcesService, Empty, Wait, Rest, Alert, ProcessErrors, ToJSON, SourcesService, Empty, Wait, Rest, Alert, ProcessErrors,
inventorySourcesOptions,$rootScope, i18n) { inventorySourcesOptions,$rootScope, i18n, InventorySource, InventoryHostsStrings) {
let form = SourcesFormDefinition; let form = SourcesFormDefinition;
init();
function init() {
$scope.mode = 'add'; $scope.mode = 'add';
// apply form definition's default field values // apply form definition's default field values
GenerateForm.applyDefaults(form, $scope, true); GenerateForm.applyDefaults(form, $scope, true);
$scope.canAdd = inventorySourcesOptions.actions.POST; $scope.canAdd = inventorySourcesOptions.actions.POST;
$scope.envParseType = 'yaml'; $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 getInventoryFiles = function (project) {
var url; var url;
@@ -225,65 +279,6 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition',
$scope.verbosity = $scope.verbosity_options[1]; $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() { $scope.formCancel = function() {
$state.go('^'); $state.go('^');
}; };
@@ -325,9 +320,17 @@ export default ['$state', '$stateParams', '$scope', 'SourcesFormDefinition',
} else { } else {
params.source = null; params.source = null;
} }
SourcesService.post(params).then((response) => {
inventorySource.request('post', {
data: params
}).then((response) => {
let inventory_source_id = response.data.id; let inventory_source_id = response.data.id;
$state.go('^.edit', {inventory_source_id: inventory_source_id}, {reload: true}); $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,15 +7,18 @@
export default ['$state', '$stateParams', '$scope', 'ParseVariableString', export default ['$state', '$stateParams', '$scope', 'ParseVariableString',
'rbacUiControlService', 'ToJSON', 'ParseTypeChange', 'GroupsService', 'rbacUiControlService', 'ToJSON', 'ParseTypeChange', 'GroupsService',
'GetChoices', 'GetBasePath', 'CreateSelect2', 'GetSourceTypeOptions', 'GetChoices', 'GetBasePath', 'CreateSelect2', 'GetSourceTypeOptions',
'inventorySourceData', 'SourcesService', 'inventoryData', 'inventorySourcesOptions', 'Empty', 'SourcesService', 'inventoryData', 'inventorySourcesOptions', 'Empty',
'Wait', 'Rest', 'Alert', '$rootScope', 'i18n', 'Wait', 'Rest', 'Alert', '$rootScope', 'i18n', 'InventoryHostsStrings',
'ProcessErrors', 'inventorySource',
function($state, $stateParams, $scope, ParseVariableString, function($state, $stateParams, $scope, ParseVariableString,
rbacUiControlService, ToJSON,ParseTypeChange, GroupsService, rbacUiControlService, ToJSON,ParseTypeChange, GroupsService,
GetChoices, GetBasePath, CreateSelect2, GetSourceTypeOptions, GetChoices, GetBasePath, CreateSelect2, GetSourceTypeOptions,
inventorySourceData, SourcesService, inventoryData, inventorySourcesOptions, Empty, SourcesService, inventoryData, inventorySourcesOptions, Empty,
Wait, Rest, Alert, $rootScope, i18n) { Wait, Rest, Alert, $rootScope, i18n, InventoryHostsStrings,
ProcessErrors, inventorySource) {
const inventorySourceData = inventorySource.get();
function init() {
$scope.projectBasePath = GetBasePath('projects') + '?not__status=never updated'; $scope.projectBasePath = GetBasePath('projects') + '?not__status=never updated';
$scope.canAdd = inventorySourcesOptions.actions.POST; $scope.canAdd = inventorySourcesOptions.actions.POST;
// instantiate expected $scope values from inventorySourceData // instantiate expected $scope values from inventorySourceData
@@ -85,7 +88,6 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString',
updateSCMProject(); updateSCMProject();
} }
}); });
}
function initVerbositySelect(){ function initVerbositySelect(){
CreateSelect2({ CreateSelect2({
@@ -366,9 +368,16 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString',
params.source = null; params.source = null;
} }
SourcesService inventorySource.request('put', {
.put(params) data: params
.then(() => $state.go('.', null, { reload: true })); }).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) { $scope.sourceChange = function(source) {
@@ -419,7 +428,5 @@ export default ['$state', '$stateParams', '$scope', 'ParseVariableString',
initRegionSelect(); initRegionSelect();
}; };
init();
} }
]; ];

View File

@@ -20,8 +20,8 @@ export default {
} }
}, },
resolve: { resolve: {
inventorySourceData: ['$stateParams', 'SourcesService', function($stateParams, SourcesService) { inventorySource: ['InventorySourceModel', '$stateParams', (InventorySource, $stateParams) => {
return SourcesService.get({id: $stateParams.inventory_source_id }).then(response => response.data.results[0]); return new InventorySource('get', $stateParams.inventory_source_id);
}], }],
inventorySourcesOptions: ['InventoriesService', '$stateParams', function(InventoriesService, $stateParams) { inventorySourcesOptions: ['InventoriesService', '$stateParams', function(InventoriesService, $stateParams) {
return InventoriesService.inventorySourcesOptions($stateParams.inventory_id) return InventoriesService.inventorySourcesOptions($stateParams.inventory_id)