Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the object representation of the CloudWatch Alarm #485

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions aws-lambda-java-events/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lombok.version>1.18.22</lombok.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msailes is this change required in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. There was a warning in the build logs, I added these to remove the warning.

</properties>

<distributionManagement>
Expand Down Expand Up @@ -69,6 +71,18 @@
<version>2.36.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-serialization</artifactId>
msailes marked this conversation as resolved.
Show resolved Hide resolved
<version>1.1.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.amazonaws.services.lambda.runtime.events;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class CloudWatchAlarmEvent {
msailes marked this conversation as resolved.
Show resolved Hide resolved

private String source;
private String alarmArn;
private String accountId;
private String time;
private String region;
private AlarmData alarmData;

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class AlarmData {
private String alarmName;
private State state;
private State previousState;
private Configuration configuration;
}

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
private static class State {
private String value;
private String reason;
private String reasonData;
private String timestamp;
private String actionsSuppressedBy;
private String actionsSuppressedReason;
}

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
private static class Configuration {
private String description;
private List<Metric> metrics;
private Boolean returnData;

private String alarmRule;
private String actionsSuppressor;
private Integer actionsSuppressorWaitPeriod;
private Integer actionsSuppressorExtensionPeriod;
}

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
private static class Metric {
private String id;
private MetricStat metricStat;
private Integer period;
private String stat;
private String unit;
}

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
private static class MetricStat {
private String namespace;
private String name;
private Map<String, String> dimensions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.amazonaws.services.lambda.runtime.events;

import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer;
import com.amazonaws.services.lambda.runtime.serialization.factories.JacksonFactory;
import org.json.JSONException;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;

public class CloudWatchAlarmEventTest {
msailes marked this conversation as resolved.
Show resolved Hide resolved

JacksonFactory jacksonFactory = JacksonFactory.getInstance();

@Test
public void serdeCloudWatchCompositeAlarmEvent() throws JSONException {
String expected = readResource("cloudwatch-composite-alarm.json");
String actual = deserializeSerializeJsonToString(expected, CloudWatchAlarmEvent.class);

assertEquals(expected, actual, STRICT);
}

@Test
public void serdeCloudWatchMetricAlarmEvent() throws JSONException {
String expected = readResource("cloudwatch-composite-alarm.json");
String actual = deserializeSerializeJsonToString(expected, CloudWatchAlarmEvent.class);

assertEquals(expected, actual, STRICT);
}

private <T> String deserializeSerializeJsonToString(String expected, Class<T> modelClass) {
PojoSerializer<T> serializer = jacksonFactory.getSerializer(modelClass);
T event = serializer.fromJson(expected);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.toJson(event, baos);

return new String(baos.toByteArray(), StandardCharsets.UTF_8);
}

private String readResource(String name) {
Path filePath = Paths.get("src", "test", "resources", name);
byte[] bytes = new byte[0];
try {
bytes = Files.readAllBytes(filePath);
} catch (IOException e) {
e.printStackTrace();
}
return new String(bytes, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"source": "aws.cloudwatch",
"alarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:SuppressionDemo.Main",
"accountId": "111122223333",
"time": "2023-08-04T12:56:46.138+0000",
"region": "us-east-1",
"alarmData": {
"alarmName": "CompositeDemo.Main",
"state": {
"value": "ALARM",
"reason": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild transitioned to ALARM at Friday 04 August, 2023 12:54:46 UTC",
"reasonData": "{\"triggeringAlarms\":[{\"arn\":\"arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild\",\"state\":{\"value\":\"ALARM\",\"timestamp\":\"2023-08-04T12:54:46.138+0000\"}}]}",
"timestamp": "2023-08-04T12:56:46.138+0000"
},
"previousState": {
"value": "ALARM",
"reason": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild transitioned to ALARM at Friday 04 August, 2023 12:54:46 UTC",
"reasonData": "{\"triggeringAlarms\":[{\"arn\":\"arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild\",\"state\":{\"value\":\"ALARM\",\"timestamp\":\"2023-08-04T12:54:46.138+0000\"}}]}",
"timestamp": "2023-08-04T12:54:46.138+0000",
"actionsSuppressedBy": "WaitPeriod",
"actionsSuppressedReason": "Actions suppressed by WaitPeriod"
},
"configuration": {
"alarmRule": "ALARM(CompositeDemo.FirstChild) OR ALARM(CompositeDemo.SecondChild)",
"actionsSuppressor": "CompositeDemo.ActionsSuppressor",
"actionsSuppressorWaitPeriod": 120,
"actionsSuppressorExtensionPeriod": 180
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"source": "aws.cloudwatch",
"alarmArn": "arn:aws:cloudwatch:us-east-1:444455556666:alarm:lambda-demo-metric-alarm",
"accountId": "444455556666",
"time": "2023-08-04T12:36:15.490+0000",
"region": "us-east-1",
"alarmData": {
"alarmName": "lambda-demo-metric-alarm",
"state": {
"value": "ALARM",
"reason": "test",
"timestamp": "2023-08-04T12:36:15.490+0000"
},
"previousState": {
"value": "INSUFFICIENT_DATA",
"reason": "Insufficient Data: 5 datapoints were unknown.",
"reasonData": "{\"version\":\"1.0\",\"queryDate\":\"2023-08-04T12:31:29.591+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[],\"threshold\":5.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2023-08-04T12:30:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:29:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:28:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:27:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:26:00.000+0000\"}]}",
"timestamp": "2023-08-04T12:31:29.595+0000"
},
"configuration": {
"description": "Metric Alarm to test Lambda actions",
"metrics": [
{
"id": "1234e046-06f0-a3da-9534-EXAMPLEe4c",
"metricStat": {
"metric": {
"namespace": "AWS/Logs",
"name": "CallCount",
"dimensions": {
"InstanceId": "i-12345678"
}
},
"period": 60,
"stat": "Average",
"unit": "Percent"
},
"returnData": True
}
]
}
}
}
Loading