-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
175 lines (147 loc) · 3.97 KB
/
common.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
package common
import (
"fmt"
"net/url"
"strings"
"github.com/SecurityGeekIO/zscaler-sdk-go/v2/zia"
)
const pageSize = 1000
type IDNameExtensions struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
type IDExtensions struct {
ID int `json:"id,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
type IDName struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type IDCustom struct {
ID int `json:"id,omitempty"`
}
type ZPAAppSegments struct {
// A unique identifier assigned to the Application Segment
ID int `json:"id"`
// The name of the Application Segment
Name string `json:"name,omitempty"`
// Indicates the external ID. Applicable only when this reference is of an external entity.
ExternalID string `json:"externalId"`
}
type UserGroups struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
IdpID int `json:"idp_id,omitempty"`
Comments string `json:"comments,omitempty"`
}
type UserDepartment struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
IdpID int `json:"idp_id,omitempty"`
Comments string `json:"comments,omitempty"`
Deleted bool `json:"deleted,omitempty"`
}
type DeviceGroups struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type Devices struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type IDNameWorkloadGroup struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type DatacenterSearchParameters struct {
RoutableIP bool
WithinCountryOnly bool
IncludePrivateServiceEdge bool
IncludeCurrentVips bool
SourceIp string
Latitude float64
Longitude float64
Subcloud string
}
type SandboxRSS struct {
Risk string `json:"Risk,omitempty"`
Signature string `json:"Signature,omitempty"`
SignatureSources string `json:"SignatureSources,omitempty"`
}
// GetPageSize returns the page size.
func GetPageSize() int {
return pageSize
}
func ReadAllPages[T any](client *zia.Client, endpoint string, list *[]T) error {
if list == nil {
return nil
}
page := 1
if !strings.Contains(endpoint, "?") {
endpoint += "?"
}
for {
pageItems := []T{}
err := client.Read(fmt.Sprintf("%s&pageSize=%d&page=%d", endpoint, pageSize, page), &pageItems)
if err != nil {
return err
}
*list = append(*list, pageItems...)
if len(pageItems) < pageSize {
break
}
page++
}
return nil
}
func ReadPage[T any](client *zia.Client, endpoint string, page int, list *[]T) error {
if list == nil {
return nil
}
// Parse the endpoint into a URL.
u, err := url.Parse(endpoint)
if err != nil {
return fmt.Errorf("could not parse endpoint URL: %w", err)
}
// Get the existing query parameters and add new ones.
q := u.Query()
q.Set("pageSize", fmt.Sprintf("%d", pageSize))
q.Set("page", fmt.Sprintf("%d", page))
// Set the URL's RawQuery to the encoded query parameters.
u.RawQuery = q.Encode()
// Convert the URL back to a string and read the page.
pageItems := []T{}
err = client.Read(u.String(), &pageItems)
if err != nil {
return err
}
*list = pageItems
return nil
}
type (
SortOrder string
SortField string
)
const (
ASCSortOrder SortOrder = "ASC"
DESCSortOrder = "DESC"
IDSortField SortField = "id"
NameSortField = "name"
CreationTimeSortField = "creationTime"
ModifiedTimeSortField = "modifiedTime"
)
func GetSortParams(sortBy SortField, sortOrder SortOrder) string {
params := ""
if sortBy != "" {
params = "sortBy=" + string(sortBy)
}
if sortOrder != "" {
if params != "" {
params += "&"
}
params += "sortOrder=" + string(sortOrder)
}
return params
}