def test_store_tags(self): cloudwatch = aws_stack.connect_to_service('cloudwatch') alarm_name = 'a-%s' % short_uid() response = cloudwatch.put_metric_alarm( AlarmName=alarm_name, EvaluationPeriods=1, ComparisonOperator='GreaterThanThreshold') self.assertEqual(response['ResponseMetadata']['HTTPStatusCode'], 200) alarm_arn = aws_stack.cloudwatch_alarm_arn(alarm_name) tags = [{ 'Key': 'tag1', 'Value': 'foo' }, { 'Key': 'tag2', 'Value': 'bar' }] response = cloudwatch.tag_resource(ResourceARN=alarm_arn, Tags=tags) self.assertEqual(response['ResponseMetadata']['HTTPStatusCode'], 200) response = cloudwatch.list_tags_for_resource(ResourceARN=alarm_arn) self.assertEqual(response['ResponseMetadata']['HTTPStatusCode'], 200) self.assertEqual(response['Tags'], tags) response = cloudwatch.untag_resource(ResourceARN=alarm_arn, TagKeys=['tag1']) self.assertEqual(response['ResponseMetadata']['HTTPStatusCode'], 200) response = cloudwatch.list_tags_for_resource(ResourceARN=alarm_arn) self.assertEqual(response['ResponseMetadata']['HTTPStatusCode'], 200) self.assertEqual(response['Tags'], [{'Key': 'tag2', 'Value': 'bar'}]) # clean up cloudwatch.delete_alarms(AlarmNames=[alarm_name])
def return_response(self, method, path, data, headers, response): req_data = parse_request_data(method, path, data) action = req_data.get('Action') if action == 'PutMetricAlarm': name = req_data.get('AlarmName') # add missing attribute "TreatMissingData" treat_missing_data = req_data.get('TreatMissingData', 'ignore') cloudwatch_backends[aws_stack.get_region( )].alarms[name].treat_missing_data = treat_missing_data # record tags arn = aws_stack.cloudwatch_alarm_arn(name) tags = aws_stack.extract_tags(req_data) if tags: TAGS.tag_resource(arn, tags) # Fix Incorrect date format to the correct format # the dictionary contains the tag as the key and the value is a # tuple (pattern, replacement) regexes1 = (r'<{}>([^<]+) ([^<+]+)(\+[^<]*)?</{}>', r'<{}>\1T\2Z</{}>') regexes2 = (r'<{}>([^<]+) ([^<+.]+)(\.[^<]*)?</{}>', r'<{}>\1T\2Z</{}>') timestamp_tags = { 'AlarmConfigurationUpdatedTimestamp': regexes1, 'StateUpdatedTimestamp': regexes1, 'member': regexes2 } for tag, value in timestamp_tags.items(): pattern, replacement = value self.fix_date_format(response, tag, pattern, replacement) response.headers['Content-Length'] = len(response.content) return response
def test_store_tags(self): cloudwatch = aws_stack.connect_to_service("cloudwatch") alarm_name = "a-%s" % short_uid() response = cloudwatch.put_metric_alarm( AlarmName=alarm_name, EvaluationPeriods=1, ComparisonOperator="GreaterThanThreshold", ) self.assertEqual(200, response["ResponseMetadata"]["HTTPStatusCode"]) alarm_arn = aws_stack.cloudwatch_alarm_arn(alarm_name) tags = [{ "Key": "tag1", "Value": "foo" }, { "Key": "tag2", "Value": "bar" }] response = cloudwatch.tag_resource(ResourceARN=alarm_arn, Tags=tags) self.assertEqual(200, response["ResponseMetadata"]["HTTPStatusCode"]) response = cloudwatch.list_tags_for_resource(ResourceARN=alarm_arn) self.assertEqual(200, response["ResponseMetadata"]["HTTPStatusCode"]) self.assertEqual(tags, response["Tags"]) response = cloudwatch.untag_resource(ResourceARN=alarm_arn, TagKeys=["tag1"]) self.assertEqual(200, response["ResponseMetadata"]["HTTPStatusCode"]) response = cloudwatch.list_tags_for_resource(ResourceARN=alarm_arn) self.assertEqual(200, response["ResponseMetadata"]["HTTPStatusCode"]) self.assertEqual([{"Key": "tag2", "Value": "bar"}], response["Tags"]) # clean up cloudwatch.delete_alarms(AlarmNames=[alarm_name])
def test_store_tags(self, cloudwatch_client): alarm_name = "a-%s" % short_uid() response = cloudwatch_client.put_metric_alarm( AlarmName=alarm_name, EvaluationPeriods=1, ComparisonOperator="GreaterThanThreshold", ) assert 200 == response["ResponseMetadata"]["HTTPStatusCode"] alarm_arn = aws_stack.cloudwatch_alarm_arn(alarm_name) tags = [{ "Key": "tag1", "Value": "foo" }, { "Key": "tag2", "Value": "bar" }] response = cloudwatch_client.tag_resource(ResourceARN=alarm_arn, Tags=tags) assert 200 == response["ResponseMetadata"]["HTTPStatusCode"] response = cloudwatch_client.list_tags_for_resource( ResourceARN=alarm_arn) assert 200 == response["ResponseMetadata"]["HTTPStatusCode"] assert tags, response["Tags"] response = cloudwatch_client.untag_resource(ResourceARN=alarm_arn, TagKeys=["tag1"]) assert 200 == response["ResponseMetadata"]["HTTPStatusCode"] response = cloudwatch_client.list_tags_for_resource( ResourceARN=alarm_arn) assert 200 == response["ResponseMetadata"]["HTTPStatusCode"] assert [{"Key": "tag2", "Value": "bar"}] == response["Tags"] # clean up cloudwatch_client.delete_alarms(AlarmNames=[alarm_name])
def put_metric_alarm( self, context: RequestContext, request: PutMetricAlarmInput, ) -> None: moto.call_moto(context) name = request.get("AlarmName") arn = aws_stack.cloudwatch_alarm_arn(name) self.tags.tag_resource(arn, request.get("Tags"))
def put_metric_alarm( self, context: RequestContext, request: PutMetricAlarmInput, ) -> None: # missing will be the default, when not set (but it will not explicitly be set) if not request.get("TreatMissingData", "missing") in [ "breaching", "notBreaching", "ignore", "missing", ]: raise ValidationError( f"The value {request['TreatMissingData']} is not supported for TreatMissingData parameter. Supported values are [breaching, notBreaching, ignore, missing]." ) # do some sanity checks: if request.get("Period"): # Valid values are 10, 30, and any multiple of 60. value = request.get("Period") if value not in (10, 30): if value % 60 != 0: raise ValidationError( "Period must be 10, 30 or a multiple of 60") if request.get("Statistic"): if not request.get("Statistic") in [ "SampleCount", "Average", "Sum", "Minimum", "Maximum", ]: raise ValidationError( f"Value '{request.get('Statistic')}' at 'statistic' failed to satisfy constraint: Member must satisfy enum value set: [Maximum, SampleCount, Sum, Minimum, Average]" ) moto.call_moto(context) name = request.get("AlarmName") arn = aws_stack.cloudwatch_alarm_arn(name) self.tags.tag_resource(arn, request.get("Tags")) self.alarm_scheduler.schedule_metric_alarm(arn)
def delete_alarms(self, context: RequestContext, alarm_names: AlarmNames) -> None: moto.call_moto(context) for alarm_name in alarm_names: arn = aws_stack.cloudwatch_alarm_arn(alarm_name) self.alarm_scheduler.delete_scheduler_for_alarm(arn)