Inventory variables now working

This commit is contained in:
chouseknecht
2013-06-20 15:20:52 -04:00
parent 4da0beb6e7
commit b511b08014
4 changed files with 144 additions and 54 deletions

View File

@@ -513,7 +513,8 @@
} }
.modal-header h3 { .modal-header h3 {
font-size: 20px; font-size: 22px;
font-weight: normal;
margin: 0; margin: 0;
text-rendering: optimizeLegibility; text-rendering: optimizeLegibility;
} }

View File

@@ -158,7 +158,8 @@ InventoriesList.$inject = [ '$scope', '$rootScope', '$location', '$log', '$route
function InventoriesAdd ($scope, $rootScope, $compile, $location, $log, $routeParams, InventoryForm, function InventoriesAdd ($scope, $rootScope, $compile, $location, $log, $routeParams, InventoryForm,
GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ReturnToCaller, ClearScope, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ReturnToCaller, ClearScope,
GenerateList, OrganizationList, SearchInit, PaginateInit, LookUpInit, GetBasePath) GenerateList, OrganizationList, SearchInit, PaginateInit, LookUpInit, GetBasePath,
ParseTypeChange)
{ {
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
//scope. //scope.
@@ -168,8 +169,11 @@ function InventoriesAdd ($scope, $rootScope, $compile, $location, $log, $routePa
var form = InventoryForm; var form = InventoryForm;
var generator = GenerateForm; var generator = GenerateForm;
var scope = generator.inject(form, {mode: 'add', related: false}); var scope = generator.inject(form, {mode: 'add', related: false});
scope.parseType = 'json';
generator.reset(); generator.reset();
LoadBreadCrumbs(); LoadBreadCrumbs();
ParseTypeChange(scope);
LookUpInit({ LookUpInit({
scope: scope, scope: scope,
@@ -181,43 +185,76 @@ function InventoriesAdd ($scope, $rootScope, $compile, $location, $log, $routePa
// Save // Save
scope.formSave = function() { scope.formSave = function() {
Rest.setUrl(defaultUrl);
var data = {} try {
for (var fld in form.fields) { // Make sure we have valid variable data
if (form.fields[fld].realName) { if (scope.parseType == 'json') {
data[form.fields[fld].realName] = scope[fld]; var myjson = JSON.parse(scope.variables); //make sure JSON parses
} var json_data = scope.variables;
else { }
data[fld] = scope[fld]; else {
} var json_data = jsyaml.load(scope.variables); //parse yaml
} }
Rest.post(data)
.success( function(data, status, headers, config) { var data = {}
$location.path('/inventories/' + data.id); for (var fld in form.fields) {
}) if (fld != 'variables') {
.error( function(data, status, headers, config) { if (form.fields[fld].realName) {
ProcessErrors(scope, data, status, form, data[form.fields[fld].realName] = scope[fld];
{ hdr: 'Error!', msg: 'Failed to add new inventory. Post returned status: ' + status }); }
}); else {
}; data[fld] = scope[fld];
}
}
}
Rest.setUrl(defaultUrl);
Rest.post(data)
.success( function(data, status, headers, config) {
var inventory_id = data.id;
if (scope.variables) {
Rest.setUrl(data.related.variable_data);
Rest.put(json_data)
.success( function(data, status, headers, config) {
$location.path('/inventories/' + inventory_id);
})
.error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to add inventory varaibles. PUT returned status: ' + status });
});
}
else {
$location.path('/inventories/' + inventory_id);
}
})
.error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to add new inventory. Post returned status: ' + status });
});
}
catch(err) {
Alert("Error", "Error parsing inventory variables. Parser returned " + err);
}
};
// Reset // Reset
scope.formReset = function() { scope.formReset = function() {
// Defaults // Defaults
generator.reset(); generator.reset();
}; };
} }
InventoriesAdd.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'InventoryForm', 'GenerateForm', InventoriesAdd.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'InventoryForm', 'GenerateForm',
'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope', 'GenerateList', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope', 'GenerateList',
'OrganizationList', 'SearchInit', 'PaginateInit', 'LookUpInit', 'GetBasePath' ]; 'OrganizationList', 'SearchInit', 'PaginateInit', 'LookUpInit', 'GetBasePath', 'ParseTypeChange'];
function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, InventoryForm, function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, InventoryForm,
GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, RelatedSearchInit, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, RelatedSearchInit,
RelatedPaginateInit, ReturnToCaller, ClearScope, LookUpInit, Prompt, RelatedPaginateInit, ReturnToCaller, ClearScope, LookUpInit, Prompt,
OrganizationList, TreeInit, GetBasePath, GroupsList, GroupsEdit, LoadInventory, OrganizationList, TreeInit, GetBasePath, GroupsList, GroupsEdit, LoadInventory,
GroupsDelete, HostsList, HostsAdd, HostsEdit, HostsDelete, RefreshTree) GroupsDelete, HostsList, HostsAdd, HostsEdit, HostsDelete, RefreshTree, ParseTypeChange)
{ {
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
//scope. //scope.
@@ -230,6 +267,9 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP
var base = $location.path().replace(/^\//,'').split('/')[0]; var base = $location.path().replace(/^\//,'').split('/')[0];
var id = $routeParams.id; var id = $routeParams.id;
ParseTypeChange(scope);
scope.parseType = 'json';
scope['inventory_id'] = id; scope['inventory_id'] = id;
// Retrieve each related set and any lookups // Retrieve each related set and any lookups
@@ -248,6 +288,27 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP
list: OrganizationList, list: OrganizationList,
field: 'organization' field: 'organization'
}); });
if (scope.variable_url) {
Rest.setUrl(scope.variable_url);
Rest.get()
.success( function(data, status, headers, config) {
if ($.isEmptyObject(data)) {
scope.variables = "\{\}";
}
else {
scope.variables = JSON.stringify(data, null, " ");
}
})
.error( function(data, status, headers, config) {
scope.variables = null;
ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to retrieve inventory variables. GET returned status: ' + status });
});
}
else {
scope.variables = "\{\}";
}
}); });
LoadInventory({ scope: scope }); LoadInventory({ scope: scope });
@@ -256,28 +317,58 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP
RefreshTree({ scope: scope }); RefreshTree({ scope: scope });
} }
// Save changes to the parent // Save
scope.formSave = function() { scope.formSave = function() {
Rest.setUrl(defaultUrl + $routeParams.id + '/'); try {
var data = {} // Make sure we have valid variable data
for (var fld in form.fields) { if (scope.parseType == 'json') {
if (form.fields[fld].realName) { var myjson = JSON.parse(scope.variables); //make sure JSON parses
data[form.fields[fld].realName] = scope[fld]; var json_data = scope.variables;
} }
else { else {
data[fld] = scope[fld]; var json_data = jsyaml.load(scope.variables); //parse yaml
} }
}
Rest.put(data) var data = {}
.success( function(data, status, headers, config) { for (var fld in form.fields) {
var base = $location.path().replace(/^\//,'').split('/')[0]; if (fld != 'variables') {
(base == 'inventories') ? ReturnToCaller() : ReturnToCaller(1); if (form.fields[fld].realName) {
}) data[form.fields[fld].realName] = scope[fld];
.error( function(data, status, headers, config) { }
ProcessErrors(scope, data, status, form, else {
{ hdr: 'Error!', msg: 'Failed to update inventory: ' + $routeParams.id + '. PUT status: ' + status }); data[fld] = scope[fld];
}); }
}; }
}
Rest.setUrl(defaultUrl + id + '/');
Rest.put(data)
.success( function(data, status, headers, config) {
if (scope.variables) {
Rest.setUrl(data.related.variable_data);
Rest.put(json_data)
.success( function(data, status, headers, config) {
$location.path('/inventories');
})
.error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to update inventory varaibles. PUT returned status: ' + status });
});
}
else {
$location.path('/inventories');
}
})
.error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to update new inventory. Post returned status: ' + status });
});
}
catch(err) {
Alert("Error", "Error parsing inventory variables. Parser returned " + err);
}
};
// Cancel // Cancel
scope.formReset = function() { scope.formReset = function() {
@@ -437,6 +528,7 @@ InventoriesEdit.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$l
'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'RelatedSearchInit', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'RelatedSearchInit',
'RelatedPaginateInit', 'ReturnToCaller', 'ClearScope', 'LookUpInit', 'Prompt', 'RelatedPaginateInit', 'ReturnToCaller', 'ClearScope', 'LookUpInit', 'Prompt',
'OrganizationList', 'TreeInit', 'GetBasePath', 'GroupsList', 'GroupsEdit', 'LoadInventory', 'OrganizationList', 'TreeInit', 'GetBasePath', 'GroupsList', 'GroupsEdit', 'LoadInventory',
'GroupsDelete', 'HostsList', 'HostsAdd', 'HostsEdit', 'HostsDelete', 'RefreshTree' 'GroupsDelete', 'HostsList', 'HostsAdd', 'HostsEdit', 'HostsDelete', 'RefreshTree',
'ParseTypeChange'
]; ];

View File

@@ -118,10 +118,6 @@ angular.module('InventoryFormDefinition', [])
label: 'Name', label: 'Name',
ngClick: "editHost(\{\{ host.id \}\}, '\{\{ host.name \}\}')" ngClick: "editHost(\{\{ host.id \}\}, '\{\{ host.name \}\}')"
}, },
description: {
label: 'Description',
ngClick: "editHost(\{\{ host.id \}\}, '\{\{ host.name \}\}')"
},
has_active_failures: { has_active_failures: {
label: 'Failures', label: 'Failures',
showValue: false, showValue: false,

View File

@@ -229,6 +229,7 @@ angular.module('InventoryHelper', [ 'RestServices', 'Utilities', 'OrganizationLi
// Load the tree view // Load the tree view
scope.TreeParams = { scope: scope, inventory: data }; scope.TreeParams = { scope: scope, inventory: data };
scope.variable_url = data.related.variable_data;
scope.relatedSets['hosts'] = { url: data.related.hosts, iterator: 'host' }; scope.relatedSets['hosts'] = { url: data.related.hosts, iterator: 'host' };
RelatedSearchInit({ scope: scope, form: form, relatedSets: scope.relatedSets }); RelatedSearchInit({ scope: scope, form: form, relatedSets: scope.relatedSets });
RelatedPaginateInit({ scope: scope, relatedSets: scope.relatedSets }); RelatedPaginateInit({ scope: scope, relatedSets: scope.relatedSets });