-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from digitalocean/feature/dcim_related-connections
dcim{,_related-connections}: added support for endpoint
- Loading branch information
Showing
7 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.