Skip to content

Commit 854fd8f

Browse files
author
Allan Liu
committed
dcim{,_related-connections}: added support for endpoint
1 parent 2a8cd77 commit 854fd8f

File tree

7 files changed

+449
-0
lines changed

7 files changed

+449
-0
lines changed

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ Original Author
66
---------------
77
Matt Layher <mlayher@digitalocean.com>
88

9+
910
Contributors
1011
------------
12+
Allan Liu <aliu@digitalocean.com>

client_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ func testDeviceIdentifier(n int) *DeviceIdentifier {
179179
}
180180
}
181181

182+
func testDeviceTypeIdentifier(n int) *DeviceTypeIdentifier {
183+
return &DeviceTypeIdentifier{
184+
ID: n,
185+
Manufacturer: testSimpleIdentifier(n),
186+
Model: fmt.Sprintf("device model %d", n),
187+
Slug: fmt.Sprintf("devicetype%d", n),
188+
}
189+
}
190+
182191
func testInterfaceIdentifier(n int) *InterfaceIdentifier {
183192
return &InterfaceIdentifier{
184193
ID: n,
@@ -255,6 +264,65 @@ func testPrefix(family Family, n int) *Prefix {
255264
}
256265
}
257266

267+
func testRackIdentifier(n int) *RackIdentifier {
268+
return &RackIdentifier{
269+
ID: n,
270+
Name: fmt.Sprintf("rack %d", n),
271+
FacilityID: fmt.Sprintf("facility%d", n),
272+
DisplayName: fmt.Sprintf("Rack %d", n),
273+
}
274+
}
275+
276+
func testRCConsolePortIdentifier(n int) *RCConsolePortIdentifier {
277+
return &RCConsolePortIdentifier{
278+
ConsoleServer: fmt.Sprintf("console server %d", n),
279+
Name: fmt.Sprintf("rc console port %d", n),
280+
Port: fmt.Sprintf("port %d", n),
281+
}
282+
}
283+
284+
func testRCInterfaceIdentifier(n int) *RCInterfaceIdentifier {
285+
return &RCInterfaceIdentifier{
286+
Device: fmt.Sprintf("device %d", n),
287+
Interface: fmt.Sprintf("interface %d", n),
288+
Name: fmt.Sprintf("rc interface %d", n),
289+
}
290+
}
291+
292+
func testRCPowerPortIdentifier(n int) *RCPowerPortIdentifier {
293+
return &RCPowerPortIdentifier{
294+
PDU: fmt.Sprintf("pdu %d", n),
295+
Name: fmt.Sprintf("rc power port %d", n),
296+
Outlet: fmt.Sprintf("outlet %d", n),
297+
}
298+
}
299+
300+
func testRelatedConnection(n int) *RelatedConnection {
301+
return &RelatedConnection{
302+
Device: &Device{
303+
ID: n,
304+
Name: fmt.Sprintf("device %d", n),
305+
DisplayName: fmt.Sprintf("Device %d", n),
306+
DeviceType: testDeviceTypeIdentifier(n),
307+
DeviceRole: testSimpleIdentifier(n),
308+
Platform: testSimpleIdentifier(n),
309+
Serial: fmt.Sprintf("relatedconnection%d", n),
310+
Rack: testRackIdentifier(n),
311+
Position: n,
312+
Face: n,
313+
ParentDevice: testDeviceIdentifier(n),
314+
Status: true,
315+
PrimaryIP: testIPAddressIdentifier(FamilyIPv4, n),
316+
PrimaryIP4: testIPAddressIdentifier(FamilyIPv4, n),
317+
PrimaryIP6: testIPAddressIdentifier(FamilyIPv6, n),
318+
Comments: "",
319+
},
320+
ConsolePorts: []*RCConsolePortIdentifier{testRCConsolePortIdentifier(n)},
321+
Interfaces: []*RCInterfaceIdentifier{testRCInterfaceIdentifier(n)},
322+
PowerPorts: []*RCPowerPortIdentifier{testRCPowerPortIdentifier(n)},
323+
}
324+
}
325+
258326
func testRIR(n int) *RIR {
259327
return &RIR{
260328
ID: n,
@@ -288,6 +356,14 @@ func testRoleIdentifier(n int) *RoleIdentifier {
288356
}
289357
}
290358

359+
func testSimpleIdentifier(n int) *SimpleIdentifier {
360+
return &SimpleIdentifier{
361+
ID: n,
362+
Name: fmt.Sprintf("simple %d", n),
363+
Slug: fmt.Sprintf("simple%d", n),
364+
}
365+
}
366+
291367
func testSite(n int) *Site {
292368
return &Site{
293369
ID: n,

dcim.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ type DeviceIdentifier struct {
3939
ID int `json:"id"`
4040
Name string `json:"name"`
4141
}
42+
43+
// SimpleIdentifier represents a simple thing that consists of only an ID, name,
44+
// and slug.
45+
type SimpleIdentifier struct {
46+
ID int `json:"id"`
47+
Name string `json:"name"`
48+
Slug string `json:"slug"`
49+
}

dcim_devices.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2016 The go-netbox Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package netbox
16+
17+
// Device is a network device.
18+
type Device struct {
19+
ID int `json:"id"`
20+
Name string `json:"name"`
21+
DisplayName string `json:"display_name"`
22+
DeviceType *DeviceTypeIdentifier `json:"device_type"`
23+
DeviceRole *SimpleIdentifier `json:"device_role"`
24+
Platform *SimpleIdentifier `json:"platform"`
25+
Serial string `json:"serial"`
26+
Rack *RackIdentifier `json:"rack"`
27+
Position int `json:"position"`
28+
Face int `json:"face"`
29+
ParentDevice *DeviceIdentifier `json:"parent_device"`
30+
Status bool `json:"status"`
31+
PrimaryIP *IPAddressIdentifier `json:"primary_ip"`
32+
PrimaryIP4 *IPAddressIdentifier `json:"primary_ip4"`
33+
PrimaryIP6 *IPAddressIdentifier `json:"primary_ip6"`
34+
Comments string `json:"comments"`
35+
}
36+
37+
// A DeviceTypeIdentifier indicates the device type of a network device.
38+
type DeviceTypeIdentifier struct {
39+
ID int `json:"id"`
40+
Manufacturer *SimpleIdentifier `json:"manufacturer"`
41+
Model string `json:"model"`
42+
Slug string `json:"slug"`
43+
}
44+
45+
// RackIdentifier represents a server rack.
46+
type RackIdentifier struct {
47+
ID int `json:"id"`
48+
Name string `json:"name"`
49+
FacilityID string `json:"facility_id"`
50+
DisplayName string `json:"display_name"`
51+
}

dcim_devices_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2016 The go-netbox Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package netbox
16+
17+
import "reflect"
18+
19+
func deviceEqual(a, b *Device) bool {
20+
var obsAB = []struct {
21+
a interface{}
22+
b interface{}
23+
}{
24+
{a: a.DeviceType, b: b.DeviceType},
25+
{a: a.DeviceRole, b: b.DeviceRole},
26+
{a: a.Platform, b: b.Platform},
27+
{a: a.Rack, b: b.Rack},
28+
{a: a.ParentDevice, b: b.ParentDevice},
29+
{a: a.ID, b: b.ID},
30+
{a: a.Name, b: b.Name},
31+
{a: a.DisplayName, b: b.DisplayName},
32+
{a: a.Serial, b: b.Serial},
33+
{a: a.Position, b: b.Position},
34+
{a: a.Face, b: b.Face},
35+
{a: a.Status, b: b.Status},
36+
{a: a.Comments, b: b.Comments},
37+
{a: a.PrimaryIP, b: b.PrimaryIP},
38+
{a: a.PrimaryIP4, b: b.PrimaryIP4},
39+
{a: a.PrimaryIP6, b: b.PrimaryIP6},
40+
}
41+
for _, o := range obsAB {
42+
43+
switch o.a.(type) {
44+
case *IPAddressIdentifier:
45+
i, j := o.a.(*IPAddressIdentifier), o.b.(*IPAddressIdentifier)
46+
if ok := ipAddressIdentifiersEqual(*i, *j); !ok {
47+
return false
48+
}
49+
default:
50+
if !reflect.DeepEqual(o.a, o.b) {
51+
return false
52+
}
53+
}
54+
}
55+
56+
return true
57+
}

dcim_related-connections.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2016 The go-netbox Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package netbox
16+
17+
import (
18+
"errors"
19+
"net/http"
20+
"net/url"
21+
)
22+
23+
// RelatedConnection represents components that have a releated peer-device and
24+
// peer-interface.
25+
type RelatedConnection struct {
26+
Device *Device `json:"device"`
27+
ConsolePorts []*RCConsolePortIdentifier `json:"console-ports"`
28+
Interfaces []*RCInterfaceIdentifier `json:"interfaces"`
29+
PowerPorts []*RCPowerPortIdentifier `json:"power-ports"`
30+
}
31+
32+
// RCConsolePortIdentifier represents a reduced version of a console port.
33+
type RCConsolePortIdentifier struct {
34+
ConsoleServer string `json:"console-server"`
35+
Name string `json:"name"`
36+
Port string `json:"port"`
37+
}
38+
39+
// RCInterfaceIdentifier represents a reduced version of a device interface.
40+
type RCInterfaceIdentifier struct {
41+
Device string `json:"device"`
42+
Interface string `json:"interface"`
43+
Name string `json:"name"`
44+
}
45+
46+
// RCPowerPortIdentifier represents a reduced version of a single power port.
47+
type RCPowerPortIdentifier struct {
48+
PDU string `json:"pdu"`
49+
Name string `json:"name"`
50+
Outlet string `json:"outlet"`
51+
}
52+
53+
// GetRelatedConnections retrieves a RelatedConnections object from NetBox.
54+
func (s *DCIMService) GetRelatedConnections(
55+
peerDevice, peerInterface string,
56+
) (*RelatedConnection, error) {
57+
58+
req, err := s.c.newRequest(
59+
http.MethodGet,
60+
"/api/dcim/related-connections/",
61+
&relatedConnectionsOptions{
62+
peerDevice,
63+
peerInterface,
64+
},
65+
)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
rc := new(RelatedConnection)
71+
err = s.c.do(req, rc)
72+
if err != nil {
73+
return nil, err
74+
}
75+
return rc, nil
76+
}
77+
78+
// relatedConnectionsOptions is used as an argument for
79+
// Client.DCIM.GetRelatedConnections.
80+
type relatedConnectionsOptions struct {
81+
PeerDevice string
82+
PeerInterface string
83+
}
84+
85+
func (o *relatedConnectionsOptions) values() (url.Values, error) {
86+
var err = errors.New(
87+
"must provide non-zero values for both peer-device and peer-interface",
88+
)
89+
if o == nil {
90+
return nil, err
91+
}
92+
93+
if o.PeerDevice == "" || o.PeerInterface == "" {
94+
return nil, err
95+
}
96+
v := url.Values{}
97+
98+
v.Set("peer-device", o.PeerDevice)
99+
100+
v.Set("peer-interface", o.PeerInterface)
101+
102+
return v, nil
103+
}

0 commit comments

Comments
 (0)