-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.go
More file actions
265 lines (215 loc) · 5.97 KB
/
event.go
File metadata and controls
265 lines (215 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package transistor
import (
"errors"
"fmt"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
json "github.com/bww/go-json"
log "github.com/codeamp/logger"
uuid "github.com/satori/go.uuid"
)
type Action string
type State string
type EventName string
func GetAction(s string) Action {
actions := []string{
"create",
"update",
"delete",
"status",
}
for _, action := range actions {
if s == action {
return Action(action)
}
}
errMsg := fmt.Sprintf("Action not found: '%s' ", s)
_, file, line, ok := runtime.Caller(1)
if ok {
errMsg += fmt.Sprintf("%s : ln %d", file, line)
}
log.Panic(errMsg)
return Action("unknown")
}
func GetState(s string) State {
states := []string{
"waiting",
"running",
"complete",
"failed",
"canceled",
}
for _, state := range states {
if s == state {
return State(state)
}
}
errMsg := fmt.Sprintf("State not found: '%s' ", s)
_, file, line, ok := runtime.Caller(1)
if ok {
errMsg += fmt.Sprintf("%s : ln %d", file, line)
}
log.Panic(errMsg)
return State("unknown")
}
type Event struct {
ID uuid.UUID `json:"id"`
ParentID uuid.UUID `json:"parentId"`
Name EventName `json:"name"`
Action Action `json:"action"`
State State `json:"state"`
StateMessage string `json:"stateMessage"`
Payload interface{} `json:"payload"`
PayloadModel string `json:"payloadModel"`
CreatedAt time.Time `json:"createdAt"`
Caller Caller `json:"caller"`
Artifacts []Artifact `json:"artifacts"`
}
type Caller struct {
File string `json:"file"`
LineNumber int `json:"line_number"`
}
type Artifact struct {
Source string `json:"source,omitempty"`
Key string `json:"key"`
Value interface{} `json:"value"`
Secret bool `json:"secret"`
}
func (a *Artifact) String() string {
return a.Value.(string)
}
func (a *Artifact) Int() int {
i, err := strconv.Atoi(a.Value.(string))
if err != nil {
log.Error(err)
}
return i
}
func (a *Artifact) StringMap() map[string]interface{} {
return a.Value.(map[string]interface{})
}
func (a *Artifact) StringSlice() []interface{} {
return a.Value.([]interface{})
}
func CreateEvent(eventName EventName, payload interface{}) Event {
return NewEvent(eventName, GetAction("create"), payload)
}
func UpdateEvent(eventName EventName, payload interface{}) Event {
return NewEvent(eventName, GetAction("update"), payload)
}
func DeleteEvent(eventName EventName, payload interface{}) Event {
return NewEvent(eventName, GetAction("delete"), payload)
}
func NewEvent(eventName EventName, action Action, payload interface{}) Event {
event := Event{
ID: uuid.NewV4(),
Name: eventName,
CreatedAt: time.Now(),
Action: action,
State: State("waiting"),
StateMessage: "Waiting for event to run",
}
event.SetPayload(payload)
return event
}
func (e *Event) CreateEvent(action Action, state State, stateMessage string) Event {
return e.NewEvent(GetAction("create"), state, stateMessage)
}
func (e *Event) UpdateEvent(action Action, state State, stateMessage string) Event {
return e.NewEvent(GetAction("update"), state, stateMessage)
}
func (e *Event) DeleteEvent(action Action, state State, stateMessage string) Event {
return e.NewEvent(GetAction("delete"), state, stateMessage)
}
func (e *Event) StatusEvent(action Action, state State, stateMessage string) Event {
return e.NewEvent(GetAction("status"), state, stateMessage)
}
func (e *Event) NewEvent(action Action, state State, stateMessage string) Event {
event := NewEvent(e.Name, action, e.Payload)
event.ParentID = e.ID
event.State = state
event.Action = action
event.StateMessage = stateMessage
return event
}
func (e *Event) SetPayload(payload interface{}) {
e.Payload = payload
if payload != nil {
e.PayloadModel = reflect.TypeOf(payload).String()
if _, ok := EventRegistry[e.PayloadModel]; ok == false {
_, file, line, ok := runtime.Caller(1)
if ok {
log.Error(fmt.Sprintf("%s : ln %d", file, line))
}
log.Fatal(fmt.Errorf("PayloadModel not found: '%s'. Did you add it to EventRegistry?", e.PayloadModel))
}
} else {
e.PayloadModel = ""
}
}
func (e *Event) Dump() {
event, _ := json.MarshalRole("dummy", e)
log.Debug(string(event))
}
func (e *Event) Event() string {
return fmt.Sprintf("%s:%s", e.Name, e.Action)
}
func (e *Event) Matches(name string) bool {
matched, err := regexp.MatchString(name, e.Event())
if err != nil {
log.ErrorWithFields("Event regex match encountered an error", log.Fields{
"regex": name,
"string": e.Event(),
"error": err,
})
}
if matched {
return true
}
// Not that important because there will be events that will
// fail without being an error condition because there will obviously
// be some events that do not match. Leaving here for future debugging, but disabling for sake of DEBUG channel
// ADB
// log.DebugWithFields("Event regex not matched", log.Fields{
// "regex": name,
// "string": e.Event(),
// })
return false
}
func (e *Event) AddArtifact(key string, value interface{}, secret bool) {
artifact := Artifact{
Key: key,
Value: value,
Secret: secret,
}
exists := false
for i, _artifact := range e.Artifacts {
if strings.ToLower(_artifact.Key) == strings.ToLower(key) {
exists = true
e.Artifacts[i] = artifact
}
}
if !exists {
e.Artifacts = append(e.Artifacts, artifact)
}
}
func (e *Event) GetArtifact(key string) (Artifact, error) {
for _, artifact := range e.Artifacts {
if artifact.Source == "" && strings.ToLower(artifact.Key) == strings.ToLower(key) {
return artifact, nil
}
}
return Artifact{}, errors.New(fmt.Sprintf("Artifact %s not found", key))
}
func (e *Event) GetArtifactFromSource(key string, source string) (Artifact, error) {
for _, artifact := range e.Artifacts {
if strings.ToLower(artifact.Source) == strings.ToLower(source) && strings.ToLower(artifact.Key) == strings.ToLower(key) {
return artifact, nil
}
}
return Artifact{}, errors.New(fmt.Sprintf("Artifact %s not found", key))
}