Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Cover system models
Browse files Browse the repository at this point in the history
  • Loading branch information
jpkrajewski committed Mar 6, 2024
1 parent f1f2fb3 commit 30ca2cf
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from ipaddress import IPv6Address
from ipaddress import IPv4Address, IPv6Address
from typing import List, Literal, Optional, Union

from pydantic import AliasPath, BaseModel, ConfigDict, Field
Expand All @@ -13,21 +13,23 @@ class ServerItem(BaseModel):
extra="forbid",
populate_by_name=True,
)
name: Union[Variable, Global[IPv6Address]] = Field(..., description="Set hostname or IP address of server")
name: Union[Variable, Global[str], Global[IPv6Address], Global[IPv4Address]] = Field(
..., description="Set hostname or IP address of server"
)
key: Optional[Union[Variable, Global[int], Default[None]]] = Field(
None, description="Set authentication key for the server"
)
vpn: Union[Variable, Global[int], Default[int]] = Field(
default=as_default(0), description="Set VPN in which NTP server is located"
)
version: Union[Variable, Global[int], Default[int]] = Field(..., description="Set NTP version")
version: Union[Variable, Global[int], Default[int]] = Field(default=as_default(4), description="Set NTP version")
source_interface: Optional[Union[Variable, Global[str], Default[None]]] = Field(
None,
serialization_alias="sourceInterface",
validation_alias="sourceInterface",
description="Set interface to use to reach NTP server",
)
prefer: Union[Variable, Global[bool], Default[Literal[False]]] = Field(
prefer: Union[Variable, Global[bool], Default[bool]] = Field(
default=as_default(False), description="Variable this NTP server"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from typing import List

from catalystwan.api.configuration_groups.parcel import Global
Expand All @@ -10,57 +11,52 @@ class AAATemplateConverter:
@staticmethod
def create_parcel(name: str, description: str, template_values: dict) -> AAAParcel:
"""
Creates an AAA object based on the provided template values.
Creates an AAAParcel object based on the provided template values.
Args:
name (str): The name of the AAAParcel.
description (str): The description of the AAAParcel.
template_values (dict): A dictionary containing the template values.
Returns:
AAA: An AAA object with the provided template values.
AAAParcel: An AAAParcel object with the provided template values.
"""
template_values["name"] = name
template_values["description"] = description

delete_properties = (
"radius_client",
"radius_trustsec_group",
"rda_server_key",
"domain_stripping",
"auth_type",
"port",
"cts_auth_list",
)

if template_values.get("server_auth_order") is not None:
global_object = template_values["server_auth_order"]
template_values["server_auth_order"] = Global[List[str]](value=global_object.value.split(","))

if template_values.get("radius") is not None:
print(template_values["radius"])
radius_list = template_values["radius"]
for radius in radius_list:
servers = radius.get("server", {})
def assign_authorization_servers(auth_server_list: List) -> None:
for auth_server in auth_server_list:
servers = auth_server.get("server", {})
for server in servers:
key_enum = server.get("key_enum")
server["key_enum"] = Global[str](value=str(key_enum.value))

if template_values.get("tacacs") is not None:
tacacs_list = template_values["tacacs"]
for tacacs in tacacs_list:
servers = tacacs.get("server", {})
for server in servers:
key_enum = server.get("key_enum")
server["key_enum"] = Global[str](value=str(key_enum.value))

if template_values.get("accounting_rule") is not None:
accounting_rule = template_values["accounting_rule"]
for rule_item in accounting_rule:
def assign_rules(rules: List) -> None:
for rule_item in rules:
rule_item["group"] = Global[List[str]](value=rule_item["group"].value.split(","))

if template_values.get("authorization_rule") is not None:
authorization_rule = template_values["authorization_rule"]
for rule_item in authorization_rule:
rule_item["group"] = Global[List[str]](value=rule_item["group"].value.split(","))
parcel_values = deepcopy(template_values)
parcel_values["parcel_name"] = name
parcel_values["parcel_description"] = description

if server_auth_order := template_values.get("server_auth_order"):
parcel_values["server_auth_order"] = Global[List[str]](value=server_auth_order.value.split(","))

for prop in delete_properties:
if template_values.get(prop) is not None:
del template_values[prop]
for server in ["radius", "tacacs"]:
if auth_server_list := parcel_values.get(server):
assign_authorization_servers(auth_server_list)

for rule in ["accounting_rule", "authorization_rule"]:
if existing_rule := parcel_values.get(rule):
assign_rules(existing_rule)

for key in [
"radius_client",
"radius_trustsec_group",
"rda_server_key",
"domain_stripping",
"auth_type",
"port",
"cts_auth_list",
]:
parcel_values.pop(key, None)

return AAAParcel(**template_values)
return AAAParcel(**parcel_values)
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ class BannerTemplateConverter:
@staticmethod
def create_parcel(name: str, description: str, template_values: dict) -> BannerParcel:
"""
Creates an AAA object based on the provided template values.
Creates a BannerParcel object based on the provided template values.
Args:
name (str): The name of the BannerParcel.
description (str): The description of the BannerParcel.
template_values (dict): A dictionary containing the template values.
Returns:
AAA: An AAA object with the provided template values.
BannerParcel: A BannerParcel object with the provided template values.
"""
template_values["name"] = name
template_values["description"] = description
return BannerParcel(**template_values)
return BannerParcel(parcel_name=name, parcel_description=description, **template_values)
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from catalystwan.api.configuration_groups.parcel import Global
from copy import deepcopy

from catalystwan.api.configuration_groups.parcel import Global, as_default, as_global
from catalystwan.models.configuration.feature_profile.sdwan.system import BasicParcel
from catalystwan.models.configuration.feature_profile.sdwan.system.basic import ConsoleBaudRate
from catalystwan.utils.timezone import Timezone


class SystemToBasicTemplateConverter:
Expand All @@ -9,25 +12,63 @@ class SystemToBasicTemplateConverter:
@staticmethod
def create_parcel(name: str, description: str, template_values: dict) -> BasicParcel:
"""
Creates an AAA object based on the provided template values.
Converts the provided template values into a BasicParcel object.
Args:
name (str): The name of the BasicParcel.
description (str): The description of the BasicParcel.
template_values (dict): A dictionary containing the template values.
Returns:
AAA: An AAA object with the provided template values.
BasicParcel: A BasicParcel object with the provided template values.
"""
template_values["name"] = name
template_values["parcel_description"] = description
if template_values.get("console_baud_rate") is not None:
value = template_values["console_baud_rate"].value
if value == "":
value = "9600" # Default value for console baud rate
template_values["console_baud_rate"] = Global[ConsoleBaudRate](value=value)

if template_values.get("site_id") is not None:
del template_values["site_id"]
if template_values.get("system_ip") is not None:
del template_values["system_ip"]
if template_values.get("host_name") is not None:
del template_values["host_name"]
if template_values.get("enable") is not None:
del template_values["enable"]
return BasicParcel(**template_values)
parcel_values = deepcopy(template_values)
parcel_values = {
"parcel_name": name,
"parcel_description": description,
}

track_default_gateway = template_values.get("track_default_gateway", as_default(False)).value
if track_default_gateway == "":
track_default_gateway = False
parcel_values["track_default_gateway"] = as_global(track_default_gateway)

clock_timezone = template_values.get("timezone", as_default("UTC")).value
parcel_values["clock"] = {"timezone": Global[Timezone](value=clock_timezone)}

console_baud_rate = template_values.get("console_baud_rate", as_default("9600")).value
if console_baud_rate == "":
console_baud_rate = "9600" # Default value for console baud rate
parcel_values["console_baud_rate"] = Global[ConsoleBaudRate](value=console_baud_rate)

parcel_values["gps_location"] = {}

longitude = parcel_values.get("longitude", as_default("")).value
latitude = parcel_values.get("latitude", as_default("")).value
if longitude and latitude:
parcel_values["gps_location"]["longitude"] = longitude
parcel_values["gps_location"]["latitude"] = latitude

if mobile_number := parcel_values.get("mobile_number", []):
parcel_values["gps_location"]["geo_fencing"] = {
"enable": as_global(True),
"range": parcel_values.get("range", as_default(100)),
"sms": {"enable": as_global(True), "mobile_number": mobile_number},
}

# Remove unnecessary keys from template_values
for key in [
"timezone",
"longitude",
"latitude",
"mobile_number",
"range",
"site_id",
"system_ip",
"host_name",
"enable",
"tracker",
]:
parcel_values.pop(key, None)

return BasicParcel(**parcel_values)
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@


class BFDTemplateConverter:
supported_template_types = ("cisco_bfd",)
supported_template_types = ("cisco_bfd", "bfd-vedge")

@staticmethod
def create_parcel(name: str, description: str, template_values: dict) -> BFDParcel:
"""
Creates an BFD object based on the provided template values.
Creates a BFDParcel object based on the provided template values.
Args:
name (str): The name of the BFDParcel.
description (str): The description of the BFDParcel.
template_values (dict): A dictionary containing the template values.
Returns:
BFD: An BFD object with the provided template values.
BFDParcel: A BFDParcel object with the provided template values.
"""
template_values["name"] = name
template_values["description"] = description

if template_values.get("color") is not None:
template_values["colors"] = template_values["color"]
del template_values["color"]

return BFDParcel(**template_values)
parcel_values = {
"parcel_name": name,
"parcel_description": description,
"colors": template_values.get("color"),
}
return BFDParcel(**parcel_values) # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .global_ import GlobalTemplateConverter
from .logging_ import LoggingTemplateConverter
from .normalizer import template_definition_normalization
from .ntp import NTPTemplateConverter
from .omp import OMPTemplateConverter
from .security import SecurityTemplateConverter

Expand All @@ -29,6 +30,7 @@
GlobalTemplateConverter,
LoggingTemplateConverter,
OMPTemplateConverter,
NTPTemplateConverter,
]


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from copy import deepcopy

from catalystwan.api.configuration_groups.parcel import as_variable
from catalystwan.models.configuration.feature_profile.sdwan.system import GlobalParcel


Expand All @@ -10,19 +13,15 @@ def create_parcel(name: str, description: str, template_values: dict) -> GlobalP
Creates an Logging object based on the provided template values.
Returns:
Logging: An Logging object with the provided template values.
GlobalParcel: A GlobalParcel object with the provided template values.
"""
template_values["services_global"] = {}
template_values["services_global"]["services_ip"] = {}

keys_to_delete = []
for key, value in template_values.items():
template_values["services_global"]["services_ip"][key] = value
keys_to_delete.append(key)

for key in keys_to_delete:
del template_values[key]
values = deepcopy(template_values)
if source_intrf := values.get("source_intrf"):
values["source_intrf"] = as_variable(source_intrf.value)

template_values["name"] = name
template_values["description"] = description
return GlobalParcel(**template_values)
parcel_values = {
"parcel_name": name,
"parcel_description": description,
"services_global": {"services_ip": {key: value for key, value in values.items()}},
}
return GlobalParcel(**parcel_values) # type: ignore
Loading

0 comments on commit 30ca2cf

Please sign in to comment.