Add yaml/json parsing to values of kwargs for state.single.

Also fixed an incorrect use sls_encoder in compile_template_str().
This commit is contained in:
Jack Kuan 2012-11-06 01:26:08 -05:00
parent 71fafe41f1
commit f2d8c5cef3
4 changed files with 27 additions and 5 deletions

View File

@ -9,6 +9,9 @@ import os
# Import Salt libs
import salt.state
import salt.payload
from salt.utils.yaml import load as yaml_load
from salt.utils.yaml import CustomLoader as YamlCustomLoader
import json
from salt._compat import string_types
__outputter__ = {
@ -190,14 +193,20 @@ def show_masterstate():
return st_.compile_master()
def single(fun, name, test=None, **kwargs):
def single(fun, name, test=None, kwval_as='yaml', **kwargs):
'''
Execute a single state function with the named kwargs, returns False if
insufficient data is sent to the command
By default, the values of the kwargs will be parsed as YAML. So, you can
specify lists values, or lists of single entry key-value maps, as you
would in a YAML salt file. Alternatively, JSON format of keyword values
is also supported.
CLI Example::
salt '*' state.single pkg.installed name=vim
'''
comps = fun.split('.')
if len(comps) < 2:
@ -213,5 +222,19 @@ def single(fun, name, test=None, **kwargs):
err = st_.verify_data(kwargs)
if err:
return err
if kwval_as == 'yaml':
def parse_kwval(value):
return yaml_load(value, YamlCustomLoader)
elif kwval_as == 'json':
def parse_kwval(value):
return json.loads(value)
else:
return 'Unknown format(%s) for state keyword arguments!' % kwval_as
for key, value in kwargs.iteritems():
if not key.startswith('__pub_'):
kwargs[key] = parse_kwval(value)
return {'{0[state]}_|-{0[__id__]}_|-{0[name]}_|-{0[fun]}'.format(kwargs):
st_.call(kwargs)}

View File

@ -72,7 +72,7 @@ def compile_template_str(template, renderers, default):
fd_, fn_ = tempfile.mkstemp()
os.close(fd_)
with open(fn_, 'wb') as f:
f.write(sls_encoder(template))
f.write(sls_encoder(template)[0])
return compile_template(fn_, renderers, default)

View File

@ -198,7 +198,7 @@ fi
try:
ret = self.run_function('state.template_str', [template])
self.assertTrue(isinstance(ret, dict)), ret
self.assertTrue(isinstance(ret, dict))
self.assertNotEqual(ret, {})
for key in ret.iterkeys():

View File

@ -166,8 +166,7 @@ class FileTest(integration.ModuleCase):
name = os.path.join(integration.TMP, 'recurse_template_dir')
ret = self.run_state(
'file.recurse', name=name, source='salt://grail',
# For some strange reason passing defaults as a map does not work
template='jinja', defaults={'spam': _ts}, spam=_ts)
template='jinja', defaults={'spam': _ts})
result = self.state_result(ret)
self.assertTrue(result)
self.assertIn(_ts, open(os.path.join(name, 'scene33'), 'r').read())