Skip to content

Commit

Permalink
Change default selector for readonly MAC module
Browse files Browse the repository at this point in the history
Initially pointed out in #18,
the MAC module doesn't show information about the port, this stems from
the default selector used in the module, which attempts to pull writable
information, however the REST API resource does not allow configuration,
so the payload is empty, there also appears to be an issue where the MAC
object's path is misconstructed in some cases.

This CL changes the default selector to 'status' to get the expected
behavior when using the get() method, and fixes the path issue by
forcing the mac address to always be lowercase.

Change-Id: I8baafae02a839e0f6feaec248240a0cdebb379f8
  • Loading branch information
dcorderohpe committed May 9, 2022
1 parent 3e8ded9 commit d5c30e8
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions pyaoscx/mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import logging
import re

from copy import deepcopy

from urllib.parse import quote_plus, unquote_plus

from netaddr import EUI as MacAddress
Expand Down Expand Up @@ -45,7 +47,9 @@ def __init__(self, session, from_id, mac_addr, parent_vlan, uri=None):

# Assign ID
self.from_id = from_id
self.mac_address = MacAddress(mac_addr, dialect=self.mac_format)
self.mac_address = str(
MacAddress(mac_addr, dialect=self.mac_format)
).lower()
# Assign parent VLAN
self._set_vlan(parent_vlan)
self._uri = uri
Expand Down Expand Up @@ -121,7 +125,9 @@ def get(self, depth=None, selector=None):
"""
logging.info("Retrieving %s from switch", self)

selector = selector or self.session.api.default_selector
# the MAC module is not a configuration module, so the default selector
# is readonly information
selector = selector or "status"

data = self._get_data(depth, selector)

Expand All @@ -131,19 +137,19 @@ def get(self, depth=None, selector=None):
self._set_configuration_items(data, selector)

# Set original attributes
self._original_attributes = data
self._original_attributes = deepcopy(data)
# Remove both parts of the ID
if "from" in self._original_attributes:
self._original_attributes.pop("from")
if "mac_addr" in self._original_attributes:
self._original_attributes.pop("mac_addr")

# Set port as an Interface Object
if hasattr(self, "port") and self.port is not None:
if hasattr(self, "port") and self.port:
port_response = self.port
# Instantiate empty object to use static method correctly
interface_cls = self.session.api.get_module(
self.session, "Interface", ""
interface_cls = self.session.api.get_module_class(
self.session, "Interface"
)
# Set port as a Interface Object
self.port = interface_cls.from_response(
Expand Down Expand Up @@ -185,10 +191,7 @@ def get_all(cls, session, parent_vlan):
data = json.loads(response.text)

mac_dict = {}
# Get all URI elements in the form of a list
uri_list = session.api.get_uri_from_data(data)

for uri in uri_list:
for uri in data.values():
# Create a Mac object
indices, mac = cls.from_uri(session, parent_vlan, uri)
mac_dict[indices] = mac
Expand Down Expand Up @@ -247,9 +250,11 @@ def from_response(cls, session, parent_vlan, response_data):
mac_addr = mac_pair[1]
from_id = mac_pair[0]

mac_address = MacAddress(unquote_plus(mac_addr), dialect=mac_format)
mac_address = str(
MacAddress(unquote_plus(mac_addr), dialect=mac_format)
).lower()

return Mac(session, from_id, mac_address, parent_vlan)
return cls(session, from_id, mac_address, parent_vlan)

@classmethod
def from_uri(cls, session, parent_vlan, uri):
Expand All @@ -266,15 +271,13 @@ def from_uri(cls, session, parent_vlan, uri):
mac_format.word_sep = ":"
# Get ID from URI. Note that using a uri object here is of no
# benefit because we are dealing with the path
index_pattern = re.compile(
r"(.*)macs/(?P<index1>.+)[,./-](?P<index2>.+)"
)
index_pattern = re.compile(r"(.*)macs/(?P<index1>.+),(?P<index2>.+)")
from_id = index_pattern.match(uri).group("index1")
reference_mac_addr = index_pattern.match(uri).group("index2")

mac_addr = MacAddress(
unquote_plus(reference_mac_addr), dialect=mac_format
)
mac_addr = str(
MacAddress(unquote_plus(reference_mac_addr), dialect=mac_format)
).lower()

mac = Mac(session, from_id, mac_addr, parent_vlan)
indices = "{0}{1}{2}".format(
Expand Down

0 comments on commit d5c30e8

Please sign in to comment.