Skip to content

Commit 99cb998

Browse files
author
wafuwafu13
committed
feat: add ECSContainerInstanceEvent
1 parent 81d63bd commit 99cb998

File tree

4 files changed

+292
-0
lines changed

4 files changed

+292
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Sample Function
2+
3+
The following is a sample class and Lambda function that receives Amazon ECS Container instance state change events record data as an input and writes some of the record data to CloudWatch Logs. (Note that anything written to stdout or stderr will be logged as CloudWatch Logs events.)
4+
5+
```go
6+
7+
package main
8+
9+
import (
10+
"context"
11+
"fmt"
12+
13+
"github.com/aws/aws-lambda-go/events"
14+
"github.com/aws/aws-lambda-go/lambda"
15+
)
16+
17+
func handler(ctx context.Context, ecsEvent events.ECSContainerInstanceEvent) {
18+
outputJSON, _ := json.MarshalIndent(ecsEvent, "", " ")
19+
fmt.Printf("Data = %s", outputJSON)
20+
}
21+
22+
func main() {
23+
lambda.Start(handler)
24+
}
25+
26+
```

events/ecs_container_instance.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package events
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
)
7+
8+
type ECSContainerInstanceEvent struct {
9+
Version string `json:"version"`
10+
ID string `json:"id"`
11+
DetailType string `json:"detail-type"`
12+
Source string `json:"source"`
13+
Account string `json:"account"`
14+
Time time.Time `json:"time"`
15+
Region string `json:"region"`
16+
Resources []string `json:"resources"`
17+
Detail ECSContainerInstanceEventDetailType `json:"detail"`
18+
}
19+
20+
type ECSContainerInstanceEventDetailType struct {
21+
AgentConnected bool `json:"agentConnected"`
22+
Attributes []Attribute `json:"attributes"`
23+
ClusterARN string `json:"clusterArn"`
24+
ContainerInstanceARN string `json:"containerInstanceArn"`
25+
EC2InstanceID string `json:"ec2InstanceId"`
26+
RegisteredResources []Resource `json:"registeredResources"`
27+
RemainingResources []Resource `json:"remainingResources"`
28+
Status string `json:"status"`
29+
Version int `json:"version"`
30+
VersionInfo VersionInfo `json:"versionInfo"`
31+
UpdatedAt time.Time `json:"updatedAt"`
32+
}
33+
34+
type Attribute struct {
35+
Name string `json:"name"`
36+
}
37+
38+
type Resource struct {
39+
Name string `json:"name"`
40+
Type string `json:"type"`
41+
IntegerValue int `json:"integerValue,omitempty"`
42+
StringSetValue []*string `json:"stringSetValue,omitempty"`
43+
}
44+
45+
type VersionInfo struct {
46+
AgentHash string `json:"agentHash"`
47+
AgentVersion string `json:"agentVersion"`
48+
DockerVersion string `json:"dockerVersion"`
49+
}
50+
51+
// MarshalJSON implements cuustom marshaling to marshal the struct into JSON format while preserving an empty string slice in `StringSetValue` field.
52+
func (r Resource) MarshalJSON() ([]byte, error) {
53+
type Alias Resource
54+
aux := struct {
55+
StringSetValue json.RawMessage `json:"stringSetValue,omitempty"`
56+
Alias
57+
}{
58+
Alias: (Alias)(r),
59+
}
60+
61+
if r.StringSetValue != nil {
62+
b, err := json.Marshal(r.StringSetValue)
63+
if err != nil {
64+
return nil, err
65+
}
66+
aux.StringSetValue = b
67+
}
68+
69+
return json.Marshal(&aux)
70+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
package events
3+
4+
import (
5+
"encoding/json"
6+
"testing"
7+
"time"
8+
9+
"github.com/aws/aws-lambda-go/events/test"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestECSContainerInstanceEventMarshaling(t *testing.T) {
14+
// 1. read JSON from file
15+
inputJSON := test.ReadJSONFromFile(t, "./testdata/ecs-container-instance-state-change.json")
16+
17+
// 2. de-serialize into Go object
18+
var inputEvent ECSContainerInstanceEvent
19+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
20+
t.Errorf("could not unmarshal event. details: %v", err)
21+
}
22+
23+
// 3. Verify values populated into Go Object, at least one validation per data type
24+
assert.Equal(t, "0", inputEvent.Version)
25+
assert.Equal(t, "8952ba83-7be2-4ab5-9c32-6687532d15a2", inputEvent.ID)
26+
assert.Equal(t, "ECS Container Instance State Change", inputEvent.DetailType)
27+
assert.Equal(t, "aws.ecs", inputEvent.Source)
28+
assert.Equal(t, "111122223333", inputEvent.Account)
29+
assert.Equal(t, "us-east-1", inputEvent.Region)
30+
assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315", inputEvent.Resources[0])
31+
testTime, err := time.Parse(time.RFC3339, "2016-12-06T16:41:06Z")
32+
if err != nil {
33+
t.Errorf("Failed to parse time: %v", err)
34+
}
35+
assert.Equal(t, testTime, inputEvent.Time)
36+
37+
var detail = inputEvent.Detail
38+
assert.True(t, detail.AgentConnected)
39+
assert.Equal(t, "com.amazonaws.ecs.capability.logging-driver.syslog", detail.Attributes[0].Name)
40+
assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:cluster/default", detail.ClusterARN)
41+
assert.Equal(t, "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315", detail.ContainerInstanceARN)
42+
assert.Equal(t, "i-f3a8506b", detail.EC2InstanceID)
43+
assert.Equal(t, "CPU", detail.RegisteredResources[0].Name)
44+
assert.Equal(t, "INTEGER", detail.RegisteredResources[0].Type)
45+
assert.Equal(t, 2048, detail.RegisteredResources[0].IntegerValue)
46+
assert.Equal(t, "MEMORY", detail.RegisteredResources[1].Name)
47+
assert.Equal(t, "INTEGER", detail.RegisteredResources[1].Type)
48+
assert.Equal(t, 3767, detail.RegisteredResources[1].IntegerValue)
49+
assert.Equal(t, "PORTS", detail.RegisteredResources[2].Name)
50+
assert.Equal(t, "STRINGSET", detail.RegisteredResources[2].Type)
51+
assert.Equal(t, []*string{ptr("22"), ptr("2376"), ptr("2375"), ptr("51678"), ptr("51679")}, detail.RegisteredResources[2].StringSetValue)
52+
assert.Equal(t, "PORTS_UDP", detail.RegisteredResources[3].Name)
53+
assert.Equal(t, "STRINGSET", detail.RegisteredResources[3].Type)
54+
assert.Equal(t, []*string{}, detail.RegisteredResources[3].StringSetValue)
55+
assert.Equal(t, "CPU", detail.RemainingResources[0].Name)
56+
assert.Equal(t, "INTEGER", detail.RemainingResources[0].Type)
57+
assert.Equal(t, 1988, detail.RemainingResources[0].IntegerValue)
58+
assert.Equal(t, "MEMORY", detail.RemainingResources[1].Name)
59+
assert.Equal(t, "INTEGER", detail.RemainingResources[1].Type)
60+
assert.Equal(t, 767, detail.RemainingResources[1].IntegerValue)
61+
assert.Equal(t, "PORTS", detail.RemainingResources[2].Name)
62+
assert.Equal(t, "STRINGSET", detail.RemainingResources[2].Type)
63+
assert.Equal(t, []*string{ptr("22"), ptr("2376"), ptr("2375"), ptr("51678"), ptr("51679")}, detail.RemainingResources[2].StringSetValue)
64+
assert.Equal(t, "PORTS_UDP", detail.RemainingResources[3].Name)
65+
assert.Equal(t, "STRINGSET", detail.RemainingResources[3].Type)
66+
assert.Equal(t, []*string{}, detail.RemainingResources[3].StringSetValue)
67+
assert.Equal(t, "ACTIVE", detail.Status)
68+
assert.Equal(t, 14801, detail.Version)
69+
assert.Equal(t, "aebcbca", detail.VersionInfo.AgentHash)
70+
assert.Equal(t, "1.13.0", detail.VersionInfo.AgentVersion)
71+
assert.Equal(t, "DockerVersion: 1.11.2", detail.VersionInfo.DockerVersion)
72+
testUpdateTime, err := time.Parse(time.RFC3339, "2016-12-06T16:41:06Z")
73+
if err != nil {
74+
t.Errorf("Failed to parse time: %v", err)
75+
}
76+
assert.Equal(t, testUpdateTime, inputEvent.Time)
77+
78+
// 4. serialize to JSON
79+
outputJSON, err := json.Marshal(inputEvent)
80+
if err != nil {
81+
t.Errorf("could not marshal event. details: %v", err)
82+
}
83+
84+
// 5. check result
85+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
86+
}
87+
88+
func ptr(s string) *string {
89+
return &s
90+
}
91+
92+
func TestECSContainerInstanceMarshalingMalformedJson(t *testing.T) {
93+
test.TestMalformedJson(t, ECSContainerInstanceEvent{})
94+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"version": "0",
3+
"id": "8952ba83-7be2-4ab5-9c32-6687532d15a2",
4+
"detail-type": "ECS Container Instance State Change",
5+
"source": "aws.ecs",
6+
"account": "111122223333",
7+
"time": "2016-12-06T16:41:06Z",
8+
"region": "us-east-1",
9+
"resources": [
10+
"arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315"
11+
],
12+
"detail": {
13+
"agentConnected": true,
14+
"attributes": [
15+
{
16+
"name": "com.amazonaws.ecs.capability.logging-driver.syslog"
17+
},
18+
{
19+
"name": "com.amazonaws.ecs.capability.task-iam-role-network-host"
20+
},
21+
{
22+
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
23+
},
24+
{
25+
"name": "com.amazonaws.ecs.capability.logging-driver.json-file"
26+
},
27+
{
28+
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.17"
29+
},
30+
{
31+
"name": "com.amazonaws.ecs.capability.privileged-container"
32+
}
33+
],
34+
"clusterArn": "arn:aws:ecs:us-east-1:111122223333:cluster/default",
35+
"containerInstanceArn": "arn:aws:ecs:us-east-1:111122223333:container-instance/b54a2a04-046f-4331-9d74-3f6d7f6ca315",
36+
"ec2InstanceId": "i-f3a8506b",
37+
"registeredResources": [
38+
{
39+
"name": "CPU",
40+
"type": "INTEGER",
41+
"integerValue": 2048
42+
},
43+
{
44+
"name": "MEMORY",
45+
"type": "INTEGER",
46+
"integerValue": 3767
47+
},
48+
{
49+
"name": "PORTS",
50+
"type": "STRINGSET",
51+
"stringSetValue": [
52+
"22",
53+
"2376",
54+
"2375",
55+
"51678",
56+
"51679"
57+
]
58+
},
59+
{
60+
"name": "PORTS_UDP",
61+
"type": "STRINGSET",
62+
"stringSetValue": []
63+
}
64+
],
65+
"remainingResources": [
66+
{
67+
"name": "CPU",
68+
"type": "INTEGER",
69+
"integerValue": 1988
70+
},
71+
{
72+
"name": "MEMORY",
73+
"type": "INTEGER",
74+
"integerValue": 767
75+
},
76+
{
77+
"name": "PORTS",
78+
"type": "STRINGSET",
79+
"stringSetValue": [
80+
"22",
81+
"2376",
82+
"2375",
83+
"51678",
84+
"51679"
85+
]
86+
},
87+
{
88+
"name": "PORTS_UDP",
89+
"type": "STRINGSET",
90+
"stringSetValue": []
91+
}
92+
],
93+
"status": "ACTIVE",
94+
"version": 14801,
95+
"versionInfo": {
96+
"agentHash": "aebcbca",
97+
"agentVersion": "1.13.0",
98+
"dockerVersion": "DockerVersion: 1.11.2"
99+
},
100+
"updatedAt": "2016-12-06T16:41:06.991Z"
101+
}
102+
}

0 commit comments

Comments
 (0)