implements sls renderer

This commit is contained in:
Xavier Barbosa 2014-04-07 18:13:23 +02:00
parent 60d2b2cad2
commit ce2cf98518
6 changed files with 138 additions and 0 deletions

View File

@ -16,6 +16,7 @@ Full list of builtin renderer modules
py
pydsl
pyobjects
sls
stateconf
wempy
yaml

View File

@ -0,0 +1,40 @@
==================
salt.renderers.sls
==================
SLS renderer is a remplacement of the YAML renderer.
It's 100% YAML with a pinch of Salt magic:
* All mappings are automatically OrderedDict
* All strings are automatically str obj
* data aggregation with !aggregation yaml tag, based on the ``salt.utils.aggregation`` module.
* data aggregation over documents for pillar
Instructed aggregation within the ``!aggregation`` and the ``!reset`` tags:
.. code-block:: yaml
#!sls
foo: !aggregate first
foo: !aggregate second
bar: !aggregate {first: foo}
bar: !aggregate {second: bar}
baz: !aggregate 42
qux: !aggregate default
!reset qux: !aggregate my custom data
is roughly equivalent to
.. code-block:: yaml
foo: [first, second]
bar: {first: foo, second: bar}
baz: [42]
qux: [my custom data]
Reference
---------
.. automodule:: salt.renderers.sls
:members:

33
salt/renderers/sls.py Normal file
View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
# Import python libs
import logging
import warnings
# Import salt libs
from salt.utils.serializers.sls import deserialize
log = logging.getLogger(__name__)
def render(sls_data, saltenv='base', sls='', **kws):
'''
Accepts SLS as a string or as a file object and runs it through the SLS
parser.
:rtype: A Python data structure
'''
with warnings.catch_warnings(record=True) as warn_list:
data = deserialize(sls_data) or {}
for item in warn_list:
log.warn(
'{warn} found in salt://{sls} environment={saltenv}'.format(
warn=item.message, sls=sls, saltenv=saltenv
)
)
log.debug('Results of SLS rendering: \n{0}'.format(data))
return data

View File

@ -157,6 +157,9 @@ for comb in """
json_jinja
json_mako
json_wempy
sls_jinja
sls_mako
sls_wempy
""".strip().split():
fmt, tmpl = comb.split('_')

View File

@ -0,0 +1 @@
# -*- coding: utf-8 -*-

View File

@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
from salttesting import skipIf, TestCase
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../')
import salt.state
from salt.config import minion_config
from salt.template import compile_template_str
from salt.utils.serializers import sls
basic_template = '''#!sls
foo: bar
'''
complex_template = '''#!sls
placeholder: {foo: !aggregate {foo: 42}}
placeholder: {foo: !aggregate {bar: null}}
placeholder: {foo: !aggregate {baz: inga}}
'''
SKIP_MESSAGE = '%s is unavailable, do prerequisites have been met?'
class RendererMixin(object):
def render(self, template, opts=None):
_config = minion_config(None)
_config['file_client'] = 'local'
if opts:
_config.update(opts)
_state = salt.state.State(_config)
return compile_template_str(template,
_state.rend,
_state.opts['renderer'])
class RendererTests(TestCase, RendererMixin):
@skipIf(not sls.available, SKIP_MESSAGE % 'sls')
def test_basic(self):
sls_obj = self.render(basic_template)
assert sls_obj == {'foo': 'bar'}, sls_obj
@skipIf(not sls.available, SKIP_MESSAGE % 'sls')
def test_complex(self):
sls_obj = self.render(complex_template)
assert sls_obj == {
'placeholder': {
'foo': {
'foo': 42,
'bar': None,
'baz': 'inga'
}
}
}, sls_obj
if __name__ == '__main__':
from integration import run_tests
run_tests(RendererTests, needs_daemon=False)