-
Notifications
You must be signed in to change notification settings - Fork 184
/
l3_edge_l3_interfaces.py
53 lines (40 loc) · 1.99 KB
/
l3_edge_l3_interfaces.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright (c) 2023 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
from __future__ import annotations
from functools import cached_property
from typing import TYPE_CHECKING
from ansible_collections.arista.avd.plugins.plugin_utils.merge import merge
from ansible_collections.arista.avd.plugins.plugin_utils.utils import get, get_item
if TYPE_CHECKING:
from .shared_utils import SharedUtils
# TODO only handle l3_edge for now need to look at core_interfaces too
class L3EdgeMixin:
"""
Mixin Class providing a subset of SharedUtils
Class should only be used as Mixin to the SharedUtils class
Using type-hint on self to get proper type-hints on attributes across all Mixins.
"""
def _apply_profile(self: SharedUtils, target_dict: dict) -> dict:
"""
Apply a profile to a p2p_link or a l3_interface
"""
if "profile" not in target_dict:
# Nothing to do
return target_dict
profiles = get(self.hostvars, "l3_edge.l3_interfaces_profiles", default=[])
profile = get_item(profiles, "profile", target_dict["profile"], default={})
target_dict = merge(profile, target_dict, list_merge="replace", destructive_merge=False)
target_dict.pop("profile", None)
return target_dict
@cached_property
def filtered_l3_interfaces(self: SharedUtils) -> list:
"""
Returns a filtered list of l3_interfaces, which only contains interfaces with our hostname.
For each interface any referenced profiles are applied.
"""
if not (l3_interfaces := get(self.hostvars, "l3_edge.l3_interfaces", default=[])):
return []
l3_interfaces = [self._apply_profile(l3_interface) for l3_interface in l3_interfaces]
# Filter to only include l3_interfaces with our hostname as node
return [l3_interface for l3_interface in l3_interfaces if self.hostname == get(l3_interface, "node")]