34

Plugs: 2 Coding 101 Lab Coding 102 Lab Use Coding 102 for system setup help: Chrome A text editor (text wrangler, notepad

Embed Size (px)

Citation preview

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/• Python

• Go to command prompt and type python• Install instructions --

http://learnpythonthehardway.org/book/ex0.html• Python Requests library -- http://docs.python-requests.org

More Plugs: learninglabs.cisco.com Check these out

What We Know

Customers Demand Easier Network Element Management

REST / JSONis the answer of the day

The Evolution of Device Interaction

Plug n Play

PoAP

Smart InstallAuto Install

CLI

Controllers

OpenstackPuppet/Chef

RESTJSON RPCNETCONF

CLI

I2RS

onePKOpenFlow

Onbox Python

EEMTraditional

Evolving

Bootstrap Configure Extend

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 APITwitter: IDs of last five followers

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

There Are LOTS of RESTful APIs

Why Does This Matter for Networking?

Human Readable

Software Friendly

Large Developer

Base

Client Libraries in Many

Languages

Easy to use

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

13

Application Policy Infrastructure Controller (APIC) Enterprise Module (EM)

3rd Party App

GET http://{APIC-EMController}/api/v0/host/{start}/{no. rec}

List of Hosts returned in JSON

Cisco APIC-EM

Request

Response

3rd Party App

Request

Response

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

JSON -- JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange.

And what is in the Response?

15

HTTP Status Codes

– http://www.w3.org/Protocols/HTTP/HTRESP.html

– 200 OK

– 201 Created

– 500 Internal Error

Headers

Body

– JSON

– XML

REST in Action: How can I try it?

16

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

Cost

REST Demo – Using Postman

19

REST Demo – Using Postman

20

REST Demo – Using Postman

21

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 }

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/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/

REST Demo – Using POST or PUT Method

23

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

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

Leverage existing automation technology

Brownfield

AD-HOC REST (REST like) Interface

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

JSON-RPC

Bonus

Comparison: REST/JSON-RPCSimilar: Both Send/Receive JSON over

HTTPREST (CSR 1000v) JSON-RPC (N9K NX-API)

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-RPCDifferent: Resources (URIs)

CSR1kV N9K

REST: Standard HTTP Methods

• GET: Retrieve/List• PUT: Replace• POST: Create New Entry• DELETE: Delete

JSON-RPC: POST + body method

Different: Methods

• 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.

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 tSwitch(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 pythonInteraction code for you.