mirror of
https://github.com/ansible/awx.git
synced 2026-08-01 10:29:56 -02:30
Added YAML support to Host and Group variable data. User can enter variables in either format, UI will parse it and send to API as JSON. Fixed post to /hosts/N/variable_data and /groups/N/variable_data so that JSON is sent as an object rather than a string.
This commit is contained in:
60
ansibleworks/ui/static/js/helpers/Parse.js
Normal file
60
ansibleworks/ui/static/js/helpers/Parse.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/*********************************************
|
||||
* Copyright (c) 2013 AnsibleWorks, Inc.
|
||||
*
|
||||
* ParseHelper
|
||||
*
|
||||
* Routines for parsing variable data and toggling
|
||||
* between JSON and YAML.
|
||||
*
|
||||
*/
|
||||
|
||||
angular.module('ParseHelper', [])
|
||||
.factory('ParseTypeChange', [function() {
|
||||
return function(scope) {
|
||||
|
||||
// Toggle displayed variable string between JSON and YAML
|
||||
|
||||
scope.blockParseTypeWatch = false;
|
||||
scope.blockVariableDataWatch = false;
|
||||
|
||||
if (scope.removeParseTypeWatch) {
|
||||
scope.removeParseTypeWatch();
|
||||
}
|
||||
scope.removeParseTypeWatch = scope.$watch('parseType', function(newVal, oldVal) {
|
||||
if (newVal !== oldVal) {
|
||||
if (newVal == 'json') {
|
||||
if ( scope.variables && !/^---$/.test(scope.variables)) {
|
||||
// convert YAML to JSON
|
||||
try {
|
||||
var json_obj = jsyaml.load(scope.variables); //parse yaml into an obj
|
||||
scope.variables = JSON.stringify(json_obj, null, " ");
|
||||
}
|
||||
catch (err) {
|
||||
// ignore parse errors. allow the user to paste values in and sync the
|
||||
// radio button later. parse errors will be flagged on save.
|
||||
}
|
||||
}
|
||||
else {
|
||||
scope.variables = "\{\}";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( scope.variables && !/^\{\}$/.test(scope.variables) ) {
|
||||
// convert JSON to YAML
|
||||
try {
|
||||
var json_obj = JSON.parse(scope.variables);
|
||||
scope.variables = jsyaml.safeDump(json_obj);
|
||||
}
|
||||
catch (err) {
|
||||
// ignore the errors. allow the user to paste values in and sync the
|
||||
// radio button later. parse errors will be flagged on save.
|
||||
}
|
||||
}
|
||||
else {
|
||||
scope.variables = "---";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}]);
|
||||
Reference in New Issue
Block a user