grains module and state: use a unique object...

instead of a magic string to check the existence of a key/value
This commit is contained in:
Christophe Drevet-Droguet 2015-10-14 20:52:17 +02:00
parent e5c77e6f64
commit 1e3f1b0961
2 changed files with 9 additions and 7 deletions

View File

@ -618,11 +618,12 @@ def set(key,
elif isinstance(val, list):
_new_value_type = 'complex'
_existing_value = get(key, _non_existent_key, delimiter)
_non_existent = object()
_existing_value = get(key, _non_existent, delimiter)
_value = _existing_value
_existing_value_type = 'simple'
if _existing_value == _non_existent_key:
if _existing_value is _non_existent:
_existing_value_type = None
elif isinstance(_existing_value, dict):
_existing_value_type = 'complex'

View File

@ -64,13 +64,14 @@ def present(name, value, delimiter=DEFAULT_TARGET_DELIM, force=False):
'changes': {},
'result': True,
'comment': ''}
existing = __salt__['grains.get'](name, 'nonexistingzniQSDpWy3ywmgFn66lL4PLG9oBrDi')
_non_existent = object()
existing = __salt__['grains.get'](name, _non_existent)
if existing == value:
ret['comment'] = 'Grain is already set'
return ret
if __opts__['test']:
ret['result'] = None
if existing == 'nonexistingzniQSDpWy3ywmgFn66lL4PLG9oBrDi':
if existing is _non_existent:
ret['comment'] = 'Grain {0} is set to be added'.format(name)
ret['changes'] = {'new': name}
else:
@ -264,14 +265,14 @@ def absent(name,
grains.absent
'''
_non_existent_key = 'NonExistentValueMagicNumberSpK3hnufdHfeBUXCfqVK'
_non_existent = object()
name = re.sub(delimiter, DEFAULT_TARGET_DELIM, name)
ret = {'name': name,
'changes': {},
'result': True,
'comment': ''}
grain = __salt__['grains.get'](name, _non_existent_key)
grain = __salt__['grains.get'](name, _non_existent)
if grain is None:
if __opts__['test']:
ret['result'] = None
@ -290,7 +291,7 @@ def absent(name,
.format(name)
ret['changes'] = {'deleted': name}
ret['name'] = name
elif grain != _non_existent_key:
elif grain is not _non_existent:
if __opts__['test']:
ret['result'] = None
if destructive is True: