-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.go
More file actions
81 lines (75 loc) · 2.63 KB
/
Copy pathinterface.go
File metadata and controls
81 lines (75 loc) · 2.63 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
package fortimgr
import (
"context"
"fmt"
"strings"
)
type apiInterface struct {
Name string `json:"name"`
IP any `json:"ip"`
Type any `json:"type"`
Status any `json:"status"`
Role any `json:"role"`
Mode any `json:"mode"`
AllowAccess any `json:"allowaccess"`
VDOM any `json:"vdom"`
Zone any `json:"zone"`
VlanID int `json:"vlanid"`
MTU int `json:"mtu"`
Speed any `json:"speed"`
Alias string `json:"alias"`
Description string `json:"description"`
}
// ListInterfaces retrieves network interfaces for a device.
//
// Pass an empty string (or "global") for vdom to use the device-wide global
// path (/pm/config/device/<dev>/global/system/interface). This returns every
// interface across all VDOMs in a single call, with each interface carrying
// its own "vdom" field — callers can derive the VDOM set from the result.
// This is the only path available to restricted admins that cannot enumerate
// /dvmdb/device/<dev>/vdom.
//
// Pass a specific vdom name to scope the query to that VDOM.
//
// Pagination is applied transparently; see WithPageSize / WithPageCallback.
func (c *Client) ListInterfaces(ctx context.Context, device, vdom string, opts ...ListOption) ([]Interface, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(device) {
return nil, fmt.Errorf("%w: device=%q", ErrInvalidName, device)
}
var apiURL string
if vdom == "" || vdom == "global" {
apiURL = fmt.Sprintf("/pm/config/device/%s/global/system/interface", device)
} else {
if !validName(vdom) {
return nil, fmt.Errorf("%w: vdom=%q", ErrInvalidName, vdom)
}
apiURL = fmt.Sprintf("/pm/config/device/%s/vdom/%s/system/interface", device, vdom)
}
items, err := getPaged[apiInterface](ctx, c, apiURL, nil, buildListConfig(opts))
if err != nil {
return nil, err
}
interfaces := make([]Interface, len(items))
for i, iface := range items {
interfaces[i] = Interface{
Name: iface.Name,
IP: formatSubnet(iface.IP),
Type: mapEnum(toString(iface.Type), interfaceTypes),
Status: mapEnum(toString(iface.Status), interfaceStatuses),
Role: mapEnum(toString(iface.Role), interfaceRoles),
Mode: mapEnum(toString(iface.Mode), interfaceModes),
AllowAccess: strings.Join(decodeAllowAccess(iface.AllowAccess), " "),
VDOM: toString(iface.VDOM),
Zone: toString(iface.Zone),
VlanID: iface.VlanID,
MTU: iface.MTU,
Speed: toString(iface.Speed),
Alias: iface.Alias,
Description: iface.Description,
}
}
return interfaces, nil
}