Skip to content
Merged
Changes from 2 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
86 changes: 84 additions & 2 deletions pynetbox/models/dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,56 @@
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):
Comment thread
raddessi marked this conversation as resolved.
@property
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 = []
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

url_path = urlsplit(hop_item_data["url"]).path
if url_path.startswith("/api/dcim/cables"):
return_obj_class = Cables
elif url_path.startswith("/api/dcim/front-ports"):
return_obj_class = FrontPorts
elif url_path.startswith("/api/dcim/interfaces"):
return_obj_class = Interfaces
elif url_path.startswith("/api/dcim/rear-ports"):
return_obj_class = RearPorts
else:
raise NotImplementedError(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this how you would want to handle this situation?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably make a dict here that mapped the endpoint name it might encounter with the custom object instead of an if/else tree (really wish switches were a thing in python). e.g.

{
    "cables": Cables,
    "front-ports": FrontPorts,
...
}

You'll probably want to add some other endpoint/objects you might come across in the traces as well like ConsolePort/ConsoleServerPort and PowerPort/Outlets. I'd probably default to just a simple Record object at the end of it instead of raising.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. I'll update to a map and return a Record instead of raising. It's been a while.. I think I verified all possible objects returned are listed here but I'll check once more, good call.

"unable to unpack item data from endpoint '{}'".format(url_path)
)
this_hop_ret.append(
return_obj_class(hop_item_data, self.endpoint.api, self.endpoint)
Comment thread
raddessi marked this conversation as resolved.
)

ret.append(this_hop_ret)

return ret


class DeviceTypes(Record):
def __str__(self):
return self.model
Expand Down Expand Up @@ -80,11 +124,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 +159,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 Expand Up @@ -154,7 +222,21 @@ def __str__(self):

class Cables(Record):
def __str__(self):
return "{} <> {}".format(self.termination_a, self.termination_b)
# populate the terminations to get the full names if they are not already
try:
termination_a_name = self.termination_a.name
except AttributeError:
self.termination_a.full_details(self)
termination_a_name = self.termination_a.name

try:
termination_b_name = self.termination_b.name
except AttributeError:
self.termination_b.full_details(self)
termination_b_name = self.termination_b.name


return "{} <> {}".format(termination_a_name, termination_b_name)

termination_a = Termination
termination_b = Termination