From 8bffff4620b7a7325a828e2c383e6f7b892b412c Mon Sep 17 00:00:00 2001 From: Tim Gatzemeier Date: Mon, 16 Mar 2020 18:48:29 +0100 Subject: [PATCH 1/5] set actions enabled in template on describe images this is to avoid errors with terraform relates to https://github.com/localstack/localstack/issues/2161 --- moto/cloudwatch/models.py | 4 ++++ moto/cloudwatch/responses.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/moto/cloudwatch/models.py b/moto/cloudwatch/models.py index 716a29633..bdba09930 100644 --- a/moto/cloudwatch/models.py +++ b/moto/cloudwatch/models.py @@ -67,6 +67,7 @@ class FakeAlarm(BaseModel): ok_actions, insufficient_data_actions, unit, + actions_enabled, ): self.name = name self.namespace = namespace @@ -80,6 +81,7 @@ class FakeAlarm(BaseModel): self.dimensions = [ Dimension(dimension["name"], dimension["value"]) for dimension in dimensions ] + self.actions_enabled = actions_enabled self.alarm_actions = alarm_actions self.ok_actions = ok_actions self.insufficient_data_actions = insufficient_data_actions @@ -215,6 +217,7 @@ class CloudWatchBackend(BaseBackend): ok_actions, insufficient_data_actions, unit, + actions_enabled, ): alarm = FakeAlarm( name, @@ -231,6 +234,7 @@ class CloudWatchBackend(BaseBackend): ok_actions, insufficient_data_actions, unit, + actions_enabled, ) self.alarms[name] = alarm return alarm diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index 7872e71fd..dbc9d8c5a 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -28,6 +28,7 @@ class CloudWatchResponse(BaseResponse): dimensions = self._get_list_prefix("Dimensions.member") alarm_actions = self._get_multi_param("AlarmActions.member") ok_actions = self._get_multi_param("OKActions.member") + actions_enabled = self._get_multi_param("ActionsEnabled") insufficient_data_actions = self._get_multi_param( "InsufficientDataActions.member" ) @@ -47,6 +48,7 @@ class CloudWatchResponse(BaseResponse): ok_actions, insufficient_data_actions, unit, + actions_enabled, ) template = self.response_template(PUT_METRIC_ALARM_TEMPLATE) return template.render(alarm=alarm) From 9d3ee116d3ec53c6ee20d2df0823ac09694e0f37 Mon Sep 17 00:00:00 2001 From: Tim Gatzemeier Date: Mon, 16 Mar 2020 20:14:41 +0100 Subject: [PATCH 2/5] add test case for actions_enabled field --- tests/test_cloudwatch/test_cloudwatch_boto3.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_cloudwatch/test_cloudwatch_boto3.py b/tests/test_cloudwatch/test_cloudwatch_boto3.py index 5bd9ed13d..1935a4181 100644 --- a/tests/test_cloudwatch/test_cloudwatch_boto3.py +++ b/tests/test_cloudwatch/test_cloudwatch_boto3.py @@ -128,11 +128,13 @@ def test_alarm_state(): len(resp["MetricAlarms"]).should.equal(1) resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm1") resp["MetricAlarms"][0]["StateValue"].should.equal("ALARM") + resp["MetricAlarms"][0]["ActionsEnabled"].should.equal("True") resp = client.describe_alarms(StateValue="OK") len(resp["MetricAlarms"]).should.equal(1) resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm2") resp["MetricAlarms"][0]["StateValue"].should.equal("OK") + resp["MetricAlarms"][0]["ActionsEnabled"].should.equal("True") # Just for sanity resp = client.describe_alarms() From 1fdb0e987dc882ba380d2665bd52f40dfd800e7e Mon Sep 17 00:00:00 2001 From: Tim Gatzemeier Date: Mon, 16 Mar 2020 21:45:18 +0100 Subject: [PATCH 3/5] get single param for actions enabled --- moto/cloudwatch/responses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index dbc9d8c5a..7993c9f06 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -28,7 +28,7 @@ class CloudWatchResponse(BaseResponse): dimensions = self._get_list_prefix("Dimensions.member") alarm_actions = self._get_multi_param("AlarmActions.member") ok_actions = self._get_multi_param("OKActions.member") - actions_enabled = self._get_multi_param("ActionsEnabled") + actions_enabled = self._get_param("ActionsEnabled") insufficient_data_actions = self._get_multi_param( "InsufficientDataActions.member" ) From 50974aa9b2d1c71b4be580e4236e04fac31c7e95 Mon Sep 17 00:00:00 2001 From: Tim Gatzemeier Date: Mon, 16 Mar 2020 21:45:29 +0100 Subject: [PATCH 4/5] add test cases to ensure actions enabled is correctly returned --- tests/test_cloudwatch/test_cloudwatch.py | 12 +++++++++--- tests/test_cloudwatch/test_cloudwatch_boto3.py | 6 +++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/test_cloudwatch/test_cloudwatch.py b/tests/test_cloudwatch/test_cloudwatch.py index dee8aa605..f86b57d54 100644 --- a/tests/test_cloudwatch/test_cloudwatch.py +++ b/tests/test_cloudwatch/test_cloudwatch.py @@ -101,15 +101,22 @@ def test_describe_alarms(): conn.create_alarm(alarm_fixture(name="nfoobaz", action="afoobaz")) conn.create_alarm(alarm_fixture(name="nbarfoo", action="abarfoo")) conn.create_alarm(alarm_fixture(name="nbazfoo", action="abazfoo")) - + + enabled = alarm_fixture(name="enabled1", action=["abarfoo"]) + enabled.add_alarm_action("arn:alarm") + conn.create_alarm(enabled) + alarms = conn.describe_alarms() - alarms.should.have.length_of(4) + alarms.should.have.length_of(5) alarms = conn.describe_alarms(alarm_name_prefix="nfoo") alarms.should.have.length_of(2) alarms = conn.describe_alarms(alarm_names=["nfoobar", "nbarfoo", "nbazfoo"]) alarms.should.have.length_of(3) alarms = conn.describe_alarms(action_prefix="afoo") alarms.should.have.length_of(2) + alarms = conn.describe_alarms(alarm_name_prefix="enabled") + alarms.should.have.length_of(1) + alarms[0].actions_enabled.should.equal("true") for alarm in conn.describe_alarms(): alarm.delete() @@ -117,7 +124,6 @@ def test_describe_alarms(): alarms = conn.describe_alarms() alarms.should.have.length_of(0) - @mock_cloudwatch_deprecated def test_get_metric_statistics(): conn = boto.connect_cloudwatch() diff --git a/tests/test_cloudwatch/test_cloudwatch_boto3.py b/tests/test_cloudwatch/test_cloudwatch_boto3.py index 1935a4181..6bef2b3f2 100644 --- a/tests/test_cloudwatch/test_cloudwatch_boto3.py +++ b/tests/test_cloudwatch/test_cloudwatch_boto3.py @@ -104,6 +104,7 @@ def test_alarm_state(): Statistic="Average", Threshold=2, ComparisonOperator="GreaterThanThreshold", + ActionsEnabled=True, ) client.put_metric_alarm( AlarmName="testalarm2", @@ -128,19 +129,18 @@ def test_alarm_state(): len(resp["MetricAlarms"]).should.equal(1) resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm1") resp["MetricAlarms"][0]["StateValue"].should.equal("ALARM") - resp["MetricAlarms"][0]["ActionsEnabled"].should.equal("True") + resp["MetricAlarms"][0]["ActionsEnabled"].should.equal(True) resp = client.describe_alarms(StateValue="OK") len(resp["MetricAlarms"]).should.equal(1) resp["MetricAlarms"][0]["AlarmName"].should.equal("testalarm2") resp["MetricAlarms"][0]["StateValue"].should.equal("OK") - resp["MetricAlarms"][0]["ActionsEnabled"].should.equal("True") + resp["MetricAlarms"][0]["ActionsEnabled"].should.equal(False) # Just for sanity resp = client.describe_alarms() len(resp["MetricAlarms"]).should.equal(2) - @mock_cloudwatch def test_put_metric_data_no_dimensions(): conn = boto3.client("cloudwatch", region_name="us-east-1") From 6e490a91909b6be7e371db6241a09291eb0d81da Mon Sep 17 00:00:00 2001 From: Tim Gatzemeier Date: Mon, 16 Mar 2020 21:58:50 +0100 Subject: [PATCH 5/5] make linter happy --- tests/test_cloudwatch/test_cloudwatch.py | 5 +++-- tests/test_cloudwatch/test_cloudwatch_boto3.py | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_cloudwatch/test_cloudwatch.py b/tests/test_cloudwatch/test_cloudwatch.py index f86b57d54..5a05a55e1 100644 --- a/tests/test_cloudwatch/test_cloudwatch.py +++ b/tests/test_cloudwatch/test_cloudwatch.py @@ -101,11 +101,11 @@ def test_describe_alarms(): conn.create_alarm(alarm_fixture(name="nfoobaz", action="afoobaz")) conn.create_alarm(alarm_fixture(name="nbarfoo", action="abarfoo")) conn.create_alarm(alarm_fixture(name="nbazfoo", action="abazfoo")) - + enabled = alarm_fixture(name="enabled1", action=["abarfoo"]) enabled.add_alarm_action("arn:alarm") conn.create_alarm(enabled) - + alarms = conn.describe_alarms() alarms.should.have.length_of(5) alarms = conn.describe_alarms(alarm_name_prefix="nfoo") @@ -124,6 +124,7 @@ def test_describe_alarms(): alarms = conn.describe_alarms() alarms.should.have.length_of(0) + @mock_cloudwatch_deprecated def test_get_metric_statistics(): conn = boto.connect_cloudwatch() diff --git a/tests/test_cloudwatch/test_cloudwatch_boto3.py b/tests/test_cloudwatch/test_cloudwatch_boto3.py index 6bef2b3f2..7fe144052 100644 --- a/tests/test_cloudwatch/test_cloudwatch_boto3.py +++ b/tests/test_cloudwatch/test_cloudwatch_boto3.py @@ -141,6 +141,7 @@ def test_alarm_state(): resp = client.describe_alarms() len(resp["MetricAlarms"]).should.equal(2) + @mock_cloudwatch def test_put_metric_data_no_dimensions(): conn = boto3.client("cloudwatch", region_name="us-east-1")