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

Commit

Permalink
Push UX2 - Create config group and feature profiles (#492)
Browse files Browse the repository at this point in the history
* Push UX2 - Create config group and feature profiles

* Simplify push_ux2_config

* UX2Config rework

* Refactor code for template definition normalization

This commit refactors the code responsible for normalizing template definitions by implementing improved type annotations, better function organization, and enhanced error handling. Key changes include:

1. Refactoring the to_snake_case function to use the str.replace method for kebab-case to snake_case conversion.
2. Improving the cast_value_to_global function to handle different types of input values, including lists and IP addresses, and utilizing IPv4Address and IPv6Address from the ipaddress module.
3. Enhancing the transform_dict function to handle nested dictionaries and lists, casting leaf values to global types recursively.
4. Updating the template_definition_normalization function to utilize the refactored transform_dict function for key transformation and value normalization.

These changes improve code readability, maintainability, and robustness, providing a more efficient and reliable solution for template definition normalization.

* Add tests for normalization and Literal casting

---------

Co-authored-by: Kuba <[email protected]>
Co-authored-by: sbasan <[email protected]>
Co-authored-by: Jakub Krajewski <[email protected]>
  • Loading branch information
4 people authored Feb 26, 2024
1 parent e79a479 commit 1ecfc0d
Show file tree
Hide file tree
Showing 23 changed files with 567 additions and 240 deletions.
16 changes: 11 additions & 5 deletions catalystwan/api/config_group_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional, Union
from uuid import UUID

from catalystwan.typed_list import DataSequence

if TYPE_CHECKING:
from catalystwan.session import ManagerSession

from catalystwan.endpoints.configuration_group import (
ConfigGroup,
ConfigGroupAssociatePayload,
ConfigGroupCreationPayload,
ConfigGroupCreationResponse,
Expand All @@ -14,7 +18,6 @@
ConfigGroupDisassociateResponse,
ConfigGroupEditPayload,
ConfigGroupEditResponse,
ConfigGroupResponsePayload,
ConfigGroupVariablesCreatePayload,
ConfigGroupVariablesCreateResponse,
ConfigGroupVariablesEditPayload,
Expand Down Expand Up @@ -108,11 +111,14 @@ def edit(

return self.endpoint.edit_config_group(config_group_id=cg_id, payload=payload)

def get(self) -> ConfigGroupResponsePayload:
def get(self, group_id: Optional[UUID] = None) -> Union[DataSequence[ConfigGroup], ConfigGroup, None]:
"""
Gets list of existing config-groups
Gets list of existing config-groups or single config-group with given ID
If given ID is not correct return None
"""
return self.endpoint.get()
if group_id is None:
return self.endpoint.get()
return self.endpoint.get().filter(id=group_id).single_or_default()

def update_variables(self, cg_id: str, solution: Solution, device_variables: list) -> None:
"""
Expand Down
19 changes: 17 additions & 2 deletions catalystwan/endpoints/configuration_group.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# mypy: disable-error-code="empty-body"
from datetime import datetime
from typing import List, Optional
from uuid import UUID

from pydantic import BaseModel, Field

Expand All @@ -11,7 +12,7 @@


class ProfileId(BaseModel):
id: str
id: UUID


# TODO Get mode from schema
Expand All @@ -35,10 +36,24 @@ class FeatureProfile(BaseModel):


class ConfigGroup(BaseModel):
id: UUID
name: str
description: Optional[str]
solution: Solution
profiles: Optional[List[FeatureProfile]]
source: Optional[str] = None
state: Optional[str] = None
devices: List = Field(default=[])
created_by: Optional[str] = Field(alias="createdBy")
last_updated_by: Optional[str] = Field(alias="lastUpdatedBy")
created_on: Optional[datetime] = Field(alias="createdOn")
last_updated_on: Optional[datetime] = Field(alias="lastUpdatedOn")
version: int
number_of_devices: int = Field(alias="numberOfDevices")
number_of_devices_up_to_date: int = Field(alias="numberOfDevicesUpToDate")
origin: Optional[str]
topology: Optional[str] = None
full_config_cli: bool = Field(alias="fullConfigCli")


class ConfigGroupResponsePayload(BaseModel):
Expand Down Expand Up @@ -104,7 +119,7 @@ class ConfigGroupDisassociateResponse(BaseModel):


class ConfigGroupCreationResponse(BaseModel):
id: str
id: UUID


class EditedProfileId(BaseModel):
Expand Down
26 changes: 12 additions & 14 deletions catalystwan/models/configuration/config_migration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import List, Union
from typing import List, Literal, Union

from pydantic import BaseModel, ConfigDict, Field
from typing_extensions import Annotated

from catalystwan.api.template_api import DeviceTemplateInformation, FeatureTemplateInformation
from catalystwan.endpoints.configuration_group import ConfigGroup
from catalystwan.models.configuration.feature_profile.common import FeatureProfileCreationPayload
from catalystwan.models.configuration.feature_profile.sdwan.policy_object import AnyPolicyObjectParcel
from catalystwan.models.configuration.feature_profile.sdwan.system import AnySystemParcel
from catalystwan.models.policy import (
Expand Down Expand Up @@ -49,6 +47,14 @@ class UX1Templates(BaseModel):
devices: List[DeviceTemplateInformation] = Field(default=[])


class ConfigGroupPreset(BaseModel):
config_group_name: str = Field(serialization_alias="name", validation_alias="name")
solution: Literal["sdwan"] = "sdwan"
profile_parcels: List[AnyParcel] = Field(
default=[], serialization_alias="profileParcels", validation_alias="profileParcels"
)


class UX1Config(BaseModel):
# All UX1 Configuration items - Mega Model
model_config = ConfigDict(populate_by_name=True)
Expand All @@ -59,15 +65,7 @@ class UX1Config(BaseModel):
class UX2Config(BaseModel):
# All UX2 Configuration items - Mega Model
model_config = ConfigDict(populate_by_name=True)
config_groups: List[ConfigGroup] = Field(
default=[], serialization_alias="configurationGroups", validation_alias="configurationGroups"
)
policy_groups: List[ConfigGroup] = Field(
default=[], serialization_alias="policyGroups", validation_alias="policyGroups"
)
feature_profiles: List[FeatureProfileCreationPayload] = Field(
default=[], serialization_alias="featureProfiles", validation_alias="featureProfiles"
)
profile_parcels: List[AnyParcel] = Field(
default=[], serialization_alias="profileParcels", validation_alias="profileParcels"
# TODO: config group name
config_group_presets: List[ConfigGroupPreset] = Field(
default=[], serialization_alias="configGroupPresets", validation_alias="configGroupPresets"
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
from typing import List, Mapping, Union

from .aaa import AAA
from .bfd import BFD
from .aaa import AAAParcel
from .bfd import BFDParcel
from .literals import SYSTEM_LITERALS
from .logging_parcel import LoggingParcel

SYSTEM_PAYLOAD_ENDPOINT_MAPPING: Mapping[type, str] = {
AAA: "aaa",
BFD: "bfd",
AAAParcel: "aaa",
BFDParcel: "bfd",
}

AnySystemParcel = Union[AAA, BFD]
AnySystemParcel = Union[AAAParcel, BFDParcel, LoggingParcel]

__all__ = ["AAA", "BFD", "AnySystemParcel", "SYSTEM_PAYLOAD_ENDPOINT_MAPPING"]
__all__ = [
"AAAParcel",
"BFDParcel",
"LoggingParcel",
"AnySystemParcel",
"SYSTEM_LITERALS",
"SYSTEM_PAYLOAD_ENDPOINT_MAPPING",
]


def __dir__() -> "List[str]":
Expand Down
Loading

0 comments on commit 1ecfc0d

Please sign in to comment.