robshakir / pyangbind

A plugin for pyang that creates Python bindings for a YANG model.
Other
204 stars 121 forks source link

Ability to delete leafs #353

Closed ianbarrere closed 3 months ago

ianbarrere commented 4 months ago

Hello,

It seems that there is no way to "unset" a leaf after it has been set, and I think this might be useful functionality. For example, looking at the MTU leaf in the OpenConfig interfaces module in particular. It seems to be initialized as 0 when populating a binding without an MTU set:

>>> print(interface.binding.config.mtu)
0
>>> interface.binding.get(filter=True)
{'name': 'eth1/1', 'config': {'name': 'eth1/1', 'description': 'spine03:eth1/1 - po3000', 'enabled': True}, 'ethernet': {'config': {'aggregate-id': 'po3000'}}}
>>>

If I then set it to some non-zero value, it shows up as I would expect:

>>> interface.binding.config.mtu = 9216
>>> interface.binding.config.mtu
9216
>>> interface.binding.get(filter=True)
{'name': 'eth1/1', 'config': {'name': 'eth1/1', 'mtu': 9216, 'description': 'spine03:eth1/1 - po3000', 'enabled': True}, 'ethernet': {'config': {'aggregate-id': 'po3000'}}}
>>>

But I can't remove it after it's been set. If I try "del interface.binding.config.mtu" I get an error about it not having a deleter:

>>> del interface.binding.config.mtu
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: property 'mtu' of 'YANGDynClass.<locals>.YANGBaseClass' object has no deleter
>>>

Even if I set it back to 0 it still shows up in the serialization as "mtu": 0. I know I can set it to the default of 1500, for example, but it would be nice to be able to remove the leaf entirely from the serialization like it was to begin with.

xavier-contreras commented 3 months ago

Have you tried interface.binding.config._unset_mtu()?

ianbarrere commented 3 months ago

Wow, did not even realize that was there. I must have missed it when looking through the code. This seems to do the trick, so I will close this issue. Thank you!