This repository has been archived by the owner on Sep 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
devices.go
151 lines (137 loc) · 4.36 KB
/
devices.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package dep
import (
"fmt"
"time"
)
const (
fetchDevicesPath = "server/devices"
syncDevicesPath = "devices/sync"
deviceDetailsPath = "devices"
)
// DeviceService allows fetching and syncing devices, as well as requesting device details
/*
Use (Cursor() and Limit() as optional arguments for Fetch Devices, example:
fetchResponse, err := client.FetchDevices(dep.Limit(100))
if err != nil {
// handle err
}
fmt.Println(fetchResponse.Devices)
*/
type DeviceService interface {
FetchDevices(opts ...DeviceRequestOption) (*DeviceResponse, error)
SyncDevices(cursor string, opts ...DeviceRequestOption) (*DeviceResponse, error)
DeviceDetails(devices []string) (*DeviceDetailsResponse, error)
}
type deviceService struct {
client *depClient
}
// Device is a DEP device
type Device struct {
SerialNumber string `json:"serial_number"`
Model string `json:"model"`
Description string `json:"description"`
Color string `json:"color"`
AssetTag string `json:"asset_tag"`
ProfileStatus string `json:"profile_status"`
ProfileUUID string `json:"profile_uuid,omitempty"`
ProfileAssignTime time.Time `json:"profile_assign_time,omitempty"`
ProfilePushTime time.Time `json:"profile_push_time,omitempty"`
DeviceAssignedDate time.Time `json:"device_assigned_date,omitempty"`
DeviceAssignedBy string `json:"device_assigned_by,omitempty"`
OS string `json:"os,omitempty"`
DeviceFamily string `json:"device_family,omitempty"`
// sync fields
OpType string `json:"op_type,omitempty"`
OpDate time.Time `json:"op_date,omitempty"`
}
// DeviceRequestOption is an optional parameter for the DeviceService API.
// The option can be used to set Cursor or Limit options for the request.
type DeviceRequestOption func(*deviceRequestOpts) error
type deviceRequestOpts struct {
Cursor string `json:"cursor,omitempty"`
Limit int `json:"limit,omitempty"`
}
// Cursor is an optional argument that can be added to FetchDevices
func Cursor(cursor string) DeviceRequestOption {
return func(opts *deviceRequestOpts) error {
opts.Cursor = cursor
return nil
}
}
// Limit is an optional argument that can be passed to FetchDevices and SyncDevices
func Limit(limit int) DeviceRequestOption {
return func(opts *deviceRequestOpts) error {
if limit > 1000 {
return fmt.Errorf("Limit must not be higher than 1000")
}
opts.Limit = limit
return nil
}
}
// DeviceResponse is a DEP FetchDevices response
type DeviceResponse struct {
Devices []Device `json:"devices"`
Cursor string `json:"cursor"`
FetchedUntil time.Time `json:"fetched_until"`
MoreToFollow bool `json:"more_to_follow"`
}
// FetchDevices returns the result of a Fetch Devices request from DEP
func (s deviceService) FetchDevices(opts ...DeviceRequestOption) (*DeviceResponse, error) {
request := &deviceRequestOpts{}
for _, option := range opts {
if err := option(request); err != nil {
return nil, err
}
}
var response DeviceResponse
req, err := s.client.NewRequest("POST", fetchDevicesPath, request)
if err != nil {
return nil, err
}
err = s.client.Do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// SyncDevices returns the result of a Sync Devices request from DEP
func (s deviceService) SyncDevices(cursor string, opts ...DeviceRequestOption) (*DeviceResponse, error) {
request := &deviceRequestOpts{Cursor: cursor}
for _, option := range opts {
if err := option(request); err != nil {
return nil, err
}
}
var response DeviceResponse
req, err := s.client.NewRequest("POST", syncDevicesPath, request)
if err != nil {
return nil, err
}
err = s.client.Do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// DeviceDetailsResponse is a response for a DeviceDetails request
type DeviceDetailsResponse struct {
Devices map[string]Device `json:"devices"`
}
// DeviceDetails returns the result of a Sync Devices request from DEP
func (s deviceService) DeviceDetails(devices []string) (*DeviceDetailsResponse, error) {
request := struct {
Devices []string `json:"devices"`
}{
Devices: devices,
}
var response DeviceDetailsResponse
req, err := s.client.NewRequest("POST", deviceDetailsPath, request)
if err != nil {
return nil, err
}
err = s.client.Do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}