Merge pull request #41053 from bodhi-space/infra4795

INFRA-4795 - fix logic for volume tagging in EC2
This commit is contained in:
Mike Place 2017-05-04 09:25:54 -06:00 committed by GitHub
commit 71627f62a2
2 changed files with 55 additions and 44 deletions

View File

@ -1758,20 +1758,18 @@ def set_volumes_tags(tag_maps, authoritative=False, dry_run=False,
''' '''
ret = {'success': True, 'comment': '', 'changes': {}} ret = {'success': True, 'comment': '', 'changes': {}}
running_states = ('pending', 'rebooting', 'running', 'stopping', 'stopped') running_states = ('pending', 'rebooting', 'running', 'stopping', 'stopped')
### First creeate a dictionary mapping all changes for a given volume to it's volume ID...
tag_sets = {}
for tm in tag_maps: for tm in tag_maps:
filters = tm.get('filters') filters = dict(tm.get('filters', {}))
tags = tm.get('tags') tags = dict(tm.get('tags', {}))
if not isinstance(filters, dict):
raise SaltInvocationError('Tag filters must be a dictionary: got {0}'.format(filters))
if not isinstance(tags, dict):
raise SaltInvocationError('Tags must be a dictionary: got {0}'.format(tags))
args = {'return_objs': True, 'region': region, 'key': key, 'keyid': keyid, 'profile': profile} args = {'return_objs': True, 'region': region, 'key': key, 'keyid': keyid, 'profile': profile}
new_filters = {} new_filters = {}
log.debug('got filters: {0}'.format(filters)) log.debug('got filters: {0}'.format(filters))
instance_id = None instance_id = None
in_states = tm.get('in_states', running_states) in_states = tm.get('in_states', running_states)
try: try:
for k, v in six.iteritems(filters): for k, v in filters.items():
if k == 'volume_ids': if k == 'volume_ids':
args['volume_ids'] = v args['volume_ids'] = v
elif k == 'instance_name': elif k == 'instance_name':
@ -1789,8 +1787,14 @@ def set_volumes_tags(tag_maps, authoritative=False, dry_run=False,
args['filters'] = new_filters args['filters'] = new_filters
volumes = get_all_volumes(**args) volumes = get_all_volumes(**args)
log.debug('got volume list: {0}'.format(volumes)) log.debug('got volume list: {0}'.format(volumes))
changes = {'old': {}, 'new': {}}
for vol in volumes: for vol in volumes:
tag_sets.setdefault(vol.id.replace('-', '_'), {'vol': vol, 'tags': tags.copy()})['tags'].update(tags.copy())
log.debug('tag_sets after munging: {0}'.format(tag_sets))
### ...then loop through all those volume->tag pairs and apply them.
changes = {'old': {}, 'new': {}}
for volume in tag_sets.values():
vol, tags = volume['vol'], volume['tags']
log.debug('current tags on vol.id {0}: {1}'.format(vol.id, dict(getattr(vol, 'tags', {})))) log.debug('current tags on vol.id {0}: {1}'.format(vol.id, dict(getattr(vol, 'tags', {}))))
curr = set(dict(getattr(vol, 'tags', {})).keys()) curr = set(dict(getattr(vol, 'tags', {})).keys())
log.debug('requested tags on vol.id {0}: {1}'.format(vol.id, tags)) log.debug('requested tags on vol.id {0}: {1}'.format(vol.id, tags))

View File

@ -1229,11 +1229,18 @@ def volumes_tagged(name, tag_maps, authoritative=False, region=None, key=None,
if __opts__['test']: if __opts__['test']:
args['dry_run'] = True args['dry_run'] = True
r = __salt__['boto_ec2.set_volumes_tags'](**args) r = __salt__['boto_ec2.set_volumes_tags'](**args)
if r['success']:
if r.get('changes'): if r.get('changes'):
ret['comment'] = 'The following changes would be applied: {0}'.format(r) ret['comment'] = 'Tags would be updated.'
ret['changes'] = r['changes']
ret['result'] = None
else:
ret['comment'] = 'Error validating requested volume tags.'
ret['result'] = False
return ret return ret
r = __salt__['boto_ec2.set_volumes_tags'](**args) r = __salt__['boto_ec2.set_volumes_tags'](**args)
if r['success'] is True: if r['success']:
if r.get('changes'):
ret['comment'] = 'Tags applied.' ret['comment'] = 'Tags applied.'
ret['changes'] = r['changes'] ret['changes'] = r['changes']
else: else: