Skip to content

Commit

Permalink
ndb.objects.interface: fix altnames before rename
Browse files Browse the repository at this point in the history
If the new name is in altnames, remove it from there first.

Bug-Url: #1178
  • Loading branch information
svinota committed Feb 26, 2024
1 parent 53fccb1 commit a28657a
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pyroute2/ndb/objects/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ def __setitem__(self, key, value):
if key == 'peer':
dict.__setitem__(self, key, value)
elif key == 'ifname':
if value in self['alt_ifname_list']:
self.del_altname(value)
if key in self and self.old_ifname is None:
self.old_ifname = self[key]
super(Interface, self).__setitem__(key, value)
Expand Down Expand Up @@ -984,9 +986,17 @@ def make_req(self, prime):
return req

@check_auth('obj:modify')
def apply_altnames(self, alt_ifname_setup, old_ifname=None):
alt_ifname_remove = set(self['alt_ifname_list']) - alt_ifname_setup
alt_ifname_add = alt_ifname_setup - set(self['alt_ifname_list'])
def apply_altnames(
self, alt_ifname_setup, alt_ifname_current, old_ifname=None
):
if alt_ifname_current is None:
# load the current state
self.load_from_system()
self.load_sql(set_state=False)
alt_ifname_current = set(self['alt_ifname_list'])

alt_ifname_remove = alt_ifname_current - alt_ifname_setup
alt_ifname_add = alt_ifname_setup - alt_ifname_current
for ifname in alt_ifname_remove:
self.sources[self['target']].api(
'link', 'property_del', index=self['index'], altname=ifname
Expand All @@ -995,6 +1005,7 @@ def apply_altnames(self, alt_ifname_setup, old_ifname=None):
self.sources[self['target']].api(
'link', 'property_add', index=self['index'], altname=ifname
)
# reload alt ifnames from the system to check the state
self.load_from_system()
self.load_sql(set_state=False)
if old_ifname is not None and old_ifname in self['alt_ifname_list']:
Expand All @@ -1012,9 +1023,15 @@ def apply(self, rollback=False, req_filter=None, mode='apply'):
remove = self.state.get() == 'remove'
alt_ifname_setup = set(self['alt_ifname_list'])
old_ifname = self.old_ifname if 'ifname' in self.changed else None
if 'alt_ifname_list' in self.changed:
self.changed.remove('alt_ifname_list')
try:
if (
old_ifname
and 'index' in self
and 'alt_ifname_list' in self.changed
):
self.apply_altnames(alt_ifname_setup, None)
if 'alt_ifname_list' in self.changed:
self.changed.remove('alt_ifname_list')
super(Interface, self).apply(rollback, req_filter, mode)
if setns:
self.load_value('target', self['net_ns_fd'])
Expand All @@ -1023,7 +1040,9 @@ def apply(self, rollback=False, req_filter=None, mode='apply'):
if spec:
self.state.set('system')
if not remove:
self.apply_altnames(alt_ifname_setup, old_ifname)
self.apply_altnames(
alt_ifname_setup, set(self['alt_ifname_list']), old_ifname
)

except NetlinkError as e:
if (
Expand Down

0 comments on commit a28657a

Please sign in to comment.