Top Banner
34

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

Dec 15, 2015

Download

Documents

Thomas Burrows
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Plugs: 2  Coding 101 Lab Coding 102 Lab Use Coding 102 for system setup help: Chrome A text editor (text wrangler, notepad.
Page 2: Plugs: 2  Coding 101 Lab Coding 102 Lab Use Coding 102 for system setup help: Chrome A text editor (text wrangler, notepad.

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

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

More Plugs: learninglabs.cisco.com Check these out

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

What We Know

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

Customers Demand Easier Network Element Management

REST / JSONis the answer of the day

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

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

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

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

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

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

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

There Are LOTS of RESTful APIs

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

Why Does This Matter for Networking?

Human Readable

Software Friendly

Large Developer

Base

Client Libraries in Many

Languages

Easy to use

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

REST: It’s Not Just for Web Services

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

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

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

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

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

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.

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

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

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

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

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

Cost

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

REST Demo – Using Postman

19

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

REST Demo – Using Postman

20

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

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 }

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

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/

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

REST Demo – Using POST or PUT Method

23

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

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

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

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

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

Leverage existing automation technology

Brownfield

AD-HOC REST (REST like) Interface

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

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

JSON-RPC

Bonus

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

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

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

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

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

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

REST: Standard HTTP Methods

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

JSON-RPC: POST + body method

Different: Methods

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

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

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

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

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

NXAPI – Web Sandbox

Point browser to IP Address of Network Element

Enter CLI Commands

See formatted input and output

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

NXAPI – Python Generation

Click on the Python button, and the tool will generate pythonInteraction code for you.