-
Notifications
You must be signed in to change notification settings - Fork 0
/
aci-add-phys.py
executable file
·158 lines (145 loc) · 6.3 KB
/
aci-add-phys.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
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
#!/usr/bin/env python
"""
//Rob van der Kind - [email protected]
"""
import acitoolkit.acitoolkit as aci
import csv
import getpass
import sys
def main():
"""
Main create routine
:return: None
"""
# Login to the APIC
#session = aci.Session(raw_input("APIC URL: "), raw_input("APIC username: "), getpass.getpass())
session = aci.Session("http://apic-amslab.cisco.com", "admin", "C1sco123")
resp = session.login()
if not resp.ok:
print('%% Could not login to APIC')
if resp.ok:
print ("\nApic login successful")
#Fabric acccess policy configuration
# Add VLAN Pool to be used for newly attached hosts
vlanpool_name = raw_input("\nEnter the VLAN Pool name to be used for the new hosts: ")
vlanpool = {"fvnsVlanInstP":{"attributes":{
"dn":"uni/infra/vlanns-[{}]-static".format(vlanpool_name),
"name":vlanpool_name,
"allocMode":"static",
"rn":"vlanns-[{}]-static".format(vlanpool_name),
"status":"created"},"children":[]}}
# Add VLANs to VLAN Pool
vlans_low = 'vlan-' + raw_input("\nEnter the low range for encap block: ")
vlans_high = 'vlan-' + raw_input("Enter the high range for encap block: ")
vlans = {"fvnsEncapBlk":{"attributes":{
"dn":"uni/infra/vlanns-[{}]-static/from-[{}]-to-[{}]".format(vlanpool_name,vlans_low,vlans_high),
"from":vlans_low,"to":vlans_high,
"allocMode":"static",
"rn":"from-[{}]-to-[{}]".format(vlans_low,vlans_high),
"status":"created"},"children":[]}}
# Create PhysDom and attach previously created vlan pool
physdom_name = raw_input("Enter the name for the PhysDom to be created: ")
physdom = {"physDomP":{"attributes":{
"dn":"uni/phys-{}".format(physdom_name),
"name":physdom_name,
"rn":"phys-{}".format(physdom_name),
"status":"created"},"children":[{
"infraRsVlanNs":{"attributes":{
"tDn":"uni/infra/vlanns-[{}]-static".format(vlanpool_name),
"status":"created"},"children":[]}}]}}
# Create AEP and attach previously created PhysDom
aep_name = raw_input("Enter the name for the AEP to be created: ")
aep = {"infraInfra":{"attributes":{
"dn":"uni/infra",
"status":"modified"},
"children":[{"infraAttEntityP":{"attributes":{"dn":"uni/infra/attentp-{}".format(aep_name),
"name":aep_name,
"rn":"attentp-{}".format(aep_name),
"status":"created"},
"children":[{
"infraRsDomP":{"attributes":{
"tDn":"uni/phys-{}".format(physdom_name),
"status":"created"},
"children":[]}}]}},
{"infraFuncP":{
"attributes":{
"dn":"uni/infra/funcprof",
"status":"modified"},
"children":[]}}]}}
# Create Interface Leaf Policy Group (Access Port Group)
intpolgroup_name = raw_input("Enter the name for the new Interface Policy Group: ")
intpolgroup = {"infraAccPortGrp":{"attributes":{
"dn":"uni/infra/funcprof/accportgrp-{}".format(intpolgroup_name),
"name":intpolgroup_name,
"rn":"accportgrp-{}".format(intpolgroup_name),
"status":"created"},
"children":[{
"infraRsAttEntP":{"attributes":{
"tDn":"uni/infra/attentp-{}".format(aep_name),
"status":"created,modified"},
"children":[]}}]}}
# Create Interface Leaf Profile (Access port Profile)
intprof_name = raw_input("Enter the name for the new Interface Profile: ")
intprof = {"infraAccPortP":{"attributes":{
"dn":"uni/infra/accportprof-{}".format(intprof_name),
"name":intprof_name,
"rn":"accportprof-{}".format(intprof_name),
"status":"created,modified"},"children":[]}}
# Create interface selector and add to interface profile
# Push the changes to the APIC
# Push Fabric policy to APIC
# Push VLAN pool
vlanpool_url = '/api/node/mo/uni/infra/vlanns-[{}]-static.json'.format(vlanpool_name)
resp = session.push_to_apic(vlanpool_url, vlanpool)
print ''
if not resp.ok:
print('%% Error: Could not push VLAN pool to APIC')
print(resp.text)
if resp.ok:
print ("Succesfully added VLAN Pool {} to ACI fabric").format(vlanpool_name)
# Add VLANs to VLAN pool
vlans_url = '/api/node/mo/uni/infra/vlanns-[{}]-static/from-[{}]-to-[{}].json'.format(vlanpool_name,vlans_low,vlans_high)
resp = session.push_to_apic(vlanpool_url, vlans)
if not resp.ok:
print('%% Error: Could not push VLAN encap block to APIC')
print(resp.text)
if resp.ok:
print ("Succesfully added VLAN encap block {} to {} to VLAN Pool {}").format(vlans_low,vlans_high,vlanpool_name)
# Push PhysDom
physdom_url = '/api/node/mo/uni/phys-{}.json'.format(physdom_name)
resp = session.push_to_apic(physdom_url, physdom)
if not resp.ok:
print('%% Error: Could not push PhysDom to APIC')
print(resp.text)
if resp.ok:
print ("Succesfully added PhysDom {} to APIC").format(physdom_name)
# Push AEP
aep_url = '/api/node/mo/uni/infra.json'
resp = session.push_to_apic(aep_url, aep)
if not resp.ok:
print('%% Error: Could not push AEP to APIC')
print(resp.text)
if resp.ok:
print ("Succesfully added AEP {} to APIC").format(aep_name)
# Push Interface Leaf Policy Group
intpolgroup_url = '/api/node/mo/uni/infra/funcprof/accportgrp-{}.json'.format(intpolgroup_name)
resp = session.push_to_apic(intpolgroup_url, intpolgroup)
if not resp.ok:
print('%% Error: Could not push Interface Policy Group to APIC')
print(resp.text)
if resp.ok:
print ("Succesfully added the Interface Policy Group {} to APIC").format(intpolgroup_name)
# Push Interface Profile
intprof_url = '/api/node/mo/uni/infra/accportprof-{}.json'.format(intprof_name)
resp = session.push_to_apic(intprof_url, intprof)
if not resp.ok:
print('%% Error: Could not push Interface Policy Group to APIC')
print(resp.text)
if resp.ok:
print ("Succesfully added the Interface Profile {} to APIC").format(intprof_name)
# Push Interface selector and port
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass