Skip to content

Commit 92d133a

Browse files
embano1alexellis
authored andcommitted
Added ObjectName to OutboundEvent
In certain cases the ManagedObjectReference alone might not be useful, e.g. when an object is deleted. This commit adds a new field `objectName` to the outbound JSON data which serves as additional metadata for the receiver. Note: ObjectName for certain vSphere objects, e.g. virtual machines, is only unique within a folder (if applicable) as per the description [here](https://docs.vmware.com/en/VMware-vSphere/6.7/com.vmware.vsphere.vm_admin.doc/GUID-76E73C62-A973-4839-BB67-AC1817908E6D.html). This added field should be treated as metadata and not for requests against vCenter (use `ManagedObjectReference` instead). Signed-off-by: Michael Gasch <[email protected]>
1 parent d6be2bd commit 92d133a

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

pkg/events/events.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import (
2222
// OutboundEvent is the JSON object sent to subscribed functions
2323
// If the ManagedObjectReference for an event cannot be retrieved, it will be nil and thus not marshaled into the JSON OutboundEvent
2424
// It's the receivers responsibility to check whether managedObjectReference key is present in the JSON message payload
25+
// ObjectName is the name of the object as it appears in vCenter - uniqueness is only guaranteed at the folder level, if applicable, where this object resides
2526
type OutboundEvent struct {
2627
Topic string `json:"topic,omitempty"`
2728
Category string `json:"category,omitempty"`
2829

2930
UserName string `json:"userName,omitempty"`
3031
CreatedTime time.Time `json:"createdTime,omitempty"`
32+
ObjectName string `json:"objectName,omitempty"`
3133
ManagedObjectReference *vtypes.ManagedObjectReference `json:"managedObjectReference,omitempty"`
3234
}
3335

@@ -103,13 +105,14 @@ func handleEvent(event vtypes.BaseEvent, m *event.Manager) (string, string, erro
103105

104106
// Get the ManagedObjectReference by converting the event into a concrete event
105107
// If we don't find a MoRef in the event, *ref will be nil and not marshaled in the OutboundEvent making it easy for the subscribed function to validate the JSON payload
106-
ref := getMoref(event)
108+
name, ref := getObjectNameAndMoref(event)
107109

108110
message, err := json.Marshal(OutboundEvent{
109111
Topic: topic,
110112
Category: category,
111113
UserName: user,
112114
CreatedTime: createdTime,
115+
ObjectName: name,
113116
ManagedObjectReference: ref,
114117
})
115118
if err != nil {
@@ -123,10 +126,11 @@ func handleEvent(event vtypes.BaseEvent, m *event.Manager) (string, string, erro
123126
// getMoref extracts the ManagedObjectReference, if any, by converting the BaseEvent to a concrete event
124127
// Details on the ManagedObjectReference:
125128
// https://code.vmware.com/docs/7371/vmware-vsphere-web-services-sdk-programming-guide-6-7-update-1#/doc/GUID-C9E81F17-2516-49EE-914F-EE9904258ED3.html
126-
func getMoref(event vtypes.BaseEvent) *vtypes.ManagedObjectReference {
129+
func getObjectNameAndMoref(event vtypes.BaseEvent) (string, *vtypes.ManagedObjectReference) {
127130
// This pointer to the ManagedObjectReference will be used during the BaseEvent switch below
128131
// If we don't find a MoRef in the event, *ref will be nil and not marshaled in the OutboundEvent
129132
var ref *vtypes.ManagedObjectReference
133+
var objName string
130134

131135
// Get the underlying concrete base event, e.g. VmEvent
132136
// vSphere Web Service API Reference 6.7
@@ -135,30 +139,35 @@ func getMoref(event vtypes.BaseEvent) *vtypes.ManagedObjectReference {
135139
// Alerts
136140
case vtypes.BaseAlarmEvent:
137141
e := baseEvent.GetAlarmEvent()
142+
objName = e.Alarm.Name
138143
ref = &e.Alarm.Alarm
139144

140145
// Datastore
141146
case vtypes.BaseDatastoreEvent:
142147
e := baseEvent.GetDatastoreEvent()
148+
objName = e.Datastore.Name
143149
ref = &e.Datastore.Datastore
144150

145151
// Host/ESX
146152
case vtypes.BaseHostEvent:
147153
e := baseEvent.GetHostEvent()
154+
objName = e.Host.Name
148155
ref = &e.Host.Host
149156

150157
// Resource Pool
151158
case vtypes.BaseResourcePoolEvent:
152159
e := baseEvent.GetResourcePoolEvent()
160+
objName = e.ResourcePool.Name
153161
ref = &e.ResourcePool.ResourcePool
154162

155163
// VM
156164
case vtypes.BaseVmEvent:
157165
e := baseEvent.GetVmEvent()
166+
objName = e.Vm.Name
158167
ref = &e.Vm.Vm
159168
}
160169

161-
return ref
170+
return objName, ref
162171
}
163172

164173
// convertToTopic converts an event type to an OpenFaaS subscriber topic, e.g. "VmPoweredOnEvent" to "vm.powered.on"

pkg/events/events_test.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package events
22

3-
import "testing"
4-
import vtypes "github.com/vmware/govmomi/vim25/types"
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
vtypes "github.com/vmware/govmomi/vim25/types"
8+
)
59

610
var (
711
vmEvent = &vtypes.VmEvent{
812
Event: vtypes.Event{
913
Vm: &vtypes.VmEventArgument{
14+
EntityEventArgument: vtypes.EntityEventArgument{
15+
Name: "Windows10-1234",
16+
},
1017
Vm: vtypes.ManagedObjectReference{
1118
Type: "VirtualMachine",
1219
Value: "vm-1234",
@@ -17,6 +24,9 @@ var (
1724

1825
resourcePoolEvent = &vtypes.ResourcePoolEvent{
1926
ResourcePool: vtypes.ResourcePoolEventArgument{
27+
EntityEventArgument: vtypes.EntityEventArgument{
28+
Name: "Management-RP-1234",
29+
},
2030
ResourcePool: vtypes.ManagedObjectReference{
2131
Type: "ResourcePool",
2232
Value: "resgroup-1234",
@@ -29,32 +39,31 @@ var (
2939
}
3040
)
3141

32-
var tests = []struct {
33-
name string
34-
event vtypes.BaseEvent
35-
moref *vtypes.ManagedObjectReference
36-
motype string
37-
movalue string
38-
}{
39-
{"valid VM Event", vmEvent, &vmEvent.Vm.Vm, "VirtualMachine", "vm-1234"},
40-
{"valid ResourcePool Event", resourcePoolEvent, &resourcePoolEvent.ResourcePool.ResourcePool, "ResourcePool", "resgroup-1234"},
41-
// assert that ManagedObjectReference will be nil for events we don't support (yet), so it won't be marshaled in the outbound JSON
42-
{"unsupported Event", unsupportedEvent, nil, "", ""},
43-
}
42+
func TestGetObjectNameAndMoRef(t *testing.T) {
4443

45-
func TestGetMoref(t *testing.T) {
46-
for _, test := range tests {
47-
t.Logf("running test: %q", test.name)
48-
ref := getMoref(test.event)
49-
if ref != test.moref {
50-
t.Errorf("Received incorrect MoRef, got: %v, want: %v", ref, test.moref)
51-
}
44+
var testCases = []struct {
45+
name string
46+
event vtypes.BaseEvent
47+
wantMoref *vtypes.ManagedObjectReference
48+
wantObjName string
49+
}{
50+
{"valid VM Event", vmEvent, &vmEvent.Vm.Vm, "Windows10-1234"},
51+
{"valid ResourcePool Event", resourcePoolEvent, &resourcePoolEvent.ResourcePool.ResourcePool, "Management-RP-1234"},
52+
// assert that ManagedObjectReference and ObjectName will be nil for events we don't support (yet), so it won't be marshaled in the outbound JSON
53+
{"unsupported Event", unsupportedEvent, nil, ""},
54+
}
5255

53-
if ref != nil {
54-
if ref.Value != test.movalue {
55-
t.Errorf("Received incorrect MoRef value, got: %v, want: %v", ref.Value, test.movalue)
56-
}
56+
for _, test := range testCases {
57+
name, ref := getObjectNameAndMoref(test.event)
58+
59+
// test MoRef
60+
if eq := reflect.DeepEqual(test.wantMoref, ref); !eq {
61+
t.Errorf("%s: wanted: %v, got: %v", test.name, test.wantMoref, ref)
5762
}
5863

64+
// test ObjectName
65+
if eq := reflect.DeepEqual(test.wantObjName, name); !eq {
66+
t.Errorf("%s: wanted: %v, got: %v", test.name, test.wantObjName, name)
67+
}
5968
}
6069
}

0 commit comments

Comments
 (0)