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

Added a trace property to the traceable interface classes #280

Merged
merged 17 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 80 additions & 1 deletion pynetbox/models/dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,67 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from six.moves.urllib.parse import urlsplit

from pynetbox.core.query import Request
from pynetbox.core.response import Record, JsonField
from pynetbox.core.endpoint import RODetailEndpoint
from pynetbox.models.ipam import IpAddresses
from pynetbox.models.circuits import Circuits


class TraceableRecord(Record):
raddessi marked this conversation as resolved.
Show resolved Hide resolved
def trace(self):
req = Request(
key=str(self.id) + "/trace" if not self.url else None,
base=self.endpoint.url,
token=self.api.token,
session_key=self.api.session_key,
http_session=self.api.http_session,
)
ret = []
# from IPython import embed
raddessi marked this conversation as resolved.
Show resolved Hide resolved
# embed()
for (termination_a_data, cable_data, termination_b_data) in req.get():
this_hop_ret = []
for hop_item_data in (termination_a_data, cable_data, termination_b_data):
# if not fully terminated then some items will be None
if not hop_item_data:
this_hop_ret.append(hop_item_data)
continue

uri_to_obj_class_map = {
"/api/dcim/cables": Cables,
"/api/dcim/front-ports": FrontPorts,
"/api/dcim/interfaces": Interfaces,
"/api/dcim/rear-ports": RearPorts,
}

uri = urlsplit(hop_item_data["url"]).path
return_obj_class = uri_to_obj_class_map.get(
"/".join(uri.split("/")[:4]), # trim the uri down to it's base
Record,
)

this_hop_ret.append(
return_obj_class(hop_item_data, self.endpoint.api, self.endpoint)
raddessi marked this conversation as resolved.
Show resolved Hide resolved
)

# Try to access an attribute of the Cable object so that the Termination
# objects inside of it get populated properly via the .full_details method
# call. Without this the Cable object will be printed as:
# "<class 'pynetbox.models.dcim.Termination'> <> <class 'pynetbox.models.dcim.Termination'>"
# until it gets accessed.
try:
this_hop_ret[1].name
except (AttributeError, IndexError):
pass

ret.append(this_hop_ret)

return ret


class DeviceTypes(Record):
def __str__(self):
return self.model
Expand Down Expand Up @@ -80,11 +135,27 @@ class ConnectedEndpoint(Record):
device = Devices


class Interfaces(Record):
class Interfaces(TraceableRecord):
interface_connection = InterfaceConnection
connected_endpoint = ConnectedEndpoint


class PowerOutlets(TraceableRecord):
device = Devices


class PowerPorts(TraceableRecord):
device = Devices


class ConsolePorts(TraceableRecord):
device = Devices


class ConsoleServerPorts(TraceableRecord):
device = Devices


class RackReservations(Record):
def __str__(self):
return self.description
Expand All @@ -99,6 +170,14 @@ class RUs(Record):
device = Devices


class FrontPorts(Record):
device = Devices


class RearPorts(Record):
device = Devices


class Racks(Record):
@property
def units(self):
Expand Down
118 changes: 118 additions & 0 deletions tests/fixtures/dcim/interface_trace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
[
[
{
"id": 39126,
"url": "http://localhost:8000/api/dcim/interfaces/39126/",
"device": {
"id": 4747,
"url": "http://localhost:8000/api/dcim/devices/4747/",
"name": "test1-core1",
"display_name": "test1-core1"
},
"name": "em1",
"cable": 9911,
"connection_status": {
"value": false,
"label": "Not Connected"
}
},
{
"id": 9911,
"url": "http://localhost:8000/api/dcim/cables/9911/",
"type": "",
"status": "planned",
"label": "",
"color": "",
"length": null,
"length_unit": ""
},
{
"id": 5583,
"url": "http://localhost:8000/api/dcim/front-ports/5583/",
"device": {
"id": 4430,
"url": "http://localhost:8000/api/dcim/devices/4430/",
"name": "test1-patchpanel1",
"display_name": "test1-patchpanel1"
},
"name": "pair-11 (ports 21-22)",
raddessi marked this conversation as resolved.
Show resolved Hide resolved
"cable": 9911
}
],
[
{
"id": 3736,
"url": "http://localhost:8000/api/dcim/rear-ports/3736/",
"device": {
"id": 4430,
"url": "http://localhost:8000/api/dcim/devices/4430/",
"name": "test1-patchpanel1",
"display_name": "test1-patchpanel1"
},
"name": "port-2",
"cable": 9229
},
{
"id": 9229,
"url": "http://localhost:8000/api/dcim/cables/9229/",
"type": "mmf-om4",
"status": "planned",
"label": "",
"color": "",
"length": null,
"length_unit": ""
},
{
"id": 3768,
"url": "http://localhost:8000/api/dcim/rear-ports/3768/",
"device": {
"id": 4436,
"url": "http://localhost:8000/api/dcim/devices/4436/",
"name": "test1-patchpanel2",
"display_name": "test1-patchpanel2"
},
"name": "port-2",
"cable": 9229
}
],
[
{
"id": 5655,
"url": "http://localhost:8000/api/dcim/front-ports/5655/",
"device": {
"id": 4436,
"url": "http://localhost:8000/api/dcim/devices/4436/",
"name": "test1-patchpanel2",
"display_name": "test1-patchpanel2"
},
"name": "pair-11 (ports 21-22)",
"cable": 9240
},
{
"id": 9240,
"url": "http://localhost:8000/api/dcim/cables/9240/",
"type": "mmf-om4",
"status": "planned",
"label": "",
"color": "",
"length": null,
"length_unit": ""
},
{
"id": 35473,
"url": "http://localhost:8000/api/dcim/interfaces/35473/",
"device": {
"id": 3930,
"url": "http://localhost:8000/api/dcim/devices/3930/",
"name": "test1-core2",
"display_name": "test1-core2"
},
"name": "Ethernet11",
"cable": 9240,
"connection_status": {
"value": false,
"label": "Not Connected"
}
}
]
]
50 changes: 50 additions & 0 deletions tests/test_dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,28 @@ def test_get_all(self, mock):
headers=HEADERS,
)

@patch(
"pynetbox.core.query.requests.sessions.Session.get",
side_effect=[
Response(fixture="dcim/interface.json"),
Response(fixture="dcim/interface_trace.json"),
Response(fixture="dcim/cable.json"),
Response(fixture="dcim/cable.json"),
Response(fixture="dcim/cable.json"),
raddessi marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_trace(self, mock):
ret = nb.interfaces.get(1)
trace = ret.trace()
self.assertTrue(len(trace) == 3)
for hop in trace:
self.assertTrue(len(hop) == 3)
self.assertTrue("Termination" not in str(hop[1]))
self.assertTrue(hasattr(hop[1], "termination_a"))
self.assertTrue(hasattr(hop[1].termination_a, "name"))




class RackTestCase(Generic.Tests):
name = "racks"
Expand Down Expand Up @@ -534,3 +556,31 @@ def test_get_circuit(self):
params={},
json=None,
)
# mock.assert_has_calls(
# [
# call(
# "http://localhost:8000/api/{}/{}/1/".format(
# self.app, self.name.replace("_", "-")
# ),
# headers=HEADERS,
# params={},
# json=None,
# ),
# call(
# "http://localhost:8000/api/{}/{}/1/".format(
# "circuits", "circuit-terminations"
# ),
# headers=HEADERS,
# params={},
# json=None,
# ),
# call(
# "http://localhost:8000/api/{}/{}/1/".format(
# "circuits", "circuit-terminations"
# ),
# headers=HEADERS,
# params={},
# json=None,
# ),
# ]
# )