From 0561a0050fb7d68c1cfac5877690e786d3d2c083 Mon Sep 17 00:00:00 2001 From: Wayne Metcalfe Date: Thu, 26 Sep 2019 17:09:10 +0100 Subject: [PATCH] feat: implement logs tag_log_group --- IMPLEMENTATION_COVERAGE.md | 2 +- moto/logs/models.py | 12 ++++++++++++ moto/logs/responses.py | 5 +++++ tests/test_logs/test_logs.py | 24 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index aac1d3858..1377c67be 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -4090,7 +4090,7 @@ - [ ] put_subscription_filter - [ ] start_query - [ ] stop_query -- [ ] tag_log_group +- [X] tag_log_group - [ ] test_metric_filter - [ ] untag_log_group diff --git a/moto/logs/models.py b/moto/logs/models.py index de23e7dfb..01738fa39 100644 --- a/moto/logs/models.py +++ b/moto/logs/models.py @@ -234,6 +234,12 @@ class LogGroup: def list_tags(self): return self.tags if self.tags else {} + def tag(self, tags): + if self.tags: + self.tags.update(tags) + else: + self.tags = tags + class LogsBackend(BaseBackend): def __init__(self, region_name): @@ -331,5 +337,11 @@ class LogsBackend(BaseBackend): log_group = self.groups[log_group_name] return log_group.list_tags() + def tag_log_group(self, log_group_name, tags): + if log_group_name not in self.groups: + raise ResourceNotFoundException() + log_group = self.groups[log_group_name] + log_group.tag(tags) + logs_backends = {region.name: LogsBackend(region.name) for region in boto.logs.regions()} diff --git a/moto/logs/responses.py b/moto/logs/responses.py index fd6084f7c..0c8750ce2 100644 --- a/moto/logs/responses.py +++ b/moto/logs/responses.py @@ -142,3 +142,8 @@ class LogsResponse(BaseResponse): 'tags': tags }) + def tag_log_group(self): + log_group_name = self._get_param('logGroupName') + tags = self._get_param('tags') + self.logs_backend.tag_log_group(log_group_name, tags) + return '' diff --git a/tests/test_logs/test_logs.py b/tests/test_logs/test_logs.py index 61dd0ad3a..fced03363 100644 --- a/tests/test_logs/test_logs.py +++ b/tests/test_logs/test_logs.py @@ -243,3 +243,27 @@ def test_list_tags_log_group(): assert response['tags'] == tags response = conn.delete_log_group(logGroupName=log_group_name) + + +@mock_logs +def test_tag_log_group(): + conn = boto3.client('logs', 'us-west-2') + log_group_name = 'dummy' + tags = {'tag_key_1': 'tag_value_1'} + response = conn.create_log_group(logGroupName=log_group_name) + + response = conn.tag_log_group(logGroupName=log_group_name, tags=tags) + response = conn.list_tags_log_group(logGroupName=log_group_name) + assert response['tags'] == tags + + tags_with_added_value = {'tag_key_1': 'tag_value_1', 'tag_key_2': 'tag_value_2'} + response = conn.tag_log_group(logGroupName=log_group_name, tags={'tag_key_2': 'tag_value_2'}) + response = conn.list_tags_log_group(logGroupName=log_group_name) + assert response['tags'] == tags_with_added_value + + tags_with_updated_value = {'tag_key_1': 'tag_value_XX', 'tag_key_2': 'tag_value_2'} + response = conn.tag_log_group(logGroupName=log_group_name, tags={'tag_key_1': 'tag_value_XX'}) + response = conn.list_tags_log_group(logGroupName=log_group_name) + assert response['tags'] == tags_with_updated_value + + response = conn.delete_log_group(logGroupName=log_group_name)