Stricter checks to create/update an Ethernet switch and add tests

This commit is contained in:
grossmj committed 2022-12-24 08:20:51 +08:00
1 parent 5666b43ee0
commit 17e0b2e259
4 files changed
+481 -9

No files matched your search

@@ -150,12 +150,22 @@ def suspend_ethernet_switch(node: EthernetSwitch = Depends(dep_node)) -> None:
pass
@router.post("/{node_id}/reload", status_code=status.HTTP_204_NO_CONTENT)
def reload_ethernet_switch(node: EthernetSwitch = Depends(dep_node)) -> None:
"""
Reload an Ethernet switch.
This endpoint results in no action since Ethernet switch nodes are always on.
"""
pass
@router.post(
"/{node_id}/adapters/{adapter_number}/ports/{port_number}/nio",
status_code=status.HTTP_201_CREATED,
response_model=schemas.UDPNIO,
)
async def create_nio(
async def create_ethernet_switch_nio(
*,
adapter_number: int = Path(..., ge=0, le=0),
port_number: int,
@@ -169,7 +179,7 @@ async def create_nio(
@router.delete("/{node_id}/adapters/{adapter_number}/ports/{port_number}/nio", status_code=status.HTTP_204_NO_CONTENT)
async def delete_nio(
async def delete_ethernet_switch_nio(
*,
adapter_number: int = Path(..., ge=0, le=0),
port_number: int,
@@ -185,7 +195,7 @@ async def delete_nio(
@router.post("/{node_id}/adapters/{adapter_number}/ports/{port_number}/capture/start")
async def start_capture(
async def start_ethernet_switch_capture(
*,
adapter_number: int = Path(..., ge=0, le=0),
port_number: int,
@@ -205,7 +215,7 @@ async def start_capture(
@router.post(
"/{node_id}/adapters/{adapter_number}/ports/{port_number}/capture/stop", status_code=status.HTTP_204_NO_CONTENT
)
async def stop_capture(
async def stop_ethernet_switch_capture(
*,
adapter_number: int = Path(..., ge=0, le=0),
port_number: int,
@@ -166,7 +166,7 @@ class EthernetSwitch(Device):
"""
if ports != self._ports:
if len(self._nios) > 0 and len(ports) != len(self._ports):
raise NodeError("Can't modify a switch already connected.")
raise NodeError("Cannot change ports on a switch that is already connected.")
port_number = 0
for port in ports:
@@ -356,7 +356,7 @@ class EthernetSwitch(Device):
elif settings["type"] == "dot1q":
await self.set_dot1q_port(port_number, settings["vlan"])
elif settings["type"] == "qinq":
await self.set_qinq_port(port_number, settings["vlan"], settings.get("ethertype"))
await self.set_qinq_port(port_number, settings["vlan"], settings.get("ethertype", "0x8100"))
async def set_access_port(self, port_number, vlan_id):
"""
@@ -427,7 +427,7 @@ class EthernetSwitch(Device):
await self._hypervisor.send(
'ethsw set_qinq_port "{name}" {nio} {outer_vlan} {ethertype}'.format(
name=self._name, nio=nio, outer_vlan=outer_vlan, ethertype=ethertype if ethertype != "0x8100" else ""
)
).strip()
)
log.info(
@@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, validator
from typing import Optional, List
from uuid import UUID
from enum import Enum
@@ -42,9 +42,17 @@ class EthernetSwitchPort(BaseModel):
name: str
port_number: int
type: EthernetSwitchPortType = Field(..., description="Port type")
vlan: Optional[int] = Field(None, ge=1, description="VLAN number")
vlan: int = Field(..., ge=1, le=4094, description="VLAN number")
ethertype: Optional[EthernetSwitchEtherType] = Field(None, description="QinQ Ethertype")
@validator("ethertype")
def validate_ethertype(cls, v, values):
if v is not None:
if "type" not in values or values["type"] != EthernetSwitchPortType.qinq:
raise ValueError("Ethertype is only for QinQ port type")
return v
class TelnetConsoleType(str, Enum):
"""