Skip to content

Commit

Permalink
Merge pull request #42 from brucehoff/IT-3686
Browse files Browse the repository at this point in the history
IT-3686: deduplicate AWS tags
  • Loading branch information
brucehoff committed Jul 5, 2024
2 parents 3e92dc7 + d52178e commit 4c83f55
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
- run: pipenv run coverage run -m pytest tests/ -svv
- name: upload coverage to coveralls
uses: coverallsapp/github-action@v2
with:
fail-on-error: false

sam-build-and-lint:
runs-on: ubuntu-latest
Expand Down
8 changes: 6 additions & 2 deletions set_tags/set_bucket_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def apply_tags(name, tags):
@helper.create
@helper.update
def create_or_update(event, context):
'''Handles customm resource create and update events'''
'''Handles custom resource create and update events'''
log.debug('Received event: ' + json.dumps(event, sort_keys=False))
log.info('Start Lambda processing')
bucket_name = get_bucket_name(event)
Expand All @@ -58,7 +58,11 @@ def create_or_update(event, context):
synapse_tags = utils.get_synapse_tags(synapse_owner_id)
# put_bucket_tagging is a replace operation. need to give it all
# tags otherwise it will remove existing tags not in the list
all_tags = list(bucket_tags + synapse_tags)
#
# In case of duplication, Synapse tags should replace existing
# bucket tags.
all_tags = utils.merge_tags(bucket_tags, synapse_tags)

log.debug(f'Apply tags: {all_tags} to bucket {bucket_name}')
apply_tags(bucket_name, all_tags)

Expand Down
7 changes: 6 additions & 1 deletion set_tags/set_instance_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ def create_or_update(event, context):
extra_tags.append(provisioned_product_name_tag)
access_approved_role_tag = utils.get_access_approved_role_tag(instance_tags)
extra_tags.append(access_approved_role_tag)
all_tags = list(synapse_tags + extra_tags)
#
# In case of duplication, Synapse tags should replace existing
# bucket tags.
#
all_tags = utils.merge_tags(extra_tags, synapse_tags)


volume_ids = get_volume_ids(instance_id)
log.debug(f'Apply tags: {all_tags} to volume {volume_ids}')
Expand Down
10 changes: 10 additions & 0 deletions set_tags/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,13 @@ def get_access_approved_role_tag(tags):
return access_approved_role_tag
else:
raise ValueError(f'Expected to find {PRINCIPAL_ARN_TAG_KEY} in {tags}')

def merge_tags(list1, list2):
'''Merge two lists of tags, the second overriding the first on key collisions
'''
unique_tags = format_tags_kv_kp(list1)
unique_tags.update(format_tags_kv_kp(list2))
result=[]
for entry in unique_tags:
result.append({'Key':entry,'Value':unique_tags[entry]})
return result
15 changes: 15 additions & 0 deletions tests/unit/utils/test_merge_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest

from set_tags import utils

class TestMergeTags(unittest.TestCase):

def test_happy_case(self):
tags = []
result = utils.merge_tags([{'Key':'foo','Value':'bar'}],[{'Key':'foo2','Value':'bar2'}])
self.assertEqual(result, [{'Key':'foo','Value':'bar'},{'Key':'foo2','Value':'bar2'}])

def test_collision(self):
tags = []
result = utils.merge_tags([{'Key':'foo','Value':'bar'}],[{'Key':'foo','Value':'bar2'}])
self.assertEqual(result, [{'Key':'foo','Value':'bar2'}])

0 comments on commit 4c83f55

Please sign in to comment.