Skip to content

Commit

Permalink
Merge pull request #3898 from dmbaturin/T6620
Browse files Browse the repository at this point in the history
vyos.configtree: T6620: allow list_nodes() to work on non-existent paths
  • Loading branch information
c-po authored Jul 29, 2024
2 parents 7724a5f + 30111fa commit ad0acad
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
9 changes: 6 additions & 3 deletions python/vyos/configtree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# configtree -- a standalone VyOS config file manipulation library (Python bindings)
# Copyright (C) 2018-2022 VyOS maintainers and contributors
# Copyright (C) 2018-2024 VyOS maintainers and contributors
#
# This library is free software; you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software Foundation;
Expand Down Expand Up @@ -290,15 +290,18 @@ def exists(self, path):
else:
return True

def list_nodes(self, path):
def list_nodes(self, path, path_must_exist=True):
check_path(path)
path_str = " ".join(map(str, path)).encode()

res_json = self.__list_nodes(self.__config, path_str).decode()
res = json.loads(res_json)

if res is None:
raise ConfigTreeError("Path [{}] doesn't exist".format(path_str))
if path_must_exist:
raise ConfigTreeError("Path [{}] doesn't exist".format(path_str))
else:
return []
else:
return res

Expand Down
8 changes: 2 additions & 6 deletions src/migration-scripts/openvpn/1-to-2
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
from vyos.configtree import ConfigTree

def migrate(config: ConfigTree) -> None:
if not config.exists(['interfaces', 'openvpn']):
# Nothing to do
return

ovpn_intfs = config.list_nodes(['interfaces', 'openvpn'])
for i in ovpn_intfs:
ovpn_intfs = config.list_nodes(['interfaces', 'openvpn'], path_must_exist=False)
for i in ovpn_intfs:
# Remove 'encryption cipher' and add this value to 'encryption ncp-ciphers'
# for server and client mode.
# Site-to-site mode still can use --cipher option
Expand Down
8 changes: 2 additions & 6 deletions src/migration-scripts/openvpn/2-to-3
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
from vyos.configtree import ConfigTree

def migrate(config: ConfigTree) -> None:
if not config.exists(['interfaces', 'openvpn']):
# Nothing to do
return

ovpn_intfs = config.list_nodes(['interfaces', 'openvpn'])
for i in ovpn_intfs:
ovpn_intfs = config.list_nodes(['interfaces', 'openvpn'], path_must_exist=False)
for i in ovpn_intfs:
mode = config.return_value(['interfaces', 'openvpn', i, 'mode'])
if mode != 'server':
# If it's a client or a site-to-site OpenVPN interface,
Expand Down
6 changes: 1 addition & 5 deletions src/migration-scripts/openvpn/3-to-4
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
from vyos.configtree import ConfigTree

def migrate(config: ConfigTree) -> None:
if not config.exists(['interfaces', 'openvpn']):
# Nothing to do
return

ovpn_intfs = config.list_nodes(['interfaces', 'openvpn'])
ovpn_intfs = config.list_nodes(['interfaces', 'openvpn'], path_must_exist=False)
for i in ovpn_intfs:
#Rename 'encryption ncp-ciphers' with 'encryption data-ciphers'
ncp_cipher_path = ['interfaces', 'openvpn', i, 'encryption', 'ncp-ciphers']
Expand Down

0 comments on commit ad0acad

Please sign in to comment.