Skip to content

Commit

Permalink
Merge pull request #1 from terminalmage/file_managed_pillar_source_sc…
Browse files Browse the repository at this point in the history
…heme

Convert pillar:// scheme to contents_pillar variable
  • Loading branch information
rca committed Aug 5, 2013
2 parents 3d4d771 + 3077e63 commit 1df0f62
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
# Import salt libs
import salt.utils
import salt.utils.templates
from salt._compat import string_types, urlparse
from salt._compat import string_types

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -689,6 +689,7 @@ def managed(name,
show_diff=True,
create=True,
contents=None,
contents_pillar=None,
**kwargs):
'''
Manage a given file, this function allows for a file to be downloaded from
Expand Down Expand Up @@ -772,9 +773,13 @@ def managed(name,
contents of the file. Should not be used in conjunction with a source
file of any kind. Ignores hashes and does not use a templating engine.
The contents parameter can extract pillar data by using a pillar://
prefix and followed by a pillar path as understood by the pillar.get
function
contents_pillar
Operates like ``contents``, but draws from a value stored in pillar,
using the pillar path syntax used in :mod:`pillar.get
<salt.modules.pillar.get>`. This is useful when the pillar value
contains newlines, as referencing a pillar variable using a jinja/mako
template can result in YAML formatting issues due to the newlines
causing indentation mismatches.
'''
# Make sure that leading zeros stripped by YAML loader are added back
mode = __salt__['config.manage_mode'](mode)
Expand Down Expand Up @@ -811,10 +816,16 @@ def managed(name,
return _error(
ret, 'Context must be formed as a dict')

# if the contents specifies a pillar scheme, convert it here
if urlparse(contents).scheme == 'pillar':
pillar_path = contents.replace('pillar://','')
contents = __salt__['pillar.get'](pillar_path)
if contents and contents_pillar:
return _error(
ret, 'Only one of contents and contents_pillar is permitted')

# If contents_pillar was used, get the pillar data
if contents_pillar:
contents = __salt__['pillar.get'](contents_pillar)
# Make sure file ends in newline
if not contents.endswith('\n'):
contents += '\n'

if not replace and os.path.exists(name):
# Check and set the permissions if necessary
Expand Down

0 comments on commit 1df0f62

Please sign in to comment.