Some cleanup of duplicated code. Fixing event comments.

This commit is contained in:
Gareth J. Greenaway 2017-10-12 17:49:40 -07:00
parent a89c26c88e
commit 2738a124fe
No known key found for this signature in database
GPG Key ID: 10B62F8A7CAD7A41
3 changed files with 66 additions and 36 deletions

View File

@ -199,16 +199,44 @@ class Beacon(object):
else:
self.opts['beacons'][name].append({'enabled': enabled_value})
def list_beacons(self, include_pillar):
def _get_beacons(self,
include_opts=True,
include_pillar=True):
'''
Return the beacons data structure
'''
beacons = {}
if include_pillar:
pillar_beacons = self.opts.get('pillar', {}).get('beacons', {})
if not isinstance(pillar_beacons, dict):
raise ValueError('Beacons must be of type dict.')
beacons.update(pillar_beacons)
if include_opts:
opts_beacons = self.opts.get('beacons', {})
if not isinstance(opts_beacons, dict):
raise ValueError('Beacons must be of type dict.')
beacons.update(opts_beacons)
return beacons
def list_beacons(self, where=None):
'''
List the beacon items
where: Whether to include beacon data from, either opts
or pillar, default is None which would include data
from both.
'''
if where == 'pillar':
beacons = self._get_beacons(include_opts=False)
elif where == 'opts':
beacons = self._get_beacons(include_pillar=False)
else:
beacons = self._get_beacons()
# Fire the complete event back along with the list of beacons
evt = salt.utils.event.get_event('minion', opts=self.opts)
_beacons = self.opts['beacons']
if include_pillar and 'beacons' in self.opts['pillar']:
_beacons.update(self.opts['pillar']['beacons'])
evt.fire_event({'complete': True, 'beacons': _beacons},
evt.fire_event({'complete': True, 'beacons': beacons},
tag='/salt/minion/minion_beacons_list_complete')
return True
@ -239,8 +267,8 @@ class Beacon(object):
del beacon_data['enabled']
valid, vcomment = self.beacons[validate_str](beacon_data)
else:
vcomment = ('Beacon %s does not have a validate'
' function, skipping validation.', name)
vcomment = 'Beacon {0} does not have a validate' \
' function, skipping validation.'.format(name)
valid = True
# Fire the complete event back along with the list of beacons
@ -260,16 +288,16 @@ class Beacon(object):
data = {}
data[name] = beacon_data
if name in self.opts.get('pillar', {}).get('beacons', {}):
comment = ('Cannot update beacon item %s, '
'it is in pillar.', name)
if name in self._get_beacons(include_opts=False):
comment = 'Cannot update beacon item {0}, ' \
'it is in pillar.'.format(name)
complete = False
else:
if name in self.opts['beacons']:
comment = ('Updating settings for beacon '
'item: %s', name)
comment = 'Updating settings for beacon ' \
'item: {0}'.format(name)
else:
comment = ('Added new beacon item %s', name)
comment = 'Added new beacon item {0}'.format(name)
complete = True
self.opts['beacons'].update(data)
@ -289,13 +317,13 @@ class Beacon(object):
data = {}
data[name] = beacon_data
if name in self.opts.get('pillar', {}).get('beacons', {}):
comment = ('Cannot modify beacon item %s, '
'it is in pillar.', name)
if name in self._get_beacons(include_opts=False):
comment = 'Cannot modify beacon item {0}, ' \
'it is in pillar.'.format(name)
complete = False
else:
comment = ('Updating settings for beacon '
'item: %s', name)
comment = 'Updating settings for beacon ' \
'item: {0}'.format(name)
complete = True
self.opts['beacons'].update(data)
@ -312,16 +340,16 @@ class Beacon(object):
Delete a beacon item
'''
if name in self.opts.get('pillar', {}).get('beacons', {}):
comment = ('Cannot delete beacon item 5s, '
'it is in pillar.', name)
if name in self._get_beacons(include_opts=False):
comment = 'Cannot delete beacon item {0}, ' \
'it is in pillar.'.format(name)
complete = False
else:
if name in self.opts['beacons']:
del self.opts['beacons'][name]
comment = ('Deleting beacon item %s', name)
comment = 'Deleting beacon item {0}'.format(name)
else:
comment = ('Beacon item %s not found.', name)
comment = 'Beacon item {0} not found.'.format(name)
complete = True
# Fire the complete event back along with updated list of beacons
@ -365,13 +393,13 @@ class Beacon(object):
Enable a beacon
'''
if name in self.opts.get('pillar', {}).get('beacons', {}):
comment = ('Cannot enable beacon item %s, '
'it is in pillar.', name)
if name in self._get_beacons(include_opts=False):
comment = 'Cannot enable beacon item {0}, ' \
'it is in pillar.'.format(name)
complete = False
else:
self._update_enabled(name, True)
comment = ('Enabling beacon item %s', name)
comment = 'Enabling beacon item {0}'.format(name)
complete = True
# Fire the complete event back along with updated list of beacons
@ -387,13 +415,13 @@ class Beacon(object):
Disable a beacon
'''
if name in self.opts.get('pillar', {}).get('beacons', {}):
comment = ('Cannot disable beacon item %s, '
'it is in pillar.', name)
if name in self._get_beacons(include_opts=False):
comment = 'Cannot disable beacon item {0}, ' \
'it is in pillar.'.format(name)
complete = False
else:
self._update_enabled(name, False)
comment = ('Disabling beacon item %s', name)
comment = 'Disabling beacon item {0}'.format(name)
complete = True
# Fire the complete event back along with updated list of beacons

View File

@ -2063,7 +2063,7 @@ class Minion(MinionBase):
func = data.get(u'func', None)
name = data.get(u'name', None)
beacon_data = data.get(u'beacon_data', None)
include_pillar = data.get(u'include_pillar', None)
where = data.get(u'where', None)
if func == u'add':
self.beacons.add_beacon(name, beacon_data)
@ -2080,7 +2080,7 @@ class Minion(MinionBase):
elif func == u'disable_beacon':
self.beacons.disable_beacon(name)
elif func == u'list':
self.beacons.list_beacons(include_pillar)
self.beacons.list_beacons(where)
elif func == u'list_available':
self.beacons.list_available_beacons()
elif func == u'validate_beacon':

View File

@ -28,11 +28,13 @@ __func_alias__ = {
}
def list_(return_yaml=True, include_pillar=True):
def list_(return_yaml=True, where=None):
'''
List the beacons currently configured on the minion
:param return_yaml: Whether to return YAML formatted output, default True
:param where: Return beacon data from opts or pillar, default is
None which will return data from opts and pillar.
:return: List of currently configured Beacons.
CLI Example:
@ -47,7 +49,7 @@ def list_(return_yaml=True, include_pillar=True):
try:
eventer = salt.utils.event.get_event('minion', opts=__opts__)
res = __salt__['event.fire']({'func': 'list',
'include_pillar': include_pillar},
'where': where},
'manage_beacons')
if res:
event_ret = eventer.get_event(tag='/salt/minion/minion_beacons_list_complete', wait=30)
@ -341,7 +343,7 @@ def save():
ret = {'comment': [],
'result': True}
beacons = list_(return_yaml=False, include_pillar=False)
beacons = list_(return_yaml=False, where='opts')
# move this file into an configurable opt
sfn = '{0}/{1}/beacons.conf'.format(__opts__['config_dir'],