-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathprepare_msite_data.py
More file actions
147 lines (127 loc) · 8.48 KB
/
Copy pathprepare_msite_data.py
File metadata and controls
147 lines (127 loc) · 8.48 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
# 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
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible.utils.display import Display
from ansible.plugins.action import ActionBase
from ansible_collections.cisco.nac_dc_vxlan.plugins.plugin_utils.helper_functions import ndfc_get_fabric_attributes
from ansible_collections.cisco.nac_dc_vxlan.plugins.plugin_utils.helper_functions import ndfc_get_fabric_switches
import re
display = Display()
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
results = super(ActionModule, self).run(tmp, task_vars)
results['failed'] = False
results['child_fabrics_data'] = {}
results['overlay_attach_groups'] = {}
data_model = self._task.args["data_model"]
parent_fabric = self._task.args["parent_fabric"]
# This is actaully not an accurrate API endpoint as it returns all fabrics in NDFC, not just the fabrics associated with MSD
# Therefore, we need to get the fabric associations response and filter out the fabrics that are not associated with the parent fabric (MSD)
msd_fabric_associations = self._execute_module(
module_name="cisco.dcnm.dcnm_rest",
module_args={
"method": "GET",
"path": "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/msd/fabric-associations",
},
task_vars=task_vars,
tmp=tmp
)
# Build a list of child fabrics that are associated with the parent fabric (MSD)
associated_child_fabrics = []
for fabric in msd_fabric_associations.get('response').get('DATA'):
if fabric.get('fabricParent') == parent_fabric:
associated_child_fabrics.append(fabric.get('fabricName'))
# Get the fabric attributes and switches for each child fabric
# These queries are potentially trying to get data for a fabric that is not associated with the parent fabric (MSD) yet
child_fabrics_data = {}
for fabric in associated_child_fabrics:
child_fabrics_data.update({fabric: {}})
child_fabrics_data[fabric].update(
{'type': [_fabric['fabricType'] for _fabric in msd_fabric_associations['response']['DATA'] if _fabric['fabricName'] == fabric][0]}
)
child_fabrics_data[fabric].update({'attributes': ndfc_get_fabric_attributes(self, task_vars, tmp, fabric)})
child_fabrics_data[fabric].update({'switches': ndfc_get_fabric_switches(self, task_vars, tmp, fabric)})
results['child_fabrics_data'] = child_fabrics_data
# Rebuild sm_data['vxlan']['multisite']['overlay']['vrf_attach_groups'] into
# a structure that is easier to use just like data_model_extended.
vrf_grp_name_list = []
data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'] = {}
data_model['vxlan']['multisite']['overlay']['vrf_attach_switches_list'] = []
for grp in data_model['vxlan']['multisite']['overlay']['vrf_attach_groups']:
data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'][grp['name']] = []
vrf_grp_name_list.append(grp['name'])
for switch in grp['switches']:
data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'][grp['name']].append(switch)
# If the switch is in the switch list and a hostname is used, replace the hostname with the management IP
for switch in data_model['vxlan']['multisite']['overlay']['vrf_attach_groups_dict'][grp['name']]:
for child_fabric in child_fabrics_data.keys():
for sw in child_fabrics_data[child_fabric]['switches']:
# When switch is in preprovision, sw['hostname'] is None.
if sw.get('hostname') is not None:
# Compare switches with regex to catch hostname when ip domain-name is configured
regex_pattern = f"^{switch['hostname']}$|^{switch['hostname']}\\..*$"
if re.search(regex_pattern, sw['hostname']):
switch['mgmt_ip_address'] = sw['mgmt_ip_address']
# Append switch to a flat list of switches for cross comparison later when we query the
# MSD fabric information. We need to stop execution if the list returned by the MSD query
# does not include one of these switches.
data_model['vxlan']['multisite']['overlay']['vrf_attach_switches_list'].append(switch['hostname'])
# Remove vrf_attach_group from vrf if the group_name is not defined
for vrf in data_model['vxlan']['multisite']['overlay']['vrfs']:
if 'vrf_attach_group' in vrf:
if vrf.get('vrf_attach_group') not in vrf_grp_name_list:
del vrf['vrf_attach_group']
# Rebuild sm_data['vxlan']['overlay']['network_attach_groups'] into
# a structure that is easier to use.
net_grp_name_list = []
data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'] = {}
data_model['vxlan']['multisite']['overlay']['network_attach_switches_list'] = []
for grp in data_model['vxlan']['multisite']['overlay']['network_attach_groups']:
data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'][grp['name']] = []
net_grp_name_list.append(grp['name'])
for switch in grp['switches']:
data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'][grp['name']].append(switch)
# If the switch is in the switch list and a hostname is used, replace the hostname with the management IP
for switch in data_model['vxlan']['multisite']['overlay']['network_attach_groups_dict'][grp['name']]:
for child_fabric in child_fabrics_data.keys():
for sw in child_fabrics_data[child_fabric]['switches']:
if sw.get('hostname') is not None:
regex_pattern = f"^{switch['hostname']}$|^{switch['hostname']}\\..*$"
if re.search(regex_pattern, sw['hostname']):
switch['mgmt_ip_address'] = sw['mgmt_ip_address']
# Append switch to a flat list of switches for cross comparison later when we query the
# MSD fabric information. We need to stop execution if the list returned by the MSD query
# does not include one of these switches.
data_model['vxlan']['multisite']['overlay']['network_attach_switches_list'].append(switch['hostname'])
# Remove network_attach_group or attach_groups from net if the group_name is not defined
for net in data_model['vxlan']['multisite']['overlay']['networks']:
if 'network_attach_group' in net:
if net.get('network_attach_group') not in net_grp_name_list:
del net['network_attach_group']
elif 'attach_groups' in net:
for grp in net.get('attach_groups'):
if grp not in net_grp_name_list:
net['attach_groups'].remove(grp)
if net.get('attach_groups') == []:
del net['attach_groups']
results['overlay_attach_groups'] = data_model['vxlan']['multisite']['overlay']
return results