From d902ec66c529bd9579046a2df69bad86fafafe1a Mon Sep 17 00:00:00 2001 From: Marin Purgar Date: Tue, 7 Jan 2020 02:36:52 +0100 Subject: [PATCH] Add CloudWatch Alarm TreatMissingData validator (#1536) --- tests/test_cloudwatch.py | 17 +++++++++++++++++ troposphere/cloudwatch.py | 14 +++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/test_cloudwatch.py b/tests/test_cloudwatch.py index cc281a79d..097100527 100644 --- a/tests/test_cloudwatch.py +++ b/tests/test_cloudwatch.py @@ -1,4 +1,5 @@ import unittest +import troposphere.cloudwatch as cloudwatch from troposphere.cloudwatch import Dashboard @@ -36,5 +37,21 @@ def test_dashboard(self): ) +class TestCloudWatchValidators(unittest.TestCase): + def test_validate_units(self): + for unit in cloudwatch.VALID_UNITS: + cloudwatch.validate_unit(unit) + for bad_unit in ['Minutes', 'Bytes/Minute', 'Bits/Hour', '']: + with self.assertRaisesRegexp(ValueError, "must be one of"): + cloudwatch.validate_unit(bad_unit) + + def test_validate_treat_missing_data(self): + for value in cloudwatch.VALID_TREAT_MISSING_DATA_TYPES: + cloudwatch.validate_treat_missing_data(value) + for bad_value in ['exists', 'notMissing', '']: + with self.assertRaisesRegexp(ValueError, "must be one of"): + cloudwatch.validate_treat_missing_data(bad_value) + + if __name__ == '__main__': unittest.main() diff --git a/troposphere/cloudwatch.py b/troposphere/cloudwatch.py index c0d5cafe7..dc168c678 100644 --- a/troposphere/cloudwatch.py +++ b/troposphere/cloudwatch.py @@ -16,6 +16,9 @@ 'Kilobits/Second', 'Megabits/Second', 'Gigabits/Second', 'Terabits/Second', 'Count/Second', 'None') +VALID_TREAT_MISSING_DATA_TYPES = ('breaching', 'notBreaching', 'ignore', + 'missing') + def validate_unit(unit): """Validate Units""" @@ -26,6 +29,15 @@ def validate_unit(unit): return unit +def validate_treat_missing_data(value): + """Validate TreatMissingData""" + + if value not in VALID_TREAT_MISSING_DATA_TYPES: + raise ValueError("Alarm TreatMissingData must be one of: %s" % + ", ".join(VALID_TREAT_MISSING_DATA_TYPES)) + return value + + class MetricDimension(AWSProperty): props = { 'Name': (basestring, True), @@ -83,7 +95,7 @@ class Alarm(AWSObject): 'Statistic': (basestring, False), 'Threshold': (double, False), 'ThresholdMetricId': (basestring, False), - 'TreatMissingData': (basestring, False), + 'TreatMissingData': (validate_treat_missing_data, False), 'Unit': (basestring, False), }