Skip to content

Commit

Permalink
netplan: set: allow the removal of an entire devtype subtree
Browse files Browse the repository at this point in the history
See LP: #1942930

Note that performance for this are horrendous, as we parse the whole
tree once for *each* removed netdef!
  • Loading branch information
schopin-pro committed Oct 4, 2021
1 parent a60f105 commit 006ee48
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion netplan/cli/commands/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ def split_tree_by_hint(self, set_tree) -> (str, dict):
for devtype in network:
if devtype in GLOBAL_KEYS:
continue # special handling of global keys down below
for netdef in network.get(devtype, []):
devtype_content = network.get(devtype, [])
# Special case: removal of a whole devtype.
# We replace the devtype null node with a dict of all defined netdefs
# set to None.
if devtype_content is None:
devtype_content = {dev: None for dev in utils.netplan_get_ids_for_devtype(devtype, self.root_dir)}
network[devtype] = devtype_content
for netdef in devtype_content:
hint = FALLBACK_HINT
filename = utils.netplan_get_filename_by_id(netdef, self.root_dir)
if filename:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ def test_set_delete(self):
self.assertNotIn('addresses:', out)
self.assertNotIn('eth0:', out)

def test_set_delete_subtree(self):
with open(self.path, 'w') as f:
f.write('''network:\n version: 2\n renderer: NetworkManager
ethernets:
eth0: {addresses: [1.2.3.4/24]}''')
self._set(['network.ethernets=null'])
self.assertTrue(os.path.isfile(self.path))
with open(self.path, 'r') as f:
out = f.read()
# print(out, flush=True) # debugging
self.assertIn('network:\n', out)
self.assertIn(' version: 2\n', out)
self.assertIn(' renderer: NetworkManager\n', out)
self.assertNotIn('ethernets:', out)

def test_set_delete_file(self):
with open(self.path, 'w') as f:
f.write('''network:
Expand Down

0 comments on commit 006ee48

Please sign in to comment.