class NetBoxTestCase(TestCase): def setUp(self): super().setUp() self.netbox = NetBox() self.netbox.api = pynetbox.api("http://netbox.example.net", token="test") @patch( "requests.sessions.Session.get", return_value=MockedResponse(fixture="netbox/tests/fixtures/devices.json"), ) def test_get_devices(self, *_): devices = self.netbox.get_devices() self.assertEqual(2, len(devices)) self.assertEqual("router01.example.net", next(devices).name) self.assertEqual("router02.example.net", next(devices).name) @patch( "pynetbox.core.endpoint.RODetailEndpoint.list", return_value=MockedGenerator( { "get_facts": MockedResponse( fixture="netbox/tests/fixtures/device_facts.json" ).json() } ), ) def test_napalm(self, *_): with patch( "requests.sessions.Session.get", return_value=MockedResponse(fixture="netbox/tests/fixtures/device.json"), ): facts = self.netbox.napalm(1, "get_facts") self.assertEqual("router01", facts["hostname"])
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if settings.NETBOX_API: self.fields["netbox_device_id"] = forms.ChoiceField( label="NetBox Device", choices=[(0, "--------")] + [(device["id"], device["display_name"]) for device in NetBox().get_devices()], ) self.fields["netbox_device_id"].widget.attrs["class"] = " ".join([ self.fields["netbox_device_id"].widget.attrs.get("class", ""), "form-control", ]).strip() else: self.fields["netbox_device_id"].widget = forms.HiddenInput()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if settings.NETBOX_API: choices = [] for device in NetBox().get_devices(): try: choices.append((device.id, device.display)) except AttributeError: # Fallback to hold API attribute choices.append((device.id, device.display_name)) self.fields["netbox_device_id"] = forms.ChoiceField( label="NetBox device", choices=[(0, "---------")] + choices, widget=StaticSelect, ) self.fields["netbox_device_id"].widget.attrs["class"] = " ".join([ self.fields["netbox_device_id"].widget.attrs.get("class", ""), "form-control", ]).strip() else: self.fields["netbox_device_id"].widget = forms.HiddenInput()
def setUp(self): super().setUp() self.netbox = NetBox() self.netbox.api = pynetbox.api("http://netbox.example.net", token="test")