/
devices_api.go
239 lines (195 loc) · 9.87 KB
/
devices_api.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package webexteams
import (
"time"
"github.com/go-resty/resty/v2"
"github.com/google/go-querystring/query"
"github.com/peterhellberg/link"
)
// DevicesService is the service to communicate with the Devices API endpoint
type DevicesService service
// Devices is the List of Devices
type Devices struct {
Items []Device `json:"items,omitempty"`
}
// DeviceCodeRequest is the Create Device Activation Code Request Parameters
type DeviceCodeRequest struct {
PlaceID string `json:"placeId,omitempty"` // The placeId of the place where the device will be activated.
}
// DeviceCode is the code to activate a device in a Place
type DeviceCode struct {
ID string `json:"id,omitempty"`
Code string `json:"code,omitempty"`
PlaceID string `json:"placeId,omitempty"`
Created time.Time `json:"created,omitempty"`
Expires time.Time `json:"expires,omitempty"`
}
// Device is the Device definition
type Device struct {
ID string `json:"id,omitempty"` // A unique identifier for the device.
DisplayName string `json:"displayName,omitempty"` // A friendly name for the device.
PlaceID string `json:"placeId,omitempty"` // The place associated with the device.
WorkspaceId string `json:"workspaceId,omitempty"` // The workspace associated with the device.
PersonId string `json:"personId,omitempty"` // The person associated with the device.
OrgID string `json:"orgId,omitempty"` // The organization associated with the device.
Capabilities []string `json:"capabilities,omitempty"` // The capabilities of the device.
Permissions []string `json:"permissions,omitempty"` // The permissions the user has for this device. For example, xapi means this user is entitled to using the xapi against this device.
ConnectionStatus string `json:"connectionStatus,omitempty"` // The connection status of the device.
Product string `json:"product,omitempty"` // The product name.
Tags []string `json:"tags,omitempty"` // Tags assigned to the device.
IP string `json:"ip,omitempty"` // The current IP address of the device.
ActiveInterface string `json:"activeInterface,omitempty"` // The current network connectivty for the device.
MAC string `json:"mac,omitempty"` // The unique address for the network adapter.
Serial string `json:"serial,omitempty"` // Serial number for the device.
Software string `json:"software,omitempty"` // The operating system name data and version tag.
UpgradeChannel string `json:"upgradeChannel,omitempty"` // The upgrade channel the device is assigned to.
PrimarySIPURL string `json:"primarySipUrl,omitempty"` // The primary SIP address to dial this device.
SIPURLs []string `json:"sipUrls,omitempty"` // All SIP addresses to dial this device.
ErrorCodes []string `json:"errorcodes,omitempty"` // Device Error codes
Created string `json:"created,omitempty"` // The date and time that the device was registered, in ISO8601 format.
}
// AddDevice is used to append a device to a slice of Devices
func (devices *Devices) AddDevice(item Device) []Device {
devices.Items = append(devices.Items, item)
return devices.Items
}
func (s *DevicesService) devicesPagination(linkHeader string, size, max int) *Devices {
items := &Devices{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := s.client.R().
SetResult(&Devices{}).
SetError(&Error{}).
Get(l.URI)
if err != nil {
return nil
}
items = response.Result().(*Devices)
size = size + len(items.Items)
if max < 0 || size < max {
devices := s.devicesPagination(response.Header().Get("Link"), size, max)
for _, device := range devices.Items {
items.AddDevice(device)
}
}
}
}
return items
}
// CreateDeviceActivationCode Create a Device Activation Code
/* Generate an activation code for a device in a specific place by placeId.
Currently, activation codes may only be generated for shared places--personal mode is not supported.
@param placeId (string) The placeId of the place where the device will be activated.
@return DeviceCode
*/
func (s *DevicesService) CreateDeviceActivationCode(deviceCodeRequest *DeviceCodeRequest) (*DeviceCode, *resty.Response, error) {
path := "/devices/activationCode"
response, err := s.client.R().
SetBody(deviceCodeRequest).
SetResult(&DeviceCode{}).
SetError(&Error{}).
Post(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*DeviceCode)
return result, response, err
}
// DeleteDevice Remove a device from the system. Only an admin can remove a device.
/* Remove a device from the system. Only an admin can remove a device.
Specify the device ID in the deviceID parameter in the URI.
@param deviceID A unique identifier for the device.
@return
*/
func (s *DevicesService) DeleteDevice(deviceID string) (*resty.Response, error) {
path := "/devices/" + deviceID
response, err := s.client.R().
SetError(&Error{}).
Delete(path)
if err != nil {
return nil, err
}
return response, err
}
//GetDevice Shows details for a device, by ID.
/* Shows details for a device, by ID. Specify the device ID in the deviceId parameter in the URI.
@param deviceID (string) unique identifier for the device.
@return Device
*/
func (s *DevicesService) GetDevice(deviceID string) (*Device, *resty.Response, error) {
path := "/devices/" + deviceID
response, err := s.client.R().
SetResult(&Device{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Device)
return result, response, err
}
// ListDevicesQueryParams are the query params for the ListDevices API Call
type ListDevicesQueryParams struct {
PersonID string `url:"personId,omitempty"` // List devices by person ID.
PlaceID string `url:"placeId,omitempty"` // List devices by place ID.
OrgID string `url:"orgId,omitempty"` // List devices in this organization. Only admin users of another organization (such as partners) may use this parameter.
DisplayName string `url:"displayName,omitempty"` // List devices with this display name.
Product string `url:"product,omitempty"` // List devices with this product name. Possible values: DX-80, RoomKit, SX-80
Tag string `url:"tag,omitempty"` // List devices which have a tag. Accepts multiple values separated by commas.
ConnectionStatus string `url:"connectionStatus,omitempty"` // List devices with this connection status.
Serial string `url:"serial,omitempty"` // List devices with this serial number.
Software string `url:"software,omitempty"` // List devices with this software version.
UpgradeChannel string `url:"upgradeChannel,omitempty"` // List devices with this upgrade channel.
ErrorCode string `url:"errorcode,omitempty"` // List devices with this error code.
Capability string `url:"capability,omitempty"` // List devices with this capability. Possible values: xapi
Permission string `url:"permission,omitempty"` // List devices with this permission.
Start int `url:"start,omitempty"` // Offset. Default is 0.
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response. Negative value will list all items (use this carefully).
RequestBy int `url:"-"` // Number of items to retrieve by request (Max if let at 0)
}
// ListDevices List devices in your organization.
/* Lists all active Webex devices associated with the authenticated user, such as devices activated in personal mode.
Administrators can list all devices within an organization.
@param personId (string) list devices by person ID.
@param placeId (string ) list devices by place ID.
@param orgId (string) list devices in this organization. Only admin users of another organization (such as partners) may use this parameter.
@param displayName (string) list devices with this display name.
@param product (string) list devices with this product name. Possible values: DX-80, RoomKit, SX-80
@param tag (string) list devices which have a tag. Accepts multiple values separated by commas.
@param connectionStatus (string) list devices with this connection status.
@param serial (string) list devices with this serial number.
@param software (string) list devices with this software version.
@param upgradechannel (string) list devices with this upgrade channel.
@param errorCode (string) list devices with this error code.
@param capability (string) list devices with this capability. Possible values: xapi
@param permission (string) list devices with this permission.
@param start (int) offset. default is 0.
@param max (int) Limit the maximum number of items in the response. Negative value will list all items (use this carefully).
@param "requestBy" (int) Number of items by request
@return Devices
*/
func (s *DevicesService) ListDevices(queryParams *ListDevicesQueryParams) (*Devices, *resty.Response, error) {
path := "/devices/"
max := queryParams.Max
if queryParams.RequestBy > 0 {
queryParams.Max = queryParams.RequestBy
} else if queryParams.Max < 0 {
queryParams.Max = 0
}
queryParamsString, _ := query.Values(queryParams)
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Devices{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Devices)
if max < 0 || len(result.Items) < max {
items := s.devicesPagination(response.Header().Get("Link"), len(result.Items), max)
for _, device := range items.Items {
result.AddDevice(device)
}
}
return result, response, err
}