Skip to content
This repository was archived by the owner on Feb 7, 2020. It is now read-only.

Commit d3f9615

Browse files
authored
Merge pull request #46 from ripienaar/19.1
(#19) expose identity in the cloud event headers as subject
2 parents 604f653 + b720b70 commit d3f9615

9 files changed

+43
-35
lines changed

alive.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ func newAliveEventFromJSON(j []byte) (*AliveEvent, error) {
4444
return nil, err
4545
}
4646

47-
switch event.Protocol {
47+
switch event.EventProtocol {
4848
case "io.choria.lifecycle.v1.alive":
4949
case "choria:lifecycle:alive:1":
50-
event.Protocol = "io.choria.lifecycle.v1.alive"
50+
event.EventProtocol = "io.choria.lifecycle.v1.alive"
5151
default:
52-
return nil, fmt.Errorf("invalid protocol '%s'", event.Protocol)
52+
return nil, fmt.Errorf("invalid protocol '%s'", event.EventProtocol)
5353
}
5454

5555
return event, nil

basic.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ import (
66
)
77

88
type basicEvent struct {
9-
Protocol string `json:"protocol"`
10-
EventID string `json:"id"`
11-
Ident string `json:"identity"`
12-
Comp string `json:"component"`
13-
Timestamp int64 `json:"timestamp"`
14-
EventFormat Format `json:"-"`
9+
EventProtocol string `json:"protocol"`
10+
EventID string `json:"id"`
11+
Ident string `json:"identity"`
12+
Comp string `json:"component"`
13+
Timestamp int64 `json:"timestamp"`
14+
EventFormat Format `json:"-"`
1515

1616
etype string
1717
dtype Type
1818
}
1919

20+
// Protocol retrieves the event protocol
21+
func (e *basicEvent) Protocol() string {
22+
return e.EventProtocol
23+
}
24+
2025
// Format retrieves the encoding format for the event
2126
func (e *basicEvent) Format() Format {
2227
return e.EventFormat
@@ -83,11 +88,11 @@ func (e *basicEvent) TimeStamp() time.Time {
8388

8489
func newBasicEvent(t string) basicEvent {
8590
return basicEvent{
86-
Protocol: fmt.Sprintf("io.choria.lifecycle.v1.%s", t),
87-
EventID: eventID(),
88-
Timestamp: timeStamp(),
89-
EventFormat: ChoriaFormat,
90-
etype: t,
91-
dtype: eventTypes[t],
91+
EventProtocol: fmt.Sprintf("io.choria.lifecycle.v1.%s", t),
92+
EventID: eventID(),
93+
Timestamp: timeStamp(),
94+
EventFormat: ChoriaFormat,
95+
etype: t,
96+
dtype: eventTypes[t],
9297
}
9398
}

event.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
// Event is event that can be published to the network
88
type Event interface {
9+
Protocol() string
910
Target() (string, error)
1011
String() string
1112
Type() Type

lifecycle.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ func protoStringToTypeString(proto string) (eventType string, err error) {
189189
func ToCloudEventV1(e Event) cloudevents.Event {
190190
event := cloudevents.NewEvent("1.0")
191191

192-
event.SetType(e.TypeString())
192+
event.SetType(e.Protocol())
193193
event.SetSource("io.choria.lifecycle")
194-
event.SetSubject(e.Component())
194+
event.SetSubject(e.Identity())
195195
event.SetID(e.ID())
196196
event.SetTime(e.TimeStamp())
197197
event.SetData(e)

lifecycle_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ var _ = Describe("Events", func() {
9292
event, err := NewFromJSON(j)
9393
Expect(err).ToNot(HaveOccurred())
9494
Expect(event.Type()).To(Equal(Shutdown))
95-
Expect(event.Component()).To(Equal("server"))
95+
Expect(event.Component()).To(Equal("ginkgo"))
9696
Expect(event.Format()).To(Equal(CloudEventV1Format))
9797
})
9898
})
@@ -117,7 +117,7 @@ var _ = Describe("Events", func() {
117117
event, err := New(Startup, Component("ginkgo"), Version("1.2.3"), Identity("ginkgo.example.net"))
118118
Expect(err).ToNot(HaveOccurred())
119119
event.SetFormat(CloudEventV1Format)
120-
conn.EXPECT().PublishRaw("choria.lifecycle.event.startup.ginkgo", []byte(`{"data":{"protocol":"io.choria.lifecycle.v1.startup","id":"01e72410-d734-4611-9485-8c6a2dd2579b","identity":"ginkgo.example.net","component":"ginkgo","timestamp":1535106973,"version":"1.2.3"},"id":"01e72410-d734-4611-9485-8c6a2dd2579b","source":"io.choria.lifecycle","specversion":"1.0","subject":"ginkgo","time":"2018-08-24T10:36:13Z","type":"startup"}`))
120+
conn.EXPECT().PublishRaw("choria.lifecycle.event.startup.ginkgo", []byte(`{"data":{"protocol":"io.choria.lifecycle.v1.startup","id":"01e72410-d734-4611-9485-8c6a2dd2579b","identity":"ginkgo.example.net","component":"ginkgo","timestamp":1535106973,"version":"1.2.3"},"id":"01e72410-d734-4611-9485-8c6a2dd2579b","source":"io.choria.lifecycle","specversion":"1.0","subject":"ginkgo.example.net","time":"2018-08-24T10:36:13Z","type":"io.choria.lifecycle.v1.startup"}`))
121121
err = PublishEvent(event, conn)
122122
Expect(err).ToNot(HaveOccurred())
123123
})

provisioned.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ func newProvisionedEventFromJSON(j []byte) (*ProvisionedEvent, error) {
4040
return nil, err
4141
}
4242

43-
switch event.Protocol {
43+
switch event.EventProtocol {
4444
case "io.choria.lifecycle.v1.provisioned":
4545
case "choria:lifecycle:provisioned:1":
46-
event.Protocol = "io.choria.lifecycle.v1.provisioned"
46+
event.EventProtocol = "io.choria.lifecycle.v1.provisioned"
4747
default:
48-
return nil, fmt.Errorf("invalid protocol '%s'", event.Protocol)
48+
return nil, fmt.Errorf("invalid protocol '%s'", event.EventProtocol)
4949
}
5050

5151
return event, nil

shutdown.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ func newShutdownEventFromJSON(j []byte) (*ShutdownEvent, error) {
3939
return nil, err
4040
}
4141

42-
switch event.Protocol {
42+
switch event.EventProtocol {
4343
case "io.choria.lifecycle.v1.shutdown":
4444
case "choria:lifecycle:shutdown:1":
45-
event.Protocol = "io.choria.lifecycle.v1.shutdown"
45+
event.EventProtocol = "io.choria.lifecycle.v1.shutdown"
4646
default:
47-
return nil, fmt.Errorf("invalid protocol '%s'", event.Protocol)
47+
return nil, fmt.Errorf("invalid protocol '%s'", event.EventProtocol)
4848
}
4949

5050
return event, nil

startup.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ func newStartupEventFromJSON(j []byte) (*StartupEvent, error) {
4444
return nil, err
4545
}
4646

47-
switch event.Protocol {
47+
switch event.EventProtocol {
4848
case "io.choria.lifecycle.v1.startup":
4949
case "choria:lifecycle:startup:1":
50-
event.Protocol = "io.choria.lifecycle.v1.startup"
50+
event.EventProtocol = "io.choria.lifecycle.v1.startup"
5151
default:
52-
return nil, fmt.Errorf("invalid protocol '%s'", event.Protocol)
52+
return nil, fmt.Errorf("invalid protocol '%s'", event.EventProtocol)
5353
}
5454

5555
return event, nil
+9-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
2+
"id": "01e72410-d734-4611-9485-8c6a2dd2579b",
3+
"source": "io.choria.lifecycle",
24
"specversion": "1.0",
5+
"subject": "ginkgo.example.net",
6+
"time": "2018-08-24T10:36:13Z",
37
"type": "io.choria.lifecycle.v1.shutdown",
4-
"source": "io.choria.lifecycle",
5-
"id": "A234-1234-1234",
6-
"time": "2019-12-04T18:58:58Z",
78
"data": {
89
"protocol": "io.choria.lifecycle.v1.shutdown",
9-
"id": "cb6f1dbc-d65b-49b9-907e-e4a51bee75f8",
10-
"identity": "dev1.devco.net",
11-
"component": "server",
12-
"timestamp": 1575482338
10+
"id": "01e72410-d734-4611-9485-8c6a2dd2579b",
11+
"identity": "ginkgo.example.net",
12+
"component": "ginkgo",
13+
"timestamp": 1535106973,
14+
"version": "1.2.3"
1315
}
1416
}

0 commit comments

Comments
 (0)