Skip to content

Commit be52e48

Browse files
committed
feat: add lifecycle event data structure and corresponding test for S3 events
1 parent 5516bce commit be52e48

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

events/s3.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type S3EventRecord struct {
2828
RestoreEventData *S3RestoreEventData `json:"restoreEventData,omitempty"`
2929
ReplicationEventData *S3ReplicationEventData `json:"replicationEventData,omitempty"`
3030
IntelligentTieringEventData *S3IntelligentTieringEventData `json:"intelligentTieringEventData,omitempty"`
31+
LifecycleEventData *S3LifecycleEventData `json:"lifecycleEventData,omitempty"`
3132
}
3233

3334
type S3UserIdentity struct {
@@ -95,6 +96,14 @@ type S3IntelligentTieringEventData struct {
9596
DestinationAccessTier string `json:"destinationAccessTier"`
9697
}
9798

99+
type S3LifecycleEventData struct {
100+
TransitionEventData *S3TransitionEventData `json:"transitionEventData"`
101+
}
102+
103+
type S3TransitionEventData struct {
104+
DestinationStorageClass string `json:"destinationStorageClass"`
105+
}
106+
98107
type S3TestEvent struct {
99108
Service string `json:"Service"`
100109
Bucket string `json:"Bucket"`

events/s3_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,41 @@ func TestS3IntelligentTieringEventMarshaling(t *testing.T) {
142142
assert.JSONEq(t, string(inputJSON), string(outputJSON))
143143
}
144144

145+
func TestS3LifecycleEventMarshaling(t *testing.T) {
146+
// 1. read JSON from file
147+
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-lifecycle-event.json")
148+
149+
// 2. de-serialize into Go object
150+
var inputEvent S3Event
151+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
152+
t.Errorf("could not unmarshal event. details: %v", err)
153+
}
154+
155+
// 3. verify lifecycleEventData is correctly parsed
156+
if inputEvent.Records[0].LifecycleEventData == nil {
157+
t.Error("lifecycleEventData should not be nil for lifecycle events")
158+
}
159+
160+
// 4. verify transitionEventData is correctly parsed
161+
if inputEvent.Records[0].LifecycleEventData.TransitionEventData == nil {
162+
t.Error("transitionEventData should not be nil")
163+
}
164+
165+
// 5. verify destinationStorageClass is correctly parsed
166+
if inputEvent.Records[0].LifecycleEventData.TransitionEventData.DestinationStorageClass == "" {
167+
t.Error("destinationStorageClass should not be empty")
168+
}
169+
170+
// 6. serialize to JSON
171+
outputJSON, err := json.Marshal(inputEvent)
172+
if err != nil {
173+
t.Errorf("could not marshal event. details: %v", err)
174+
}
175+
176+
// 7. check result
177+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
178+
}
179+
145180
func TestS3ReplicationEventMarshaling(t *testing.T) {
146181
// 1. read JSON from file
147182
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-replication-event.json")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"Records": [
3+
{
4+
"eventVersion": "2.3",
5+
"eventSource": "aws:s3",
6+
"awsRegion": "us-west-2",
7+
"eventTime": "2023-08-20T14:25:00.321Z",
8+
"eventName": "s3:LifecycleTransition",
9+
"userIdentity": {
10+
"principalId": "s3.amazonaws.com"
11+
},
12+
"requestParameters": {
13+
"sourceIPAddress": "s3.amazonaws.com"
14+
},
15+
"responseElements": {
16+
"x-amz-request-id": "LIFECYCLE123456789",
17+
"x-amz-id-2": "LIFECYCLEabcdefghijklmnopqrstuvwxyz"
18+
},
19+
"s3": {
20+
"s3SchemaVersion": "1.0",
21+
"configurationId": "LifecycleTransitionRule",
22+
"bucket": {
23+
"name": "example-bucket",
24+
"ownerIdentity": {
25+
"principalId": "EXAMPLE"
26+
},
27+
"arn": "arn:aws:s3:::example-bucket"
28+
},
29+
"object": {
30+
"key": "lifecycle%2Dtest.dat",
31+
"urlDecodedKey": "lifecycle-test.dat",
32+
"size": 10485760,
33+
"versionId": "lifecycleVersion123",
34+
"eTag": "abcd1234ef5678901234567890abcdef",
35+
"sequencer": "0066LIFECYCLE890"
36+
}
37+
},
38+
"lifecycleEventData": {
39+
"transitionEventData": {
40+
"destinationStorageClass": "GLACIER"
41+
}
42+
}
43+
}
44+
]
45+
}

0 commit comments

Comments
 (0)