Fix: Cloudwatch delete Alarm status code handling on invalid alarm name (#3028)

* CloudWwatch delete Alarm status code handling on invalid alarm Name

* Handled cases where a mix of existent and non existent alarms are tried to delete

* Linting

Co-authored-by: usmankb <usman@krazybee.com>
Co-authored-by: Bert Blommers <info@bertblommers.nl>
This commit is contained in:
usmangani1 2020-05-28 20:22:56 +05:30 committed by GitHub
parent 162a38bb10
commit 7a6d78afde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -304,6 +304,13 @@ class CloudWatchBackend(BaseBackend):
)
def delete_alarms(self, alarm_names):
for alarm_name in alarm_names:
if alarm_name not in self.alarms:
raise RESTError(
"ResourceNotFound",
"Alarm {0} not found".format(alarm_name),
status=404,
)
for alarm_name in alarm_names:
self.alarms.pop(alarm_name, None)

View File

@ -92,6 +92,37 @@ def test_get_dashboard_fail():
raise RuntimeError("Should of raised error")
@mock_cloudwatch
def test_delete_invalid_alarm():
cloudwatch = boto3.client("cloudwatch", "eu-west-1")
cloudwatch.put_metric_alarm(
AlarmName="testalarm1",
MetricName="cpu",
Namespace="blah",
Period=10,
EvaluationPeriods=5,
Statistic="Average",
Threshold=2,
ComparisonOperator="GreaterThanThreshold",
ActionsEnabled=True,
)
# trying to delete an alarm which is not created along with valid alarm.
with assert_raises(ClientError) as e:
cloudwatch.delete_alarms(AlarmNames=["InvalidAlarmName", "testalarm1"])
e.exception.response["Error"]["Code"].should.equal("ResourceNotFound")
resp = cloudwatch.describe_alarms(AlarmNames=["testalarm1"])
# making sure other alarms are not deleted in case of an error.
len(resp["MetricAlarms"]).should.equal(1)
# test to check if the error raises if only one invalid alarm is tried to delete.
with assert_raises(ClientError) as e:
cloudwatch.delete_alarms(AlarmNames=["InvalidAlarmName"])
e.exception.response["Error"]["Code"].should.equal("ResourceNotFound")
@mock_cloudwatch
def test_alarm_state():
client = boto3.client("cloudwatch", region_name="eu-central-1")