Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit f1bedbb

Browse files
authored
Merge pull request #3 from cisco-open/fix/cisco_vpn_model
Fix Cisco Vpn Feature Template model
2 parents cdb3db1 + 17be01f commit f1bedbb

File tree

2 files changed

+62
-53
lines changed

2 files changed

+62
-53
lines changed

catalystwan/api/templates/feature_template_field.py

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -115,61 +115,59 @@ def nest_value_in_output(value: Any) -> dict:
115115
)
116116
return nest_value_in_output(vip_variable.model_dump(by_alias=True, exclude_none=True))
117117

118+
if value is not None and not self.children:
119+
output["vipType"] = vip_type or FeatureTemplateOptionType.CONSTANT.value
120+
output["vipValue"] = json_dumped_value
121+
elif value is not None and self.children:
122+
output["vipType"] = vip_type or FeatureTemplateOptionType.CONSTANT.value
123+
children_output = []
124+
for obj in value:
125+
obj_json_dump = obj.model_dump(mode="json")
126+
child_payload: dict = {}
127+
for child in self.children: # Child in schema
128+
obj: FeatureTemplate # type: ignore
129+
130+
# We are searching for the model field that
131+
# corresponds to the child element (field from vManage schema)
132+
# If this line fails, it means that the schema from vManage has more fields than the model
133+
# and the model needs to be updated (add missing fields)
134+
model_tuple = next(
135+
filter(
136+
lambda f: (
137+
f[1].alias == child.key
138+
or get_extra_field(f[1], "vmanage_key") == child.key
139+
or f[0] == child.key
140+
),
141+
obj.model_fields.items(),
142+
)
143+
)
144+
# We get the pydantic model field and the value from the object
145+
model_field = model_tuple[1]
146+
obj_value = getattr(obj, model_tuple[0])
147+
obj_json_value = obj_json_dump.get(model_tuple[0])
148+
po = get_extra_field(model_field, "priority_order")
149+
vip_type = get_extra_field(model_field, "vip_type")
150+
# After we get the data, we create the payload by populating the schema
151+
# Then we merge the payload to the output
152+
merge(
153+
child_payload,
154+
child.payload_scheme(
155+
obj_value,
156+
json_dumped_value=obj_json_value,
157+
priority_order=po,
158+
vip_type=vip_type,
159+
),
160+
)
161+
if priority_order:
162+
child_payload.update({"priority-order": priority_order})
163+
children_output.append(child_payload)
164+
output["vipValue"] = children_output
118165
else:
119-
if value is not None:
120-
output["vipType"] = vip_type or FeatureTemplateOptionType.CONSTANT.value
121-
if self.children:
122-
children_output = []
123-
for obj in value: # obj is User, atomic value. Loop every child
124-
obj_json_dump = obj.model_dump(mode="json")
125-
child_payload: dict = {}
126-
for child in self.children: # Child in schema
127-
obj: FeatureTemplate # type: ignore
128-
model_tuple = next(
129-
filter(
130-
lambda f: get_extra_field(f[1], "data_path", []) == child.dataPath
131-
and (
132-
f[1].alias == child.key
133-
or get_extra_field(f[1], "vmanage_key") == child.key
134-
or f[0] == child.key
135-
),
136-
obj.model_fields.items(),
137-
)
138-
)
139-
model_field = model_tuple[1]
140-
obj_value = getattr(obj, model_tuple[0])
141-
obj_json_value = obj_json_dump.get(model_tuple[0])
142-
po = get_extra_field(model_field, "priority_order")
143-
vip_type = get_extra_field(model_field, "vip_type")
144-
merge(
145-
child_payload,
146-
child.payload_scheme(
147-
obj_value,
148-
json_dumped_value=obj_json_value,
149-
priority_order=po,
150-
vip_type=vip_type,
151-
),
152-
)
153-
if priority_order:
154-
child_payload.update({"priority-order": priority_order})
155-
children_output.append(child_payload)
156-
output["vipValue"] = children_output
157-
else:
158-
output["vipValue"] = json_dumped_value
166+
if value is None or "default" in self.dataType:
167+
return {}
159168
else:
160-
if value is None:
161-
return {}
162-
if "default" in self.dataType:
163-
return {}
164-
# output["vipValue"] = self.dataType["default"] if value is None else value
165-
# output["vipType"] = self.defaultOption.value
166-
else:
167-
output["vipValue"] = []
168-
output["vipType"] = FeatureTemplateOptionType.IGNORE.value
169-
170-
# TODO
171-
# DataType to dataclass Model
172-
# No default values for everything
169+
output["vipValue"] = []
170+
output["vipType"] = FeatureTemplateOptionType.IGNORE.value
173171

174172
if self.primaryKeys:
175173
output["vipPrimaryKey"] = self.primaryKeys

catalystwan/api/templates/models/cisco_vpn_model.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ class NextHopWithTrack(FeatureTemplateValidator):
7777
tracker: str
7878

7979

80+
class RouteInterface(FeatureTemplateValidator):
81+
interface_name: str = Field(json_schema_extra={"vmanage_key": "interface-name"})
82+
interface_next_hop: Optional[List[NextHop]] = Field(
83+
default=None, json_schema_extra={"vmanage_key": "interface-next-hop", "priority_order": ["address", "distance"]}
84+
)
85+
model_config = ConfigDict(populate_by_name=True)
86+
87+
8088
class Routev4(FeatureTemplateValidator):
8189
prefix: Optional[str] = None
8290
next_hop: Optional[List[NextHop]] = Field(
@@ -85,6 +93,9 @@ class Routev4(FeatureTemplateValidator):
8593
next_hop_with_track: Optional[List[NextHopWithTrack]] = Field(
8694
default=None, json_schema_extra={"vmanage_key": "next-hop-with-track"}
8795
)
96+
route_interface: Optional[RouteInterface] = Field(
97+
default=None, json_schema_extra={"vmanage_key": "route-interface"}
98+
)
8899
null0: Optional[bool] = None
89100
distance: Optional[int] = None
90101
vpn: Optional[int] = None

0 commit comments

Comments
 (0)