Skip to content

Commit 4a2ac19

Browse files
committed
Add S3Event
1 parent eb9e85c commit 4a2ac19

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2021 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package feral.lambda.events
18+
19+
import io.circe.Decoder
20+
21+
import java.time.Instant
22+
23+
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/aws-lambda/trigger/s3.d.ts
24+
25+
final case class S3Event(records: List[S3EventRecord])
26+
27+
object S3Event {
28+
implicit val decoder: Decoder[S3Event] = Decoder.forProduct1("Records")(S3Event.apply)
29+
}
30+
31+
final case class S3EventRecord(
32+
eventVersion: String,
33+
eventSource: String,
34+
awsRegion: String,
35+
eventTime: Instant,
36+
eventName: String,
37+
userIdentity: S3UserIdentity,
38+
requestParameters: S3RequestParameters,
39+
responseElements: S3ResponseElements,
40+
s3: S3,
41+
glacierEventData: Option[S3EventRecordGlacierEventData])
42+
43+
object S3EventRecord {
44+
45+
implicit val decoder: Decoder[S3EventRecord] =
46+
Decoder.forProduct10(
47+
"eventVersion",
48+
"eventSource",
49+
"awsRegion",
50+
"eventTime",
51+
"eventName",
52+
"userIdentity",
53+
"requestParameters",
54+
"responseElements",
55+
"s3",
56+
"glacierEventData"
57+
)(S3EventRecord.apply)
58+
59+
}
60+
61+
final case class S3UserIdentity(principalId: String)
62+
63+
object S3UserIdentity {
64+
65+
implicit val decoder: Decoder[S3UserIdentity] =
66+
Decoder.forProduct1("principalId")(S3UserIdentity.apply)
67+
68+
}
69+
70+
final case class S3RequestParameters(sourceIPAddress: String)
71+
72+
object S3RequestParameters {
73+
74+
implicit val decoder: Decoder[S3RequestParameters] =
75+
Decoder.forProduct1("sourceIPAddress")(S3RequestParameters.apply)
76+
77+
}
78+
79+
final case class S3ResponseElements(`x-amz-request-id`: String, `x-amz-id-2`: String)
80+
81+
object S3ResponseElements {
82+
83+
implicit val decoder: Decoder[S3ResponseElements] =
84+
Decoder.forProduct2("x-amz-request-id", "x-amz-id-2")(S3ResponseElements.apply)
85+
86+
}
87+
88+
final case class S3(
89+
s3SchemaVersion: String,
90+
configurationId: String,
91+
bucket: S3Bucket,
92+
`object`: S3Object)
93+
94+
object S3 {
95+
96+
implicit val decoder: Decoder[S3] =
97+
Decoder.forProduct4("s3SchemaVersion", "configurationId", "bucket", "object")(S3.apply)
98+
99+
}
100+
101+
final case class S3Bucket(name: String, ownerIdentity: S3UserIdentity, arn: String)
102+
103+
object S3Bucket {
104+
105+
implicit val decoder: Decoder[S3Bucket] =
106+
Decoder.forProduct3("name", "ownerIdentity", "arn")(S3Bucket.apply)
107+
108+
}
109+
110+
final case class S3Object(
111+
key: String,
112+
size: Long,
113+
eTag: String,
114+
versionId: Option[String],
115+
sequencer: String)
116+
117+
object S3Object {
118+
119+
implicit val decoder: Decoder[S3Object] =
120+
Decoder.forProduct5("key", "size", "eTag", "versionId", "sequencer")(S3Object.apply)
121+
122+
}
123+
124+
final case class S3EventRecordGlacierEventData(
125+
restoreEventData: S3EventRecordGlacierRestoreEventData)
126+
127+
object S3EventRecordGlacierEventData {
128+
129+
implicit val decoder: Decoder[S3EventRecordGlacierEventData] =
130+
Decoder.forProduct1("restoreEventData")(S3EventRecordGlacierEventData.apply)
131+
132+
}
133+
134+
final case class S3EventRecordGlacierRestoreEventData(
135+
lifecycleRestorationExpiryTime: Instant,
136+
lifecycleRestoreStorageClass: String)
137+
138+
object S3EventRecordGlacierRestoreEventData {
139+
140+
implicit val decoder: Decoder[S3EventRecordGlacierRestoreEventData] =
141+
Decoder.forProduct2("lifecycleRestorationExpiryTime", "lifecycleRestoreStorageClass")(
142+
S3EventRecordGlacierRestoreEventData.apply
143+
)
144+
145+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2021 Typelevel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package feral.lambda.events
18+
19+
import io.circe.literal._
20+
import munit.FunSuite
21+
22+
import java.time.Instant
23+
24+
class S3EventSuite extends FunSuite {
25+
26+
test("decoder") {
27+
assertEquals(event.as[S3Event].toTry.get, result)
28+
}
29+
30+
def event = json"""
31+
{
32+
"Records":[
33+
{
34+
"eventVersion":"2.1",
35+
"eventSource":"aws:s3",
36+
"awsRegion":"us-west-2",
37+
"eventTime":"1970-01-01T00:00:00.000Z",
38+
"eventName":"ObjectCreated:Put",
39+
"userIdentity":{
40+
"principalId":"AIDAJDPLRKLG7UEXAMPLE"
41+
},
42+
"requestParameters":{
43+
"sourceIPAddress":"127.0.0.1"
44+
},
45+
"responseElements":{
46+
"x-amz-request-id":"C3D13FE58DE4C810",
47+
"x-amz-id-2":"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
48+
},
49+
"s3":{
50+
"s3SchemaVersion":"1.0",
51+
"configurationId":"testConfigRule",
52+
"bucket":{
53+
"name":"mybucket",
54+
"ownerIdentity":{
55+
"principalId":"A3NL1KOZZKExample"
56+
},
57+
"arn":"arn:aws:s3:::mybucket"
58+
},
59+
"object":{
60+
"key":"HappyFace.jpg",
61+
"size":1024,
62+
"eTag":"d41d8cd98f00b204e9800998ecf8427e",
63+
"versionId":"096fKKXTRTtl3on89fVO.nfljtsv6qko",
64+
"sequencer":"0055AED6DCD90281E5"
65+
}
66+
}
67+
}
68+
]
69+
}
70+
"""
71+
72+
def result = S3Event(records = List(
73+
S3EventRecord(
74+
eventVersion = "2.1",
75+
eventSource = "aws:s3",
76+
awsRegion = "us-west-2",
77+
eventTime = Instant.ofEpochSecond(0),
78+
eventName = "ObjectCreated:Put",
79+
userIdentity = S3UserIdentity("AIDAJDPLRKLG7UEXAMPLE"),
80+
requestParameters = S3RequestParameters("127.0.0.1"),
81+
responseElements = S3ResponseElements(
82+
"C3D13FE58DE4C810",
83+
"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
84+
),
85+
s3 = S3(
86+
s3SchemaVersion = "1.0",
87+
configurationId = "testConfigRule": String,
88+
bucket = S3Bucket(
89+
name = "mybucket",
90+
ownerIdentity = S3UserIdentity("A3NL1KOZZKExample"),
91+
arn = "arn:aws:s3:::mybucket"
92+
),
93+
`object` = S3Object(
94+
key = "HappyFace.jpg",
95+
size = 1024L,
96+
eTag = "d41d8cd98f00b204e9800998ecf8427e",
97+
versionId = Option("096fKKXTRTtl3on89fVO.nfljtsv6qko"),
98+
sequencer = "0055AED6DCD90281E5"
99+
)
100+
),
101+
glacierEventData = None
102+
)
103+
))
104+
105+
}

0 commit comments

Comments
 (0)