forked from micromdm/micromdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acknowledge_event.go
82 lines (73 loc) · 1.91 KB
/
acknowledge_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
package mdm
import (
"time"
"github.com/gogo/protobuf/proto"
"github.com/micromdm/micromdm/mdm/internal/connectproto"
)
type AcknowledgeEvent struct {
ID string
Time time.Time
Response Response
Params map[string]string
Raw []byte
}
type Response struct {
RequestType string `json:"request_type,omitempty" plist:",omitempty"`
UDID string
UserID *string `json:"user_id,omitempty" plist:"UserID,omitempty"`
Status string
CommandUUID string
ErrorChain []ErrorChainItem `json:"error_chain" plist:",omitempty"`
}
type ErrorChainItem struct {
ErrorCode int `json:"error_code,omitempty"`
ErrorDomain string `json:"error_domain,omitempty"`
LocalizedDescription string `json:"localized_description,omitempty"`
USEnglishDescription string `json:"us_english_description,omitempty"`
}
func MarshalAcknowledgeEvent(e *AcknowledgeEvent) ([]byte, error) {
response := &connectproto.Response{
CommandUuid: e.Response.CommandUUID,
Udid: e.Response.UDID,
Status: e.Response.Status,
RequestType: e.Response.RequestType,
}
if e.Response.UserID != nil {
response.UserId = *e.Response.UserID
}
return proto.Marshal(&connectproto.Event{
Id: e.ID,
Time: e.Time.UnixNano(),
Response: response,
Params: e.Params,
Raw: e.Raw,
})
}
func UnmarshalAcknowledgeEvent(data []byte, e *AcknowledgeEvent) error {
var pb connectproto.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.Response == nil {
return nil
}
r := pb.GetResponse()
e.Response = Response{
UDID: r.GetUdid(),
UserID: strPtr(r.GetUserId()),
Status: r.GetStatus(),
RequestType: r.GetRequestType(),
CommandUUID: r.GetCommandUuid(),
}
e.Raw = pb.GetRaw()
e.Params = pb.GetParams()
return nil
}
func strPtr(s string) *string {
if s == "" {
return nil
}
return &s
}