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): elif isinstance(val, list):
_new_value_type = 'complex' _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 _value = _existing_value
_existing_value_type = 'simple' _existing_value_type = 'simple'
if _existing_value == _non_existent_key: if _existing_value is _non_existent:
_existing_value_type = None _existing_value_type = None
elif isinstance(_existing_value, dict): elif isinstance(_existing_value, dict):
_existing_value_type = 'complex' _existing_value_type = 'complex'

View File

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