fixing the beacon module and state module to handle passing enabled properly. Also reworking how what is returned from the validating functions is handled to ensure when beacon configurations aren't validate the results indicate exactly why.

This commit is contained in:
Gareth J. Greenaway 2016-02-11 22:06:01 -08:00
parent c8047d979d
commit 91b14dca40
14 changed files with 78 additions and 79 deletions

View File

@ -56,8 +56,10 @@ def validate(config):
''' '''
# Configuration for load beacon should be a list of dicts # Configuration for load beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
return False, ('Configuration for btmp beacon must '
'be a list of dictionaries.')
return False return False
return True return True, 'Valid beacon configuration'
# TODO: add support for only firing events for specific users and login times # TODO: add support for only firing events for specific users and login times

View File

@ -42,9 +42,9 @@ def validate(config):
''' '''
# Configuration for diskusage beacon should be a list of dicts # Configuration for diskusage beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for diskusage beacon must be a dictionary.') return False, ('Configuration for diskusage beacon '
return False 'must be a dictionary.')
return True return True, 'Valid beacon configuration'
def beacon(config): def beacon(config):

View File

@ -95,44 +95,39 @@ def validate(config):
] ]
# Configuration for diskusage beacon should be a list of dicts # Configuration for diskusage beacon should be a list of dicts
log.debug('config {0}'.format(config))
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for inotify beacon must be a dictionary.') return False, 'Configuration for inotify beacon must be a dictionary.'
return False
else: else:
for config_item in config: for config_item in config:
if not isinstance(config[config_item], dict): if not isinstance(config[config_item], dict):
log.info('Configuration for inotify beacon must ' return False, ('Configuration for inotify beacon must '
'be a dictionary of dictionaries.') 'be a dictionary of dictionaries.')
return False
else: else:
if not any(j in ['mask', 'recurse', 'auto_add'] for j in config[config_item]): if not any(j in ['mask', 'recurse', 'auto_add'] for j in config[config_item]):
log.info('Configuration for inotify beacon must ' return False, ('Configuration for inotify beacon '
'contain mask, recurse or auto_add items.') 'must contain mask, recurse or auto_add items.')
return False
if 'auto_add' in config[config_item]: if 'auto_add' in config[config_item]:
if not isinstance(config[config_item]['auto_add'], bool): if not isinstance(config[config_item]['auto_add'], bool):
log.info('Configuration for inotify beacon ' return False, ('Configuration for inotify beacon '
'auto_add must be boolean.') 'auto_add must be boolean.')
return False
if 'recurse' in config[config_item]: if 'recurse' in config[config_item]:
if not isinstance(config[config_item]['recurse'], bool): if not isinstance(config[config_item]['recurse'], bool):
log.info('Configuration for inotify beacon ' return False, ('Configuration for inotify beacon '
'recurse must be boolean.') ' recurse must be boolean.')
return False
if 'mask' in config[config_item]: if 'mask' in config[config_item]:
if not isinstance(config[config_item]['mask'], list): if not isinstance(config[config_item]['mask'], list):
log.info('Configuration for inotify beacon ' return False, ('Configuration for inotify beacon '
'mask must be list.') ' mask must be list.')
return False
for mask in config[config_item]['mask']: for mask in config[config_item]['mask']:
if mask not in VALID_MASK: if mask not in VALID_MASK:
log.info('Configuration for inotify beacon ' return False, ('Configuration for inotify beacon '
'invalid mask option {0}.'.format(mask)) 'invalid mask option {0}.'.format(mask))
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -54,10 +54,9 @@ def validate(config):
else: else:
for item in config: for item in config:
if not isinstance(config[item], dict): if not isinstance(config[item], dict):
log.info('Configuration for journald beacon must ' return False, ('Configuration for journald beacon must '
'be a dictionary of dictionaries.') 'be a dictionary of dictionaries.')
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -33,33 +33,28 @@ def validate(config):
# Configuration for load beacon should be a list of dicts # Configuration for load beacon should be a list of dicts
if not isinstance(config, list): if not isinstance(config, list):
log.info('Configuration for load beacon must be a list.') return False, ('Configuration for load beacon must be a list.')
return False
else: else:
for config_item in config: for config_item in config:
if not isinstance(config_item, dict): if not isinstance(config_item, dict):
log.info('Configuration for load beacon must ' return False, ('Configuration for load beacon must '
'be a list of dictionaries.') 'be a list of dictionaries.')
return False
else: else:
if not any(j in ['1m', '5m', '15m'] for j in config_item.keys()): if not any(j in ['1m', '5m', '15m'] for j in config_item.keys()):
log.info('Configuration for load beacon must ' return False, ('Configuration for load beacon must '
'contain 1m, 5m and 15m items.') 'contain 1m, 5m and 15m items.')
return False
for item in config_item: for item in config_item:
if not isinstance(config_item[item], list): if not isinstance(config_item[item], list):
log.info('Configuration for load beacon: ' return False, ('Configuration for load beacon: '
'1m, 5m and 15m items must be ' '1m, 5m and 15m items must be '
'a list of two items.') 'a list of two items.')
return False
else: else:
if len(config_item[item]) != 2: if len(config_item[item]) != 2:
log.info('Configuration for load beacon: ' return False, ('Configuration for load beacon: '
'1m, 5m and 15m items must be ' '1m, 5m and 15m items must be '
'a list of two items.') 'a list of two items.')
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -48,20 +48,17 @@ def validate(config):
# Configuration for load beacon should be a list of dicts # Configuration for load beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for load beacon must be a dictionary.') return False, ('Configuration for load beacon must be a dictionary.')
return False
else: else:
for item in config: for item in config:
if not isinstance(config[item], dict): if not isinstance(config[item], dict):
log.info('Configuration for load beacon must ' return False, ('Configuration for load beacon must '
'be a dictionary of dictionaries.') 'be a dictionary of dictionaries.')
return False
else: else:
if not any(j in VALID_ITEMS for j in config[item]): if not any(j in VALID_ITEMS for j in config[item]):
log.info('Invalid configuration item in ' return False, ('Invalid configuration item in '
'Beacon configuration.') 'Beacon configuration.')
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -38,9 +38,8 @@ def validate(config):
Validate the beacon configuration Validate the beacon configuration
''' '''
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for rest_example beacon must be a dictionary.') return False, ('Configuration for rest_example beacon must be a dictionary.')
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -18,9 +18,8 @@ def validate(config):
''' '''
# Configuration for ps beacon should be a list of dicts # Configuration for ps beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for ps beacon must be a dictionary.') return False, ('Configuration for ps beacon must be a dictionary.')
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -20,9 +20,8 @@ def validate(config):
''' '''
# Configuration for service beacon should be a list of dicts # Configuration for service beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for service beacon must be a dictionary.') return False, ('Configuration for service beacon must be a dictionary.')
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -47,9 +47,8 @@ def validate(config):
''' '''
# Configuration for sh beacon should be a list of dicts # Configuration for sh beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for sh beacon must be a dictionary.') return False, ('Configuration for sh beacon must be a dictionary.')
return False return True, 'Valid beacon configuration'
return True
def beacon(config): def beacon(config):

View File

@ -32,9 +32,9 @@ def validate(config):
''' '''
# Configuration for twilio_txt_msg beacon should be a list of dicts # Configuration for twilio_txt_msg beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for twilio_txt_msg beacon must be a dictionary.') return False, ('Configuration for twilio_txt_msg beacon '
return False 'must be a dictionary.')
return True return True, 'Valid beacon configuration'
def beacon(config): def beacon(config):

View File

@ -61,9 +61,8 @@ def validate(config):
''' '''
# Configuration for wtmp beacon should be a list of dicts # Configuration for wtmp beacon should be a list of dicts
if not isinstance(config, dict): if not isinstance(config, dict):
log.info('Configuration for wtmp beacon must be a dictionary.') return False, ('Configuration for wtmp beacon must be a dictionary.')
return False return True, 'Valid beacon configuration'
return True
# TODO: add support for only firing events for specific users and login times # TODO: add support for only firing events for specific users and login times

View File

@ -100,14 +100,19 @@ def add(name, beacon_data, **kwargs):
# Attempt to validate # Attempt to validate
if hasattr(beacon_module, 'validate'): if hasattr(beacon_module, 'validate'):
valid = beacon_module.validate(beacon_data) _beacon_data = beacon_data
if 'enabled' in _beacon_data:
del _beacon_data['enabled']
valid, vcomment = beacon_module.validate(_beacon_data)
else: else:
log.info('Beacon {0} does not have a validate' log.info('Beacon {0} does not have a validate'
' function, skipping validation.'.format(name)) ' function, skipping validation.'.format(name))
valid = True valid = True
if not valid: if not valid:
ret['comment'] = 'Beacon {0} configuration invalid, not adding.'.format(name) ret['result'] = False
ret['comment'] = ('Beacon {0} configuration invalid, '
'not adding.\n{1}'.format(name, vcomment))
return ret return ret
try: try:
@ -164,14 +169,19 @@ def modify(name, beacon_data, **kwargs):
# Attempt to validate # Attempt to validate
if hasattr(beacon_module, 'validate'): if hasattr(beacon_module, 'validate'):
valid = beacon_module.validate(beacon_data) _beacon_data = beacon_data
if 'enabled' in _beacon_data:
del _beacon_data['enabled']
valid, vcomment = beacon_module.validate(_beacon_data)
else: else:
log.info('Beacon {0} does not have a validate' log.info('Beacon {0} does not have a validate'
' function, skipping validation.'.format(name)) ' function, skipping validation.'.format(name))
valid = True valid = True
if not valid: if not valid:
ret['comment'] = 'Beacon {0} configuration invalid, not modifying.'.format(name) ret['result'] = False
ret['comment'] = ('Beacon {0} configuration invalid, '
'not adding.\n{1}'.format(name, vcomment))
return ret return ret
_current = current_beacons[name] _current = current_beacons[name]

View File

@ -30,6 +30,9 @@ Management of the Salt beacons
''' '''
import logging
log = logging.getLogger(__name__)
def present(name, def present(name,
**kwargs): **kwargs):
@ -66,8 +69,11 @@ def present(name,
ret['comment'] = result['comment'] ret['comment'] = result['comment']
return ret return ret
else: else:
ret['comment'].append('Modifying {0} in beacons'.format(name)) if 'changes' in result:
ret['changes'] = result['changes'] ret['comment'].append('Modifying {0} in beacons'.format(name))
ret['changes'] = result['changes']
else:
ret['comment'].append(result['comment'])
else: else:
if 'test' in __opts__ and __opts__['test']: if 'test' in __opts__ and __opts__['test']:
kwargs['test'] = True kwargs['test'] = True