Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 80 additions & 64 deletions plugins/module_utils/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, module, pfsense=None):
self.obj = dict()

self.root_elt = self.pfsense.interfaces
self.interface_list = None
self.setup_interface_cmds = ""
self.setup_interface_pre_cmds = ""
self.gateways_changed = False
Expand Down Expand Up @@ -180,7 +181,10 @@ def _validate_params(self):
if params.get("mss") is not None and (params["mss"] < 576 or params["mss"] > 65535):
self.module.fail_json(msg="mss must be between 576 and 65535.")

interfaces = self._get_interface_list()
# The aggregate stages all VLANs before validating interfaces.
if self.interface_list is None:
self.interface_list = self._get_interface_list()
interfaces = self.interface_list
if params.get("interface") is not None:
if params["interface"] not in interfaces.keys():
self.module.fail_json(
Expand All @@ -202,10 +206,12 @@ def _validate_params(self):
if params["mtu"] < min_mtu or params["mtu"] > max_mtu:
self.module.fail_json(msg="mtu must be between {0} and {1}.".format(min_mtu, max_mtu))

media_modes = set(self._get_media_mode(params["interface"]))
media_modes.add("autoselect")
if params.get("speed_duplex") and params["speed_duplex"] not in media_modes:
self.module.fail_json(msg="For this interface, media mode may only be one the following: {0}".format(media_modes))
speed_duplex = params.get("speed_duplex")
if speed_duplex and speed_duplex != "autoselect":
media_modes = set(self._get_media_mode(params["interface"]))
if speed_duplex not in media_modes:
media_modes.add("autoselect")
self.module.fail_json(msg="For this interface, media mode may only be one the following: {0}".format(media_modes))

if params["ipv4_type"] == "static":
if params.get("ipv4_address") and not self.pfsense.is_ipv4_address(params["ipv4_address"]):
Expand Down Expand Up @@ -436,67 +442,77 @@ def _run_post(self):
def _get_interface_list(self):
# The configuration is read through config_get_path() when pfSense has it: pfSense Plus can serve its configuration
# from a controller (Netgate Nexus), and then leaves $config unset.
return self.pfsense.php(
"require_once('/etc/inc/interfaces.inc');"
"$cfg = function_exists('config_get_path') ? config_get_path('', []) : $config;"
"$portlist = get_interface_list();"
""
"/* add wireless clone interfaces */"
"if (is_array($cfg['wireless']) && is_array($cfg['wireless']['clone']) && count($cfg['wireless']['clone']))"
" foreach ($cfg['wireless']['clone'] as $clone) $portlist[$clone['cloneif']] = $clone;"
""
"/* add VLAN interfaces */"
"if (is_array($cfg['vlans']) && is_array($cfg['vlans']['vlan']) && count($cfg['vlans']['vlan']))"
" foreach ($cfg['vlans']['vlan'] as $vlan) $portlist[$vlan['vlanif']] = $vlan;"
""
"/* add Bridge interfaces */"
"if (is_array($cfg['bridges']) && is_array($cfg['bridges']['bridged']) && count($cfg['bridges']['bridged']))"
" foreach ($cfg['bridges']['bridged'] as $bridge) $portlist[$bridge['bridgeif']] = $bridge;"
""
"/* add GIF interfaces */"
"if (is_array($cfg['gifs']) && is_array($cfg['gifs']['gif']) && count($cfg['gifs']['gif']))"
" foreach ($cfg['gifs']['gif'] as $gif) $portlist[$gif['gifif']] = $gif;"
""
"/* add GRE interfaces */"
"if (is_array($cfg['gres']) && is_array($cfg['gres']['gre']) && count($cfg['gres']['gre']))"
" foreach ($cfg['gres']['gre'] as $gre) $portlist[$gre['greif']] = $gre;"
""
"/* add LAGG interfaces */"
"if (is_array($cfg['laggs']) && is_array($cfg['laggs']['lagg']) && count($cfg['laggs']['lagg']))"
" foreach ($cfg['laggs']['lagg'] as $lagg) {"
" $portlist[$lagg['laggif']] = $lagg;"
" /* LAGG members cannot be assigned */"
" $lagifs = explode(',', $lagg['members']);"
" foreach ($lagifs as $lagif)"
" if (isset($portlist[$lagif])) unset($portlist[$lagif]);"
" }"
""
"/* add QinQ interfaces */"
"if (is_array($cfg['qinqs']) && is_array($cfg['qinqs']['qinqentry']) && count($cfg['qinqs']['qinqentry']))"
" foreach ($cfg['qinqs']['qinqentry'] as $qinq) {"
" $portlist[\"{$qinq['vlanif']}\"] = $qinq;"
" /* QinQ members */"
" $qinqifs = explode(' ', $qinq['members']);"
" foreach ($qinqifs as $qinqif) $portlist[\"{$qinq['vlanif']}.{$qinqif}\"] = $qinqif;"
" }"
""
"/* add PPP interfaces */"
"if (is_array($cfg['ppps']) && is_array($cfg['ppps']['ppp']) && count($cfg['ppps']['ppp']))"
" foreach ($cfg['ppps']['ppp'] as $pppid => $ppp) $portlist[$ppp['if']] = $ppp;"
""
"if (is_array($cfg['openvpn'])) {"
" if (is_array($cfg['openvpn']['openvpn-server']))"
" foreach ($cfg['openvpn']['openvpn-server'] as $s) $portlist[\"ovpns{$s['vpnid']}\"] = $s;"
" if (is_array($cfg['openvpn']['openvpn-client']))"
" foreach ($cfg['openvpn']['openvpn-client'] as $c) $portlist[\"ovpnc{$c['vpnid']}\"] = $c;"
"}"
""
"$ipsec_descrs = interface_ipsec_vti_list_all();"
"foreach ($ipsec_descrs as $ifname => $ifdescr) $portlist[$ifname] = array('descr' => $ifdescr);"
""
"echo json_encode($portlist, JSON_PRETTY_PRINT);"
interfaces = (
self.pfsense.php(
"require_once('/etc/inc/interfaces.inc');"
"$cfg = function_exists('config_get_path') ? config_get_path('', []) : $config;"
"$portlist = get_interface_list();"
""
"/* add wireless clone interfaces */"
"if (is_array($cfg['wireless']) && is_array($cfg['wireless']['clone']) && count($cfg['wireless']['clone']))"
" foreach ($cfg['wireless']['clone'] as $clone) $portlist[$clone['cloneif']] = $clone;"
""
"/* add VLAN interfaces */"
"if (is_array($cfg['vlans']) && is_array($cfg['vlans']['vlan']) && count($cfg['vlans']['vlan']))"
" foreach ($cfg['vlans']['vlan'] as $vlan) $portlist[$vlan['vlanif']] = $vlan;"
""
"/* add Bridge interfaces */"
"if (is_array($cfg['bridges']) && is_array($cfg['bridges']['bridged']) && count($cfg['bridges']['bridged']))"
" foreach ($cfg['bridges']['bridged'] as $bridge) $portlist[$bridge['bridgeif']] = $bridge;"
""
"/* add GIF interfaces */"
"if (is_array($cfg['gifs']) && is_array($cfg['gifs']['gif']) && count($cfg['gifs']['gif']))"
" foreach ($cfg['gifs']['gif'] as $gif) $portlist[$gif['gifif']] = $gif;"
""
"/* add GRE interfaces */"
"if (is_array($cfg['gres']) && is_array($cfg['gres']['gre']) && count($cfg['gres']['gre']))"
" foreach ($cfg['gres']['gre'] as $gre) $portlist[$gre['greif']] = $gre;"
""
"/* add LAGG interfaces */"
"if (is_array($cfg['laggs']) && is_array($cfg['laggs']['lagg']) && count($cfg['laggs']['lagg']))"
" foreach ($cfg['laggs']['lagg'] as $lagg) {"
" $portlist[$lagg['laggif']] = $lagg;"
" /* LAGG members cannot be assigned */"
" $lagifs = explode(',', $lagg['members']);"
" foreach ($lagifs as $lagif)"
" if (isset($portlist[$lagif])) unset($portlist[$lagif]);"
" }"
""
"/* add QinQ interfaces */"
"if (is_array($cfg['qinqs']) && is_array($cfg['qinqs']['qinqentry']) && count($cfg['qinqs']['qinqentry']))"
" foreach ($cfg['qinqs']['qinqentry'] as $qinq) {"
" $portlist[\"{$qinq['vlanif']}\"] = $qinq;"
" /* QinQ members */"
" $qinqifs = explode(' ', $qinq['members']);"
" foreach ($qinqifs as $qinqif) $portlist[\"{$qinq['vlanif']}.{$qinqif}\"] = $qinqif;"
" }"
""
"/* add PPP interfaces */"
"if (is_array($cfg['ppps']) && is_array($cfg['ppps']['ppp']) && count($cfg['ppps']['ppp']))"
" foreach ($cfg['ppps']['ppp'] as $pppid => $ppp) $portlist[$ppp['if']] = $ppp;"
""
"if (is_array($cfg['openvpn'])) {"
" if (is_array($cfg['openvpn']['openvpn-server']))"
" foreach ($cfg['openvpn']['openvpn-server'] as $s) $portlist[\"ovpns{$s['vpnid']}\"] = $s;"
" if (is_array($cfg['openvpn']['openvpn-client']))"
" foreach ($cfg['openvpn']['openvpn-client'] as $c) $portlist[\"ovpnc{$c['vpnid']}\"] = $c;"
"}"
""
"$ipsec_descrs = interface_ipsec_vti_list_all();"
"foreach ($ipsec_descrs as $ifname => $ifdescr) $portlist[$ifname] = array('descr' => $ifdescr);"
""
"echo json_encode($portlist, JSON_PRETTY_PRINT);"
)
or {}
)

# Aggregate VLAN additions are staged in XML and applied before interface setup.
for vlan in self.pfsense.get_elements("vlans/vlan"):
vlanif = vlan.findtext("vlanif")
if vlanif:
interfaces.setdefault(vlanif, self.pfsense.element_to_dict(vlan))
return interfaces

def _get_media_mode(self, interface):
"""Find all possible media options for the interface"""
return self.pfsense.php(
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/plugins/modules/test_pfsense_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,43 @@ def test_aggregate_interface_gateway(self):
self.assert_xml_elt_dict("gateways", {"name": "UPSTREAM_GW"}, {"interface": "wan", "gateway": "192.0.2.1"})
self.assertEqual(self.xml_result.findtext("interfaces/wan/gateway"), "UPSTREAM_GW")

def test_aggregate_assigns_new_vlan_to_interface(self):
self.load_fixtures()

def php_response(command):
if "get_interface_list()" in command:
# The new VLAN exists only in the in-memory XML until the aggregate is saved.
return {"vmx0": {}}
return ["autoselect"]

self.php.side_effect = php_response
args = dict(
aggregated_vlans=[dict(vlan_id=101, interface="vmx0", descr="voice")],
aggregated_interfaces=[dict(descr="VOICE", interface="vmx0.101", enable=True)],
)
with set_module_args(dict(args, _ansible_check_mode=True)):
preview = self.changed(True)
self.assertFalse(self.load_xml_result())
self.phpshell.assert_not_called()

with set_module_args(args):
result = self.changed(True)
self.assertEqual(result["result_vlans"], preview["result_vlans"])
self.assertEqual(result["result_interfaces"], preview["result_interfaces"])
self.assert_find_vlan("vmx0", "101")
interface = self.find_xml_tag(self.xml_result.find("interfaces"), {"descr": "VOICE", "if": "vmx0.101"})
self.assertIsNotNone(interface)
self.phpshell.assert_called_once()
php = self.phpshell.call_args[0][0]
self.assertLess(php.index("interface_vlan_configure("), php.index(f"interface_configure('{interface.tag}', true)"))

self.phpshell.reset_mock()
with set_module_args(args):
result = self.changed(False)
self.assertEqual(result["result_vlans"], [])
self.assertEqual(result["result_interfaces"], [])
self.phpshell.assert_not_called()

def test_aggregate_aliases_purge(self):
"""test creation of a some aliases with purge"""
args = dict(
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/plugins/modules/test_pfsense_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ def test_interface_create_by_descr(self):
command = "create interface 'VOICE', port='vmx0.100'"
self.do_module_test(interface, command=command)

def test_interface_create_on_configured_vlan_with_no_php_inventory(self):
def php_response(command):
if "get_interface_list()" in command:
return None
return ["autoselect"]

self.php.side_effect = php_response
interface = dict(descr="VOICE", interface="vmx0.100")
self.do_module_test(interface, command="create interface 'VOICE', port='vmx0.100'")

def test_interface_create_static(self):
"""test creation of a new interface with a static ip"""
interface = dict(descr="VOICE", interface="vmx0.100", ipv4_type="static", ipv4_address="10.20.30.40", ipv4_prefixlen=24)
Expand Down Expand Up @@ -464,7 +474,10 @@ def test_interface_error_if(self):
interface = dict(
descr="wlan_1100", interface="vmx1.1200", enable=True, ipv4_type="static", ipv4_address="172.16.151.210", ipv4_prefixlen=24, ipv4_gateway="voice_gw"
)
msg = "vmx1.1200 can't be assigned. Interface may only be one the following: ['vmx0', 'vmx1', 'vmx2', 'vmx3', 'vmx0.100', 'vmx1.1100']"
msg = (
"vmx1.1200 can't be assigned. Interface may only be one the following: "
"['vmx0', 'vmx1', 'vmx2', 'vmx3', 'vmx0.100', 'vmx1.1100', 'vmx1.100', 'vmx2.1100']"
)
self.do_module_test(interface, failed=True, msg=msg)

def test_interface_error_eq(self):
Expand Down