Trying to create a L2VPN Termination #19575
-
Hello, running netbox 4.2.9 here. Trying to create a L2VPN Termination for VLAN using Netbox Scripts. The function is the following one: @staticmethod
def associate_l2vpn_vlan(caller, l2vpn_id, vlan_id):
MacVRF_L2VPN = L2VPN.objects.get(id=l2vpn_id)
MacVRF_VLAN = VLAN.objects.get(id=vlan_id)
caller.log_info(f"Will create L2VPN termination for VLAN [{MacVRF_VLAN}](MacVRF.get_absolute_url()) on L2VPN [{MacVRF_L2VPN}](MacVRF_L2VPN.get_absolute_url())")
macvrf = L2VPNTermination(
l2vpn=MacVRF_L2VPN,
#assigned_object_type='ipam.vlan',
assigned_object_type=VLAN,
assigned_object_id=vlan_id,
)
macvrf.save()
caller.log_success(f"Created L2VPN termination for VLAN [{MacVRF_VLAN}](MacVRF.get_absolute_url()) on L2VPN [{MacVRF_L2VPN}](MacVRF_L2VPN.get_absolute_url())") But when executing the call using : vlan_mac_vrf = LocalUtils.associate_l2vpn_vlan(
self,
l2vpn_id=data["l2vpn_termination"].id,
vlan_id=new_vlan.id,
) I got the folllowing errors:
Anything I missed ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi there, You're on the right track! When creating an Instead of doing: macvrf = L2VPNTermination(
l2vpn=MacVRF_L2VPN,
assigned_object_type=VLAN,
assigned_object_id=vlan_id,
) You can just use the assigned_object parameter directly: macvrf = L2VPNTermination(
l2vpn=MacVRF_L2VPN,
assigned_object=MacVRF_VLAN,
) This way, NetBox will automatically set the correct ContentType and object ID under the hood. If for some reason you do need to set the content type manually (not recommended unless necessary), then you’d need: from django.contrib.contenttypes.models import ContentType
vlan_ct = ContentType.objects.get_for_model(VLAN)
macvrf = L2VPNTermination(
l2vpn=MacVRF_L2VPN,
assigned_object_type=vlan_ct.pk,
assigned_object_id=vlan_id,
) But again — using assigned_object is the preferred and more Pythonic way. Hope that clears it up! |
Beta Was this translation helpful? Give feedback.
-
Thanks, indeed, was stucked because doing Netbox API and Netbox Scripts, so was not able to understand what I did wrong. |
Beta Was this translation helpful? Give feedback.
Hi there,
You're on the right track! When creating an
L2VPNTermination
, you can manually specifyassigned_object_type
andassigned_object_id
, but it’s cleaner and less error-prone to let NetBox handle that part automatically.Instead of doing:
You can just use the assigned_object parameter directly:
This way, NetBox will automatically set the correct ContentType and object ID under the hood.
If for some reason you do need to set the content type manually (not recommended unless necessary), …