-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_network.go
More file actions
419 lines (396 loc) · 16 KB
/
Copy pathdevice_network.go
File metadata and controls
419 lines (396 loc) · 16 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package fortimgr
import (
"context"
"encoding/json"
"fmt"
)
type apiDeviceDNS struct {
OID int `json:"oid"`
Primary string `json:"primary"`
Secondary string `json:"secondary"`
AltPrimary string `json:"alt-primary"`
AltSecondary string `json:"alt-secondary"`
IPv6Primary string `json:"ip6-primary"`
IPv6Secondary string `json:"ip6-secondary"`
Protocol any `json:"protocol"`
ServerSelectMethod any `json:"server-select-method"`
ServerHostname []string `json:"server-hostname"`
Domain []string `json:"domain"`
InterfaceSelectMethod any `json:"interface-select-method"`
Interfaces []string `json:"interface"`
SourceIP string `json:"source-ip"`
Retry int `json:"retry"`
Timeout int `json:"timeout"`
DNSCacheLimit int `json:"dns-cache-limit"`
DNSCacheTTL int `json:"dns-cache-ttl"`
FQDNCacheTTL int `json:"fqdn-cache-ttl"`
FQDNMinRefresh int `json:"fqdn-min-refresh"`
FQDNMaxRefresh int `json:"fqdn-max-refresh"`
CacheNotFoundResponses any `json:"cache-notfound-responses"`
Log any `json:"log"`
SSLCertificate []string `json:"ssl-certificate"`
}
type apiDeviceDDNS struct {
Name string `json:"name"`
DDNSDomain string `json:"ddns-domain"`
MonitorInterface any `json:"monitor-interface"`
Server any `json:"server"`
Status any `json:"status"`
Username string `json:"username"`
}
type apiDeviceIPAM struct {
OID int `json:"oid"`
Status any `json:"status"`
ServerType any `json:"server-type"`
AutomaticConflictResolution any `json:"automatic-conflict-resolution"`
ManageLANAddresses any `json:"manage-lan-addresses"`
ManageLANExtensionAddresses any `json:"manage-lan-extension-addresses"`
ManageSSIDAddresses any `json:"manage-ssid-addresses"`
RequireSubnetSizeMatch any `json:"require-subnet-size-match"`
}
type apiSDWANSettings struct {
OID int `json:"oid"`
Status any `json:"status"`
LoadBalanceMode any `json:"load-balance-mode"`
FailDetect any `json:"fail-detect"`
AppPerfLogPeriod int `json:"app-perf-log-period"`
DuplicationMaxNum int `json:"duplication-max-num"`
NeighborHoldBootTime int `json:"neighbor-hold-boot-time"`
NeighborHoldDown any `json:"neighbor-hold-down"`
NeighborHoldDownTime int `json:"neighbor-hold-down-time"`
SpeedtestBypassRouting any `json:"speedtest-bypass-routing"`
FailAlertInterfaces []string `json:"fail-alert-interfaces"`
HealthChecks []apiSDWANHealthCheck `json:"health-check"`
Zones []apiSDWANZone `json:"zone"`
}
type apiSDWANHealthCheck struct {
Name string `json:"name"`
OID int `json:"oid"`
Protocol any `json:"protocol"`
Server []string `json:"server"`
Members []int `json:"members"`
Interval int `json:"interval"`
Failtime int `json:"failtime"`
Recoverytime int `json:"recoverytime"`
Source string `json:"source"`
Source6 string `json:"source6"`
SystemDNS any `json:"system-dns"`
UpdateStaticRoute any `json:"update-static-route"`
UpdateCascadeInterface any `json:"update-cascade-interface"`
SLA []apiSDWANSLA `json:"sla"`
}
type apiSDWANSLA struct {
ID int `json:"id"`
OID int `json:"oid"`
LatencyThreshold int `json:"latency-threshold"`
JitterThreshold int `json:"jitter-threshold"`
PacketLossThreshold int `json:"packetloss-threshold"`
LinkCostFactor any `json:"link-cost-factor"`
}
type apiSDWANZone struct {
Name string `json:"name"`
OID int `json:"oid"`
MinimumSLAMeetMembers int `json:"minimum-sla-meet-members"`
ServiceSLATieBreak any `json:"service-sla-tie-break"`
ADVPNSelect any `json:"advpn-select"`
}
type apiSDWANMember struct {
SeqNum int `json:"seq-num"`
Interface string `json:"interface"`
Gateway string `json:"gateway"`
Gateway6 string `json:"gateway6"`
Zone string `json:"zone"`
Status any `json:"status"`
Cost int `json:"cost"`
Priority int `json:"priority"`
Comment string `json:"comment"`
}
type apiSDWANService struct {
ID int `json:"id"`
Name string `json:"name"`
Mode any `json:"mode"`
Status any `json:"status"`
AddressMode any `json:"addr-mode"`
InputDevice any `json:"input-device"`
Src any `json:"src"`
Dst any `json:"dst"`
InternetService any `json:"internet-service"`
PriorityMembers any `json:"priority-members"`
}
type apiSDWANDuplication struct {
ID int `json:"id"`
Name string `json:"name"`
ServiceID int `json:"service-id"`
PacketDuplication any `json:"packet-duplication"`
Fields any `json:"fields"`
}
var sdwanSettingsFields = []string{
"oid", "status", "load-balance-mode", "fail-detect",
"app-perf-log-period", "duplication-max-num",
"neighbor-hold-boot-time", "neighbor-hold-down", "neighbor-hold-down-time",
"speedtest-bypass-routing", "fail-alert-interfaces", "health-check", "zone",
}
// DeviceDNS retrieves global DNS settings from a managed device.
func (c *Client) DeviceDNS(ctx context.Context, device string) (*DeviceDNS, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) {
return nil, fmt.Errorf("%w: device=%q", ErrInvalidName, device)
}
var raw apiDeviceDNS
if err := c.forwardObject(ctx, fmt.Sprintf("/pm/config/device/%s/global/system/dns", device), nil, &raw); err != nil {
return nil, err
}
out := DeviceDNS{
OID: raw.OID,
Primary: raw.Primary,
Secondary: raw.Secondary,
AltPrimary: raw.AltPrimary,
AltSecondary: raw.AltSecondary,
IPv6Primary: raw.IPv6Primary,
IPv6Secondary: raw.IPv6Secondary,
Protocol: toString(raw.Protocol),
ServerSelectMethod: toString(raw.ServerSelectMethod),
ServerHostname: raw.ServerHostname,
Domain: raw.Domain,
InterfaceSelectMethod: toString(raw.InterfaceSelectMethod),
Interfaces: raw.Interfaces,
SourceIP: raw.SourceIP,
Retry: raw.Retry,
Timeout: raw.Timeout,
DNSCacheLimit: raw.DNSCacheLimit,
DNSCacheTTL: raw.DNSCacheTTL,
FQDNCacheTTL: raw.FQDNCacheTTL,
FQDNMinRefresh: raw.FQDNMinRefresh,
FQDNMaxRefresh: raw.FQDNMaxRefresh,
CacheNotFoundResponses: mapEnum(toString(raw.CacheNotFoundResponses), enableDisable),
Log: mapEnum(toString(raw.Log), enableDisable),
SSLCertificate: raw.SSLCertificate,
}
return &out, nil
}
// ListDeviceDDNS retrieves global DDNS settings from a managed device.
func (c *Client) ListDeviceDDNS(ctx context.Context, device string, opts ...ListOption) ([]DeviceDDNS, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) {
return nil, fmt.Errorf("%w: device=%q", ErrInvalidName, device)
}
items, err := getPaged[apiDeviceDDNS](ctx, c, fmt.Sprintf("/pm/config/device/%s/global/system/ddns", device), nil, buildListConfig(opts))
if err != nil {
return nil, err
}
out := make([]DeviceDDNS, len(items))
for i, item := range items {
out[i] = DeviceDDNS{
Name: item.Name,
DDNSDomain: item.DDNSDomain,
MonitorInterface: toString(item.MonitorInterface),
Server: toString(item.Server),
Status: mapEnum(toString(item.Status), enableDisable),
Username: item.Username,
}
}
return out, nil
}
// DeviceIPAM retrieves IPAM settings from a device VDOM.
func (c *Client) DeviceIPAM(ctx context.Context, device, vdom string) (*DeviceIPAM, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) || !validName(vdom) {
return nil, fmt.Errorf("%w: device=%q vdom=%q", ErrInvalidName, device, vdom)
}
var raw apiDeviceIPAM
if err := c.forwardObject(ctx, fmt.Sprintf("/pm/config/device/%s/vdom/%s/system/ipam", device, vdom), nil, &raw); err != nil {
return nil, err
}
out := DeviceIPAM{
OID: raw.OID,
Status: mapEnum(toString(raw.Status), enableDisable),
ServerType: toString(raw.ServerType),
AutomaticConflictResolution: mapEnum(toString(raw.AutomaticConflictResolution), enableDisable),
ManageLANAddresses: mapEnum(toString(raw.ManageLANAddresses), enableDisable),
ManageLANExtensionAddresses: mapEnum(toString(raw.ManageLANExtensionAddresses), enableDisable),
ManageSSIDAddresses: mapEnum(toString(raw.ManageSSIDAddresses), enableDisable),
RequireSubnetSizeMatch: mapEnum(toString(raw.RequireSubnetSizeMatch), enableDisable),
}
return &out, nil
}
// SDWANSettings retrieves SD-WAN settings from a device VDOM. Sensitive fields
// such as health-check passwords are deliberately omitted from the request
// allowlist and public model.
func (c *Client) SDWANSettings(ctx context.Context, device, vdom string) (*SDWANSettings, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) || !validName(vdom) {
return nil, fmt.Errorf("%w: device=%q vdom=%q", ErrInvalidName, device, vdom)
}
var raw apiSDWANSettings
if err := c.forwardObject(ctx, fmt.Sprintf("/pm/config/device/%s/vdom/%s/system/sdwan", device, vdom), map[string]any{"fields": sdwanSettingsFields}, &raw); err != nil {
return nil, err
}
out := convertSDWANSettings(raw)
return &out, nil
}
// ListSDWANMembers retrieves SD-WAN members from a device VDOM.
func (c *Client) ListSDWANMembers(ctx context.Context, device, vdom string, opts ...ListOption) ([]SDWANMember, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) || !validName(vdom) {
return nil, fmt.Errorf("%w: device=%q vdom=%q", ErrInvalidName, device, vdom)
}
items, err := getPaged[apiSDWANMember](ctx, c, fmt.Sprintf("/pm/config/device/%s/vdom/%s/system/sdwan/members", device, vdom), nil, buildListConfig(opts))
if err != nil {
return nil, err
}
out := make([]SDWANMember, len(items))
for i, item := range items {
out[i] = SDWANMember{
SeqNum: item.SeqNum,
Interface: item.Interface,
Gateway: item.Gateway,
Gateway6: item.Gateway6,
Zone: item.Zone,
Status: mapEnum(toString(item.Status), enableDisable),
Cost: item.Cost,
Priority: item.Priority,
Comment: item.Comment,
}
}
return out, nil
}
// ListSDWANServices retrieves SD-WAN service rules from a device VDOM.
func (c *Client) ListSDWANServices(ctx context.Context, device, vdom string, opts ...ListOption) ([]SDWANService, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) || !validName(vdom) {
return nil, fmt.Errorf("%w: device=%q vdom=%q", ErrInvalidName, device, vdom)
}
items, err := getPaged[apiSDWANService](ctx, c, fmt.Sprintf("/pm/config/device/%s/vdom/%s/system/sdwan/service", device, vdom), nil, buildListConfig(opts))
if err != nil {
return nil, err
}
out := make([]SDWANService, len(items))
for i, item := range items {
out[i] = SDWANService{
ID: item.ID,
Name: item.Name,
Mode: toString(item.Mode),
Status: mapEnum(toString(item.Status), enableDisable),
AddressMode: toString(item.AddressMode),
InputDevice: toStringSlice(item.InputDevice),
Source: toStringSlice(item.Src),
Destination: toStringSlice(item.Dst),
InternetService: mapEnum(toString(item.InternetService), enableDisable),
PriorityMembers: toStringSlice(item.PriorityMembers),
}
}
return out, nil
}
// ListSDWANDuplication retrieves SD-WAN duplication rules from a device VDOM.
func (c *Client) ListSDWANDuplication(ctx context.Context, device, vdom string, opts ...ListOption) ([]SDWANDuplication, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) || !validName(vdom) {
return nil, fmt.Errorf("%w: device=%q vdom=%q", ErrInvalidName, device, vdom)
}
items, err := getPaged[apiSDWANDuplication](ctx, c, fmt.Sprintf("/pm/config/device/%s/vdom/%s/system/sdwan/duplication", device, vdom), nil, buildListConfig(opts))
if err != nil {
return nil, err
}
out := make([]SDWANDuplication, len(items))
for i, item := range items {
out[i] = SDWANDuplication{
ID: item.ID,
Name: item.Name,
ServiceID: item.ServiceID,
PacketDuplication: toString(item.PacketDuplication),
Fields: toStringSlice(item.Fields),
}
}
return out, nil
}
func (c *Client) forwardObject(ctx context.Context, apiURL string, extra map[string]any, out any) error {
var (
data json.RawMessage
err error
)
if extra == nil {
data, err = c.forward(ctx, apiURL)
} else {
data, err = c.forwardExtra(ctx, apiURL, extra)
}
if err != nil {
return err
}
if err := json.Unmarshal(data, out); err != nil {
return fmt.Errorf("fortimgr: unmarshal response: %w", err)
}
return nil
}
func convertSDWANSettings(raw apiSDWANSettings) SDWANSettings {
out := SDWANSettings{
OID: raw.OID,
Status: mapEnum(toString(raw.Status), enableDisable),
LoadBalanceMode: toString(raw.LoadBalanceMode),
FailDetect: mapEnum(toString(raw.FailDetect), enableDisable),
AppPerfLogPeriod: raw.AppPerfLogPeriod,
DuplicationMaxNum: raw.DuplicationMaxNum,
NeighborHoldBootTime: raw.NeighborHoldBootTime,
NeighborHoldDown: mapEnum(toString(raw.NeighborHoldDown), enableDisable),
NeighborHoldDownTime: raw.NeighborHoldDownTime,
SpeedtestBypassRouting: mapEnum(toString(raw.SpeedtestBypassRouting), enableDisable),
FailAlertInterfaces: raw.FailAlertInterfaces,
Zones: make([]SDWANZone, len(raw.Zones)),
HealthChecks: make([]SDWANHealthCheck, len(raw.HealthChecks)),
}
for i, zone := range raw.Zones {
out.Zones[i] = SDWANZone{
Name: zone.Name,
OID: zone.OID,
MinimumSLAMeetMembers: zone.MinimumSLAMeetMembers,
ServiceSLATieBreak: toString(zone.ServiceSLATieBreak),
ADVPNSelect: mapEnum(toString(zone.ADVPNSelect), enableDisable),
}
}
for i, health := range raw.HealthChecks {
out.HealthChecks[i] = convertSDWANHealthCheck(health)
}
return out
}
func convertSDWANHealthCheck(raw apiSDWANHealthCheck) SDWANHealthCheck {
out := SDWANHealthCheck{
Name: raw.Name,
OID: raw.OID,
Protocol: toString(raw.Protocol),
Server: raw.Server,
Members: raw.Members,
Interval: raw.Interval,
Failtime: raw.Failtime,
Recoverytime: raw.Recoverytime,
Source: raw.Source,
Source6: raw.Source6,
SystemDNS: mapEnum(toString(raw.SystemDNS), enableDisable),
UpdateStaticRoute: mapEnum(toString(raw.UpdateStaticRoute), enableDisable),
UpdateCascadeInterface: mapEnum(toString(raw.UpdateCascadeInterface), enableDisable),
SLA: make([]SDWANSLA, len(raw.SLA)),
}
for i, sla := range raw.SLA {
out.SLA[i] = SDWANSLA{
ID: sla.ID,
OID: sla.OID,
LatencyThreshold: sla.LatencyThreshold,
JitterThreshold: sla.JitterThreshold,
PacketLossThreshold: sla.PacketLossThreshold,
LinkCostFactor: toString(sla.LinkCostFactor),
}
}
return out
}