Unverified Commit 7593296b authored by Caleb St. John's avatar Caleb St. John Committed by GitHub
Browse files

Merge pull request #78 from truenas/NAS-119204-22.12.1

NAS-119204 / 22.12.1 / Allow setting failover network interface options (by themylogin)
Showing with 65 additions and 24 deletions
+65 -24
......@@ -20,15 +20,36 @@ class NetworkInterfaceCreate(Steps):
method = StepsMethod.CREATE
def step1(self, data):
return list(filter(None, [
Header("Interface Settings"),
Input("type") if self.method == StepsMethod.CREATE else None,
with self.context.get_client() as c:
failover_licensed = c.call("failover.licensed")
result = [Header("Interface Settings")]
if self.method == StepsMethod.CREATE:
result.append(Input("type"))
result.extend([
Input("name"),
Input("description"),
Input("ipv4_dhcp"),
Input("ipv6_auto"),
])
if not failover_licensed:
result.extend([
Input("ipv4_dhcp"),
Input("ipv6_auto"),
])
result.extend([
Input("aliases", delegate=AliasesInputDelegate),
]))
])
if failover_licensed:
result.extend([
Header("Failover Settings"),
Input("failover_critical"),
Input("failover_group"),
Input("failover_aliases", delegate=lambda: AliasesInputDelegate(netmask=False)),
Input("failover_virtual_aliases", delegate=lambda: AliasesInputDelegate(netmask=False)),
])
return result
def step2(self, data):
with self.context.get_client() as c:
......@@ -42,7 +63,9 @@ class NetworkInterfaceCreate(Steps):
elif data["type"] == "LINK_AGGREGATION":
result.append(Header("Link Aggregation Settings"))
result.append(Input("lag_protocol", required=True))
result.append(Input("lag_ports", enum=c.call("interface.lag_ports_choices", data.get("name")), empty=False))
result.append(Input("lag_ports",
enum=c.call("interface.lag_ports_choices", data.get("name")),
empty=False))
result.append(Input("xmit_hash_policy"))
result.append(Input("lacpdu_rate"))
elif data["type"] == "VLAN":
......
# -*- coding=utf-8 -*-
import functools
import logging
from prompt_toolkit.styles import Style
......@@ -19,11 +20,16 @@ def split(value):
class AliasesInputDelegate(InputDelegate):
def __init__(self, netmask=True):
self.netmask = netmask
self.alias_to_str = functools.partial(alias_to_str, netmask=netmask)
self.str_to_alias = functools.partial(str_to_alias, netmask=netmask)
def create_input_app(self, title, text, value):
return input_dialog(
title,
" ".join(map(alias_to_str, value)) if value != undefined else "",
validator=AliasesInputDelegateValidator(),
" ".join(map(self.alias_to_str, value)) if value != undefined else "",
validator=AliasesInputDelegateValidator(self.netmask),
style=Style.from_dict({
"dialog.body text-area": "#ffffff bg:#4444ff",
}),
......@@ -31,21 +37,24 @@ class AliasesInputDelegate(InputDelegate):
def display_value(self, value):
if value:
return " ".join(map(alias_to_str, value))
return " ".join(map(self.alias_to_str, value))
else:
return "<empty list>"
def handle_value(self, value):
return list(map(str_to_alias, split(value)))
return list(map(self.str_to_alias, split(value)))
class AliasesInputDelegateValidator(Validator):
def __init__(self, netmask=True):
self.str_to_alias = functools.partial(str_to_alias, netmask=netmask)
def validate(self, document):
text = document.text
if text:
for v in split(text):
try:
str_to_alias(v)
self.str_to_alias(v)
except ValueError as e:
raise ValidationError(message=str(e))
......@@ -7,30 +7,39 @@ logger = logging.getLogger(__name__)
__all__ = ["alias_to_str", "str_to_alias"]
def alias_to_str(alias):
return (
alias["address"]
if {"INET": 32, "INET6": 128}[alias["type"]] == alias["netmask"]
else f"{alias['address']}/{alias['netmask']}"
)
def alias_to_str(alias, netmask=True):
if netmask:
return f"{alias['address']}/{alias['netmask']}"
else:
return alias["address"]
def str_to_alias(alias):
def str_to_alias(alias, netmask=True):
ip_network = ipaddress.ip_network(alias, strict=False)
if netmask:
if "/" not in alias:
raise ValueError("Please specify the IP address in CIDR notation")
else:
if "/" in alias:
raise ValueError("Please specify single IP address")
if isinstance(ip_network, ipaddress.IPv4Network):
return {
result = {
"type": "INET",
"address": alias.split("/")[0],
"netmask": ip_network.prefixlen,
}
if netmask:
result["netmask"] = ip_network.prefixlen
return result
if isinstance(ip_network, ipaddress.IPv6Network):
return {
result = {
"type": "INET6",
"address": alias.split("/")[0],
"netmask": ip_network.prefixlen,
}
if netmask:
result["netmask"] = ip_network.prefixlen
return result
raise ValueError(f"Unknown address type: {ip_network!r}")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment