Fix exception type in cloudwatch get_metric_statistics (#4775)

This commit is contained in:
Ranjithkumar Krishnan 2022-01-25 11:22:29 +01:00 committed by GitHub
parent 245443d910
commit 4c309e7dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View File

@ -15,6 +15,13 @@ class InvalidParameterValue(RESTError):
super().__init__(__class__.__name__, message)
class InvalidParameterCombination(RESTError):
code = 400
def __init__(self, message):
super().__init__(__class__.__name__, message)
class ResourceNotFound(RESTError):
code = 404

View File

@ -5,6 +5,7 @@ from dateutil.parser import parse as dtparse
from moto.core.responses import BaseResponse
from moto.core.utils import amzn_request_id
from .models import cloudwatch_backends, MetricDataQuery, MetricStat, Metric, Dimension
from .exceptions import InvalidParameterCombination
class CloudWatchResponse(BaseResponse):
@ -187,9 +188,8 @@ class CloudWatchResponse(BaseResponse):
unit = self._get_param("Unit")
extended_statistics = self._get_param("ExtendedStatistics")
# TODO: this should instead throw InvalidParameterCombination
if not statistics and not extended_statistics:
raise NotImplementedError(
raise InvalidParameterCombination(
"Must specify either Statistics or ExtendedStatistics"
)

View File

@ -105,6 +105,31 @@ def test_get_metric_statistics():
datapoint["Sum"].should.equal(1.5)
@mock_cloudwatch
def test_get_metric_invalid_parameter_combination():
conn = boto3.client("cloudwatch", region_name="us-east-1")
utc_now = datetime.now(tz=pytz.utc)
conn.put_metric_data(
Namespace="tester",
MetricData=[dict(MetricName="metric", Value=1.5, Timestamp=utc_now)],
)
with pytest.raises(ClientError) as exc:
# make request without both statistics or extended statistics parameters
conn.get_metric_statistics(
Namespace="tester",
MetricName="metric",
StartTime=utc_now - timedelta(seconds=60),
EndTime=utc_now + timedelta(seconds=60),
Period=60,
)
err = exc.value.response["Error"]
err["Code"].should.equal("InvalidParameterCombination")
err["Message"].should.equal("Must specify either Statistics or ExtendedStatistics")
@mock_cloudwatch
def test_get_metric_statistics_dimensions():
conn = boto3.client("cloudwatch", region_name="us-east-1")