netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
543 stars 167 forks source link

Cannot update mac address to an exsisting Interface #500

Closed HarishankarYellapragada closed 1 year ago

HarishankarYellapragada commented 1 year ago

I am sharing an example below with an error while updating mac_address.

Interface Nic 1 is an existing interface without any mac address. id used is interface id.

def update_device(device, mac_address, nic_name):
       nb = pynetbox.api(netbox_url, token=netbox_token)
       nic_id = nb.dcim.interfaces.get(device='server02', name='Nic 1').id
       print("Nic id: ",nic_id)

       mac_address = nb.dcim.interfaces.create(device='server202', name='Nic 1', mac_address= 'xx:xx:xx:xx:xx:xx', id= '21562')
       print(mac_address)
if __name__ == '__main__':
    update_device('server202', 'xx:xx:xx:xx:xx:xx', 'Nic 1')

output

Nic id:  21562
The request failed with code 400 Bad Request: {'device': ['Related objects must be referenced by numeric ID or by dictionary of attributes. Received an unrecognized value: server202'], 'type': ['This field is required.']}

Looking forward to fixing the mistake in my script

markkuleinio commented 1 year ago
>>> nic = netbox.dcim.interfaces.get(device="Test", name="eth0")
>>> print(nic)
eth0
>>> print(nic.mac_address)
None
>>> nic.mac_address = "11:22:33:44:55:66"
>>> nic.save()
True
>>> nic = netbox.dcim.interfaces.get(device="Test", name="eth0")
>>> print(nic.mac_address)
11:22:33:44:55:66
>>>
HarishankarYellapragada commented 1 year ago

Worked like a charm @markkuleinio