Files
paralus/pkg/event/event.go
nirav-rafay c66bdc25cd restructure rcloud-base as a single base controller (#37)
* restructure rcloud-base as a single base controller
* updated master.rest
* moved sentry from internal to pkg as it is used by relay
* removing unused rpc and it's dependencies
* Fix usermgmt tests
* Don't redefine variables in rest file
Co-authored-by: Abin Simon <abin.simon@rafay.co>
2022-03-03 17:59:06 +05:30

55 lines
1.4 KiB
Go

package event
import (
"github.com/segmentio/encoding/json"
)
// ResourceEventType is type of resource event
type ResourceEventType int
// Resource event types
const (
EventTypeNotSet ResourceEventType = iota
ResourceCreate
ResourceUpdate
ResourceDelete
ResourceUpdateStatus
)
// Resource represents an event generated when resource changes
// it should be looked as an parameter to the event handler callback
type Resource struct {
PartnerID string `json:"pa,omitempty"`
OrganizationID string `json:"or,omitempty"`
ProjectID string `json:"pr,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"n,omitempty"`
EventType ResourceEventType `json:"t,omitempty"`
}
// Key is the key for this event which can be used as a cache key etc
func (sr Resource) Key() string {
b, _ := json.Marshal(&sr)
return string(b)
}
// Handler is the interface for notifying resource changes
type Handler interface {
OnChange(r Resource)
}
// HandlerFuncs is a utility for creating event handler with functions
type HandlerFuncs struct {
OnChangeFunc func(r Resource)
NameFunc func() string
}
// OnChange is the callback for notifying when resource is changed
func (f HandlerFuncs) OnChange(r Resource) {
if f.OnChangeFunc != nil {
f.OnChangeFunc(r)
}
}
var _ Handler = (*HandlerFuncs)(nil)