diff options
author | zsdc <taras@vyos.io> | 2021-08-26 23:18:29 +0300 |
---|---|---|
committer | zsdc <taras@vyos.io> | 2021-08-26 23:18:29 +0300 |
commit | eb11d4b688d883d0c1d150b00eee40b54df42b32 (patch) | |
tree | 271446b3fbedca66da86afc42fc1d2d03d04dfb1 | |
parent | 4523e9c897b3fa8d12c1b16c830c01820fee5583 (diff) | |
download | vyos-1x-eb11d4b688d883d0c1d150b00eee40b54df42b32.tar.gz vyos-1x-eb11d4b688d883d0c1d150b00eee40b54df42b32.zip |
vyos.util: T3763: Optimized the check_port_availability function
`print` was removed or replaced to `ValueError`, where possible.
-rw-r--r-- | python/vyos/util.py | 12 | ||||
-rwxr-xr-x | src/conf_mode/interfaces-wireguard.py | 9 |
2 files changed, 9 insertions, 12 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index fc2834a97..93a2f6640 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -834,16 +834,13 @@ def check_port_availability(ipaddress, port, protocol): try: ipaddress = ip_address(ipaddress).compressed except: - print(f'The {ipaddress} is not a valid IPv4 or IPv6 address') - return + raise ValueError(f'The {ipaddress} is not a valid IPv4 or IPv6 address') if port not in range(1, 65536): - print(f'The port number {port} is not in the 1-65535 range') - return + raise ValueError(f'The port number {port} is not in the 1-65535 range') if protocol not in ['tcp', 'udp']: - print( + raise ValueError( f'The protocol {protocol} is not supported. Only tcp and udp are allowed' ) - return # check port availability try: @@ -854,7 +851,4 @@ def check_port_availability(ipaddress, port, protocol): server.server_close() return True except: - print( - f'The {protocol} port {port} on the {ipaddress} is busy or unavailable' - ) return False diff --git a/src/conf_mode/interfaces-wireguard.py b/src/conf_mode/interfaces-wireguard.py index ad3ddcba2..68181465e 100755 --- a/src/conf_mode/interfaces-wireguard.py +++ b/src/conf_mode/interfaces-wireguard.py @@ -74,9 +74,12 @@ def verify(wireguard): if 'peer' not in wireguard: raise ConfigError('At least one Wireguard peer is required!') - if 'port' in wireguard and check_port_availability( - '0.0.0.0', int(wireguard['port']), 'udp') is not True: - raise ConfigError('The port cannot be used for the interface') + listen_port = int(wireguard['port']) + if 'port' in wireguard and check_port_availability('0.0.0.0', listen_port, + 'udp') is not True: + raise ConfigError( + f'The UDP port {listen_port} is busy or unavailable and cannot be used for the interface' + ) # run checks on individual configured WireGuard peer for tmp in wireguard['peer']: |