/
device.go
277 lines (247 loc) · 8.52 KB
/
device.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package zero_trust
import (
"context"
"fmt"
"net/http"
"reflect"
"time"
"github.com/cloudflare/cloudflare-go/v2/internal/apijson"
"github.com/cloudflare/cloudflare-go/v2/internal/pagination"
"github.com/cloudflare/cloudflare-go/v2/internal/param"
"github.com/cloudflare/cloudflare-go/v2/internal/requestconfig"
"github.com/cloudflare/cloudflare-go/v2/internal/shared"
"github.com/cloudflare/cloudflare-go/v2/option"
"github.com/tidwall/gjson"
)
// DeviceService contains methods and other services that help with interacting
// with the cloudflare API. Note, unlike clients, this service does not read
// variables from the environment automatically. You should not instantiate this
// service directly, and instead use the [NewDeviceService] method instead.
type DeviceService struct {
Options []option.RequestOption
DEXTests *DeviceDEXTestService
Networks *DeviceNetworkService
Policies *DevicePolicyService
Posture *DevicePostureService
Revoke *DeviceRevokeService
Settings *DeviceSettingService
Unrevoke *DeviceUnrevokeService
OverrideCodes *DeviceOverrideCodeService
}
// NewDeviceService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewDeviceService(opts ...option.RequestOption) (r *DeviceService) {
r = &DeviceService{}
r.Options = opts
r.DEXTests = NewDeviceDEXTestService(opts...)
r.Networks = NewDeviceNetworkService(opts...)
r.Policies = NewDevicePolicyService(opts...)
r.Posture = NewDevicePostureService(opts...)
r.Revoke = NewDeviceRevokeService(opts...)
r.Settings = NewDeviceSettingService(opts...)
r.Unrevoke = NewDeviceUnrevokeService(opts...)
r.OverrideCodes = NewDeviceOverrideCodeService(opts...)
return
}
// Fetches a list of enrolled devices.
func (r *DeviceService) List(ctx context.Context, query DeviceListParams, opts ...option.RequestOption) (res *pagination.SinglePage[Device], err error) {
var raw *http.Response
opts = append(r.Options, opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := fmt.Sprintf("accounts/%s/devices", query.AccountID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// Fetches a list of enrolled devices.
func (r *DeviceService) ListAutoPaging(ctx context.Context, query DeviceListParams, opts ...option.RequestOption) *pagination.SinglePageAutoPager[Device] {
return pagination.NewSinglePageAutoPager(r.List(ctx, query, opts...))
}
// Fetches details for a single device.
func (r *DeviceService) Get(ctx context.Context, deviceID string, query DeviceGetParams, opts ...option.RequestOption) (res *DeviceGetResponseUnion, err error) {
opts = append(r.Options[:], opts...)
var env DeviceGetResponseEnvelope
path := fmt.Sprintf("accounts/%s/devices/%s", query.AccountID, deviceID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}
type Device struct {
// Device ID.
ID string `json:"id"`
// When the device was created.
Created time.Time `json:"created" format:"date-time"`
// True if the device was deleted.
Deleted bool `json:"deleted"`
DeviceType DeviceDeviceType `json:"device_type"`
// IPv4 or IPv6 address.
IP string `json:"ip"`
// The device's public key.
Key string `json:"key"`
// When the device last connected to Cloudflare services.
LastSeen time.Time `json:"last_seen" format:"date-time"`
// The device mac address.
MacAddress string `json:"mac_address"`
// The device manufacturer name.
Manufacturer string `json:"manufacturer"`
// The device model name.
Model string `json:"model"`
// The device name.
Name string `json:"name"`
// The Linux distro name.
OSDistroName string `json:"os_distro_name"`
// The Linux distro revision.
OSDistroRevision string `json:"os_distro_revision"`
// The operating system version.
OSVersion string `json:"os_version"`
// The operating system version extra parameter.
OSVersionExtra string `json:"os_version_extra"`
// When the device was revoked.
RevokedAt time.Time `json:"revoked_at" format:"date-time"`
// The device serial number.
SerialNumber string `json:"serial_number"`
// When the device was updated.
Updated time.Time `json:"updated" format:"date-time"`
User DeviceUser `json:"user"`
// The WARP client version.
Version string `json:"version"`
JSON deviceJSON `json:"-"`
}
// deviceJSON contains the JSON metadata for the struct [Device]
type deviceJSON struct {
ID apijson.Field
Created apijson.Field
Deleted apijson.Field
DeviceType apijson.Field
IP apijson.Field
Key apijson.Field
LastSeen apijson.Field
MacAddress apijson.Field
Manufacturer apijson.Field
Model apijson.Field
Name apijson.Field
OSDistroName apijson.Field
OSDistroRevision apijson.Field
OSVersion apijson.Field
OSVersionExtra apijson.Field
RevokedAt apijson.Field
SerialNumber apijson.Field
Updated apijson.Field
User apijson.Field
Version apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Device) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r deviceJSON) RawJSON() string {
return r.raw
}
type DeviceDeviceType string
const (
DeviceDeviceTypeWindows DeviceDeviceType = "windows"
DeviceDeviceTypeMac DeviceDeviceType = "mac"
DeviceDeviceTypeLinux DeviceDeviceType = "linux"
DeviceDeviceTypeAndroid DeviceDeviceType = "android"
DeviceDeviceTypeIos DeviceDeviceType = "ios"
)
func (r DeviceDeviceType) IsKnown() bool {
switch r {
case DeviceDeviceTypeWindows, DeviceDeviceTypeMac, DeviceDeviceTypeLinux, DeviceDeviceTypeAndroid, DeviceDeviceTypeIos:
return true
}
return false
}
type DeviceUser struct {
// UUID
ID string `json:"id"`
// The contact email address of the user.
Email string `json:"email"`
// The enrolled device user's name.
Name string `json:"name"`
JSON deviceUserJSON `json:"-"`
}
// deviceUserJSON contains the JSON metadata for the struct [DeviceUser]
type deviceUserJSON struct {
ID apijson.Field
Email apijson.Field
Name apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *DeviceUser) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r deviceUserJSON) RawJSON() string {
return r.raw
}
// Union satisfied by [zero_trust.DeviceGetResponseUnknown] or
// [shared.UnionString].
type DeviceGetResponseUnion interface {
ImplementsZeroTrustDeviceGetResponseUnion()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*DeviceGetResponseUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
)
}
type DeviceListParams struct {
AccountID param.Field[string] `path:"account_id,required"`
}
type DeviceGetParams struct {
AccountID param.Field[string] `path:"account_id,required"`
}
type DeviceGetResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Result DeviceGetResponseUnion `json:"result,required"`
// Whether the API call was successful.
Success DeviceGetResponseEnvelopeSuccess `json:"success,required"`
JSON deviceGetResponseEnvelopeJSON `json:"-"`
}
// deviceGetResponseEnvelopeJSON contains the JSON metadata for the struct
// [DeviceGetResponseEnvelope]
type deviceGetResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Result apijson.Field
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *DeviceGetResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r deviceGetResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful.
type DeviceGetResponseEnvelopeSuccess bool
const (
DeviceGetResponseEnvelopeSuccessTrue DeviceGetResponseEnvelopeSuccess = true
)
func (r DeviceGetResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case DeviceGetResponseEnvelopeSuccessTrue:
return true
}
return false
}