Added remaining grains module functions to grains states

This commit is contained in:
rallytime 2014-05-06 11:52:43 -06:00
parent 4873c9736c
commit 668604ff0e
2 changed files with 134 additions and 17 deletions

View File

@ -178,7 +178,7 @@ def setvals(grains, destructive=False):
.. code-block:: bash
salt '*' grains.setvals "{'key1': 'vali1', 'key2': 'val2'}"
salt '*' grains.setvals "{'key1': 'val1', 'key2': 'val2'}"
'''
new_grains = grains
if not isinstance(new_grains, collections.Mapping):

View File

@ -71,9 +71,9 @@ def list_present(name, value):
The grain name
value
The value is present in the list type grain
The value is present in the list type grain
The grain should be `list type <http://docs.python.org/2/tutorial/datastructures.html#data-structures>`_ .
The grain should be `list type <http://docs.python.org/2/tutorial/datastructures.html#data-structures>`_
.. code-block:: yaml
@ -127,9 +127,9 @@ def list_absent(name, value):
The grain name
value
The value is absent in the list type grain
The value is absent in the list type grain
The grain should be `list type <http://docs.python.org/2/tutorial/datastructures.html#data-structures>`_ .
The grain should be `list type <http://docs.python.org/2/tutorial/datastructures.html#data-structures>`_
.. code-block:: yaml
@ -147,7 +147,7 @@ def list_absent(name, value):
# check whether grain is a list
if not isinstance(grain, list):
ret['result'] = False
ret['coment'] = 'Grain {0} is not a valid list'
ret['comment'] = 'Grain {0} is not a valid list'.format(name)
return ret
if value not in grain:
@ -155,7 +155,7 @@ def list_absent(name, value):
return ret
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'Value {1} is set to be remove from grain {0}'.format(name, value)
ret['comment'] = 'Value {1} is set to be removed from grain {0}'.format(name, value)
ret['changes'] = {'old': grain}
return ret
__salt__['grains.remove'](name, value)
@ -163,15 +163,15 @@ def list_absent(name, value):
if value in __grains__.get(name):
ret['result'] = False
ret['comment'] = 'Failed remove value {1} from grain {0}'.format(name, value)
ret['comment'] = 'Failed to remove value {1} from grain {0}'.format(name, value)
return ret
ret['comment'] = 'Remove value {1} from grain {0}'.format(name, value)
ret['comment'] = 'Removed value {1} from grain {0}'.format(name, value)
else:
ret['comment'] = 'Grain {0} is not exist or empty'.format(name)
ret['comment'] = 'Grain {0} does not exist or is empty'.format(name)
return ret
def deleted(name, destructive=False):
def absent(name, destructive=False):
'''
.. versionadded:: Helium
@ -180,13 +180,14 @@ def deleted(name, destructive=False):
name
The grain name
:param destructive: If destructive is true, delete the entire grain. If destructive
is false, set the grain's value to None. Defaults to False.
:param destructive: If destructive is True, delete the entire grain.
If destructive is False, set the grain's value to None. Defaults
to False.
.. code-block:: yaml
grain_name:
grains.deleted
grains.absent
'''
ret = {'name': name,
@ -198,10 +199,12 @@ def deleted(name, destructive=False):
if __opts__['test']:
ret['result'] = None
if destructive is True:
ret['comment'] = 'Grain {0} is set to be deleted'.format(name)
ret['comment'] = 'Grain {0} is set to be deleted'\
.format(name)
ret['changes'] = {'deleted': name}
else:
ret['comment'] = 'Value for grain {0} is set to be deleted (None)'.format(name)
ret['comment'] = 'Value for grain {0} is set to be ' \
'deleted (None)'.format(name)
ret['changes'] = {'grain': name, 'value': None}
return ret
__salt__['grains.delval'](name, destructive)
@ -209,8 +212,122 @@ def deleted(name, destructive=False):
ret['comment'] = 'Grain {0} was deleted'.format(name)
ret['changes'] = {'deleted': name}
else:
ret['comment'] = 'Value for grain {0} was set to {1}'.format(name, None)
ret['comment'] = 'Value for grain {0} was set to {1}'\
.format(name, None)
ret['changes'] = {'grain': name, 'value': None}
else:
ret['comment'] = 'Grain {0} does not exist'.format(name)
return ret
def absent_list(name, value):
'''
.. versionadded:: Helium
Delete a value from a list in the grains config file
name
The grain name
value
The value to delete
.. code-block:: yaml
grain_name:
grains.absent_list:
- value: to_be_absent
'''
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
grain = __grains__.get(name)
if grain:
if isinstance(grain, list):
if value not in grain:
ret['comment'] = 'Value {1} was not found in the list ' \
'for grain {0}'.format(name, value)
return ret
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'Value {1} in grain {0} is set to ' \
'be deleted'.format(name, value)
ret['changes'] = {'deleted': value}
return ret
__salt__['grains.remove'](name, value)
ret['comment'] = 'Value {1} was deleted from grain {0}'\
.format(name, value)
ret['changes'] = {'deleted': value}
else:
ret['comment'] = 'Grain {0} is not a valid list'\
.format(name)
else:
ret['comment'] = 'Grain {0} does not exist'.format(name)
return ret
def append(name, value, convert=False):
'''
.. versionadded:: Helium
Append a value to a list in the grains config file
name
The grain name
value
The value to append
:param convert: If convert is True, convert non-list contents
into a list. If convert is False and the grain contains non-list
contents, an error is given. Defaults to False.
.. code-block:: yaml
grain_name:
grains.append:
- value: to_be_appended
'''
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
grain = __grains__.get(name)
if grain:
if isinstance(grain, list):
if value in grain:
ret['comment'] = 'Value {1} is already in the list ' \
'for grain {0}'.format(name, value)
return ret
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'Value {1} in grain {0} is set to ' \
'be added'.format(name, value)
ret['changes'] = {'added': value}
return ret
__salt__['grains.append'](name, value)
ret['comment'] = 'Value {1} was added to grain {0}'\
.format(name, value)
ret['changes'] = {'added': value}
else:
if convert is True:
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'Grain {0} is set to be converted ' \
'to list and value {1} will be ' \
'added'.format(name, value)
ret['changes'] = {'added': value}
return ret
grain = [grain]
grain.append(value)
__salt__['grains.setval'](name, grain)
ret['comment'] = 'Value {1} was added to grain {0}'\
.format(name, value)
ret['changes'] = {'added': value}
else:
ret['comment'] = 'Grain {0} is not a valid list'\
.format(name)
else:
ret['comment'] = 'Grain {0} does not exist'.format(name)
return ret