Download Slide Deck - Confluence

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Plugs:
https://learninglabs.cisco.com
• Coding 101 Lab
• Coding 102 Lab
Use Coding 102 for system setup help:
• Chrome
• A text editor (text wrangler, notepad ++, sublime text etc.)
• Postman REST Client -- http://www.getpostman.com/
• Python
• Go to command prompt and type python
• Install instructions -http://learnpythonthehardway.org/book/ex0.html
• Python Requests library -- http://docs.python-requests.org
2
More Plugs: learninglabs.cisco.com
Check these out
What
We
Know
REST / JSON
is the answer of the day
Customers Demand
Easier Network Element Management
The Evolution of Device Interaction
Bootstrap
Configure
Extend
Plug n Play
Controllers
I2RS
PoAP
Openstack
Puppet/Chef
onePK
Evolving
Smart Install
Auto Install
CLI
Traditional
REST
JSON RPC
NETCONF
CLI
OpenFlow
Onbox
Python
EEM
REST Follows a Familiar Model
Web Browsing
REST API
Twitter: IDs of last five followers
{"ids":[303776224, 19449911,
607032789, 86544242, 2506725913,
17631389], "next_cursor":0,
"next_cursor_str":"0",
"previous_cursor":0,
"previous_cursor_str":"0"}
HTTP GET
HTTP GET
HTML
JSON/XML
Describes how data
should be displayed to
please human viewer
Describes data in a
format applications can
understand
REST is An Architectural Style (Not a protocol)
REST= REpresentational State Transfer
Proposed by Roy Fielding in 2000
Developed by W3C in parallel with HTTP 1.1
Simple CRUD using HTTP
Stateless client-server model
Uses URIs to identify resources of interest
There Are LOTS of RESTful APIs
Why Does This Matter for Networking?
Easy to use
Human
Readable
Software
Friendly
Large
Developer
Base
Client Libraries
in Many
Languages
REST: It’s Not Just for Web Services
REST: Coming Soon to a Device Near You
Supported on CSR1kV since XE 3.10
• ASR1K in XE 3.14
Primarily for Config
• DNS, NTP, Interface, Routing, ACL, NAT
Some Stats
• Interface, CPU, Memory
Runs in a service container
• Uses onePK Python APIs under the hood
Sample: Grab Hosts from APIC-EM
Application Policy Infrastructure Controller (APIC) Enterprise Module (EM)
Request
3rd Party App
Response
GET http://{APIC-EMController}/api/v0/host/{start}/{no. rec}
Request
3rd Party App
List of Hosts returned in JSON
Cisco APIC-EM
Response
13
Anatomy of a REST Request
Method
– GET, POST, PUT, DELETE
URL
– Example: http://{APIC-EMController}/api/v0/host/1/3
Authentication
– Basic HTTP, OAuth, none
Custom Headers
– HTTP Headers
– Example: Content-Type: application/json
Request Body
– JSON or XML containing data needed to complete request
JSON -- JavaScript Object Notation, is a lightweight text-based open standard
designed for human-readable data interchange.
14
And what is in the Response?
HTTP Status Codes
– http://www.w3.org/Protocols/HTTP/HTRESP.html
– 200 OK
– 201 Created
– 500 Internal Error
Headers
Body
– JSON
– XML
15
REST in Action: How can I try it?
HTTP clients can help you quickly test web services
Postman - http://www.getpostman.com/
Firefox RestClient - https://addons.mozilla.org/en-US/firefox/addon/restclient/
Command Line using curl - http://curl.haxx.se/docs/httpscripting.html#GET
SOAPUI
Many IDEs have consoles for testing REST Services built in
16
Cost
REST Demo – Using Postman
19
REST Demo – Using Postman
20
REST Demo – Using Postman


–
–
–
–


–
–
–
–
Get Hosts
Method: GET
URL: http://APIC-EMController/api/v0/host/{startIndex}/{recordsToReturn}
Get Devices
Method: GET
URL: http://APIC-EMController/api/v0/network-device/{startIndex}/{recordsToReturn}
Get Policies
Method: GET
URL: http://APIC-EMController/api/v0/policy/{startIndex}/{recordsToReturn}
Get Applications
Method: GET
URL: http://APIC-EMController/api/v0/application/{startIndex}/{recordsToReturn}
21
REST DEMO – Using the POST or PUT Method
To send data to a REST service and either create or update data, you will
need to use POST or PUT.
Create Policy Example
– Method: POST
– URL: http://APIC-EMController/api/v0/policy
– Custom Headers: Content-Type: application/json
– Request Body: JSON that specifies details of new policy
What if the Content-Type header is missing?
What if there is a mistake in the JSON Request Body?
Handy tool for validating JSON -- http://jsonlint.com/
22
REST Demo – Using POST or PUT Method
23
First REST call from Python
#import requests library
Source code file: apic-em1.py
import requests
#specify URL
url = 'http://Your-API-EM-Controller/api/v0/host’
#Call REST API
response = requests.get(url)
#Print Response
print response.text
24
Python Examples
Download Sample Code
https://github.com/CiscoDevNet/coding-skills-sample-code
Coding 102
– apic-em1.py – simple example to get list of hosts
– apic-em-helloworld.py – “hello world” type example to show list of devices
– learning-lab-basics.py – Retrieves device list and pretty prints JSON
– learning-lab-basics-step2.py – Retrieves network device list and parses JSON to display
networkDeviceId values
– learning-lab-basics-step3.py – Retrieves and lists all devices, hosts, policies and
configured applications
– learning-lab-create-policy.py – Shows how to create a new policy using the POST
Method
25
AD-HOC REST (REST like) Interface
Leverage existing automation technology
Brownfield
Bonus
JSON-RPC
© 2013-2014 Cisco and/or its affiliates. All rights reserved.
Cisco Confidential
27
Comparison: REST/JSON-RPC
Similar: Both Send/Receive JSON over HTTP
REST (CSR 1000v)
JSON-RPC (N9K NX-API)
Comparison: REST/JSON-RPC
Different: Resources (URIs)
REST: Many Resources
• https://172.6.1.118/api/v1
…
JSON-RPC: Few Resources
• https://10.10.10.8/ins
…/global/banner
…/global/hostname
…/global/reload
…/interfaces/…
…/routing-svc/…
…/nat-svc/…
…/acl/…
…
CSR1kV
N9K
Different: Methods
REST: Standard HTTP
Methods
•
•
•
•
GET: Retrieve/List
PUT: Replace
POST: Create New Entry
DELETE: Delete
JSON-RPC: POST + body
method
JSON-RPC Details
•
A very simple remote procedure call protocol encoded in JSON, sent over
HTTP
•
http://www.jsonrpc.org/specification
JSON RPC Request Properties
• method – (string) name of the
method to be invoked.
• params – (array) objects to be
passed as parameters to the
defined method.
• Id – (any type) used to match the
response with request
JSON RPC Response Properties
• result - data returned by the
invoked method.
• error - specified Error code if
there was an error invoking the
method, otherwise null.
• id - id of the corresponding
request.
NXAPI
•
•
CLI Interaction with device over HTTP / HTTPS
Input/Output encoded in JSON or XML (key for programmability)
[
{
Show
clock
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "show clock",
"version": 1
},
"id": 1
NXAPI Web Server
(NGINX)
}
]
HTTP / HTTPS
{
"jsonrpc": "2.0",
"result": {
"body": {
"simple_time": "15:00:37.762 PST Mon Aug 18 2014\n"
}
},
"id": 1
}
Switch# conf t
Switch(config)# feature
nxapi
Switch(config)# exit
NXAPI – Web Sandbox
Point browser to IP Address of Network Element
Enter CLI Commands
See formatted input and output
NXAPI – Python Generation
Click on the Python button, and the tool will generate python
Interaction code for you.