Presentation is loading. Please wait.

Presentation is loading. Please wait.

Plugs: 2 https://learninglabs.cisco.com Coding 101 Lab Coding 102 Lab Use Coding 102 for system setup help: Chrome A text editor (text wrangler, notepad.

Similar presentations


Presentation on theme: "Plugs: 2 https://learninglabs.cisco.com Coding 101 Lab Coding 102 Lab Use Coding 102 for system setup help: Chrome A text editor (text wrangler, notepad."— Presentation transcript:

1

2 Plugs: 2 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/http://www.getpostman.com/ Python Go to command prompt and type python Install instructions -- http://learnpythonthehardway.org/book/ex0.html http://learnpythonthehardway.org/book/ex0.html Python Requests library -- http://docs.python-requests.orghttp://docs.python-requests.org

3 More Plugs: learninglabs.cisco.com Check these out

4 WhatWeKnow

5 Customers Demand Easier Network Element Management REST / JSON is the answer of the day REST / JSON is the answer of the day

6 The Evolution of Device Interaction Plug n Play PoAP Smart Install Auto Install CLI Plug n Play PoAP Smart Install Auto Install CLI Controllers Openstack Puppet/Chef REST JSON RPC NETCONF CLI Controllers Openstack Puppet/Chef REST JSON RPC NETCONF CLI I2RS onePK OpenFlow Onbox Python EEM I2RS onePK OpenFlow Onbox Python EEM Traditional Evolving Bootstrap Configure Extend

7 REST Follows a Familiar Model HTTP GET HTML Describes how data should be displayed to please human viewer HTTP GET JSON/XML Describes data in a format applications can understand {"ids":[303776224, 19449911, 607032789, 86544242, 2506725913, 17631389], "next_cursor":0, "next_cursor_str":"0", "previous_cursor":0, "previous_cursor_str":"0"} Web Browsing REST API Twitter: IDs of last five followers

8 REST= REpresentational State Transfer Proposed by Roy Fielding in 2000 Developed by W3C in parallel with HTTP 1.1 REST is An Architectural Style (Not a protocol) Simple CRUD using HTTP Stateless client-server model Uses URIs to identify resources of interest

9 There Are LOTS of RESTful APIs

10 Why Does This Matter for Networking? Human Readable Software Friendly Large Developer Base Client Libraries in Many Languages Easy to use

11 REST: It’s Not Just for Web Services

12 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

13 Sample: Grab Hosts from APIC-EM 13 Application Policy Infrastructure Controller (APIC) Enterprise Module (EM) 3 rd Party App GET http://{APIC-EMController}/api/v0/host/{start}/{no. rec} List of Hosts returned in JSON Cisco APIC-EM Request Response 3 rd Party App Request Response

14 Anatomy of a REST Request 14 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 JSONJSON -- JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange.open standardhuman-readable

15 And what is in the Response? 15 HTTP Status Codes – http://www.w3.org/Protocols/HTTP/HTRESP.html http://www.w3.org/Protocols/HTTP/HTRESP.html – 200 OK – 201 Created – 500 Internal Error Headers Body – JSON – XML

16 REST in Action: How can I try it? 16 HTTP clients can help you quickly test web services Postman - http://www.getpostman.com/http://www.getpostman.com/ Firefox RestClient - https://addons.mozilla.org/en-US/firefox/addon/restclient/https://addons.mozilla.org/en-US/firefox/addon/restclient/ Command Line using curl - http://curl.haxx.se/docs/httpscripting.html#GEThttp://curl.haxx.se/docs/httpscripting.html#GET SOAPUI Many IDEs have consoles for testing REST Services built in

17

18 Cost

19 REST Demo – Using Postman 19

20 REST Demo – Using Postman 20

21 REST Demo – Using Postman 21  Get Hosts – Method: GET – URL: http://APIC-EMController/api/v0/host/{startIndex}/{recordsToReturn}http://APIC-EMController/api/v0/host/{startIndex}/{recordsToReturn  Get Devices – Method: GET – URL: http://APIC-EMController/api/v0/network-device/{startIndex}/{recordsToReturn}http://APIC-EMController/api/v0/network-device/{startIndex}/{recordsToReturn  Get Policies – Method: GET – URL: http://APIC-EMController/api/v0/policy/{startIndex}/{recordsToReturn}http://APIC-EMController/api/v0/policy/{startIndex}/{recordsToReturn  Get Applications – Method: GET – URL: http://APIC-EMController/api/v0/application/{startIndex}/{recordsToReturn}http://APIC-EMController/api/v0/application/{startIndex}/{recordsToReturn

22 REST DEMO – Using the POST or PUT Method 22 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/policyhttp://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/http://jsonlint.com/

23 REST Demo – Using POST or PUT Method 23

24 First REST call from Python 24 #import requests library 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 Source code file: apic-em1.py

25 Python Examples 25 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 https://github.com/CiscoDevNet/coding-skills-sample-code Download Sample Code

26 Leverage existing automation technology Brownfield AD-HOC REST (REST like) Interface

27 Cisco Confidential 27 © 2013-2014 Cisco and/or its affiliates. All rights reserved. JSON-RPC Bonus

28 Comparison: REST/JSON-RPC Similar: Both Send/Receive JSON over HTTP REST (CSR 1000v) JSON-RPC (N9K NX-API)

29 REST: Many Resources https://172.6.1.118/api/v1 … …/global/banner …/global/hostname …/global/reload …/interfaces/… …/routing-svc/… …/nat-svc/… …/acl/… … JSON-RPC: Few Resources https://10.10.10.8/ins Comparison: REST/JSON-RPC Different: Resources (URIs) CSR1kV N9K

30 REST: Standard HTTP Methods GET: Retrieve/List PUT: Replace POST: Create New Entry DELETE: Delete JSON-RPC: POST + body method Different: Methods

31 A very simple remote procedure call protocol encoded in JSON, sent over HTTP http://www.jsonrpc.org/specification JSON-RPC Details 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.

32 NXAPI CLI Interaction with device over HTTP / HTTPS Input/Output encoded in JSON or XML (key for programmability) Show clock NXAPI Web Server (NGINX) [ { "jsonrpc": "2.0", "method": "cli", "params": { "cmd": "show clock", "version": 1 }, "id": 1 } ] { "jsonrpc": "2.0", "result": { "body": { "simple_time": "15:00:37.762 PST Mon Aug 18 2014\n" } }, "id": 1 } HTTP / HTTPS Switch# conf t Switch(config)# feature nxapi Switch(config)# exit

33 NXAPI – Web Sandbox Point browser to IP Address of Network Element Enter CLI Commands See formatted input and output

34 NXAPI – Python Generation Click on the Python button, and the tool will generate python Interaction code for you.


Download ppt "Plugs: 2 https://learninglabs.cisco.com Coding 101 Lab Coding 102 Lab Use Coding 102 for system setup help: Chrome A text editor (text wrangler, notepad."

Similar presentations


Ads by Google