Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dcim{,_related-connections}: added support for endpoint #5

Merged
merged 1 commit into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ Original Author
---------------
Matt Layher <mlayher@digitalocean.com>


Contributors
------------
Allan Liu <aliu@digitalocean.com>
76 changes: 76 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ func testDeviceIdentifier(n int) *DeviceIdentifier {
}
}

func testDeviceTypeIdentifier(n int) *DeviceTypeIdentifier {
return &DeviceTypeIdentifier{
ID: n,
Manufacturer: testSimpleIdentifier(n),
Model: fmt.Sprintf("device model %d", n),
Slug: fmt.Sprintf("devicetype%d", n),
}
}

func testInterfaceIdentifier(n int) *InterfaceIdentifier {
return &InterfaceIdentifier{
ID: n,
Expand Down Expand Up @@ -255,6 +264,65 @@ func testPrefix(family Family, n int) *Prefix {
}
}

func testRackIdentifier(n int) *RackIdentifier {
return &RackIdentifier{
ID: n,
Name: fmt.Sprintf("rack %d", n),
FacilityID: fmt.Sprintf("facility%d", n),
DisplayName: fmt.Sprintf("Rack %d", n),
}
}

func testRCConsolePortIdentifier(n int) *RCConsolePortIdentifier {
return &RCConsolePortIdentifier{
ConsoleServer: fmt.Sprintf("console server %d", n),
Name: fmt.Sprintf("rc console port %d", n),
Port: fmt.Sprintf("port %d", n),
}
}

func testRCInterfaceIdentifier(n int) *RCInterfaceIdentifier {
return &RCInterfaceIdentifier{
Device: fmt.Sprintf("device %d", n),
Interface: fmt.Sprintf("interface %d", n),
Name: fmt.Sprintf("rc interface %d", n),
}
}

func testRCPowerPortIdentifier(n int) *RCPowerPortIdentifier {
return &RCPowerPortIdentifier{
PDU: fmt.Sprintf("pdu %d", n),
Name: fmt.Sprintf("rc power port %d", n),
Outlet: fmt.Sprintf("outlet %d", n),
}
}

func testRelatedConnection(n int) *RelatedConnection {
return &RelatedConnection{
Device: &Device{
ID: n,
Name: fmt.Sprintf("device %d", n),
DisplayName: fmt.Sprintf("Device %d", n),
DeviceType: testDeviceTypeIdentifier(n),
DeviceRole: testSimpleIdentifier(n),
Platform: testSimpleIdentifier(n),
Serial: fmt.Sprintf("relatedconnection%d", n),
Rack: testRackIdentifier(n),
Position: n,
Face: n,
ParentDevice: testDeviceIdentifier(n),
Status: true,
PrimaryIP: testIPAddressIdentifier(FamilyIPv4, n),
PrimaryIP4: testIPAddressIdentifier(FamilyIPv4, n),
PrimaryIP6: testIPAddressIdentifier(FamilyIPv6, n),
Comments: "",
},
ConsolePorts: []*RCConsolePortIdentifier{testRCConsolePortIdentifier(n)},
Interfaces: []*RCInterfaceIdentifier{testRCInterfaceIdentifier(n)},
PowerPorts: []*RCPowerPortIdentifier{testRCPowerPortIdentifier(n)},
}
}

func testRIR(n int) *RIR {
return &RIR{
ID: n,
Expand Down Expand Up @@ -288,6 +356,14 @@ func testRoleIdentifier(n int) *RoleIdentifier {
}
}

func testSimpleIdentifier(n int) *SimpleIdentifier {
return &SimpleIdentifier{
ID: n,
Name: fmt.Sprintf("simple %d", n),
Slug: fmt.Sprintf("simple%d", n),
}
}

func testSite(n int) *Site {
return &Site{
ID: n,
Expand Down
8 changes: 8 additions & 0 deletions dcim.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ type DeviceIdentifier struct {
ID int `json:"id"`
Name string `json:"name"`
}

// SimpleIdentifier represents a simple object that consists of only an ID,
// name, and slug.
type SimpleIdentifier struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
}
51 changes: 51 additions & 0 deletions dcim_devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2016 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netbox

// Device is a network device.
type Device struct {
ID int `json:"id"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
DeviceType *DeviceTypeIdentifier `json:"device_type"`
DeviceRole *SimpleIdentifier `json:"device_role"`
Platform *SimpleIdentifier `json:"platform"`
Serial string `json:"serial"`
Rack *RackIdentifier `json:"rack"`
Position int `json:"position"`
Face int `json:"face"`
ParentDevice *DeviceIdentifier `json:"parent_device"`
Status bool `json:"status"`
PrimaryIP *IPAddressIdentifier `json:"primary_ip"`
PrimaryIP4 *IPAddressIdentifier `json:"primary_ip4"`
PrimaryIP6 *IPAddressIdentifier `json:"primary_ip6"`
Comments string `json:"comments"`
}

// A DeviceTypeIdentifier indicates the device type of a network device.
type DeviceTypeIdentifier struct {
ID int `json:"id"`
Manufacturer *SimpleIdentifier `json:"manufacturer"`
Model string `json:"model"`
Slug string `json:"slug"`
}

// RackIdentifier represents a server rack.
type RackIdentifier struct {
ID int `json:"id"`
Name string `json:"name"`
FacilityID string `json:"facility_id"`
DisplayName string `json:"display_name"`
}
57 changes: 57 additions & 0 deletions dcim_devices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2016 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netbox

import "reflect"

func deviceEqual(a, b *Device) bool {
var obsAB = []struct {
a interface{}
b interface{}
}{
{a: a.DeviceType, b: b.DeviceType},
{a: a.DeviceRole, b: b.DeviceRole},
{a: a.Platform, b: b.Platform},
{a: a.Rack, b: b.Rack},
{a: a.ParentDevice, b: b.ParentDevice},
{a: a.ID, b: b.ID},
{a: a.Name, b: b.Name},
{a: a.DisplayName, b: b.DisplayName},
{a: a.Serial, b: b.Serial},
{a: a.Position, b: b.Position},
{a: a.Face, b: b.Face},
{a: a.Status, b: b.Status},
{a: a.Comments, b: b.Comments},
{a: a.PrimaryIP, b: b.PrimaryIP},
{a: a.PrimaryIP4, b: b.PrimaryIP4},
{a: a.PrimaryIP6, b: b.PrimaryIP6},
}
for _, o := range obsAB {

switch o.a.(type) {
case *IPAddressIdentifier:
i, j := o.a.(*IPAddressIdentifier), o.b.(*IPAddressIdentifier)
if ok := ipAddressIdentifiersEqual(*i, *j); !ok {
return false
}
default:
if !reflect.DeepEqual(o.a, o.b) {
return false
}
}
}

return true
}
102 changes: 102 additions & 0 deletions dcim_related-connections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2016 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netbox

import (
"errors"
"net/http"
"net/url"
)

// RelatedConnection represents components that have a related peer-device and
// peer-interface.
type RelatedConnection struct {
Device *Device `json:"device"`
ConsolePorts []*RCConsolePortIdentifier `json:"console-ports"`
Interfaces []*RCInterfaceIdentifier `json:"interfaces"`
PowerPorts []*RCPowerPortIdentifier `json:"power-ports"`
}

// RCConsolePortIdentifier represents a reduced version of a console port.
type RCConsolePortIdentifier struct {
ConsoleServer string `json:"console-server"`
Name string `json:"name"`
Port string `json:"port"`
}

// RCInterfaceIdentifier represents a reduced version of a device interface.
type RCInterfaceIdentifier struct {
Device string `json:"device"`
Interface string `json:"interface"`
Name string `json:"name"`
}

// RCPowerPortIdentifier represents a reduced version of a single power port.
type RCPowerPortIdentifier struct {
PDU string `json:"pdu"`
Name string `json:"name"`
Outlet string `json:"outlet"`
}

// GetRelatedConnections retrieves a RelatedConnection object from NetBox.
func (s *DCIMService) GetRelatedConnections(
peerDevice, peerInterface string,
) (*RelatedConnection, error) {
req, err := s.c.newRequest(
http.MethodGet,
"/api/dcim/related-connections/",
&relatedConnectionsOptions{
peerDevice,
peerInterface,
},
)
if err != nil {
return nil, err
}

rc := new(RelatedConnection)
err = s.c.do(req, rc)
if err != nil {
return nil, err
}
return rc, nil
}

// relatedConnectionsOptions is used as an argument for
// Client.DCIM.GetRelatedConnections.
type relatedConnectionsOptions struct {
PeerDevice string
PeerInterface string
}

func (o *relatedConnectionsOptions) values() (url.Values, error) {
err := errors.New(
"must provide non-zero values for both peer-device and peer-interface",
)
if o == nil {
return nil, err
}

if o.PeerDevice == "" || o.PeerInterface == "" {
return nil, err
}
v := url.Values{}

v.Set("peer-device", o.PeerDevice)

v.Set("peer-interface", o.PeerInterface)

return v, nil
}
Loading