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

Add device template model with id and devices attached #539

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions catalystwan/api/template_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class TemplateInformation(BaseModel):
devices_attached: int = Field(serialization_alias="devicesAttached", validation_alias="devicesAttached")
description: str = Field(serialization_alias="templateDescription", validation_alias="templateDescription")
last_updated_on: dt.datetime = Field(serialization_alias="lastUpdatedOn", validation_alias="lastUpdatedOn")
resource_group: Optional[str] = Field(None, serialization_alias="resourceGroup", validation_alias="resourceGroup")
resource_group: Optional[str] = Field(
default=None, serialization_alias="resourceGroup", validation_alias="resourceGroup"
)


class FeatureTemplateInformation(TemplateInformation):
Expand All @@ -91,7 +93,7 @@ class FeatureTemplateInformation(TemplateInformation):
device_type: List[str] = Field(serialization_alias="deviceType", validation_alias="deviceType")
version: str = Field(serialization_alias="templateMinVersion", validation_alias="templateMinVersion")
template_definiton: Optional[str] = Field(
None, serialization_alias="templateDefinition", validation_alias="templateDefinition"
default=None, serialization_alias="templateDefinition", validation_alias="templateDefinition"
)


Expand All @@ -102,8 +104,8 @@ class DeviceTemplateInformation(TemplateInformation):
template_class: str = Field(serialization_alias="templateClass", validation_alias="templateClass")
config_type: str = Field(serialization_alias="configType", validation_alias="configType")
template_attached: int = Field(serialization_alias="templateAttached", validation_alias="templateAttached")
draft_mode: Optional[str] = Field(None, serialization_alias="draftMode", validation_alias="draftMode")
device_role: Optional[str] = Field(None, serialization_alias="deviceRole", validation_alias="deviceRole")
draft_mode: Optional[str] = Field(default=None, serialization_alias="draftMode", validation_alias="draftMode")
device_role: Optional[str] = Field(default=None, serialization_alias="deviceRole", validation_alias="deviceRole")


class TemplatesAPI:
Expand Down
22 changes: 13 additions & 9 deletions catalystwan/api/templates/device_template/device_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from pathlib import Path
from typing import TYPE_CHECKING, Final, List
from typing import TYPE_CHECKING, ClassVar, List

from jinja2 import DebugUndefined, Environment, FileSystemLoader, meta # type: ignore
from pydantic import BaseModel, ConfigDict, Field, field_validator
Expand Down Expand Up @@ -42,13 +42,17 @@ class DeviceTemplate(BaseModel):
>>> session.api.templates.create(device_template)
"""

template_name: str = Field(alias="templateName")
template_description: str = Field(alias="templateDescription")
general_templates: List[GeneralTemplate] = Field(default=[], alias="generalTemplates")
device_role: str = Field(default="sdwan-edge", alias="deviceRole")
device_type: str = Field(alias="deviceType")
security_policy_id: str = Field(default="", alias="securityPolicyId")
policy_id: str = Field(default="", alias="policyId")
template_name: str = Field(serialization_alias="templateName", validation_alias="templateName")
template_description: str = Field(serialization_alias="templateDescription", validation_alias="templateDescription")
general_templates: List[GeneralTemplate] = Field(
default=[], serialization_alias="generalTemplates", validation_alias="generalTemplates"
)
device_role: str = Field(default="sdwan-edge", serialization_alias="deviceRole", validation_alias="deviceRole")
device_type: str = Field(serialization_alias="deviceType", validation_alias="deviceType")
security_policy_id: str = Field(
default="", serialization_alias="securityPolicyId", validation_alias="securityPolicyId"
)
policy_id: str = Field(default="", serialization_alias="policyId", validation_alias="policyId")

def get_flattened_general_templates(self) -> List[GeneralTemplate]:
"""
Expand Down Expand Up @@ -97,7 +101,7 @@ def parse_templates(cls, value):
output.append(template)
return output

payload_path: Final[Path] = Path(__file__).parent / "device_template_payload.json.j2"
payload_path: ClassVar[Path] = Path(__file__).parent / "device_template_payload.json.j2"

@classmethod
def get(self, name: str, session: ManagerSession) -> DeviceTemplate:
Expand Down
24 changes: 21 additions & 3 deletions catalystwan/models/configuration/config_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import BaseModel, ConfigDict, Field, model_validator
from typing_extensions import Annotated

from catalystwan.api.template_api import FeatureTemplateInformation
from catalystwan.api.template_api import FeatureTemplateInformation, TemplateInformation
from catalystwan.api.templates.device_template.device_template import DeviceTemplate
from catalystwan.endpoints.configuration_group import ConfigGroupCreationPayload
from catalystwan.models.configuration.feature_profile.common import FeatureProfileCreationPayload, ProfileType
Expand All @@ -27,6 +27,23 @@
]


class DeviceTemplateWithInfo(DeviceTemplate):
model_config = ConfigDict(populate_by_name=True)
template_id: str = Field(serialization_alias="templateId", validation_alias="templateId")
factory_default: bool = Field(serialization_alias="factoryDefault", validation_alias="factoryDefault")
devices_attached: int = Field(serialization_alias="devicesAttached", validation_alias="devicesAttached")

@staticmethod
def from_merged(template: DeviceTemplate, info: TemplateInformation) -> "DeviceTemplateWithInfo":
info_dict = template.model_dump()
return DeviceTemplateWithInfo(
template_id=info.id,
factory_default=info.factory_default,
devices_attached=info.devices_attached,
**info_dict
)


class UX1Policies(BaseModel):
model_config = ConfigDict(populate_by_name=True)
centralized_policies: List[CentralizedPolicyInfo] = Field(
Expand All @@ -50,14 +67,13 @@ class UX1Templates(BaseModel):
feature_templates: List[FeatureTemplateInformation] = Field(
default=[], serialization_alias="featureTemplates", validation_alias="featureTemplates"
)
device_templates: List[DeviceTemplate] = Field(
device_templates: List[DeviceTemplateWithInfo] = Field(
default=[], serialization_alias="deviceTemplates", validation_alias="deviceTemplates"
)


class UX1Config(BaseModel):
# All UX1 Configuration items - Mega Model
model_config = ConfigDict(populate_by_name=True)
policies: UX1Policies = UX1Policies()
templates: UX1Templates = UX1Templates()

Expand Down Expand Up @@ -114,6 +130,8 @@ class UX2Config(BaseModel):
@classmethod
def insert_parcel_type_from_headers(cls, values: Dict[str, Any]):
profile_parcels = values.get("profileParcels", [])
if not profile_parcels:
profile_parcels = values.get("profile_parcels", [])
for profile_parcel in profile_parcels:
profile_parcel["parcel"]["type_"] = profile_parcel["header"]["type"]
return values
Expand Down
11 changes: 7 additions & 4 deletions catalystwan/workflows/config_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from catalystwan.api.policy_api import POLICY_LIST_ENDPOINTS_MAP
from catalystwan.endpoints.configuration_group import ConfigGroupCreationPayload
from catalystwan.models.configuration.config_migration import (
DeviceTemplateWithInfo,
TransformedConfigGroup,
TransformedFeatureProfile,
TransformedParcel,
Expand Down Expand Up @@ -210,10 +211,12 @@ def collect_ux1_config(session: ManagerSession, progress: Callable[[str, int, in
ux1.templates.feature_templates = [t for t in template_api.get_feature_templates()]
progress("Collecting Feature Templates", 1, 2)

device_templates_ids = [t.id for t in template_api.get_device_templates()]
for i, dtid in enumerate(device_templates_ids):
ux1.templates.device_templates.append(template_api.get_device_template(dtid))
progress("Collecting Device Templates", i + 1, len(device_templates_ids))
device_templates_information = template_api.get_device_templates()
for i, device_template_information in enumerate(device_templates_information):
device_template = template_api.get_device_template(device_template_information.id)
device_template_with_info = DeviceTemplateWithInfo.from_merged(device_template, device_template_information)
ux1.templates.device_templates.append(device_template_with_info)
progress("Collecting Device Templates", i + 1, len(device_templates_information))

return ux1

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "catalystwan"
version = "0.31.2dev1"
version = "0.31.2dev2"
description = "Cisco Catalyst WAN SDK for Python"
authors = ["kagorski <[email protected]>"]
readme = "README.md"
Expand Down
Loading