netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
567 stars 168 forks source link

unable to update tagged vlan using update() #504

Closed HarishankarYellapragada closed 1 year ago

HarishankarYellapragada commented 1 year ago

Unable to update using update. Manually updated MG interface. So, I can see that

interface = nb.dcim.interfaces.get(device=device.name, name="GigabitEthernet1/50")
print("Interface name:", interface.name)
print("Interface tagged vlans:", interface.tagged_vlans)
vlan_obj = nb.ipam.vlans.get(vid = 9, group_id=3)
print("VLAN name:",vlan_obj.name, vlan_obj.vid , "| id: ",vlan_obj.id)
print("vlan id:", vlan_obj.id)
vlan_obj.update({'tagged_vlans': [27]})   //Used a different Vlan id manually 
print("tagged_vlans:", interface.tagged_vlans)

output:

Device name: switch-A
Interface name: GigabitEthernet1/50
Interface tagged vlans: [MG]
VLAN name: Inside 9 | id: 27
vlan id: 27
tagged_vlans: [MG]
markkuleinio commented 1 year ago

You code doesn't make any sense: You are calling .update() on a VLAN object, but there is no tagged_vlans attribute in a VLAN object:

>>> vlan = netbox.ipam.vlans.get(4)
>>> vlan
TestVLAN2
>>> vlan.tagged_vlans
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/markku/temp/pynetbox-venv/lib/python3.9/site-packages/pynetbox/core/response.py", line 308, in __getattr__
    raise AttributeError('object has no attribute "{}"'.format(k))
AttributeError: object has no attribute "tagged_vlans"
>>> import pprint
>>> pprint.pprint(dict(vlan))
{'comments': '',
 'created': '2023-06-17T14:08:47.562558+03:00',
 'custom_fields': {},
 'description': '',
 'display': 'TestVLAN2 (2)',
 'group': {'display': 'Group1',
           'id': 1,
           'name': 'Group1',
           'slug': 'group1',
           'url': 'http://netbox-test.lein.io/api/ipam/vlan-groups/1/'},
 'id': 4,
 'l2vpn_termination': None,
 'last_updated': '2023-06-17T14:08:47.562583+03:00',
 'name': 'TestVLAN2',
 'prefix_count': 0,
 'role': None,
 'site': None,
 'status': {'label': 'Active', 'value': 'active'},
 'tags': [],
 'tenant': None,
 'url': 'http://netbox-test.lein.io/api/ipam/vlans/4/',
 'vid': 2}
>>>

This works as expected:

>>> interface = netbox.dcim.interfaces.get(device="Test", name="eth0")
>>> interface.tagged_vlans
[TestVLAN2]
>>> interface.update({"tagged_vlans":[5,6]})
True
>>> interface = netbox.dcim.interfaces.get(device="Test", name="eth0")
>>> interface.tagged_vlans
[TestVLAN3, TestVLAN4]
>>>