github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/event/event.go (about) 1 // Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package event 6 7 import ( 8 "fmt" 9 "time" 10 11 "github.com/choria-io/go-choria/aagent/model" 12 cloudevents "github.com/cloudevents/sdk-go/v2" 13 14 "github.com/choria-io/go-choria/internal/util" 15 ) 16 17 // New creates a new event 18 func New(name string, mtype string, version string, machine model.Machine) Event { 19 return Event{ 20 Name: name, 21 Protocol: fmt.Sprintf("io.choria.machine.watcher.%s.%s.state", mtype, version), 22 Type: mtype, 23 Identity: machine.Identity(), 24 ID: machine.InstanceID(), 25 Version: machine.Version(), 26 Timestamp: machine.TimeStampSeconds(), 27 Machine: machine.Name(), 28 } 29 } 30 31 type Event struct { 32 Protocol string `json:"protocol"` 33 Identity string `json:"identity"` 34 ID string `json:"id"` 35 Version string `json:"version"` 36 Timestamp int64 `json:"timestamp"` 37 Type string `json:"type"` 38 Machine string `json:"machine"` 39 Name string `json:"name"` 40 } 41 42 // CloudEvent creates a CloudEvent from the state notification 43 func (e *Event) CloudEvent(data any) cloudevents.Event { 44 event := cloudevents.NewEvent("1.0") 45 46 event.SetType(e.Protocol) 47 event.SetSource("io.choria.machine") 48 event.SetSubject(e.Identity) 49 event.SetID(util.UniqueID()) 50 event.SetTime(time.Unix(e.Timestamp, 0)) 51 event.SetData(cloudevents.ApplicationJSON, data) 52 53 return event 54 } 55 56 // WatcherType is the type of watcher the notification is for - exec, file etc 57 func (e *Event) WatcherType() string { 58 return e.Type 59 } 60 61 // SenderID is the identity of the event producer 62 func (e *Event) SenderID() string { 63 return e.Identity 64 } 65 66 // MachineName is the name of the autonomous agent 67 func (e *Event) MachineName() string { 68 return e.Machine 69 }