Refactor Mikrotik model and update template structure
- Changed the parsing method in `NodeConfig` to use `parse()` instead of `parse_config()`. - Updated the template loading path in `BaseDevice` to reflect the new directory structure. - Enhanced the `Mikrotik` class with new methods for `system`, `interfaces`, and `vlans`, including debug print statements. - Expanded the TTP template for Mikrotik to include structured variable definitions and groups for system, interfaces, and VLANs.
This commit is contained in:
@@ -16,7 +16,7 @@ class NodeConfig:
|
||||
self._device: type[BaseDevice] = device_registry.get(self._model.lower())
|
||||
if self._device is None:
|
||||
raise ValueError(f"Device model '{self._model}' not found in registry")
|
||||
self._parsed_data = self._device(self.text).parse_config()
|
||||
self._parsed_data = self._device(self.text).parse()
|
||||
|
||||
@cached_property
|
||||
def _response(self):
|
||||
|
||||
@@ -68,8 +68,10 @@ class BaseDevice(ABC):
|
||||
...
|
||||
|
||||
def _load_template(self):
|
||||
path = Path(__file__).parent / "template" / self.template
|
||||
path = Path(__file__).parent / "models" / "templates" / self.template
|
||||
if not path.exists():
|
||||
print("-" * 12)
|
||||
print(path)
|
||||
raise FileNotFoundError(f"Template {self.template} not found")
|
||||
return path.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
@@ -1,11 +1,27 @@
|
||||
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
|
||||
|
||||
|
||||
@register_parser(["routeros", "ros", "mikrotik"])
|
||||
class Mikrotik(BaseDevice):
|
||||
template = "mikrotik.ttp"
|
||||
|
||||
def system(self) -> "System":
|
||||
print(self._raw["system"])
|
||||
print("-" * 12)
|
||||
|
||||
def interfaces(self) -> "Interfaces":
|
||||
print(f"{self._raw["interfaces"]=}")
|
||||
print("-" * 12)
|
||||
|
||||
def vlans(self) -> list["Vlans"]:
|
||||
print(f"{self._raw["vlans"]=}")
|
||||
print("-" * 12)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mikr = Mikrotik()
|
||||
|
||||
@@ -1,7 +1,31 @@
|
||||
<doc>
|
||||
</doc>
|
||||
<var>
|
||||
</var>
|
||||
<template>
|
||||
<vars>
|
||||
default_system = {
|
||||
"model": "",
|
||||
"serial_number": ""
|
||||
}
|
||||
default_vlans = {
|
||||
"id": "",
|
||||
"name": ""
|
||||
}
|
||||
</vars>
|
||||
|
||||
<group>
|
||||
|
||||
<group name="system" default="default_system">
|
||||
# version: {{ version }} {{ ignore }}
|
||||
# model = {{ model }}
|
||||
# serial number = {{ serial_number }}
|
||||
</group>
|
||||
|
||||
<group name="interfaces">
|
||||
/ip address
|
||||
add address={{ address }} interface={{ interface }} network={{ network }}
|
||||
</group>
|
||||
|
||||
<group name="vlans" default="default_vlans">
|
||||
/vlans add {{ id }} name= {{ name }}
|
||||
</group>
|
||||
|
||||
</template>
|
||||
Reference in New Issue
Block a user