Merge pull request #2599 from bblommers/feature/#2010
CloudWatch - Implement list_metrics pagination
This commit is contained in:
commit
1c5ea4b545
4 changed files with 130 additions and 8 deletions
|
|
@ -5,6 +5,7 @@ from moto.core.exceptions import RESTError
|
|||
import boto.ec2.cloudwatch
|
||||
from datetime import datetime, timedelta
|
||||
from dateutil.tz import tzutc
|
||||
from uuid import uuid4
|
||||
from .utils import make_arn_for_dashboard
|
||||
|
||||
DEFAULT_ACCOUNT_ID = 123456789012
|
||||
|
|
@ -193,6 +194,7 @@ class CloudWatchBackend(BaseBackend):
|
|||
self.alarms = {}
|
||||
self.dashboards = {}
|
||||
self.metric_data = []
|
||||
self.paged_metric_data = {}
|
||||
|
||||
def put_metric_alarm(
|
||||
self,
|
||||
|
|
@ -377,6 +379,36 @@ class CloudWatchBackend(BaseBackend):
|
|||
|
||||
self.alarms[alarm_name].update_state(reason, reason_data, state_value)
|
||||
|
||||
def list_metrics(self, next_token, namespace, metric_name):
|
||||
if next_token:
|
||||
if next_token not in self.paged_metric_data:
|
||||
raise RESTError(
|
||||
"PaginationException", "Request parameter NextToken is invalid"
|
||||
)
|
||||
else:
|
||||
metrics = self.paged_metric_data[next_token]
|
||||
del self.paged_metric_data[next_token] # Cant reuse same token twice
|
||||
return self._get_paginated(metrics)
|
||||
else:
|
||||
metrics = self.get_filtered_metrics(metric_name, namespace)
|
||||
return self._get_paginated(metrics)
|
||||
|
||||
def get_filtered_metrics(self, metric_name, namespace):
|
||||
metrics = self.get_all_metrics()
|
||||
if namespace:
|
||||
metrics = [md for md in metrics if md.namespace == namespace]
|
||||
if metric_name:
|
||||
metrics = [md for md in metrics if md.name == metric_name]
|
||||
return metrics
|
||||
|
||||
def _get_paginated(self, metrics):
|
||||
if len(metrics) > 500:
|
||||
next_token = str(uuid4())
|
||||
self.paged_metric_data[next_token] = metrics[500:]
|
||||
return next_token, metrics[0:500]
|
||||
else:
|
||||
return None, metrics
|
||||
|
||||
|
||||
class LogGroup(BaseModel):
|
||||
def __init__(self, spec):
|
||||
|
|
|
|||
|
|
@ -120,9 +120,14 @@ class CloudWatchResponse(BaseResponse):
|
|||
|
||||
@amzn_request_id
|
||||
def list_metrics(self):
|
||||
metrics = self.cloudwatch_backend.get_all_metrics()
|
||||
namespace = self._get_param("Namespace")
|
||||
metric_name = self._get_param("MetricName")
|
||||
next_token = self._get_param("NextToken")
|
||||
next_token, metrics = self.cloudwatch_backend.list_metrics(
|
||||
next_token, namespace, metric_name
|
||||
)
|
||||
template = self.response_template(LIST_METRICS_TEMPLATE)
|
||||
return template.render(metrics=metrics)
|
||||
return template.render(metrics=metrics, next_token=next_token)
|
||||
|
||||
@amzn_request_id
|
||||
def delete_dashboards(self):
|
||||
|
|
@ -340,9 +345,11 @@ LIST_METRICS_TEMPLATE = """<ListMetricsResponse xmlns="http://monitoring.amazona
|
|||
</member>
|
||||
{% endfor %}
|
||||
</Metrics>
|
||||
{% if next_token is not none %}
|
||||
<NextToken>
|
||||
96e88479-4662-450b-8a13-239ded6ce9fe
|
||||
{{ next_token }}
|
||||
</NextToken>
|
||||
{% endif %}
|
||||
</ListMetricsResult>
|
||||
</ListMetricsResponse>"""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue