diff --git a/lib/ui/static/js/controllers/Groups.js b/lib/ui/static/js/controllers/Groups.js index 2025d762fa..d86341242a 100644 --- a/lib/ui/static/js/controllers/Groups.js +++ b/lib/ui/static/js/controllers/Groups.js @@ -166,25 +166,45 @@ function GroupsAdd ($scope, $rootScope, $compile, $location, $log, $routeParams, // Save scope.formSave = function() { - Rest.setUrl(defaultUrl); - var data = {} - for (var fld in form.fields) { - data[fld] = scope[fld]; - } - - if ($routeParams.inventory_id) { - data['inventory'] = $routeParams.inventory_id; - } + try { + // Make sure we have valid JSON + var myjson = JSON.parse(scope.variables); + + var data = {} + for (var fld in form.fields) { + if (fld != 'variables') { + data[fld] = scope[fld]; + } + } - Rest.post(data) - .success( function(data, status, headers, config) { - ReturnToCaller(1); - }) - .error( function(data, status, headers, config) { - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to add new group. Post returned status: ' + status }); - }); - }; + if ($routeParams.inventory_id) { + data['inventory'] = $routeParams.inventory_id; + } + + Rest.setUrl(defaultUrl); + Rest.post(data) + .success( function(data, status, headers, config) { + if (scope.variables) { + Rest.setUrl(data.related.variable_data); + Rest.put({data: scope.variables}) + .success( function(data, status, headers, config) { + ReturnToCaller(1); + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to add group varaibles. PUT returned status: ' + status }); + }); + } + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to add new group. Post returned status: ' + status }); + }); + } + catch(err) { + Alert("Error", "Error parsing group variables. Expecting valid JSON. Parser returned " + err); + } + } // Cancel scope.formReset = function() { @@ -222,6 +242,26 @@ function GroupsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams for (var set in relatedSets) { scope.search(relatedSets[set].iterator); } + if (scope.variable_url) { + Rest.setUrl(scope.variable_url); + Rest.get() + .success( function(data, status, headers, config) { + if ($.isEmptyObject(data.data)) { + scope.variables = "\{\}"; + } + else { + scope.variables = data.data; + } + }) + .error( function(data, status, headers, config) { + scope.variables = null; + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to retrieve host variables. GET returned status: ' + status }); + }); + } + else { + scope.variables = "\{\}"; + } }); // Retrieve detail record and prepopulate the form @@ -241,6 +281,9 @@ function GroupsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams relatedSets[set] = { url: related[set], iterator: form.related[set].iterator }; } } + + scope.variable_url = data.related.variable_data; + // Initialize related search functions. Doing it here to make sure relatedSets object is populated. RelatedSearchInit({ scope: scope, form: form, relatedSets: relatedSets }); RelatedPaginateInit({ scope: scope, relatedSets: relatedSets }); @@ -253,21 +296,42 @@ function GroupsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams // Save changes to the parent scope.formSave = function() { - Rest.setUrl(defaultUrl); - var data = {} - for (var fld in form.fields) { - data[fld] = scope[fld]; - } - Rest.put(data) - .success( function(data, status, headers, config) { - var base = $location.path().replace(/^\//,'').split('/')[0]; - (base == 'groups') ? ReturnToCaller() : ReturnToCaller(1); - }) - .error( function(data, status, headers, config) { - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to update group: ' + id + '. PUT status: ' + status }); - }); - }; + try { + // Make sure we have valid JSON + var myjson = JSON.parse(scope.variables); + + var data = {} + for (var fld in form.fields) { + data[fld] = scope[fld]; + } + + Rest.setUrl(defaultUrl); + Rest.put(data) + .success( function(data, status, headers, config) { + if (scope.variables) { + Rest.setUrl(GetBasePath('groups') + data.id + '/variable_data/'); + Rest.put({data: scope.variables}) + .success( function(data, status, headers, config) { + var base = $location.path().replace(/^\//,'').split('/')[0]; + (base == 'groups') ? ReturnToCaller() : ReturnToCaller(1); + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to update group varaibles. PUT returned status: ' + status }); + }); + } + var base = $location.path().replace(/^\//,'').split('/')[0]; + (base == 'groups') ? ReturnToCaller() : ReturnToCaller(1); + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to update group: ' + id + '. PUT status: ' + status }); + }); + } + catch(err) { + Alert("Error", "Error parsing group variables. Expecting valid JSON. Parser returned " + err); + } + }; // Cancel scope.formReset = function() { diff --git a/lib/ui/static/js/controllers/Hosts.js b/lib/ui/static/js/controllers/Hosts.js index 4a711e84d0..a65004d844 100644 --- a/lib/ui/static/js/controllers/Hosts.js +++ b/lib/ui/static/js/controllers/Hosts.js @@ -175,27 +175,35 @@ function HostsAdd ($scope, $rootScope, $compile, $location, $log, $routeParams, if ($routeParams.inventory_id) { data['inventory'] = $routeParams.inventory_id; } - Rest.post(data) - .success( function(data, status, headers, config) { - if (scope.variables) { - Rest.setUrl(GetBasePath('hosts') + data.id + '/variable_data/'); - Rest.post({data: scope.variables}) - .success( function(data, status, headers, config) { - ReturnToCaller(1); - }) - .error( function(data, status, headers, config) { - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to add host varaibles. POST returned status: ' + status }); - }); - } - else { - ReturnToCaller(1); - } - }) - .error( function(data, status, headers, config) { - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to add new host. POST returned status: ' + status }); - }); + try { + // Make sure we have valid JSON + var myjson = JSON.parse(scope.variables); + Rest.post(data) + .success( function(data, status, headers, config) { + if (scope.variables) { + Rest.setUrl(data.related.variable_data); + Rest.put({data: scope.variables}) + .success( function(data, status, headers, config) { + ReturnToCaller(1); + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to add host varaibles. POST returned status: ' + status }); + }); + } + else { + ReturnToCaller(1); + } + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to add new host. POST returned status: ' + status }); + }); + } + catch(err) { + Alert("Error", "Error parsing host variables. Expecting valid JSON. Parser returned " + err); + } + }; // Cancel @@ -236,7 +244,12 @@ function HostsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, Rest.setUrl(scope.variable_url); Rest.get() .success( function(data, status, headers, config) { - scope.variables = data.data; + if ($.isEmptyObject(data.data)) { + scope.variables = "\{\}"; + } + else { + scope.variables = data.data; + } }) .error( function(data, status, headers, config) { scope.variables = null; @@ -245,7 +258,7 @@ function HostsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, }); } else { - scope.variables = null; + scope.variables = "\{\}"; } }); @@ -283,28 +296,37 @@ function HostsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, var data = {} for (var fld in form.fields) { data[fld] = scope[fld]; - } - Rest.put(data) - .success( function(data, status, headers, config) { - if (scope.variables) { - Rest.setUrl(GetBasePath('hosts') + data.id + '/variable_data/'); - Rest.put({data: scope.variables}) - .success( function(data, status, headers, config) { - (base == 'hosts') ? ReturnToCaller() : ReturnToCaller(1); - }) - .error( function(data, status, headers, config) { - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to update host varaibles. PUT returned status: ' + status }); - }); - } - else { - (base == 'hosts') ? ReturnToCaller() : ReturnToCaller(1); - } - }) - .error( function(data, status, headers, config) { - ProcessErrors(scope, data, status, form, - { hdr: 'Error!', msg: 'Failed to update host: ' + id + '. PUT status: ' + status }); - }); + } + + try { + // Make sure we have valid JSON + var myjson = JSON.parse(scope.variables); + Rest.put(data) + .success( function(data, status, headers, config) { + if (scope.variables) { + Rest.setUrl(GetBasePath('hosts') + data.id + '/variable_data/'); + Rest.put({data: scope.variables}) + .success( function(data, status, headers, config) { + (base == 'hosts') ? ReturnToCaller() : ReturnToCaller(1); + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to update host varaibles. PUT returned status: ' + status }); + }); + } + else { + (base == 'hosts') ? ReturnToCaller() : ReturnToCaller(1); + } + }) + .error( function(data, status, headers, config) { + ProcessErrors(scope, data, status, form, + { hdr: 'Error!', msg: 'Failed to update host: ' + id + '. PUT status: ' + status }); + }); + } + catch(err) { + Alert("Error", "Error parsing host variables. Expecting valid JSON. Parser returned " + err); + } + }; // Cancel diff --git a/lib/ui/static/js/controllers/Inventories.js b/lib/ui/static/js/controllers/Inventories.js index 20004f133c..c421672bce 100644 --- a/lib/ui/static/js/controllers/Inventories.js +++ b/lib/ui/static/js/controllers/Inventories.js @@ -378,12 +378,6 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP show: true }); } - }, - vairables: { - label: 'Host Variables', - separator_before: true, - "_disabled": true, - action: function(obj) { } } } } @@ -392,17 +386,7 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP addGroup: { label: 'Add Group', action: function() { changePath($location.path() + '/groups'); } - }, - addHost: { - label: 'Add Host', - action: function() { changePath($location.path() + '/hosts'); } - }, - variables: { - label: 'Inventory variables', - action: function(obj) { }, - "_disabled": true, - separator_before: true - } + } } } else { @@ -459,14 +443,8 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP show: true }); }, - "_disabled": (nodeType == 'all-hosts-group') ? true : false - }, - variables: { - label: 'Group variables', - action: function(obj) { }, - "_disabled": true, - separator_before: true - } + "_disabled": (nodeType == 'all-hosts-group') ? true : false + } } } } diff --git a/lib/ui/static/js/forms/Groups.js b/lib/ui/static/js/forms/Groups.js index 28258a6887..7acf50d6da 100644 --- a/lib/ui/static/js/forms/Groups.js +++ b/lib/ui/static/js/forms/Groups.js @@ -27,14 +27,16 @@ angular.module('GroupFormDefinition', []) type: 'text', addRequired: false, editRequired: false + }, + variables: { + label: 'Variables', + type: 'textarea', + addRequired: false, + editRequird: false, + rows: 10, + class: 'span12', + default: "\{\}" } - // , - // inventory: { - // label: 'Inventory', - // type: 'hidden', - // includeOnEdit: true, - // includeOnAdd: true - // } }, buttons: { //for now always generates