-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_switch_profiles.py
126 lines (115 loc) · 5.33 KB
/
create_switch_profiles.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
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
from Aci_Cal_Toolkit import FabLogin, Query, FabAccPol
from credentials import apic_ip, apic_pwd, apic_user
# to avoid using Acitoolkit
apic = FabLogin (apic_ip, apic_user, apic_pwd)
cookies = apic.login()
print('Logged into apic ...')
print('Retrieving existing leafs ...')
req = Query(apic_ip, cookies)
[status, payload] = req.query_url('/api/node/class/fabricNode.json?'
'query-target-filter=eq(fabricNode.role,"leaf")')
leafs = {}
if (status == 200):
for node in payload['imdata']:
leafs[node['fabricNode']['attributes']['id']] = node['fabricNode']['attributes']['name']
else:
print ('Error for query response code :' + status)
fabric = FabAccPol (apic_ip, cookies)
# checking for existent interface profiles
print('Retrieving interface profiles ...')
int_prof = {}
[status, payload] = req.query_class('infraAccPortP')
if (status == 200):
for obj in payload['imdata']:
name = obj['infraAccPortP']['attributes']['name']
int_prof[name] = 'uni/infra/accportprof-' + name
print('Checking for single-switch interface profile missing ... ')
for node_id in leafs:
if not 'Leaf-'+node_id+'_IntProf' in int_prof:
print ('Switch interface profile for leaf '+node_id+' has to be created')
status = fabric.int_profile(name='Leaf-'+node_id+'_IntProf',
status='created')
if (status==200):
print('Missing single switch interface profile has been created')
else:
print('Error creating missing profile switch')
print('Checking for double-switch interface profile missing ... ')
new_couple = 0
old_id = 0
for node_id in sorted(leafs):
if not new_couple:
old_id = node_id
new_couple = 1
else:
new_couple = 0
if not 'Leaf-'+old_id+'-'+node_id+'_IntProf' in int_prof:
print ('Switch interface profile "Leaf-'+old_id+'-'+node_id+'_IntProf" has to be created')
status = fabric.int_profile(name= 'Leaf-'+old_id+'-'+node_id+'_IntProf',
status= 'created')
if (status==200):
print('Missing vpc switch interface profile has been created')
else:
print('Error creating missing vpc interface profile switch')
print('Retrieving switch profiles ...')
sw_prof = {}
[status, payload] = req.query_class('infraNodeP')
if (status == 200):
for obj in payload['imdata']:
name = obj['infraNodeP']['attributes']['name']
sw_prof[name] = 'uni/infra/nprof-' + name
# we now create with a post, a switch profile with a switch selector,
# then we add the interface profile.
print('Checking for single switch profile missing ... ')
for node_id in leafs:
sw_prof_name = 'Leaf-'+node_id+'_LeafProf'
sw_sel_name = 'Leaf-'+node_id+'_SwSel'
sw_int_prof = 'Leaf-'+node_id+'_IntProf'
if not sw_prof_name in sw_prof:
print ('Switch profile for '+leafs[node_id]+' has to be created')
status = fabric.swPro_swSel_single(name = sw_prof_name,
swSelName = sw_sel_name,
status='created',
sw1=str(node_id))
if (status==200):
print('Missing single switch profile has been created')
status = fabric.int_prof_to_sw_profile(name = sw_prof_name,
status = 'created',
int_profile = sw_int_prof)
if (status==200):
print('Interface profile added to switch profile')
else:
print('Error adding interface profile to switch profile')
else:
print('Error creating missing profile switch')
# we now create with a post, a switch profile with a switch selector,
# then we add the interface profile.
print('Checking for "pair" switch profile missing ... ')
new_couple = 0
old_id = 0
for node_id in sorted(leafs):
if not new_couple:
old_id = node_id
new_couple = 1
else:
new_couple = 0
sw_prof_name = 'Leaf-'+old_id+'-'+node_id+'_LeafProf'
sw_sel_name = 'Leaf-'+old_id+'-'+node_id+'_SwSel'
sw_int_prof = 'Leaf-'+old_id+'-'+node_id+'_IntProf'
if not 'Leaf-'+old_id+'-'+node_id+'_LeafProf' in sw_prof:
print ('Switch profile for "Leaf-'+old_id+'-'+node_id+'_LeafProf" has to be created')
status = fabric.swPro_swSel_vpc(name = sw_prof_name,
swSelName = sw_sel_name,
status = 'created,modified',
sw1 = old_id,
sw2 = node_id)
if (status==200):
print('Missing "pair" switch profile and selector has been created')
status = fabric.int_prof_to_sw_profile(name = sw_prof_name,
status = 'created',
int_profile = sw_int_prof)
if (status==200):
print('Interface profile added to switch profile')
else:
print('Error adding interface profile to switch profile')
else:
print('Error creating missing vpc profile switch')