From a37838b6386a98433a2d4beb14b2abae616185c4 Mon Sep 17 00:00:00 2001 From: Ilya Sukhanov Date: Mon, 1 Aug 2016 17:30:11 -0400 Subject: [PATCH] Preserve Namespace and MetricName when creating cloudwatch alarm. --- moto/cloudwatch/models.py | 10 ++++++---- moto/cloudwatch/responses.py | 5 ++++- setup.py | 2 +- tests/test_cloudwatch/test_cloudwatch.py | 4 ++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/moto/cloudwatch/models.py b/moto/cloudwatch/models.py index 6b7587a94..d7f76daf6 100644 --- a/moto/cloudwatch/models.py +++ b/moto/cloudwatch/models.py @@ -10,10 +10,12 @@ class Dimension(object): class FakeAlarm(object): - def __init__(self, name, comparison_operator, evaluation_periods, period, - threshold, statistic, description, dimensions, alarm_actions, + def __init__(self, name, namespace, metric_name, comparison_operator, evaluation_periods, + period, threshold, statistic, description, dimensions, alarm_actions, ok_actions, insufficient_data_actions, unit): self.name = name + self.namespace = namespace + self.metric_name = metric_name self.comparison_operator = comparison_operator self.evaluation_periods = evaluation_periods self.period = period @@ -43,10 +45,10 @@ class CloudWatchBackend(BaseBackend): self.alarms = {} self.metric_data = [] - def put_metric_alarm(self, name, comparison_operator, evaluation_periods, + def put_metric_alarm(self, name, namespace, metric_name, comparison_operator, evaluation_periods, period, threshold, statistic, description, dimensions, alarm_actions, ok_actions, insufficient_data_actions, unit): - alarm = FakeAlarm(name, comparison_operator, evaluation_periods, period, + alarm = FakeAlarm(name, namespace, metric_name, comparison_operator, evaluation_periods, period, threshold, statistic, description, dimensions, alarm_actions, ok_actions, insufficient_data_actions, unit) self.alarms[name] = alarm diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index 5fd95bbaf..0d2cfacf5 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -7,6 +7,8 @@ class CloudWatchResponse(BaseResponse): def put_metric_alarm(self): name = self._get_param('AlarmName') + namespace = self._get_param('Namespace') + metric_name = self._get_param('MetricName') comparison_operator = self._get_param('ComparisonOperator') evaluation_periods = self._get_param('EvaluationPeriods') period = self._get_param('Period') @@ -19,7 +21,8 @@ class CloudWatchResponse(BaseResponse): insufficient_data_actions = self._get_multi_param("InsufficientDataActions.member") unit = self._get_param('Unit') cloudwatch_backend = cloudwatch_backends[self.region] - alarm = cloudwatch_backend.put_metric_alarm(name, comparison_operator, + alarm = cloudwatch_backend.put_metric_alarm(name, namespace, metric_name, + comparison_operator, evaluation_periods, period, threshold, statistic, description, dimensions, diff --git a/setup.py b/setup.py index f1700305d..c69757cfa 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ extras_require = { setup( name='moto', - version='0.4.25', + version='0.4.26', description='A library that allows your python tests to easily' ' mock out the boto library', author='Steve Pulec', diff --git a/tests/test_cloudwatch/test_cloudwatch.py b/tests/test_cloudwatch/test_cloudwatch.py index 388871fd8..7354241f0 100644 --- a/tests/test_cloudwatch/test_cloudwatch.py +++ b/tests/test_cloudwatch/test_cloudwatch.py @@ -8,6 +8,8 @@ def alarm_fixture(name="tester", action=None): action = action or ['arn:alarm'] return MetricAlarm( name=name, + namespace="{0}_namespace".format(name), + metric="{0}_metric".format(name), comparison='>=', threshold=2.0, period=60, @@ -32,6 +34,8 @@ def test_create_alarm(): alarms.should.have.length_of(1) alarm = alarms[0] alarm.name.should.equal('tester') + alarm.namespace.should.equal('tester_namespace') + alarm.metric.should.equal('tester_metric') alarm.comparison.should.equal('>=') alarm.threshold.should.equal(2.0) alarm.period.should.equal(60)