This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevent.go
89 lines (81 loc) · 2.1 KB
/
event.go
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
package command
import (
"time"
"github.com/gogo/protobuf/proto"
"github.com/micromdm/mdm"
uuid "github.com/satori/go.uuid"
"github.com/micromdm/command/internal/commandproto"
)
type Event struct {
ID string
Time time.Time
Payload mdm.Payload
}
// NewEvent returns an Event with a unique ID and the current time.
func NewEvent(cmd mdm.Payload) *Event {
event := Event{
ID: uuid.NewV4().String(),
Time: time.Now().UTC(),
Payload: cmd,
}
return &event
}
// MarshalEvent serializes an event to a protocol buffer wire format.
func MarshalEvent(e *Event) ([]byte, error) {
payload := &commandproto.Payload{
CommandUuid: e.Payload.CommandUUID,
}
if e.Payload.Command != nil {
payload.Command = &commandproto.Command{
RequestType: e.Payload.Command.RequestType,
}
}
switch e.Payload.Command.RequestType {
case "DeviceInformation":
payload.Command.DeviceInformation = &commandproto.DeviceInformation{
Queries: e.Payload.Command.DeviceInformation.Queries,
}
case "InstallProfile":
payload.Command.InstallProfile = &commandproto.InstallProfile{
Payload: e.Payload.Command.InstallProfile.Payload,
}
}
return proto.Marshal(&commandproto.Event{
Id: e.ID,
Time: e.Time.UnixNano(),
Payload: payload,
})
}
// UnmarshalEvent parses a protocol buffer representation of data into
// the Event.
func UnmarshalEvent(data []byte, e *Event) error {
var pb commandproto.Event
if err := proto.Unmarshal(data, &pb); err != nil {
return err
}
e.ID = pb.Id
e.Time = time.Unix(0, pb.Time).UTC()
if pb.Payload == nil {
return nil
}
e.Payload = mdm.Payload{
CommandUUID: pb.Payload.CommandUuid,
}
if pb.Payload.Command == nil {
return nil
}
e.Payload.Command = &mdm.Command{
RequestType: pb.Payload.Command.RequestType,
}
switch pb.Payload.Command.RequestType {
case "DeviceInformation":
e.Payload.Command.DeviceInformation = mdm.DeviceInformation{
Queries: pb.Payload.Command.DeviceInformation.Queries,
}
case "InstallProfile":
e.Payload.Command.InstallProfile = mdm.InstallProfile{
Payload: pb.Payload.Command.InstallProfile.Payload,
}
}
return nil
}