Devnet Session 2 Intro To Coding
Devnet Session 2 Intro To Coding
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2
Matt Denapoli
Joining Developer Evangelist
DevNet, Cisco
You
Giuseppe Cinque
Today: Manager for the Emerging
Technologies
NetAcad, Cisco
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 3
Next DevNet Session
THIRD SESSION:
Intent Networks
25 October – 9:00 A.M. PT
Register at: http://bit.ly/DevNetSession3
Intro to Coding
Giuseppe Cinque & Wadih Zaatar
Intro to Coding
Matthew DeNapoli
DevNet Developer Evangelist
Agenda
Getting Started
Learning APIC-EM
APIC-EM with Postman – HTTP Calls &
Generate Code
Calling APIC-EM REST APIs with Python
Q&A
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 10
Learning APIC-EM
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 11
APIC-EM Applications and Use Cases
• Easy QoS
• Application Priority
• Plug-n-Play
• Agent based control
• IWAN
• Policy based automated deployment
• Path Trace
• Path troubleshooting
APIC-EM Uses REST
Easy to use:
• In mobile apps
• In console apps
• In web apps
Cisco APIC-EM REST APIs
• Hosts
• Devices
• Users
• + more
How does this work?
13
Anatomy of a REST Request
Method
– POST, GET, PUT, DELETE (CRUD)
URL
– Example: http://{APIC-EMController}/api/v1/host
Authentication
– Basic HTTP, OAuth, none, Custom
Custom Headers
– HTTP Headers
– Example: Content-Type: application/json
Request Body
– JSON or XML containing data needed to complete request
14
APIC-EM Example: Post Ticket
Application Policy Infrastructure Controller (APIC) Enterprise Module (EM)
POST http://{APIC-EMController}/api/v1/ticket
3rd Party
Request
App
Authorization Code returned in JSON
Response
15
APIC-EM Example: Get Host
Application Policy Infrastructure Controller (APIC) Enterprise Module (EM)
GET http://{APIC-EMController}/api/v1/host
3rd Party
Request
App
List of Hosts returned in JSON
Response
16
Using the API Reference Documentation
API Reference Guide Details
APIC-EM with Postman
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 19
https://www.getpostman.com
Postman
Create a ticket
method url
Request body
22
Header Specification
23
Ticket Returned in Response Body
Response Ticket
(save this)
24
Use ticket in Header in all API Calls
Header Ticket
25
APIC-EM – List of Hosts
URL
Method Header
Return Code
Response
Body
26
Using Postman to Generate Code
Selecting the Code to Generate
Generated Python Requests Code
REST Demo – Using Postman
Get Hosts
– Method: GET
– Headers: ‘X-Auth-Token’ (insert your ticket value)
– URL: http://<APIC-EMController>/api/v1/host
Get Devices
– Method: GET
– Headers: ‘X-Auth-Token’ (insert your ticket value)
– URL: http://<APIC-EMController>/api/v1/network-device
Get Users
– Method: GET
– Headers: ‘X-Auth-Token’ (insert your ticket value)
– URL: http://<APIC-EMController>/api/v1/user
30
APIC-EM with Python
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 31
First APIC-EM REST call from Python
import requests
import json
url = 'https://198.18.129.100/api/v1/ticket’
payload = {"username":“admin","password":"C1sco12345"}
header = {"content-type": "application/json”}
response= requests.post(url,data=json.dumps(payload), headers=header,
verify=False)
print(response.text)
32
Getting Ticket Function
apic_em_ip = "https://198.18.129.100/api/v1"
def get_token(url):
api_call = "/ticket"
payload = {"username": "admin", "password": "C1sco12345"}
headers = {"content-type": "application/json"}
url += api_call
response = requests.post(url, data=json.dumps(payload),
headers=headers, verify=False).json()
return response["response"]["serviceTicket"]
Getting Network Device ID/Config Functions
def get_device_id(token, url):
api_call = "/network-device"
headers = {"X-AUTH-TOKEN": token}
url += api_call
response = requests.get(url, headers=headers, verify=False).json()
for item in response['response']:
if item['role'] == 'ACCESS':
return item['id']
Q&A
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 35
This is the Digital Transformation
Intent Networks
25 October – 9:00 A.M. PT
Register at: http://bit.ly/DevNetSession3