diff --git a/moto/cloudwatch/models.py b/moto/cloudwatch/models.py index 43be48b36..4ed86a41a 100644 --- a/moto/cloudwatch/models.py +++ b/moto/cloudwatch/models.py @@ -25,10 +25,19 @@ class FakeAlarm(object): self.unit = unit +class MetricDatum(object): + def __init__(self, namespace, name, value, dimensions): + self.namespace = namespace + self.name = name + self.value = value + self.dimensions = [Dimension(dimension['name'], dimension['value']) for dimension in dimensions] + + class CloudWatchBackend(BaseBackend): def __init__(self): self.alarms = {} + self.metric_data = [] def put_metric_alarm(self, name, comparison_operator, evaluation_periods, period, threshold, statistic, description, dimensions, @@ -46,5 +55,12 @@ class CloudWatchBackend(BaseBackend): for alarm_name in alarm_names: self.alarms.pop(alarm_name, None) + def put_metric_data(self, namespace, metric_data): + for name, value, dimensions in metric_data: + self.metric_data.append(MetricDatum(namespace, name, value, dimensions)) + + def get_all_metrics(self): + return self.metric_data + cloudwatch_backend = CloudWatchBackend() diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index 6c990b84f..5e6f87530 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -38,6 +38,38 @@ class CloudWatchResponse(BaseResponse): template = self.response_template(DELETE_METRIC_ALARMS_TEMPLATE) return template.render() + def put_metric_data(self): + namespace = self._get_param('Namespace') + metric_data = [] + metric_index = 1 + while True: + try: + metric_name = self.querystring['MetricData.member.{0}.MetricName'.format(metric_index)][0] + except KeyError: + break + value = self.querystring.get('MetricData.member.{0}.Value'.format(metric_index), [None])[0] + dimensions = [] + dimension_index = 1 + while True: + try: + dimension_name = self.querystring['MetricData.member.{0}.Dimensions.member.{1}.Name'.format(metric_index, dimension_index)][0] + except KeyError: + break + dimension_value = self.querystring['MetricData.member.{0}.Dimensions.member.{1}.Value'.format(metric_index, dimension_index)][0] + dimensions.append({'name': dimension_name, 'value': dimension_value}) + dimension_index += 1 + metric_data.append([metric_name, value, dimensions]) + metric_index += 1 + cloudwatch_backend.put_metric_data(namespace, metric_data) + template = self.response_template(PUT_METRIC_DATA_TEMPLATE) + return template.render() + + def list_metrics(self): + metrics = cloudwatch_backend.get_all_metrics() + template = self.response_template(LIST_METRICS_TEMPLATE) + return template.render(metrics=metrics) + + PUT_METRIC_ALARM_TEMPLATE = """ @@ -102,3 +134,35 @@ DELETE_METRIC_ALARMS_TEMPLATE = """ + + + 2690d7eb-ed86-11dd-9877-6fad448a8419 + + +""" + +LIST_METRICS_TEMPLATE = """ + + + {% for metric in metrics %} + + + {% for dimension in metric.dimensions %} + + {{ dimension.name }} + {{ dimension.value }} + + {% endfor %} + + {{ metric.name }} + {{ metric.namespace }} + + {% endfor %} + + + 96e88479-4662-450b-8a13-239ded6ce9fe + + +""" diff --git a/tests/test_cloudwatch/test_cloudwatch.py b/tests/test_cloudwatch/test_cloudwatch.py index df3e41762..0320ba794 100644 --- a/tests/test_cloudwatch/test_cloudwatch.py +++ b/tests/test_cloudwatch/test_cloudwatch.py @@ -69,3 +69,22 @@ def test_delete_alarm(): alarms = conn.describe_alarms() alarms.should.have.length_of(0) + + +@mock_cloudwatch +def test_put_metric_data(): + conn = boto.connect_cloudwatch() + + conn.put_metric_data( + namespace='tester', + name='metric', + value=1.5, + dimensions={'InstanceId': ['i-0123456,i-0123457']}, + ) + + metrics = conn.list_metrics() + metrics.should.have.length_of(1) + metric = metrics[0] + metric.namespace.should.equal('tester') + metric.name.should.equal('metric') + dict(metric.dimensions).should.equal({'InstanceId': ['i-0123456,i-0123457']})