...
1 package model
2
3 import (
4 "encoding/json"
5
6 "cloud.google.com/go/pubsub"
7 )
8
9 const (
10 AttrActor = "actor"
11 AttrClusterEdgeID = "cluster_edge_id"
12 EventTypeReconcile = "reconcile"
13 EventTypeStart = "start"
14 EventTypeStop = "stop"
15 EventTypeRestart = "restart"
16 EventTypeLiveMigration = "liveMigration"
17 EdgeAgentTopicAndOwner = "edge-agent"
18 ChariotSync = "chariot-sync"
19 ErrValidation = "malformed-request"
20 )
21
22
23
24 type EventNotificationJSON struct {
25 ClusterProviders []string `json:"cluster_providers,omitempty"`
26 FleetType string `json:"fleet_type,omitempty"`
27 Labels map[string]string `json:"labels,omitempty"`
28 Events []Event `json:"events"`
29 }
30
31 type Event struct {
32 Name string `json:"name"`
33 Kind string `json:"kind"`
34 Namespace string `json:"namespace"`
35 Type string `json:"type"`
36 }
37
38 type NotificationMessage struct {
39
40 Actor string
41 ClusterEdgeID string
42 ClusterProviders []string
43 FleetType string
44 Labels map[string]string
45 Events []Event
46 }
47
48 func NewNotificationMessage() *NotificationMessage {
49 return &NotificationMessage{}
50 }
51
52
53 func (nm *NotificationMessage) SetActor(actor string) *NotificationMessage {
54 nm.Actor = actor
55 return nm
56 }
57
58
59 func (nm *NotificationMessage) SetClusterEdgeID(clusterEdgeID string) *NotificationMessage {
60 nm.ClusterEdgeID = clusterEdgeID
61 return nm
62 }
63
64
65 func (nm *NotificationMessage) SetClusterProviders(clusterProviders []string) *NotificationMessage {
66 nm.ClusterProviders = clusterProviders
67 return nm
68 }
69
70
71 func (nm *NotificationMessage) SetFleetType(fleetType string) *NotificationMessage {
72 nm.FleetType = fleetType
73 return nm
74 }
75
76
77 func (nm *NotificationMessage) SetEvents(events []Event) *NotificationMessage {
78 nm.Events = events
79 return nm
80 }
81
82
83 func (nm *NotificationMessage) AddEvent(events ...Event) *NotificationMessage {
84 nm.Events = append(nm.Events, events...)
85 return nm
86 }
87
88
89 func (nm *NotificationMessage) SetLabels(labels map[string]string) *NotificationMessage {
90 nm.Labels = labels
91 return nm
92 }
93
94 func (nm *NotificationMessage) Data() []byte {
95 var ed = EventNotificationJSON{
96 ClusterProviders: nm.ClusterProviders,
97 FleetType: nm.FleetType,
98 Labels: nm.Labels,
99 Events: nm.Events,
100 }
101 var eventData, err = json.Marshal(ed)
102 if err != nil {
103 return nil
104 }
105 return eventData
106 }
107
108 func (nm *NotificationMessage) Attributes() map[string]string {
109 attributes := map[string]string{
110 AttrActor: nm.Actor,
111 AttrClusterEdgeID: nm.ClusterEdgeID,
112 }
113 return attributes
114 }
115
116 func FromPubSubMessage(msg *pubsub.Message) (*NotificationMessage, error) {
117 var ed = &EventNotificationJSON{}
118 err := json.Unmarshal(msg.Data, ed)
119 if err != nil {
120 return nil, err
121 }
122 var notificationMessage = &NotificationMessage{
123 Actor: msg.Attributes[AttrActor],
124 ClusterEdgeID: msg.Attributes[AttrClusterEdgeID],
125 ClusterProviders: ed.ClusterProviders,
126 FleetType: ed.FleetType,
127 Labels: ed.Labels,
128 Events: ed.Events,
129 }
130 return notificationMessage, err
131 }
132
133 func BuildEdgeAgentEvent(name, kind, namespace, eventType string) *Event {
134 return &Event{
135 Name: name,
136 Kind: kind,
137 Namespace: namespace,
138 Type: eventType,
139 }
140 }
141
View as plain text