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

Progress logger in pusher class #531

Merged
merged 4 commits into from
Mar 20, 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
15 changes: 13 additions & 2 deletions catalystwan/models/configuration/config_migration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2024 Cisco Systems, Inc. and its affiliates

from typing import List, Set, Union
from typing import List, Set, Tuple, Union
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field
Expand All @@ -9,7 +9,7 @@
from catalystwan.api.template_api import FeatureTemplateInformation
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
from catalystwan.models.configuration.feature_profile.common import FeatureProfileCreationPayload, ProfileType
from catalystwan.models.configuration.feature_profile.sdwan.other import AnyOtherParcel
from catalystwan.models.configuration.feature_profile.sdwan.policy_object import AnyPolicyObjectParcel
from catalystwan.models.configuration.feature_profile.sdwan.service import AnyServiceParcel
Expand Down Expand Up @@ -109,3 +109,14 @@ class UX2Config(BaseModel):
profile_parcels: List[TransformedParcel] = Field(
default=[], serialization_alias="profileParcels", validation_alias="profileParcels"
)


class UX2ConfigRollback(BaseModel):
config_groups_ids: List[UUID] = Field(default_factory=list)
feature_profiles_ids: List[Tuple[UUID, ProfileType]] = Field(default_factory=list)

def add_config_group(self, config_group_id: UUID) -> None:
self.config_groups_ids.append(config_group_id)

def add_feature_profile(self, feature_profile_id: UUID, profile_type: ProfileType) -> None:
self.feature_profiles_ids.append((feature_profile_id, profile_type))
32 changes: 16 additions & 16 deletions catalystwan/utils/config_migration/creators/config_pusher.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from typing import Dict, List, Tuple, cast
from typing import Callable, Dict, List, cast
from uuid import UUID
from venv import logger

from pydantic import BaseModel, Field
from pydantic import BaseModel

from catalystwan.endpoints.configuration_group import ProfileId
from catalystwan.exceptions import CatalystwanException
from catalystwan.models.configuration.config_migration import TransformedFeatureProfile, TransformedParcel, UX2Config
from catalystwan.models.configuration.config_migration import (
TransformedFeatureProfile,
TransformedParcel,
UX2Config,
UX2ConfigRollback,
)
from catalystwan.models.configuration.feature_profile.common import ProfileType
from catalystwan.session import ManagerSession
from catalystwan.utils.config_migration.factories.feature_profile_api import FeatureProfileAPIFactory
Expand All @@ -18,23 +23,15 @@ class ConfigurationMapping(BaseModel):
parcel_map: Dict[UUID, TransformedParcel]


class UX2ConfigRollback(BaseModel):
config_groups_ids: List[UUID] = Field(default_factory=list)
feature_profiles_ids: List[Tuple[UUID, ProfileType]] = Field(default_factory=list)

def add_config_group(self, config_group_id: UUID) -> None:
self.config_groups_ids.append(config_group_id)

def add_feature_profile(self, feature_profile_id: UUID, profile_type: ProfileType) -> None:
self.feature_profiles_ids.append((feature_profile_id, profile_type))


class UX2ConfigPusher:
def __init__(self, session: ManagerSession, ux2_config: UX2Config) -> None:
def __init__(
self, session: ManagerSession, ux2_config: UX2Config, logger: Callable[[str, int, int], None]
) -> None:
self._session = session
self._config_map = self._create_config_map(ux2_config)
self._config_rollback = UX2ConfigRollback()
self._ux2_config = ux2_config
self._logger = logger

def _create_config_map(self, ux2_config: UX2Config) -> ConfigurationMapping:
return ConfigurationMapping(
Expand All @@ -50,12 +47,15 @@ def push(self) -> UX2ConfigRollback:
return self._config_rollback

def _create_config_groups(self):
for transformed_config_group in self._ux2_config.config_groups:
config_groups = self._ux2_config.config_groups
config_groups_length = len(config_groups)
for i, transformed_config_group in enumerate(config_groups):
config_group_payload = transformed_config_group.config_group
config_group_payload.profiles = self._create_feature_profile_and_parcels(
transformed_config_group.header.subelements
)
cg_id = self._session.endpoints.configuration_group.create_config_group(config_group_payload).id
self._logger("Creating Configuration Groups", i + 1, config_groups_length)
self._config_rollback.add_config_group(cg_id)

def _create_feature_profile_and_parcels(self, feature_profiles_ids: List[UUID]) -> List[ProfileId]:
Expand Down
2 changes: 1 addition & 1 deletion catalystwan/workflows/config_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def collect_ux1_config(session: ManagerSession, progress: Callable[[str, int, in
def push_ux2_config(
session: ManagerSession, config: UX2Config, progress: Callable[[str, int, int], None] = log_progress
) -> UX2ConfigRollback:
config_pusher = UX2ConfigPusher(session, config)
config_pusher = UX2ConfigPusher(session, config, progress)
rollback = config_pusher.push()
return rollback

Expand Down
Loading