Network Programmability With Cisco APIC-EM
Network Programmability With Cisco APIC-EM
Network Programmability With Cisco APIC-EM
ingress egress
Data Plane
Hardware Purpose Example Processes
Dedicated forwards traffic to the selected packet switching, L2 switching,
ASICs destination QoS, policies, ACLs
SDN Controller
OpenFlow based controller
(control plane)
OpenFlow Southbound API
Physical Topology
(data plane)
Servers
Physical Topology
(data plane fabric)
Expand
API
Navigation Bar
Services
Applications
example topology
Request
GET http://{APIC-EMController}/api/v1/host
Request Body
– JSON or XML containing data needed to complete request
© 2020 Cisco Ing. Juan Villegas Cubas 16
16
What is in the Response?
HTTP Status Codes
– http://www.w3.org/Protocols/HTTP/HTRESP.html
– 200 OK
– 201 Created
– 401, 403 Authorization error
– 404 Resource not found
– 500 Internal Error
Headers
Body
– JSON
– XML
Example output of a HTTP response in the Postman application
© 2020 Cisco Ing. Juan Villegas Cubas 17
17
JSON and XML
JSON XML
Automation No No Yes
Customization No No Yes
request
• Tabs
• Collections
• Presets
response
• Code
• Environments
• Collaboration
24
Lab: Getting a Service Ticket with Python
Part 1 Step 5b: View the Response
request status
response body
authentication token
(service ticket number)
© 2020 Cisco Ing. Juan Villegas Cubas 25
25
Overview of the Request Process
1. Build request Build
• Method Request
• URL Components
• Headers
• Body
• Authentication Send
2. Send request Request
3. Evaluate response
• Response code Evaluate
• Desired data features Response
requests.packages.urllib3.disable_warnings()
body_json = {
"username": "!!!REPLACE ME with the username!!!",
"password": "!!!REPLACE ME with the password!!!",
}
verify=False is used to turn off SSL verification (OK for learning, NOT OK in production!)
© 2020 Cisco Ing. Juan Villegas Cubas 30
Lab 1: Getting a Service Ticket with Python:
Part 2 Step 4. Evaluate the Response
print ("Ticket request status: ", resp.status_code)
response_json = resp.json()
serviceTicket = response_json["response"]["serviceTicket"]
response[0]['hostIP']
response[0]['hostType']
Note that the Python file that contains your service ticket function is
imported for use here. The name of the functions file will vary depending
on whether you are using your own file or the provided solution file.
Save your new code file as print_hosts.py
© 2020 Cisco Ing. Juan Villegas Cubas 36
Lab: Create Host Inventory in Python
Part 2 Step 2: Build Request Components
api_url = "https://{YOUR-APICEM}.cisco.com/api/v1/host"
ticket = get_ticket()
headers = {
"content-type": "application/json",
"X-Auth-Token": ticket
}
Note that the get_ticket() function that you created earlier is reused
here and the value is supplied to the headers object.
ticket = get_ticket()
1. Copy your program
headers = {"content-type": "application/json", "X-Auth-Token": ticket} into the functions file
resp = requests.get(api_url, headers=headers, verify=False) (my_apic_em_functions.py).
print ("Status of /host request: ", resp.status_code)
if resp.status_code != 200:
raise Exception("Status code is not 200/OK. Resp text: " +
resp.text) 2. Define the function
response_json = resp.json()
with def print_hosts():
host_list=[]
i=0
for item in response_json["response"]: 3. Indent everything by
i+=1
host = [ i, item["hostType"], item["hostIp"] ] four additional spaces
host_list.append( host )
table_header = [ "number", "type", "host IP" ]
print( tabulate(host_list, table_header) )
4. Save the functions file.
© 2020 Cisco Ing. Juan Villegas Cubas 40
Lab - Create a Network
Device Inventory in Python