Add minimal defaults modification based on cnfvar type/mode
authorPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Thu, 12 May 2022 04:00:58 +0000 (07:00 +0300)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 19 May 2022 09:13:27 +0000 (11:13 +0200)
Some cnfvars like providers have different modes and some cnfvars
like nics have differnet types that change a large number of default
child variables. This in turn results in large modification overhead
when changing their type/mode, the largest of which could be handled
by the templates themselves.

src/cnfvar/templates.py

index e36854b..c59bc55 100644 (file)
@@ -280,6 +280,14 @@ def nic(name, instance=-1, **kwargs):
     log.info(f"Generating a nic cnfvar")
     nic_cnf = template("nic", "", instance=instance, defaults=nic_defaults, **kwargs)
     nic_cnf.children.single_with_name("nic_comment").value = name
+    if nic_cnf.children.single_with_name("nic_type").value in ["NATLAN", "PUBLICLAN", "PROXYARP"]:
+        nic_cnf.add_child("nic_lan_ip", "192.168.1.1")
+        nic_cnf.add_child("nic_lan_netmask", "255.255.255.0")
+        nic_cnf.add_child("nic_lan_dns_relaying_allowed", "0")
+        nic_cnf.add_child("nic_lan_email_relaying_allowed", "0")
+        nic_cnf.add_child("nic_lan_nat_into", "0")
+        nic_cnf.add_child("nic_lan_proxy_profile_ref", "-1")
+        nic_cnf.add_child("nic_lan_firewall_ruleset_ref", "1")
     return nic_cnf
 
 
@@ -310,6 +318,14 @@ def provider(name, instance=-1, **kwargs):
     log.info(f"Generating a provider {name} cnfvar")
     provider_cnf = template("provider", name, instance=instance,
                             defaults=provider_defaults, **kwargs)
+    # the validation of the LOCALIP will not be ignored despite choosing a different mode
+    if provider_cnf.children.single_with_name("provider_mode").value not in ["ROUTER", "GWINLAN"]:
+        provider_cnf.children.remove_where(lambda c: c.name == "provider_ip")
+        provider_cnf.children.remove_where(lambda c: c.name == "provider_netmask")
+    if provider_cnf.children.single_with_name("provider_mode").value != "ROUTER":
+        provider_cnf.children.remove_where(lambda c: c.name == "provider_localip")
+    if provider_cnf.children.single_with_name("provider_dns_mode").value != "IP":
+        provider_cnf.children.remove_where(lambda c: c.name == "provider_dns")
     return provider_cnf
 
 
@@ -325,6 +341,12 @@ def port_forwarding(name, instance=-1, **kwargs):
     log.info(f"Generating a port forwarding {name} cnfvar")
     port_forwarding_cnf = template("port_forwarding", name, instance=instance,
                                    defaults=port_forwarding_defaults, **kwargs)
+    if port_forwarding_cnf.children.single_with_name("port_forwarding_protocol_type").value == "OTHER":
+        port_forwarding_cnf.children.remove_where(lambda c: c.name == "port_forwarding_src_port")
+        port_forwarding_cnf.children.remove_where(lambda c: c.name == "port_forwarding_dst_port")
+        port_forwarding_cnf.children.remove_where(lambda c: c.name == "port_forwarding_src_port_end")
+        port_forwarding_cnf.children.remove_where(lambda c: c.name == "port_forwarding_dst_port_end")
+        port_forwarding_cnf.add_child("port_forwarding_protocol_num", "47")
     return port_forwarding_cnf
 
 
@@ -385,4 +407,10 @@ def vpnconn(name, instance=-1, **kwargs):
     log.info(f"Generating a vpn connection {name} cnfvar")
     vpnconn_cnf = template("vpnconn", name, instance=instance,
                            defaults=vpnconn_defaults, **kwargs)
+    if vpnconn_cnf.children.single_with_name("vpnconn_lan_type").value not in ["NIC", "CUSTOM"]:
+        vpnconn_cnf.children.remove_where(lambda c: c.name == "vpnconn_lan_net")
+    if vpnconn_cnf.children.single_with_name("vpnconn_remote_type").value != "CUSTOM":
+        vpnconn_cnf.children.remove_where(lambda c: c.name == "vpnconn_remote_net")
+    if vpnconn_cnf.children.single_with_name("vpnconn_remote_type").value != "MODECONFIG":
+        vpnconn_cnf.children.remove_where(lambda c: c.name == "vpnconn_remote_modeconfig_ip")
     return vpnconn_cnf