def test_vlan_create_from_pynetbox_with_tags(netbox_api_base): api = pynetbox.api(url="http://mock", token="1234567890") data = yaml.safe_load(open(f"{ROOT}/{FIXTURE_29}/vlan_101_tags_01.json")) pnb = pynetbox.core.response.Record(values=data, api=api, endpoint=1) netbox_api_base.add( NetboxDevice(name="devA", site_name="nyc", remote_id=30)) item = NetboxVlan.create_from_pynetbox(diffsync=netbox_api_base, obj=pnb, site_name="nyc") assert isinstance(item, NetboxVlan) is True assert item.remote_id == 1 assert item.vid == 101 assert item.associated_devices == ["devA"] # Try again with one additional device in the inventory netbox_api_base.add( NetboxDevice(name="devB", site_name="nyc", remote_id=31)) item = NetboxVlan.create_from_pynetbox(diffsync=netbox_api_base, obj=pnb, site_name="nyc") assert isinstance(item, NetboxVlan) is True assert item.remote_id == 1 assert item.vid == 101 assert item.associated_devices == ["devA", "devB"]
def test_update_clean_tags_with_incoming_tags(netbox_api_base): vlan = NetboxVlan(vid=100, site_name="HQ", remote_id=30) netbox_api_base.add(vlan) netbox_api_base.add( NetboxDevice(name="dev1", site_name="HQ", remote_id=32, device_tag_id=12)) netbox_api_base.add( NetboxDevice(name="dev2", site_name="HQ", remote_id=33, device_tag_id=13)) api = pynetbox.api(url="http://mock", token="1234567890") data = yaml.safe_load(open(f"{ROOT}/{FIXTURE_29}/vlan_101_tags_01.json")) pnb = pynetbox.core.response.Record(values=data, api=api, endpoint=1) params = vlan.translate_attrs_for_netbox({ "name": "VOICE", "associated_devices": ["dev1", "dev2"] }) clean_params = vlan.update_clean_tags(nb_params=params, obj=pnb) assert "tags" in clean_params assert sorted(clean_params["tags"]) == [1, 2, 3, 12, 13]
def test_translate_attrs_for_netbox_with_attrs(netbox_api_base): vlan = NetboxVlan(vid=100, site_name="HQ", remote_id=30) netbox_api_base.add(vlan) netbox_api_base.add( NetboxDevice(name="dev1", site_name="HQ", remote_id=32, device_tag_id=12)) netbox_api_base.add( NetboxDevice(name="dev2", site_name="HQ", remote_id=33, device_tag_id=13)) params = vlan.translate_attrs_for_netbox({ "name": "VOICE", "associated_devices": ["dev1", "dev2"] }) assert "name" in params assert params["name"] == "VOICE" assert "site" in params assert params["site"] == 10 assert "tags" in params assert sorted(params["tags"]) == [12, 13]
def test_translate_attrs_for_netbox_missing_site(netbox_api_base): vlan = NetboxVlan(vid=100, site_name="NOTPRESENT", remote_id=30) netbox_api_base.add(vlan) with pytest.raises(ObjectNotFound): vlan.translate_attrs_for_netbox({}) assert True
def test_translate_attrs_for_netbox_no_attrs(netbox_api_base): vlan = NetboxVlan(vid=100, site_name="HQ", remote_id=30) netbox_api_base.add(vlan) params = vlan.translate_attrs_for_netbox({}) assert "name" in params assert params["name"] == "vlan-100" assert "site" in params assert params["site"] == 10 assert "tags" not in params
def test_update_clean_tags_no_incoming_tags(netbox_api_base): vlan = NetboxVlan(vid=100, site_name="HQ", remote_id=30) netbox_api_base.add(vlan) api = pynetbox.api(url="http://mock", token="1234567890") data = yaml.safe_load(open(f"{ROOT}/{FIXTURE_29}/vlan_101_tags_01.json")) pnb = pynetbox.core.response.Record(values=data, api=api, endpoint=1) params = vlan.translate_attrs_for_netbox({"name": "VOICE"}) clean_params = vlan.update_clean_tags(nb_params=params, obj=pnb) assert "tags" not in clean_params
def test_vlan_create_from_pynetbox(netbox_api_base): api = pynetbox.api(url="http://mock", token="1234567890") data = yaml.safe_load(open(f"{ROOT}/{FIXTURE_29}/vlan_101_no_tag.json")) pnb = pynetbox.core.response.Record(values=data, api=api, endpoint=1) item = NetboxVlan.create_from_pynetbox(diffsync=netbox_api_base, obj=pnb, site_name="nyc") assert isinstance(item, NetboxVlan) is True assert item.remote_id == 1 assert item.vid == 101 assert item.associated_devices == []
def netbox_api_base(): """Provide an instance of NetBoxAPIAdapter with pynetbox initiliazed.""" diffsync = NetBoxAPIAdapter(nornir=None, settings={}) diffsync.netbox = pynetbox.api(url="http://mock", token="1234567890") diffsync.add(NetboxSite(name="HQ", remote_id=10)) diffsync.add( NetboxDevice(name="HQ-CORE-SW02", site_name="HQ", remote_id=29)) diffsync.add( NetboxInterface(name="TenGigabitEthernet1/0/1", device_name="HQ-CORE-SW02", remote_id=302)) diffsync.add(NetboxVlan(vid=111, site_name="HQ", remote_id=23)) return diffsync
def test_translate_attrs_for_netbox_vlan(netbox_api_base): vlan = NetboxVlan(vid=100, site_name="HQ", remote_id=30) netbox_api_base.add(vlan) config.load(config_data=dict( main=dict(import_vlans=True, backend="netbox"))) intf = NetboxInterface(device_name="HQ-CORE-SW02", name="ge-0/0/0") netbox_api_base.add(intf) params = intf.translate_attrs_for_netbox({}) assert_baseline assert "mode" not in params params = intf.translate_attrs_for_netbox({ "switchport_mode": "ACCESS", "mode": "ACCESS", "access_vlan": "HQ__111" }) assert_baseline assert params["mode"] == "access" assert params["untagged_vlan"] == 23 params = intf.translate_attrs_for_netbox({ "switchport_mode": "ACCESS", "mode": "ACCESS", "access_vlan": None }) assert_baseline assert params["mode"] == "access" assert params["untagged_vlan"] is None params = intf.translate_attrs_for_netbox({ "switchport_mode": "ACCESS", "mode": "ACCESS" }) assert_baseline assert params["mode"] == "access" assert params["untagged_vlan"] is None params = intf.translate_attrs_for_netbox({ "switchport_mode": "TRUNK", "mode": "TRUNK", "allowed_vlans": ["HQ__111", "HQ__100"] }) assert_baseline assert params["mode"] == "tagged" assert params["tagged_vlans"] == [23, 30] params = intf.translate_attrs_for_netbox({ "switchport_mode": "TRUNK", "mode": "TRUNK" }) assert_baseline assert params["mode"] == "tagged" assert params["tagged_vlans"] == [] params = intf.translate_attrs_for_netbox({ "switchport_mode": "TRUNK", "mode": "L3_SUB_VLAN" }) assert_baseline assert params["mode"] == "tagged" assert params["tagged_vlans"] == []