From a938fe2d475099ababc3052c23154ebec093f3a0 Mon Sep 17 00:00:00 2001 From: IluaAir Date: Thu, 19 Feb 2026 00:31:54 +0300 Subject: [PATCH] Update Interfaces model and Mikrotik template for improved data handling - Made the `description` field in the `Interfaces` model optional to enhance flexibility. - Renamed `id` to `vlan_id` in the `Vlans` model for clarity. - Refactored the `Mikrotik` class methods to streamline raw data access and removed unnecessary print statements. - Updated the Mikrotik TTP template to reflect changes in variable names and improve overall structure for interface and VLAN configurations. --- oxi/interfaces/contract.py | 4 ++-- oxi/interfaces/models/mikrotik.py | 20 ++++++++------------ oxi/interfaces/models/templates/mikrotik.ttp | 18 +++++++++--------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/oxi/interfaces/contract.py b/oxi/interfaces/contract.py index 34b36df..fdd7f1e 100644 --- a/oxi/interfaces/contract.py +++ b/oxi/interfaces/contract.py @@ -6,7 +6,7 @@ class Interfaces(BaseModel): name: str ip_address: IPv4Address | None = None mask: int | None = None - description: str + description: str | None = None class System(BaseModel): @@ -16,7 +16,7 @@ class System(BaseModel): class Vlans(BaseModel): - id: int + vlan_id: int name: str | None = Field(default=None, alias="description") diff --git a/oxi/interfaces/models/mikrotik.py b/oxi/interfaces/models/mikrotik.py index 6b6f638..721905d 100644 --- a/oxi/interfaces/models/mikrotik.py +++ b/oxi/interfaces/models/mikrotik.py @@ -1,10 +1,6 @@ -import re -from typing import TYPE_CHECKING from oxi.interfaces import register_parser from oxi.interfaces.base import BaseDevice - -if TYPE_CHECKING: - from oxi.interfaces.contract import Interfaces, System, Vlans +from oxi.interfaces.contract import Interfaces, System, Vlans @register_parser(["routeros", "ros", "mikrotik"]) @@ -12,16 +8,16 @@ class Mikrotik(BaseDevice): template = "mikrotik.ttp" def system(self) -> "System": - print(self._raw["system"]) - print("-" * 12) + systems = self._raw.get("system") + return System(**systems) def interfaces(self) -> "Interfaces": - print(f"{self._raw["interfaces"]=}") - print("-" * 12) + print(self._raw.get("interfaces")) + return [Interfaces(**item) for item in self._raw.get("interfaces")] def vlans(self) -> list["Vlans"]: - raw = self._raw.get("vlans", []) - print(raw) + print(self._raw.get("vlans")) + return [Vlans(**item) for item in self._raw.get("vlans")] if __name__ == "__main__": @@ -29,4 +25,4 @@ if __name__ == "__main__": data = file.read() mikr = Mikrotik(data) mikr.parse() - print(mikr.load) + print(mikr) diff --git a/oxi/interfaces/models/templates/mikrotik.ttp b/oxi/interfaces/models/templates/mikrotik.ttp index 7cc958f..a6cbace 100644 --- a/oxi/interfaces/models/templates/mikrotik.ttp +++ b/oxi/interfaces/models/templates/mikrotik.ttp @@ -24,24 +24,24 @@ default_vlans = { /ip address ## not disabled and no comment -add address={{ ip | _start_ }}/{{ mask }} interface={{ interface }} network={{ network }} +add address={{ ip_address | _start_ }}/{{ mask }} interface={{ name }} network={{ network }} ## not disabled and comment with/without quotes -add address={{ ip | _start_ }}/{{ mask }} comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"')}} interface={{ interface }} network={{ network }} +add address={{ ip_address | _start_ }}/{{ mask }} comment={{ description | ORPHRASE | exclude("disabled=") | strip('"')}} interface={{ name }} network={{ network }} ## disabled no comment -add address={{ ip | _start_ }}/{{ mask }} disabled={{ disabled | replace("yes","True") | strip('"')}} interface={{ interface }} network={{ network }} +add address={{ ip_address | _start_ }}/{{ mask }} disabled={{ disabled | replace("yes","True") | strip('"')}} interface={{ name }} network={{ network }} ## disabled with comment with/without quotes -add address={{ ip | _start_ }}/{{ mask }} comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"') }} disabled={{ disabled }} interface={{ interface }} network={{ network }} +add address={{ ip_address | _start_ }}/{{ mask }} comment={{ description | ORPHRASE | exclude("disabled=") | strip('"') }} disabled={{ disabled }} interface={{ name }} network={{ network }} -/interface vlan {{ _start_ }} +/interface vlan ## not disabled and no comment -add interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }} +add interface={{ interface | _start_ }} name={{ name | ORPHRASE | strip('"') }} vlan-id={{ vlan_id }} ## not disabled and comment with/without quotes -add comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"')}} interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }} +add comment={{ comment | _start_ | ORPHRASE | exclude("disabled=") | strip('"')}} interface={{ interface }} name={{ name | ORPHRASE | strip('"') }} vlan-id={{ vlan_id }} ## disabled with comment with/without quotes -add comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"')}} disabled={{ disabled }} interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }} +add comment={{ comment | _start_ | ORPHRASE | exclude("disabled=") | strip('"')}} disabled={{ disabled | replace("yes","True") | strip('"') }} interface={{ interface }} name={{ name | ORPHRASE | strip('"') }} vlan-id={{ vlan_id }} ## disabled no comment -add interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }} disabled={{ disabled }} +add interface={{ interface | _start_ }} name={{ name | ORPHRASE | strip('"') }} vlan-id={{ vlan_id }} disabled={{ disabled | replace("yes","True") | strip('"') }}