Skip to content

Commit

Permalink
Closes #139: Enable querying by device hostname as well as internal ID
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed May 30, 2021
1 parent ed21382 commit 4c12f13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion hyperglass/models/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def query(self):

def export_dict(self, pretty=False):
"""Create dictionary representation of instance."""

if pretty:
items = {
"query_location": self.device.name,
Expand All @@ -148,19 +149,26 @@ def export_json(self):
@validator("query_type")
def validate_query_type(cls, value):
"""Ensure query_type is enabled."""

query = params.queries[value]

if not query.enable:
raise InputInvalid(
params.messages.feature_not_enabled,
level="warning",
feature=query.display_name,
)

return value

@validator("query_location")
def validate_query_location(cls, value):
"""Ensure query_location is defined."""
if value not in devices._ids:

valid_id = value in devices._ids
valid_hostname = value in devices.hostnames

if not any((valid_id, valid_hostname)):
raise InputInvalid(
params.messages.invalid_field,
level="warning",
Expand All @@ -172,13 +180,16 @@ def validate_query_location(cls, value):
@validator("query_vrf")
def validate_query_vrf(cls, value, values):
"""Ensure query_vrf is defined."""

vrf_object = get_vrf_object(value)
device = devices[values["query_location"]]
device_vrf = None

for vrf in device.vrfs:
if vrf == vrf_object:
device_vrf = vrf
break

if device_vrf is None:
raise InputInvalid(
params.messages.vrf_not_associated,
Expand Down
2 changes: 2 additions & 0 deletions hyperglass/models/config/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,7 @@ def __getitem__(self, accessor: str) -> Device:
for device in self.objects:
if device._id == accessor:
return device
elif device.name == accessor:
return device

raise AttributeError(f"No device named '{accessor}'")

0 comments on commit 4c12f13

Please sign in to comment.