Skip to content

Commit

Permalink
Merge pull request #16 from netboxlabs/develop
Browse files Browse the repository at this point in the history
🚚 release
  • Loading branch information
mfiedorowicz authored Jul 25, 2024
2 parents 8b04e4f + 397beb1 commit f24705c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
python-version: [ "3.10" ]
python: [ "3.10" ]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
18 changes: 14 additions & 4 deletions netbox_diode_plugin/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def _get_lookups(self, object_type_model: str) -> tuple:
"""
if "'ipam.models.ip.ipaddress'" in object_type_model:
return "interface", "interface__device", "interface__device__site"
return (
"assigned_object",
"assigned_object__device",
"assigned_object__device__site",
)
if "'dcim.models.device_components.interface'" in object_type_model:
return "device", "device__site"
if "'dcim.models.devices.device'" in object_type_model:
Expand Down Expand Up @@ -127,9 +131,15 @@ def get(self, request, *args, **kwargs):
},
)

if len(serializer.data) > 0:
return Response(serializer.data[0])
return Response({})
try:
if len(serializer.data) > 0:
return Response(serializer.data[0])
return Response({})
except AttributeError as e:
return Response(
{"errors": [f"Serializer error: {e.args[0]}"]},
status=status.HTTP_400_BAD_REQUEST,
)

def _additional_attributes_query_filter(self):
"""Get the additional attributes query filter."""
Expand Down
63 changes: 59 additions & 4 deletions netbox_diode_plugin/tests/test_object_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,31 @@ def test_invalid_object_state_using_q_objects_and_wrong_additional_attributes_re

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_common_user_with_permissions_get_device_state(self):
def test_common_user_with_permissions_get_ip_state_using_id(self):
"""Test searching for ip using id."""
query_parameters = {
"id": self.ip_addresses[0].id,
"object_type": "ipam.ipaddress",
}

response = self.client.get(self.url, query_parameters, **self.user_header)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json().get("object_type"), "ipam.ipaddress")
self.assertEqual(
response.json().get("object").get("address"),
self.ip_addresses[0].address.__str__(),
)
self.assertEqual(
response.json()
.get("object")
.get("assigned_object")
.get("interface")
.get("name"),
self.interfaces[0].name,
)

def test_common_user_with_permissions_get_device_state_using_q_objects(self):
"""Test searching for device using q parameter."""
query_parameters = {
"q": self.devices[0].name,
Expand All @@ -300,7 +324,15 @@ def test_common_user_with_permissions_get_device_state(self):
response = self.client.get(self.url, query_parameters, **self.user_header)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_common_user_with_permissions_get_interface_state(self):
self.assertEqual(response.json().get("object_type"), "dcim.device")
self.assertEqual(
response.json().get("object").get("name"), self.devices[0].name
)
self.assertEqual(
response.json().get("object").get("site").get("name"), self.sites[0].name
)

def test_common_user_with_permissions_get_interface_state_using_q_objects(self):
"""Test searching for interface using q parameter."""
query_parameters = {
"q": self.interfaces[0].name,
Expand All @@ -312,10 +344,19 @@ def test_common_user_with_permissions_get_interface_state(self):
response = self.client.get(self.url, query_parameters, **self.user_header)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_common_user_with_permissions_get_ip_state(self):
self.assertEqual(response.json().get("object_type"), "dcim.interface")
self.assertEqual(
response.json().get("object").get("name"), self.interfaces[0].name
)
self.assertEqual(
response.json().get("object").get("device").get("name"),
self.devices[0].name,
)

def test_common_user_with_permissions_get_ip_state_using_q_objects(self):
"""Test searching for ip using q parameter."""
query_parameters = {
"q": self.ip_addresses[0].address.ip,
"q": self.ip_addresses[0].address.__str__(),
"object_type": "ipam.ipaddress",
"interface": self.interfaces[0].id,
"interface__device": self.devices[0].id,
Expand All @@ -324,3 +365,17 @@ def test_common_user_with_permissions_get_ip_state(self):

response = self.client.get(self.url, query_parameters, **self.user_header)
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(response.json().get("object_type"), "ipam.ipaddress")
self.assertEqual(
response.json().get("object").get("address"),
self.ip_addresses[0].address.__str__(),
)
self.assertEqual(
response.json()
.get("object")
.get("assigned_object")
.get("interface")
.get("name"),
self.interfaces[0].name,
)

0 comments on commit f24705c

Please sign in to comment.