diff --git a/moto/cloudwatch/models.py b/moto/cloudwatch/models.py index 5d956215c..b81347313 100644 --- a/moto/cloudwatch/models.py +++ b/moto/cloudwatch/models.py @@ -31,6 +31,33 @@ class Dimension(object): return self != item +class Metric(object): + def __init__(self, metric_name, namespace, dimensions): + self.metric_name = metric_name + self.namespace = namespace + self.dimensions = dimensions + + +class MetricStat(object): + def __init__(self, metric, period, stat, unit): + self.metric = metric + self.period = period + self.stat = stat + self.unit = unit + + +class MetricDataQuery(object): + def __init__( + self, id, label, period, return_data, expression=None, metric_stat=None + ): + self.id = id + self.label = label + self.period = period + self.return_data = return_data + self.expression = expression + self.metric_stat = metric_stat + + def daterange(start, stop, step=timedelta(days=1), inclusive=False): """ This method will iterate from `start` to `stop` datetimes with a timedelta step of `step` @@ -65,8 +92,10 @@ class FakeAlarm(BaseModel): name, namespace, metric_name, + metric_data_queries, comparison_operator, evaluation_periods, + datapoints_to_alarm, period, threshold, statistic, @@ -81,8 +110,10 @@ class FakeAlarm(BaseModel): self.name = name self.namespace = namespace self.metric_name = metric_name + self.metric_data_queries = metric_data_queries self.comparison_operator = comparison_operator self.evaluation_periods = evaluation_periods + self.datapoints_to_alarm = datapoints_to_alarm self.period = period self.threshold = threshold self.statistic = statistic @@ -235,8 +266,10 @@ class CloudWatchBackend(BaseBackend): name, namespace, metric_name, + metric_data_queries, comparison_operator, evaluation_periods, + datapoints_to_alarm, period, threshold, statistic, @@ -252,8 +285,10 @@ class CloudWatchBackend(BaseBackend): name, namespace, metric_name, + metric_data_queries, comparison_operator, evaluation_periods, + datapoints_to_alarm, period, threshold, statistic, diff --git a/moto/cloudwatch/responses.py b/moto/cloudwatch/responses.py index f6e003ee2..c4b427dc6 100644 --- a/moto/cloudwatch/responses.py +++ b/moto/cloudwatch/responses.py @@ -1,7 +1,7 @@ import json from moto.core.utils import amzn_request_id from moto.core.responses import BaseResponse -from .models import cloudwatch_backends +from .models import cloudwatch_backends, MetricDataQuery, MetricStat, Metric, Dimension from dateutil.parser import parse as dtparse @@ -19,8 +19,37 @@ class CloudWatchResponse(BaseResponse): name = self._get_param("AlarmName") namespace = self._get_param("Namespace") metric_name = self._get_param("MetricName") + metrics = self._get_multi_param("Metrics.member") + metric_data_queries = None + if metrics: + metric_data_queries = [ + MetricDataQuery( + id=metric.get("Id"), + label=metric.get("Label"), + period=metric.get("Period"), + return_data=metric.get("ReturnData"), + expression=metric.get("Expression"), + metric_stat=MetricStat( + metric=Metric( + metric_name=metric.get("MetricStat.Metric.MetricName"), + namespace=metric.get("MetricStat.Metric.Namespace"), + dimensions=[ + Dimension(name=dim["Name"], value=dim["Value"]) + for dim in metric["MetricStat.Metric.Dimensions.member"] + ], + ), + period=metric.get("MetricStat.Period"), + stat=metric.get("MetricStat.Stat"), + unit=metric.get("MetricStat.Unit"), + ) + if "MetricStat.Metric.MetricName" in metric + else None, + ) + for metric in metrics + ] comparison_operator = self._get_param("ComparisonOperator") evaluation_periods = self._get_param("EvaluationPeriods") + datapoints_to_alarm = self._get_param("DatapointsToAlarm") period = self._get_param("Period") threshold = self._get_param("Threshold") statistic = self._get_param("Statistic") @@ -37,8 +66,10 @@ class CloudWatchResponse(BaseResponse): name, namespace, metric_name, + metric_data_queries, comparison_operator, evaluation_periods, + datapoints_to_alarm, period, threshold, statistic, @@ -261,35 +292,92 @@ DESCRIBE_ALARMS_TEMPLATE = """