Skip to content

Commit abb7ee7

Browse files
committed
feat: add Glacier event data structure and corresponding test for S3 events
1 parent 1191ffb commit abb7ee7

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

events/s3.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type S3EventRecord struct {
2424
RequestParameters S3RequestParameters `json:"requestParameters"`
2525
ResponseElements map[string]string `json:"responseElements"`
2626
S3 S3Entity `json:"s3"`
27+
GlacierEventData *S3GlacierEventData `json:"glacierEventData,omitempty"`
2728
ReplicationEventData *S3ReplicationEventData `json:"replicationEventData,omitempty"`
2829
}
2930

@@ -71,6 +72,15 @@ func (o *S3Object) UnmarshalJSON(data []byte) error {
7172
return nil
7273
}
7374

75+
type S3GlacierEventData struct {
76+
RestoreEventData *S3RestoreEventData `json:"restoreEventData"`
77+
}
78+
79+
type S3RestoreEventData struct {
80+
LifecycleRestorationExpiryTime time.Time `json:"lifecycleRestorationExpiryTime"`
81+
LifecycleRestoreStorageClass string `json:"lifecycleRestoreStorageClass"`
82+
}
83+
7484
type S3ReplicationEventData struct {
7585
ReplicationRuleID string `json:"replicationRuleId"`
7686
DestinationBucket string `json:"destinationBucket"`

events/s3_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ func TestS3MarshalingMalformedJSON(t *testing.T) {
5757
test.TestMalformedJson(t, S3Event{})
5858
}
5959

60+
func TestS3GlacierEventMarshaling(t *testing.T) {
61+
// 1. read JSON from file
62+
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-glacier-event.json")
63+
64+
// 2. de-serialize into Go object
65+
var inputEvent S3Event
66+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
67+
t.Errorf("could not unmarshal event. details: %v", err)
68+
}
69+
70+
// 3. verify glacierEventData is correctly parsed
71+
if inputEvent.Records[0].GlacierEventData == nil {
72+
t.Error("glacierEventData should not be nil for glacier restore events")
73+
}
74+
75+
// 4. verify restoreEventData is correctly parsed
76+
if inputEvent.Records[0].GlacierEventData.RestoreEventData == nil {
77+
t.Error("restoreEventData should not be nil")
78+
}
79+
80+
// 5. serialize to JSON
81+
outputJSON, err := json.Marshal(inputEvent)
82+
if err != nil {
83+
t.Errorf("could not marshal event. details: %v", err)
84+
}
85+
86+
// 6. check result
87+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
88+
}
89+
6090
func TestS3ReplicationEventMarshaling(t *testing.T) {
6191
// 1. read JSON from file
6292
inputJSON := test.ReadJSONFromFile(t, "./testdata/s3-replication-event.json")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"Records": [
3+
{
4+
"eventVersion": "2.1",
5+
"eventSource": "aws:s3",
6+
"awsRegion": "us-west-2",
7+
"eventTime": "2023-05-10T15:00:00.123Z",
8+
"eventName": "s3:ObjectRestore:Completed",
9+
"userIdentity": {
10+
"principalId": "EXAMPLE"
11+
},
12+
"requestParameters": {
13+
"sourceIPAddress": "127.0.0.1"
14+
},
15+
"responseElements": {
16+
"x-amz-request-id": "EXAMPLE123456789",
17+
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
18+
},
19+
"s3": {
20+
"s3SchemaVersion": "1.0",
21+
"configurationId": "testConfigRule",
22+
"bucket": {
23+
"name": "example-bucket",
24+
"ownerIdentity": {
25+
"principalId": "EXAMPLE"
26+
},
27+
"arn": "arn:aws:s3:::example-bucket"
28+
},
29+
"object": {
30+
"key": "glacier%2Dtest.txt",
31+
"urlDecodedKey": "glacier-test.txt",
32+
"size": 1024,
33+
"versionId": "abcdeH0Xp66ep__QDjR76LK7Gc9X4wKO",
34+
"eTag": "0123456789abcdef0123456789abcdef",
35+
"sequencer": "0A1B2C3D4E5F678901"
36+
}
37+
},
38+
"glacierEventData": {
39+
"restoreEventData": {
40+
"lifecycleRestorationExpiryTime": "2023-05-17T15:00:00.456Z",
41+
"lifecycleRestoreStorageClass": "STANDARD"
42+
}
43+
}
44+
}
45+
]
46+
}

0 commit comments

Comments
 (0)