-
Notifications
You must be signed in to change notification settings - Fork 1
/
devicegroups.go
184 lines (153 loc) · 5.28 KB
/
devicegroups.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
package devicegroups
import (
"fmt"
"net/url"
"strings"
"github.com/zscaler/zscaler-sdk-go/zia/services/common"
)
const (
deviceGroupEndpoint = "/deviceGroups"
devicesEndpoint = "/deviceGroups/devices"
)
type DeviceGroups struct {
// The unique identifer for the device group
ID int `json:"id"`
// The device group name
Name string `json:"name,omitempty"`
// The device group type
GroupType string `json:"groupType,omitempty"`
// The device group's description
Description string `json:"description,omitempty"`
// The operating system (OS)
OSType string `json:"osType,omitempty"`
// Indicates whether this is a predefined device group. If this value is set to true, the group is predefined
Predefined bool `json:"predefined"`
DeviceNames string `json:"deviceNames,omitempty"`
// The number of devices within the group
DeviceCount int `json:"deviceCount,omitempty"`
}
type Devices struct {
// The unique identifier for the device
ID int `json:"id"`
// The device name
Name string `json:"name,omitempty"`
// The device group type
DeviceGroupType string `json:"deviceGroupType,omitempty"`
// The device model
DeviceModel string `json:"deviceModel,omitempty"`
// The operating system (OS)
OSType string `json:"osType,omitempty"`
// The operating system version
OSVersion string `json:"osVersion,omitempty"`
// The device's description
Description string `json:"description,omitempty"`
// The unique identifier of the device owner (i.e., user)
OwnerUserId int `json:"ownerUserId,omitempty"`
// The device owner's user name
OwnerName string `json:"ownerName,omitempty"`
// The hostname of the device
HostName string `json:"hostName,omitempty"`
}
func (service *Service) GetDeviceGroups(deviceGroupId int) (*DeviceGroups, error) {
var group DeviceGroups
err := service.Client.Read(fmt.Sprintf("%s/%d", deviceGroupEndpoint, deviceGroupId), &group)
if err != nil {
return nil, err
}
service.Client.Logger.Printf("[DEBUG]Returning firewall rule from Get: %d", group.ID)
return &group, nil
}
func (service *Service) GetDeviceGroupByName(deviceGroupName string) (*DeviceGroups, error) {
var deviceGroups []DeviceGroups
err := service.Client.Read(deviceGroupEndpoint, &deviceGroups)
if err != nil {
return nil, err
}
for _, deviceGroup := range deviceGroups {
if strings.EqualFold(deviceGroup.Name, deviceGroupName) {
return &deviceGroup, nil
}
}
return nil, fmt.Errorf("no device group found with name: %s", deviceGroupName)
}
func (service *Service) GetDevicesByID(deviceId int) (*Devices, error) {
var device Devices
err := service.Client.Read(fmt.Sprintf("%s/%d", devicesEndpoint, deviceId), &device)
if err != nil {
return nil, err
}
service.Client.Logger.Printf("[DEBUG]Returning device from Get: %d", device.ID)
return &device, nil
}
// Get Devices by Name.
func (service *Service) GetDevicesByName(deviceName string) (*Devices, error) {
var devices []Devices
// We are assuming this device name will be in the firsy 1000 obejcts
err := common.ReadAllPages(service.Client, fmt.Sprintf("%s?page=1&pageSize=1000", devicesEndpoint), &devices)
if err != nil {
return nil, err
}
for _, device := range devices {
if strings.EqualFold(device.Name, deviceName) {
return &device, nil
}
}
return nil, fmt.Errorf("no device found with name: %s", deviceName)
}
func (service *Service) GetDevicesByModel(deviceModel string) (*Devices, error) {
var models []Devices
err := common.ReadAllPages(service.Client, fmt.Sprintf("%s?model=%s", devicesEndpoint, url.QueryEscape(deviceModel)), &models)
if err != nil {
return nil, err
}
for _, model := range models {
if strings.EqualFold(model.DeviceModel, deviceModel) {
return &model, nil
}
}
return nil, fmt.Errorf("no device found with model: %s", deviceModel)
}
func (service *Service) GetDevicesByOwner(ownerName string) (*Devices, error) {
var owners []Devices
err := common.ReadAllPages(service.Client, fmt.Sprintf("%s?owner=%s", devicesEndpoint, url.QueryEscape(ownerName)), &owners)
if err != nil {
return nil, err
}
for _, owner := range owners {
if strings.EqualFold(owner.OwnerName, ownerName) {
return &owner, nil
}
}
return nil, fmt.Errorf("no device found for owner: %s", ownerName)
}
func (service *Service) GetDevicesByOSType(osTypeName string) (*Devices, error) {
var osTypes []Devices
err := common.ReadAllPages(service.Client, fmt.Sprintf("%s?osType=%s", devicesEndpoint, url.QueryEscape(osTypeName)), &osTypes)
if err != nil {
return nil, err
}
for _, osType := range osTypes {
if strings.EqualFold(osType.OSType, osTypeName) {
return &osType, nil
}
}
return nil, fmt.Errorf("no device found for type: %s", osTypeName)
}
func (service *Service) GetDevicesByOSVersion(osVersionName string) (*Devices, error) {
var osVersions []Devices
err := common.ReadAllPages(service.Client, fmt.Sprintf("%s?osVersion=%s", devicesEndpoint, url.QueryEscape(osVersionName)), &osVersions)
if err != nil {
return nil, err
}
for _, osVersion := range osVersions {
if strings.EqualFold(osVersion.OSVersion, osVersionName) {
return &osVersion, nil
}
}
return nil, fmt.Errorf("no device found for version: %s", osVersionName)
}
func (service *Service) GetAll() ([]Devices, error) {
var owners []Devices
err := common.ReadAllPages(service.Client, devicesEndpoint, &owners)
return owners, err
}