Skip to content

🚚 release #16

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

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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,
)