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

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sbasan committed Feb 21, 2024
1 parent 1824b8b commit 77edd2a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
6 changes: 3 additions & 3 deletions catalystwan/api/configuration_groups/parcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class _ParcelBase(BaseModel):
validation_alias="description",
description="Set the parcel description",
)
data: Optional[Any] = None
# data: Optional[Any] = None
_parcel_data_key: str = PrivateAttr(default="data")

@model_serializer(mode="wrap")
@model_serializer(mode="wrap", when_used="json")
def envelope_parcel_data(self, handler) -> Dict[str, Any]:
model_dict = handler(self)
model_dict[self._parcel_data_key] = {}
Expand All @@ -49,7 +49,7 @@ class OptionType(str, Enum):


class ParcelAttribute(BaseModel):
model_config = ConfigDict(extra="forbid")
model_config = ConfigDict(extra="forbid", populate_by_name=True)
option_type: OptionType = Field(serialization_alias="optionType", validation_alias="optionType")


Expand Down
14 changes: 13 additions & 1 deletion catalystwan/api/policy_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, List, Mapping, Optional, Type, overload
from typing import TYPE_CHECKING, Any, Dict, List, Mapping, Optional, Tuple, Type, overload
from uuid import UUID

from catalystwan.api.task_status_api import Task
Expand Down Expand Up @@ -637,6 +637,12 @@ def get(self, type: Type[AnyPolicyList], id: Optional[UUID] = None) -> Any:
return endpoints.get_lists_by_id(id=id)
return endpoints.get_policy_lists()

def get_all(self) -> List[AnyPolicyList]:
infos: List[AnyPolicyList] = []
for list_type, _ in POLICY_LIST_ENDPOINTS_MAP.items():
infos.extend(self.get(list_type))
return infos


class PolicyDefinitionsAPI:
def __init__(self, session: ManagerSession):
Expand Down Expand Up @@ -780,6 +786,12 @@ def get(self, type: Type[AnyPolicyDefinition], id: Optional[UUID] = None) -> Any
return endpoints.get_policy_definition(id=id)
return endpoints.get_definitions()

def get_all(self) -> List[Tuple[type, PolicyDefinitionInfo]]:
all_items: List[Tuple[type, PolicyDefinitionInfo]] = []
for definition_type, _ in POLICY_DEFINITION_ENDPOINTS_MAP.items():
all_items.extend([(definition_type, info) for info in self.get(definition_type)])
return all_items


class PolicyAPI:
"""This is exposing so called 'UX 1.0' API"""
Expand Down
4 changes: 2 additions & 2 deletions catalystwan/models/policy/centralized.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ def try_parse(cls, policy_definition):
# while POST /template/policy/vsmart requires a regular object
# it makes sense to reuse that model for both requests and present parsed data to the user
if isinstance(policy_definition, str):
return CentralizedPolicyDefinition.parse_raw(policy_definition)
return CentralizedPolicyDefinition.model_validate_json(policy_definition)
return policy_definition


class CentralizedPolicyEditPayload(PolicyEditPayload, CentralizedPolicy):
rid: Optional[str] = Field(default=None, serialization_alias="@rid", validation_alias="@rid")
rid: Optional[int] = Field(default=None, serialization_alias="@rid", validation_alias="@rid")


class CentralizedPolicyInfo(PolicyInfo, CentralizedPolicyEditPayload):
Expand Down
4 changes: 3 additions & 1 deletion catalystwan/models/policy/lists_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ class SLAClassListEntry(BaseModel):
latency: Optional[str] = None
loss: Optional[str] = None
jitter: Optional[str] = None
app_probe_class: Optional[UUID] = Field(serialization_alias="appProbeClass", validation_alias="appProbeClass")
app_probe_class: Optional[UUID] = Field(
default=None, serialization_alias="appProbeClass", validation_alias="appProbeClass"
)
fallback_best_tunnel: Optional[FallbackBestTunnel] = Field(
default=None, serialization_alias="fallbackBestTunnel", validation_alias="fallbackBestTunnel"
)
Expand Down
50 changes: 38 additions & 12 deletions catalystwan/workflows/config_migration.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import logging
from typing import Callable

from catalystwan.api.policy_api import POLICY_LIST_ENDPOINTS_MAP
from catalystwan.models.configuration.config_migration import UX1Config, UX2Config
from catalystwan.session import ManagerSession

logger = logging.getLogger(__name__)


def log_progress(task: str, completed: int, total: int) -> None:
logger.info("{task} {completed}/{total}")
logger.info(f"{task} {completed}/{total}")


def transform(ux1: UX1Config) -> UX2Config:
Expand All @@ -17,19 +18,44 @@ def transform(ux1: UX1Config) -> UX2Config:
return ux2


def collect_ux1_config(
session: ManagerSession, progress_callback: Callable[[str, int, int], None] = log_progress
) -> UX1Config:
def collect_ux1_config(session: ManagerSession, progress: Callable[[str, int, int], None] = log_progress) -> UX1Config:
ux1 = UX1Config()
# Policies part

"""Collect Policies"""
policy_api = session.api.policy
progress_callback("Collecting Policy Info", 3, 0)
# centralized_policy_ids = [info.policy_id for info in policy_api.centralized.get()]
for uid in [info.policy_id for info in policy_api.centralized.get()]:
ux1.policies.centralized_policies.append(policy_api.centralized.get(id=uid))
for uid in [info.policy_id for info in policy_api.localized.get()]:
ux1.policies.localized_policies.append(policy_api.localized.get(id=uid))
# Templates part
progress("Collecting Policy Info", 0, 3)

centralized_policy_ids = [info.policy_id for info in policy_api.centralized.get()]
progress("Collecting Policy Info", 1, 3)

localized_policy_ids = [info.policy_id for info in policy_api.localized.get()]
progress("Collecting Policy Info", 2, 3)

policy_definition_types_and_ids = [
(policy_type, info.definition_id) for policy_type, info in policy_api.definitions.get_all()
]
progress("Collecting Policy Info", 3, 3)

policy_list_types = POLICY_LIST_ENDPOINTS_MAP.keys()
for i, policy_list_type in enumerate(policy_list_types):
ux1.policies.policy_lists.extend(policy_api.lists.get(policy_list_type))
progress("Collecting Policy Lists", i + 1, len(policy_list_types))

for i, type_and_id in enumerate(policy_definition_types_and_ids):
ux1.policies.policy_definitions.append(policy_api.definitions.get(*type_and_id))
progress("Collecting Policy Definitions", i + 1, len(policy_definition_types_and_ids))

for i, cpid in enumerate(centralized_policy_ids):
ux1.policies.centralized_policies.append(policy_api.centralized.get(id=cpid))
progress("Collecting Centralized Policies", i + 1, len(centralized_policy_ids))

for i, lpid in enumerate(localized_policy_ids):
ux1.policies.localized_policies.append(policy_api.localized.get(id=lpid))
progress("Collecting Localized Policies", i + 1, len(localized_policy_ids))

ux1.policies.policy_lists = policy_api.lists.get_all()

"""Collect Templates"""
# TODO
return ux1

Expand Down
4 changes: 2 additions & 2 deletions examples/policies_configuration_guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
PrefixList,
RegionList,
SiteList,
SLAClassList,
TLOCList,
TrafficDataPolicy,
VPNList,
VPNMembershipPolicy,
)
from catalystwan.models.policy.lists import SLAClassList

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -184,7 +184,7 @@ def configure_groups_of_interest(api: PolicyAPI) -> List[ConfigItem]:
configured_items.append(ConfigItem(ClassMapList, class_map.name, class_map_id))

app_probe_class = AppProbeClassList(name="MyAppProbeClass")
app_probe_class.assign_forwarding_class("MyClassMap").add_color_mapping("3g", 5)
app_probe_class.assign_forwarding_class("MyClassMap").add_color_mapping("green", 5)
app_probe_class_id = api.lists.create(app_probe_class)
configured_items.append(ConfigItem(AppProbeClassList, app_probe_class.name, app_probe_class_id))

Expand Down

0 comments on commit 77edd2a

Please sign in to comment.