Skip to content

Commit c2095fd

Browse files
committedApr 22, 2020
updated code for ACI API
1 parent f75c45b commit c2095fd

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
 
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import requests
2+
import xml.dom.minidom
3+
# We need to import the JSON library just to handle our request to the APIC for login
4+
import json
5+
6+
7+
# We need to log in to the APIC and gather a token, before we can access any data
8+
# Let's construct a request with a body
9+
10+
# We'll need to disable certificate warnings
11+
requests.packages.urllib3.disable_warnings()
12+
13+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
14+
encoded_body = json.dumps({
15+
"aaaUser": {
16+
"attributes": {
17+
"name": "admin",
18+
"pwd": "ciscopsdt"
19+
}
20+
}
21+
})
22+
23+
# Now lets make the request and store the data
24+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
25+
26+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
27+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
28+
29+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
30+
# We leverage the .xml ending to receive the data back as XML
31+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.xml?rsp-subtree-include=health,faults", headers=header, verify=False)
32+
33+
# Requests stores the text of the response in the .text attribute. Lets print it to see raw XML
34+
print(tenants.text)
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import requests
2+
import xml.dom.minidom
3+
# We need to import the JSON library just to handle our request to the APIC for login
4+
import json
5+
6+
7+
# We need to log in to the APIC and gather a token, before we can access any data
8+
# Let's construct a request with a body
9+
10+
# We'll need to disable certificate warnings
11+
requests.packages.urllib3.disable_warnings()
12+
13+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
14+
encoded_body = json.dumps({
15+
"aaaUser": {
16+
"attributes": {
17+
"name": "admin",
18+
"pwd": "ciscopsdt"
19+
}
20+
}
21+
})
22+
23+
# Now lets make the request and store the data
24+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
25+
26+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
27+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
28+
29+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
30+
# We leverage the .xml ending to receive the data back as XML
31+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.xml?rsp-subtree-include=health,faults", headers=header, verify=False)
32+
33+
# Now lets use DOM to clean up the XML from its completely raw format
34+
dom = xml.dom.minidom.parseString(tenants.text)
35+
xml = dom.toprettyxml()
36+
print(xml)
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import requests
2+
import xml.dom.minidom
3+
# We need to import the JSON library just to handle our request to the APIC for login
4+
import json
5+
6+
7+
# We need to log in to the APIC and gather a token, before we can access any data
8+
# Let's construct a request with a body
9+
10+
# We'll need to disable certificate warnings
11+
requests.packages.urllib3.disable_warnings()
12+
13+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
14+
encoded_body = json.dumps({
15+
"aaaUser": {
16+
"attributes": {
17+
"name": "admin",
18+
"pwd": "ciscopsdt"
19+
}
20+
}
21+
})
22+
23+
# Now lets make the request and store the data
24+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
25+
26+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
27+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
28+
29+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
30+
# We leverage the .xml ending to receive the data back as XML
31+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.xml?rsp-subtree-include=health,faults", headers=header, verify=False)
32+
33+
# Now lets use DOM to clean up the XML from its completely raw format
34+
dom = xml.dom.minidom.parseString(tenants.text)
35+
xml = dom.toprettyxml()
36+
print(xml)
37+
38+
# Now we want to parse the resulting XML and print only the tenant name and its current health score. We'll do this through iteration over the elements in the XML
39+
tenant_objects = dom.firstChild
40+
if tenant_objects.hasChildNodes:
41+
tenant_element = tenant_objects.firstChild
42+
while tenant_element is not None:
43+
if tenant_element.tagName == 'fvTenant':
44+
health_element = tenant_element.firstChild
45+
output = "Tenant: " + tenant_element.getAttribute('name') + '\t Health Score: ' + health_element.getAttribute('cur')
46+
print(output.expandtabs(40))
47+
tenant_element = tenant_element.nextSibling
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import requests
2+
import xml.dom.minidom
3+
# We need to import the JSON library just to handle our request to the APIC for login
4+
import json
5+
6+
7+
# We need to log in to the APIC and gather a token, before we can access any data
8+
# Let's construct a request with a body
9+
10+
# We'll need to disable certificate warnings
11+
requests.packages.urllib3.disable_warnings()
12+
13+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
14+
encoded_body = json.dumps({
15+
"aaaUser": {
16+
"attributes": {
17+
"name": "admin",
18+
"pwd": "ciscopsdt"
19+
}
20+
}
21+
})
22+
23+
# Now lets make the request and store the data
24+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
25+
26+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
27+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
28+
29+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
30+
# We leverage the .xml ending to receive the data back as XML
31+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.xml?rsp-subtree-include=health,faults", headers=header, verify=False)
32+
33+
# Now lets use DOM to clean up the XML from its completely raw format
34+
dom = xml.dom.minidom.parseString(tenants.text)
35+
xml = dom.toprettyxml()
36+
print(xml)
37+
38+
# Now we'll do something similar, but done using a direct access method of the data, rather than interation and loops
39+
tenant_list = dom.getElementsByTagName('fvTenant')
40+
for tenants in tenant_list:
41+
tenant_name = tenants.getAttribute('name')
42+
tenant_dn = tenants.getAttribute('dn')
43+
health_score = tenants.firstChild.getAttribute('cur')
44+
output = "Tenant: " + tenant_name + "\t Health Score: " + health_score + "\n DN: " + tenant_dn
45+
print(output.expandtabs(40))

0 commit comments

Comments
 (0)
Failed to load comments.