-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
181 lines (154 loc) · 5 KB
/
interface.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
package netbox
import (
"fmt"
"github.com/netbox-community/go-netbox/v3/netbox/client"
"github.com/netbox-community/go-netbox/v3/netbox/client/dcim"
"github.com/netbox-community/go-netbox/v3/netbox/models"
)
// Extracted from netbox/models/device's Interface, but reduced to
// basic Go types to make it easier to work with the subset that I
// care about.
type Interface struct {
CableID int64
CableDisplay string
CableLabel string
CableEnd string
ConnectedEndpoints []string
ConnectedEndpointsReachable bool // why is it a bool when ConnectedEndpoints is a list?
CustomFields map[string]interface{}
Description string
DeviceID int64
Display string
Enabled bool
ID int64
Label string
LAGName string // lag.name
LinkPeers []string
LinkPeersType string
MACAddress string
MarkConnected bool
MgmtOnly bool
MTU int64
Name string
ParentID int64
ParentName string
PoeMode string // poe_mode.value
PoeType string // poe_type.value
Speed int64
TaggedVLANNames []string
TaggedVLANIDs []int64
TaggedVLANVIDs []int64
Tags map[string]bool // Tags.Name -> true
Type string // type.value
UntaggedVLANName string
UntaggedVLANID int64
UntaggedVLANVID int64
VRF string // presumably? Not currently using.
WirelessLANs []string // wireless_lans.ssid
// Defined in NetBox 3.3 but not populated in my config.
//
// Bridge
// ConnectedEndpointsType string // Always "dcim.interface" or null in my sample.
// Duplex unused in my sample
// L2vpnTermination unused in my sample
// Mode unused in my sample
// Module unused in my sample
// RfChannel
// RfChannelFrequency
// RfChannelWidth
// RfRole
// WWN
}
type Interfaces []*Interface
func (ints Interfaces) ForDeviceID(id int64) Interfaces {
ret := []*Interface{}
for _, i := range ints {
if i.DeviceID == id {
ret = append(ret, i)
}
}
return ret
}
func dcimInterfaceToInterface(i *models.Interface) (*Interface, error) {
in := &Interface{
CableEnd: i.CableEnd,
CustomFields: make(map[string]interface{}),
ConnectedEndpoints: []string{},
ConnectedEndpointsReachable: Bool(i.ConnectedEndpointsReachable),
Description: i.Description,
DeviceID: i.Device.ID,
Display: i.Display,
Enabled: i.Enabled,
ID: i.ID,
Label: i.Label,
LinkPeers: []string{},
LinkPeersType: i.LinkPeersType,
MACAddress: String(i.MacAddress),
MarkConnected: i.MarkConnected,
MgmtOnly: i.MgmtOnly,
MTU: Int64(i.Mtu),
Name: String(i.Name),
Speed: Int64(i.Speed),
Tags: make(map[string]bool),
}
// if dev.CustomFields != nil {
// }
if i.Cable != nil {
in.CableID = i.Cable.ID
in.CableDisplay = i.Cable.Display
in.CableLabel = i.Cable.Label
}
// for _, ep := range i.ConnectedEndpoints {
// in.ConnectedEndpoints = append(in.ConnectedEndpoints, String(ep))
// }
if i.Lag != nil {
in.LAGName = String(i.Lag.Name)
}
//for _, lp := range i.LinkPeers {
// in.LinkPeers = append(in.LinkPeers, String(lp))
//}
if i.Parent != nil {
in.ParentID = i.Parent.ID
in.ParentName = String(i.Parent.Name)
}
for _, t := range i.Tags {
in.Tags[*t.Name] = true
}
if i.Type != nil {
in.Type = String(i.Type.Value)
}
if i.Vrf != nil {
in.VRF = String(i.Vrf.Name)
}
return in, nil
}
func ListInterfaces(c *client.NetBoxAPI) (Interfaces, error) {
limit := int64(0)
r := dcim.NewDcimInterfacesListParams()
r.Limit = &limit
rs, err := c.Dcim.DcimInterfacesList(r, nil)
if err != nil {
return nil, fmt.Errorf("Unable to call DcimInterfacesList: %v", err)
}
ifs := make([]*Interface, len(rs.Payload.Results))
for i, result := range rs.Payload.Results {
in, err := dcimInterfaceToInterface(result)
if err != nil {
return nil, fmt.Errorf("Unable to convert dcim.Interface to netbox.Interface: %v", err)
}
ifs[i] = in
}
return ifs, nil
}
func GetInterface(c *client.NetBoxAPI, id int64) (*Interface, error) {
limit := int64(0)
idStr := fmt.Sprint(id)
r := dcim.NewDcimInterfacesListParams()
r.Limit = &limit
r.ID = &idStr
rs, err := c.Dcim.DcimInterfacesList(r, nil)
if err != nil {
return nil, err
}
return dcimInterfaceToInterface(rs.Payload.Results[0])
}