Updating Interface Tagged VLANs in a Script #18233
-
Hello, I am trying to import all VLANs from a switch stack. It is a mix of parsing data from the config file of the switch itself and getting data from our API (whenever you see PrsID something, it is some code we use to reference stuff). Here I am trying to create each VLANs needed to be imported (which is already tested and working). And then I want to retrieve the switch (working) to update the interface each VLANs are tagged on. I also succeeded retrieving each Interface needed. # DON'T MIND UNDEFINED VARIABLES :)
vlan_group = VLANGroup.objects.get(custom_field_data={"PrsID": response_data[0]['EnregTerID']})
vlan_name = "ID" + vlan_id + "-P" + PrsID_fibre
vlan = VLAN.objects.create(name=vlan_name, vid=int(vlan_id), group=vlan_group)
# self.log_info(f"PrsID_fibre: {PrsID_fibre}, PrsID_Porte de collecte: {response_data[0]['EnregTerID']}, interface: {interface}")
PrsID_switch = int((data['conf'].name.split('-')[0])[1:])
if interface.split('-')[1][0] == "0":
switch = VirtualChassis.objects.get(custom_field_data={'PrsID': PrsID_switch}).members.get(vc_position=1)
else:
switch = VirtualChassis.objects.get(custom_field_data={'PrsID': PrsID_switch}).members.get(vc_position=2)
interface_switch = switch.interfaces.get(name=interface)
with transaction.atomic():
self.log_info(f"{interface_switch} on {interface_switch.device}")
interface_switch.mode = "tagged"
interface_switch.tagged_vlans.add(vlan)
interface_switch.save()
self.log_info(f"{vlan} add on interface {interface} of {switch}") I don't understand why my interface is not changed. Thank you for your help ❤️ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Answer : vlan_group = VLANGroup.objects.get(custom_field_data={"PrsID": response_data[0]['EnregTerID']})
vlan_name = "ID" + vlan_id + "-P" + PrsID_fibre
vlan = VLAN.objects.create(name=vlan_name, vid=int(vlan_id), group=vlan_group)
# self.log_info(f"PrsID_fibre: {PrsID_fibre}, PrsID_Porte de collecte: {response_data[0]['EnregTerID']}, interface: {interface}")
PrsID_switch = int((data['conf'].name.split('-')[0])[1:])
if interface.split('-')[1][0] == "0":
switch = VirtualChassis.objects.get(custom_field_data={'PrsID': PrsID_switch}).members.get(vc_position=1)
else:
switch = VirtualChassis.objects.get(custom_field_data={'PrsID': PrsID_switch}).members.get(vc_position=2)
interface_switch = switch.interfaces.get(name=interface)
with transaction.atomic():
self.log_info(f"{interface_switch} on {interface_switch.device}")
interface_switch.mode = "tagged"
interface_switch.save() # Swap those two lines :)
interface_switch.tagged_vlans.add(vlan) # and this one
self.log_info(f"{vlan} add on interface {interface} of {switch}") |
Beta Was this translation helpful? Give feedback.
Answer :