Skip to content

Commit fdc9f93

Browse files
committedJul 8, 2020
adding json parsing updates
1 parent c2095fd commit fdc9f93

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import requests
2+
# We need to import the JSON library just to handle our request to the APIC for login
3+
import json
4+
5+
6+
# We need to log in to the APIC and gather a token, before we can access any data
7+
# Let's construct a request with a body
8+
9+
# We'll need to disable certificate warnings
10+
requests.packages.urllib3.disable_warnings()
11+
12+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
13+
encoded_body = json.dumps({
14+
"aaaUser": {
15+
"attributes": {
16+
"name": "admin",
17+
"pwd": "ciscopsdt"
18+
}
19+
}
20+
})
21+
22+
# Now lets make the request and store the data
23+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
24+
25+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
26+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
27+
28+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
29+
# We leverage the .xml ending to receive the data back as XML. We're adding health and faults to the printout to ensure that we get levels of data back from the APIC
30+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.json?rsp-subtree-include=health,faults", headers=header, verify=False)
31+
32+
# Requests stores the text of the response in the .text attribute. Lets print it to see raw JSON
33+
print(tenants.text)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import requests
2+
# We need to import the JSON library just to handle our request to the APIC for login
3+
import json
4+
5+
6+
# We need to log in to the APIC and gather a token, before we can access any data
7+
# Let's construct a request with a body
8+
9+
# We'll need to disable certificate warnings
10+
requests.packages.urllib3.disable_warnings()
11+
12+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
13+
encoded_body = json.dumps({
14+
"aaaUser": {
15+
"attributes": {
16+
"name": "admin",
17+
"pwd": "ciscopsdt"
18+
}
19+
}
20+
})
21+
22+
# Now lets make the request and store the data
23+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
24+
25+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
26+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
27+
28+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
29+
# We leverage the .xml ending to receive the data back as XML. We're adding health and faults to the printout to ensure that we get levels of data back from the APIC
30+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.json?rsp-subtree-include=health,faults", headers=header, verify=False)
31+
32+
# Requests stores the text of the response in the .text attribute. Lets print it to see raw JSON
33+
#print(tenants.text)
34+
35+
# Lets make this response a bit prettier, by converting it to a JSON object and using the dumps method to provide indentation
36+
json_response = json.loads(tenants.text)
37+
print(json.dumps(json_response, sort_keys=True, indent=4))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import requests
2+
# We need to import the JSON library just to handle our request to the APIC for login
3+
import json
4+
5+
6+
# We need to log in to the APIC and gather a token, before we can access any data
7+
# Let's construct a request with a body
8+
9+
# We'll need to disable certificate warnings
10+
requests.packages.urllib3.disable_warnings()
11+
12+
# We need to have a body of data consisting of a username and password to gather a cookie from APIC
13+
encoded_body = json.dumps({
14+
"aaaUser": {
15+
"attributes": {
16+
"name": "admin",
17+
"pwd": "ciscopsdt"
18+
}
19+
}
20+
})
21+
22+
# Now lets make the request and store the data
23+
resp = requests.post("https://sandboxapicdc.cisco.com/api/aaaLogin.json", data=encoded_body, verify=False)
24+
25+
# This stores the received APIC-cookie from the login as a value to be used in subsequent REST calls
26+
header = {"Cookie": "APIC-cookie=" + resp.cookies["APIC-cookie"]}
27+
28+
# Now we make a call towards the tenant class on the ACI fabric with the proper header value set.
29+
# We leverage the .xml ending to receive the data back as XML. We're adding health and faults to the printout to ensure that we get levels of data back from the APIC
30+
tenants = requests.get("https://sandboxapicdc.cisco.com/api/node/class/fvTenant.json?rsp-subtree-include=health,faults", headers=header, verify=False)
31+
32+
# Requests stores the text of the response in the .text attribute. Lets print it to see raw XML
33+
#print(tenants.text)
34+
35+
# Lets make this response a bit prettier, by converting it to a JSON object and using the dumps method to provide indentation
36+
json_response = json.loads(tenants.text)
37+
#print(json.dumps(json_response, sort_keys=True, indent=4))
38+
39+
40+
# We've commented out the pretty-print, but now lets return only the values we want.
41+
# Everything within the returned JSON is containted within the `imdata` dictionary, so lets strip that away
42+
json_tenants = json_response['imdata']
43+
44+
# Now we have to loop through the resulting list (using the `for` loop) and drill into the subsequent dictionaries by name
45+
# When we get to `tenant health`, we move from `attributes` dictionary to `children` dictionary.
46+
# Inside of the `children` dictionary is a list, which will need an index value to move further.
47+
# Since there is only one child object within the `children` list, we can safely set this value to `0`
48+
# This is the result of how the original query was structured, so don't assume you can always set that value to `0`
49+
for tenant in json_tenants:
50+
tenant_name = tenant['fvTenant']['attributes']['name']
51+
tenant_dn = tenant['fvTenant']['attributes']['dn']
52+
tenant_health = tenant['fvTenant']['children'][0]['healthInst']['attributes']['cur']
53+
output = "Tenant: " + tenant_name + "\t Health Score: " + tenant_health + "\n DN: " + tenant_dn
54+
print(output.expandtabs(40))

0 commit comments

Comments
 (0)
Failed to load comments.