Make setting dns servers more robust
Dynamically find the uids of each forwarding server
This commit is contained in:
+17
-9
@@ -102,17 +102,25 @@ def validate_dns_servers(ctx: click.Context, param: str, value: Any) -> tuple[IP
|
|||||||
@click.pass_obj
|
@click.pass_obj
|
||||||
async def set_dns_servers(client: SagemcomClient, dns_servers: tuple[IPv4Address]) -> None:
|
async def set_dns_servers(client: SagemcomClient, dns_servers: tuple[IPv4Address]) -> None:
|
||||||
try:
|
try:
|
||||||
# disable IPCP servers
|
forwards = await client.get_value_by_xpath('Device/DNS/Relay/Forwardings')
|
||||||
for i in range(3, 5):
|
autos = {forward['uid'] for forward in forwards \
|
||||||
await client.set_value_by_xpath(f"Device/DNS/Relay/Forwardings/Forwarding[@uid={i}]/Enable", False)
|
if 'uid' in forward and \
|
||||||
# set and enable manual servers
|
'Alias' in forward and forward['Alias'].startswith('IPCP') and \
|
||||||
for i, dns_server in enumerate(dns_servers, start=1):
|
'Interface' in forward and forward['Interface'].endswith('[IP_DATA]') and \
|
||||||
await client.set_value_by_xpath(f"Device/DNS/Relay/Forwardings/Forwarding[@uid={i}]/DNSServer", dns_server)
|
'Enable' in forward and not forward['Enable']}
|
||||||
|
statics = {forward['uid'] for forward in forwards \
|
||||||
|
if 'uid' in forward and \
|
||||||
|
'Alias' in forward and forward['Alias'].startswith('STATIC') and \
|
||||||
|
'Interface' in forward and forward['Interface'].endswith(('[IP_DATA]', '[IP_BR_LAN]'))}
|
||||||
|
for uid in autos:
|
||||||
|
await client.set_value_by_xpath(f"Device/DNS/Relay/Forwardings/Forwarding[@uid={uid}]/Enable", False)
|
||||||
|
for uid, dns_server in zip(statics, dns_servers):
|
||||||
|
await client.set_value_by_xpath(f"Device/DNS/Relay/Forwardings/Forwarding[@uid={uid}]/DNSServer", dns_server)
|
||||||
await client.set_value_by_xpath(
|
await client.set_value_by_xpath(
|
||||||
f"Device/DNS/Relay/Forwardings/Forwarding[@uid={i}]/Interface",
|
f"Device/DNS/Relay/Forwardings/Forwarding[@uid={uid}]/Interface",
|
||||||
'Device/IP/Interfaces/Interface[IP_BR_LAN]' if dns_server.is_private else 'Device/IP/Interfaces/Interface[IP_DATA]'
|
'Device/IP/Interfaces/Interface[IP_BR_LAN]' if dns_server.is_private else 'Device/IP/Interfaces/Interface[IP_DATA]'
|
||||||
)
|
)
|
||||||
await client.set_value_by_xpath(f"Device/DNS/Relay/Forwardings/Forwarding[@uid={i}]/Enable", True)
|
await client.set_value_by_xpath(f"Device/DNS/Relay/Forwardings/Forwarding[@uid={uid}]/Enable", True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
click.echo(e, err=True)
|
click.echo(e, err=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user