-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathprep_108_topology_vpc_interfaces.py
More file actions
161 lines (154 loc) · 9 KB
/
prep_108_topology_vpc_interfaces.py
File metadata and controls
161 lines (154 loc) · 9 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# SPDX-License-Identifier: MIT
# Group vPC interfaces by vpc_peers, vpc_id and switch_name
# This helps in identifying vPC interfaces for a given vpc_peer, vpc_id and switch_name
# Reduces the need to loop through all interfaces to find vPC interfaces in Jinja2 templates
class PreparePlugin:
def __init__(self, **kwargs):
self.kwargs = kwargs
self.keys = []
def prepare(self):
data_model = self.kwargs['results']['model_extended']
# Check if vxlan.topology is defined
if data_model.get('vxlan').get('topology') is not None:
data_model['vxlan']['topology'] = data_model.get('vxlan').get('topology', {})
# Check if vxlan.topology.switches is defined
if data_model.get('vxlan').get('topology').get('switches') is not None:
# Initialize vxlan.topology.interfaces.vpc_interfaces
data_model['vxlan']['topology']['interfaces'] = data_model.get('vxlan').get('topology').get('interfaces', {})
data_model['vxlan']['topology']['interfaces']['vpc_interfaces'] = \
data_model.get('vxlan').get('topology').get('interfaces').get('vpc_interfaces', {})
# if vxlan.topology.vpc_peers is defined
if data_model.get('vxlan').get('topology').get('vpc_peers') is not None:
# Loop through each vpc_peers
for vpc_peer in data_model.get('vxlan').get('topology').get('vpc_peers'):
# Loop through each switch
for switch in data_model.get('vxlan').get('topology').get('switches'):
# Check if switch name is part of vpc_peer
if switch.get('name') == vpc_peer.get('peer1') or switch.get('name') == vpc_peer.get('peer2'):
# Check if switch has interfaces
if switch.get('interfaces') is not None:
# Loop through each interface
for interface in switch.get('interfaces'):
# Check if interface has vpc_id
if interface.get('vpc_id') is not None:
# Initialize vxlan.topology.interfaces.vpc_interfaces.<peer1>___<peer2>.<vpc_id>.<switch_name>
data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')] = data_model['vxlan']['topology']['interfaces']['vpc_interfaces'].get(vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2'), {}) # noqa: E501
data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')] = data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')].get(interface.get('vpc_id'), {}) # noqa: E501
data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')] = data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')].get(switch.get('name'), {}) # noqa: E501
# Assign interface to vxlan.topology.interfaces.vpc_interfaces.<peer1>___<peer2>.<vpc_id>.<switch_name>
data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')] = interface # noqa: E501
if switch.get('management').get('management_ipv4_address'):
data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')].update({'mgmt_ip_address': switch.get('management').get('management_ipv4_address')}) # noqa: E501
elif switch.get('management').get('management_ipv6_address'):
data_model['vxlan']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')].update({'mgmt_ip_address': switch.get('management').get('management_ipv6_address')}) # noqa: E501
# Update model_extended with updated data_model
self.kwargs['results']['model_extended'] = data_model
return self.kwargs['results']
# ========================================================
# Sample Input
# ========================================================
# vxlan:
# topology:
# vpc_peers:
# - peer1: dc1-leaf1
# peer2: dc1-leaf2
# domain_id: 10
# vtep_vip: "10.10.88.1"
# switches:
# - name: dc1-leaf1
# role: leaf
# interfaces:
# - name: port-channel1
# mode: access
# description: 'topology_switch_access_po_interface'
# vpc_id: 3
# mtu: jumbo
# speed: auto
# enabled: True
# spanning_tree_portfast: True
# pc_mode: active
# members:
# - eth1/16
# - eth1/17
# - name: dc1-leaf2
# role: leaf
# interfaces:
# - name: port-channel1
# mode: access
# description: 'topology_switch_access_po_interface1'
# vpc_id: 3
# mtu: jumbo
# speed: auto
# enabled: True
# spanning_tree_portfast: True
# pc_mode: active
# members:
# - eth1/16
# - eth1/17
# ========================================================
# Sample Outout (data_model_extended)
# ========================================================
# {
# "vxlan": {
# "topology": {
# "interfaces": {
# "vpc_interfaces": {
# "dc1-leaf1___dc1-leaf2": {
# "3": {
# "dc1-leaf1": {
# "description": "topology_switch_access_po_interface",
# "enabled": true,
# "members": [
# "eth1/16",
# "eth1/17"
# ],
# "mode": "access",
# "mtu": "jumbo",
# "name": "port-channel1",
# "pc_mode": "active",
# "spanning_tree_portfast": true,
# "speed": "auto",
# "vpc_id": 3
# },
# "dc1-leaf2": {
# "description": "topology_switch_access_po_interface1",
# "enabled": true,
# "members": [
# "eth1/16",
# "eth1/17"
# ],
# "mode": "access",
# "mtu": "jumbo",
# "name": "port-channel1",
# "pc_mode": "active",
# "spanning_tree_portfast": true,
# "speed": "auto",
# "vpc_id": 3
# }
# }
# }
# }
# }
# }
# }
# }
# ========================================================