Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c99dbd8

Browse files
author
Nicole Thomas
authored
Merge pull request saltstack#45730 from mchugh19/state.exists
RFC: create state.exists
2 parents de30884 + 8ec1c66 commit c99dbd8

File tree

2 files changed

+100
-14
lines changed

2 files changed

+100
-14
lines changed

salt/modules/state.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,8 +1257,7 @@ def sls(mods, test=None, exclude=None, queue=False, **kwargs):
12571257
return st_.state.call_high(high_, orchestration_jid)
12581258
os.umask(umask)
12591259

1260-
if isinstance(mods, six.string_types):
1261-
mods = mods.split(',')
1260+
mods = salt.utils.args.split_input(mods)
12621261

12631262
st_.push_active()
12641263
try:
@@ -1269,8 +1268,7 @@ def sls(mods, test=None, exclude=None, queue=False, **kwargs):
12691268
return errors
12701269

12711270
if exclude:
1272-
if isinstance(exclude, six.string_types):
1273-
exclude = exclude.split(',')
1271+
exclude = salt.utils.args.split_input(exclude)
12741272
if '__exclude__' in high_:
12751273
high_['__exclude__'].extend(exclude)
12761274
else:
@@ -1665,8 +1663,7 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs):
16651663
__context__['retcode'] = 5
16661664
return ['Pillar failed to render with the following messages:'] + errors
16671665

1668-
if isinstance(mods, six.string_types):
1669-
split_mods = mods.split(',')
1666+
split_mods = salt.utils.args.split_input(mods)
16701667
st_.push_active()
16711668
try:
16721669
high_, errors = st_.render_highstate({opts['saltenv']: split_mods})
@@ -1774,8 +1771,7 @@ def show_low_sls(mods, test=None, queue=False, **kwargs):
17741771
__context__['retcode'] = 5
17751772
raise CommandExecutionError('Pillar failed to render', info=errors)
17761773

1777-
if isinstance(mods, six.string_types):
1778-
mods = mods.split(',')
1774+
mods = salt.utils.args.split_input(mods)
17791775
st_.push_active()
17801776
try:
17811777
high_, errors = st_.render_highstate({opts['saltenv']: mods})
@@ -1863,8 +1859,7 @@ def show_sls(mods, test=None, queue=False, **kwargs):
18631859
__context__['retcode'] = 5
18641860
raise CommandExecutionError('Pillar failed to render', info=errors)
18651861

1866-
if isinstance(mods, six.string_types):
1867-
mods = mods.split(',')
1862+
mods = salt.utils.args.split_input(mods)
18681863
st_.push_active()
18691864
try:
18701865
high_, errors = st_.render_highstate({opts['saltenv']: mods})
@@ -1880,6 +1875,56 @@ def show_sls(mods, test=None, queue=False, **kwargs):
18801875
return high_
18811876

18821877

1878+
def sls_exists(mods, test=None, queue=False, **kwargs):
1879+
'''
1880+
Tests for the existance the of a specific SLS or list of SLS files on the
1881+
master. Similar to :py:func:`state.show_sls <salt.modules.state.show_sls>`,
1882+
rather than returning state details, returns True or False. The default
1883+
environment is ``base``, use ``saltenv`` to specify a different environment.
1884+
1885+
.. versionadded:: Fluorine
1886+
1887+
saltenv
1888+
Specify a salt fileserver environment from which to look for the SLS files
1889+
specified in the ``mods`` argument
1890+
1891+
CLI Example:
1892+
1893+
.. code-block:: bash
1894+
1895+
salt '*' state.sls_exists core,edit.vim saltenv=dev
1896+
'''
1897+
return isinstance(
1898+
show_sls(mods, test=test, queue=queue, **kwargs),
1899+
dict
1900+
)
1901+
1902+
1903+
def id_exists(ids, mods, test=None, queue=False, **kwargs):
1904+
'''
1905+
Tests for the existence of a specific ID or list of IDs within the
1906+
specified SLS file(s). Similar to :py:func:`state.sls_exists
1907+
<salt.modules.state.sls_exists>`, returns True or False. The default
1908+
environment is base``, use ``saltenv`` to specify a different environment.
1909+
1910+
.. versionadded:: Fluorine
1911+
1912+
saltenv
1913+
Specify a salt fileserver environment from which to look for the SLS files
1914+
specified in the ``mods`` argument
1915+
1916+
CLI Example:
1917+
1918+
.. code-block:: bash
1919+
1920+
salt '*' state.id_exists create_myfile,update_template filestate saltenv=dev
1921+
'''
1922+
ids = salt.utils.args.split_input(ids)
1923+
ids = set(ids)
1924+
sls_ids = set(x['__id__'] for x in show_low_sls(mods, test=test, queue=queue, **kwargs))
1925+
return ids.issubset(sls_ids)
1926+
1927+
18831928
def show_top(queue=False, **kwargs):
18841929
'''
18851930
Return the top data that the minion will use for a highstate
@@ -2116,8 +2161,7 @@ def disable(states):
21162161
'msg': ''
21172162
}
21182163

2119-
if isinstance(states, six.string_types):
2120-
states = states.split(',')
2164+
states = salt.utils.args.split_input(states)
21212165

21222166
msg = []
21232167
_disabled = __salt__['grains.get']('state_runs_disabled')
@@ -2168,8 +2212,7 @@ def enable(states):
21682212
'msg': ''
21692213
}
21702214

2171-
if isinstance(states, six.string_types):
2172-
states = states.split(',')
2215+
states = salt.utils.args.split_input(states)
21732216
log.debug('states %s', states)
21742217

21752218
msg = []

tests/unit/modules/test_state.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,49 @@ def test_show_sls(self):
698698
self.assertListEqual(state.show_sls("foo"),
699699
['a', 'b'])
700700

701+
def test_sls_exists(self):
702+
'''
703+
Test of sls_exists
704+
'''
705+
test_state = {}
706+
test_missing_state = []
707+
708+
mock = MagicMock(return_value=test_state)
709+
with patch.object(state, 'show_sls', mock):
710+
self.assertTrue(state.sls_exists("state_name"))
711+
mock = MagicMock(return_value=test_missing_state)
712+
with patch.object(state, 'show_sls', mock):
713+
self.assertFalse(state.sls_exists("missing_state"))
714+
715+
def test_id_exists(self):
716+
'''
717+
Test of id_exists
718+
'''
719+
test_state = [{
720+
"key1": "value1",
721+
"name": "value1",
722+
"state": "file",
723+
"fun": "test",
724+
"__env__": "base",
725+
"__sls__": "test-sls",
726+
"order": 10000,
727+
"__id__": "state_id1"
728+
},
729+
{
730+
"key2": "value2",
731+
"name": "value2",
732+
"state": "file",
733+
"fun": "directory",
734+
"__env__": "base",
735+
"__sls__": "test-sls",
736+
"order": 10001,
737+
"__id__": "state_id2"
738+
}]
739+
mock = MagicMock(return_value=test_state)
740+
with patch.object(state, 'show_low_sls', mock):
741+
self.assertTrue(state.id_exists("state_id1,state_id2", "test-sls"))
742+
self.assertFalse(state.id_exists("invalid", "state_name"))
743+
701744
def test_top(self):
702745
'''
703746
Test to execute a specific top file

0 commit comments

Comments
 (0)