Skip to content

Commit f3a18e0

Browse files
committed
Created a Class that checks if objects exist in netbox. Renamed netbox_dcim.py to netbox_create.py as that class now only creates netbox objects.
1 parent 8a5f011 commit f3a18e0

File tree

4 files changed

+71
-42
lines changed

4 files changed

+71
-42
lines changed

Netbox_CSV_Read/Netbox_Api/netbox_dcim.py renamed to Netbox_CSV_Read/Netbox_Api/netbox_create.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class NetboxDCIM:
66
"""
7-
This class contains methods that will interact with the Netbox Api.
7+
This class contains methods that will interact create objects in Netbox.
88
"""
99

1010
def __init__(self, url: str, token: str, api: Optional = None):
@@ -13,24 +13,6 @@ def __init__(self, url: str, token: str, api: Optional = None):
1313
else:
1414
self.netbox = api
1515

16-
def check_device_exists(self, device_name: str) -> bool:
17-
"""
18-
This method will check if a device exists in Netbox.
19-
:param device_name: The name of the device.
20-
:return: Returns bool.
21-
"""
22-
device = self.netbox.dcim.devices.get(name=device_name)
23-
return bool(device)
24-
25-
def check_device_type_exists(self, device_type: str) -> bool:
26-
"""
27-
This method will check if a device exists in Netbox.
28-
:param device_type: The name of the device.
29-
:return: Returns bool.
30-
"""
31-
device_type = self.netbox.dcim.device_types.get(slug=device_type)
32-
return bool(device_type)
33-
3416
def create_device(self, data: dict | list) -> bool:
3517
"""
3618
This method uses the pynetbox Api to create a device in Netbox.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Optional
2+
from Netbox_Api.netbox_connect import NetboxConnect
3+
4+
5+
class NetboxExistence:
6+
"""
7+
This class contains methods that check if an object exists in Netbox.
8+
"""
9+
10+
def __init__(self, url: str, token: str, api: Optional = None):
11+
if not api:
12+
self.netbox = NetboxConnect(url, token).api_object()
13+
else:
14+
self.netbox = api
15+
16+
def check_device_exists(self, device_name: str) -> bool:
17+
"""
18+
This method will check if a device exists in Netbox.
19+
:param device_name: The name of the device.
20+
:return: Returns bool.
21+
"""
22+
device = self.netbox.dcim.devices.get(name=device_name)
23+
return bool(device)
24+
25+
def check_device_type_exists(self, device_type: str) -> bool:
26+
"""
27+
This method will check if a device exists in Netbox.
28+
:param device_type: The name of the device.
29+
:return: Returns bool.
30+
"""
31+
device_type = self.netbox.dcim.device_types.get(slug=device_type)
32+
return bool(device_type)

Netbox_CSV_Read/Tests/test_netbox_dcim.py renamed to Netbox_CSV_Read/Tests/test_netbox_create.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from unittest.mock import MagicMock, NonCallableMock
2-
from Netbox_Api.netbox_dcim import NetboxDCIM
2+
from Netbox_Api.netbox_create import NetboxDCIM
33
import pytest
44

55

@@ -15,28 +15,6 @@ def instance_fixture(api_mock):
1515
return NetboxDCIM(url, token, api_mock)
1616

1717

18-
def test_check_device_exists(instance):
19-
"""
20-
This test ensures the .get method is called once with the correct argument.
21-
"""
22-
mock_device = MagicMock()
23-
device_name = NonCallableMock()
24-
instance.netbox.dcim.devices = mock_device
25-
instance.check_device_exists(device_name)
26-
mock_device.get.assert_called_once_with(name=device_name)
27-
28-
29-
def test_check_device_type_exists(instance):
30-
"""
31-
This test ensures the .get method is called once with the correct argument.
32-
"""
33-
mock_device_types = MagicMock()
34-
device_type = NonCallableMock()
35-
instance.netbox.dcim.device_types = mock_device_types
36-
instance.check_device_type_exists(device_type)
37-
mock_device_types.get.assert_called_once_with(slug=device_type)
38-
39-
4018
def test_create_device(instance):
4119
"""
4220
This test ensures the .create method is called once with the correct argument.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from unittest.mock import MagicMock, NonCallableMock
2+
from Netbox_Api.netbox_existence import NetboxExistence
3+
import pytest
4+
5+
6+
@pytest.fixture(name="api_mock", scope="function")
7+
def instance_api_fixture():
8+
return MagicMock()
9+
10+
11+
@pytest.fixture(name="instance")
12+
def instance_fixture(api_mock):
13+
url = "not real url"
14+
token = "not real token"
15+
return NetboxExistence(url, token, api_mock)
16+
17+
18+
def test_check_device_exists(instance):
19+
"""
20+
This test ensures the .get method is called once with the correct argument.
21+
"""
22+
mock_device = MagicMock()
23+
device_name = NonCallableMock()
24+
instance.netbox.dcim.devices = mock_device
25+
instance.check_device_exists(device_name)
26+
mock_device.get.assert_called_once_with(name=device_name)
27+
28+
29+
def test_check_device_type_exists(instance):
30+
"""
31+
This test ensures the .get method is called once with the correct argument.
32+
"""
33+
mock_device_types = MagicMock()
34+
device_type = NonCallableMock()
35+
instance.netbox.dcim.device_types = mock_device_types
36+
instance.check_device_type_exists(device_type)
37+
mock_device_types.get.assert_called_once_with(slug=device_type)

0 commit comments

Comments
 (0)