Coding 102: REST API Basics using APIC-EM

41
Coding 102 - REST API Basics using APIC-EM Brett Tiller Developer Evangelist DEVNET-2003

Transcript of Coding 102: REST API Basics using APIC-EM

Page 1: Coding 102: REST API Basics using APIC-EM

Coding 102 - REST API Basics using APIC-EM

Brett Tiller Developer EvangelistDEVNET-2003

Page 2: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

• Intended Audience• DNA• Coding 101 Recap• Lab Set up• Let’s Write Some Python!• Final Thoughts

Agenda

DEVNET-2003 2

Page 3: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Intended Audience

DEVNET-2003 3

Page 4: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 4

Who is Coding 102 for?

New coders

Returning coders

NetOps

DevOps

DEVNET-2003

Page 5: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

DNA

DEVNET-2003 5

Page 6: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-2003 6

Page 7: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

APIC-EM

DEVNET-2003 7

Page 8: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Coding 101 Recap

DEVNET-2003 8

Page 9: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 9

Coding 101 Recap• REST

• HTTP

• CRUD

• POST, GET, PUT, DELETE

• JSON• Java script Object Notation

• food={“vegetables”:[“carrots”,“kale”,“cucumber”,”tomato”]}

• Postman• HTTP Client

• Python

DEVNET-2003

Page 10: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Lab Set up

DEVNET-2003 10

Page 11: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 11

Put these URLs in a text document

https://learninglabs.cisco.com

https://github.com/CiscoDevNet

http://www.python.org

http://www.getpostman.com

http://www.git-scm.com

DEVNET-2003

Page 12: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 12

Set up : Tools Python 3.4+

https://www.python.org/downloads/release/python-351/ Checking python version: Open your laptop on command line type:

Command: python –V

Install Python Requests Module Command: pip install requests

Http Client - Postman Install/Use Chrome Browser Postman REST Client -- http://www.getpostman.com/

DEVNET-2003

Page 13: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 13

Set up : Tools (Continued) Editor

A capable text editor or Integrated Development Environment Notepad ++ VI Eclipse with PyDev Plugin PyCharm WingIDE

DEVNET-2003

Page 14: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

• On your desktop open a command terminal.

• Go to the root directory by typing: cd \• Create a directory called 'C:\DevNetCode\yourname' by typing: mkdir

DevNetCode\<your-name>• For example: mkdir DevNetCode\brtiller• Go to the new directory by typing: cd \DevNetCode\<your-name>• For example: cd \DevNetCode\brtiller

Create your Source Code Directory

14DEVNET-2003

Page 15: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 15

Set up : Download Sample Code Get Source code

https://github.com/CiscoDevNet/coding-skills-sample-code

To clone repository locally run the following command: git clone https://

github.com/CiscoDevNet/coding-skills-sample-code.git

Or get code via download https://github.com/CiscoDevNet/coding-skills-sample-code

Click ‘Clone or download’ Click ‘Download Zip’ Copy file to your directory C:\DevNetCode\<your name> Unzip

DEVNET-2003

Page 16: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Follow along with the Learning Lab

• Login with DevNet/Cisco.com ID• Coding 101 Lab – REST Basics• Coding 102 Lab - Calling REST APIs with Pythons

• How to Set up Your Own Computer

https://learninglabs.cisco.com

DEVNET-2003 16

Page 17: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 17

Disclaimer: Sample Code vs. Real Code

• The examples and code in this presentation are for Learning and Educational purposes.

• The samples were created with the goals of clarity and ease of understanding.

• If you are writing code for a real application, you would write the code in a more efficient and structured style.

DEVNET-2003

Page 18: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 18DEVNET-2003

Python Introduction

Page 19: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 19

Running Python and scripts Running Python 3: Checking your version

python -V or py -3 -V [ Windows] python3 –V [ Linux, OS X]

Running a python script python script.py or py -3 script.py [ Windows] python3 script.py [ Linux, OS X]

Running Python Interpreter on the Command line python or py -3 [ Windows] Python3 [ Linux, OS X] To exit: Type quit() or Ctrl Z

DEVNET-2003

Page 20: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 20

Python Syntax, Data Types, Loops …Etc Indentation defines scope

Python code blocks do not have explicit statement scope delimiters Indentation amount is relative

Data Types Statements

Import Print Conditional Looping Functions

DEVNET-2003

Page 21: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Python Indentation

21

print ("Hello World!")

num = 1

if num < 1:

print ("I'm less than 1!")

print ("Goodbye Cruel World!")

DEVNET-2003

Page 22: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Numeric int / float / complex Types that describe numeric content

Boolean bool Types that define true/false relationships Logical operators: and / or / not

Text str Immutable string objects 1,2, or 3 quotes

Sequence Lists

Typically homogeneous sequences of objects An mutable, ordered array Square Brackets

Tuple Typically heterogeneous sequences of objects An immutable collection Parenthesis

Mapping Dictionaries

A mutable, unordered, associative array Curly Brackets

Python Data Types

22DEVNET-2003

Page 23: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Python Data Types

23

Type Example• Numeric: Integer, Float x = 10 x = 1.0

• String x= ‘Mike’

• Boolean y = True x = False

• List my_list = [10, 20, 30]• Tuple my_tuple = (‘Brett’, ‘Cisco',

‘Cary’,2015)• Dictionary my_dict = {"one":1, "two":2}• Lists in Lists my_list2=[[10,20,30], [‘Cisco

Live’, ‘May’, 2016]]

DEVNET-2003

Page 24: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 24DEVNET-2003

Demo

Page 25: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Labs 1 & 2

DEVNET-2003 25

Page 26: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

• Add to your python script myworld.py

• Add a list with two elements

• Add a tuple with three elements

• Add a dictionary with two name value pairs

• Print one value from each data type you created

Lab -2 : 10 minutes

26

Lab -1 : 5 minutes• Code and Run:Python Hello

World • Name file: myworld.py• Prints Hello World!• Assigns a variable• Checks the value “Remember

if (value == value):”• Prints statement within if

statement. Must be indented!

DEVNET-2003

Page 27: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 27

Coding-102-REST-python-ga Examples• create-ticket.py

• simple example to create ticket for authentication

• get-network-hosts.py • returns links of hosts

• get-network-devices.py• returns list of network devices

• build-topology.py• Reads and displays topology in spreadsheet format.

DEVNET-2003

Page 28: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

First REST call from Pythonimport requestsimport json

url = 'https://sandboxapic.cisco.com/api/v1/ticket’payload = {"username":“devnetuser","password":"Cisco123!"}header = {"content-type": "application/json”}response= requests.post(url,data=json.dumps(payload), headers=header, verify=False)

print(response.text)

Source code file: create-ticket.py

DEVNET-2003 28

Page 29: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 29DEVNET-2003

Demo

Page 30: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 30DEVNET-2003

Lab-3

Page 31: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Lab 3 – 10 Minutes

Go to directory: coding102-REST-python-ga get-network-hosts.py

Edit file to store values below in variable(s) and print to screen: hostIP, hostMac, hostType

DEVNET-2003 31

Page 32: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Final Thoughts

32DEVNET-2003

Page 33: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

DevNet GitHub

coding-skills-sample-code repository• All the code for this session • All the code for all of the Coding Skills Learning Labs

https://github.com/CiscoDevNet

DEVNET-2003 33

Page 34: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

• Learning Labs• http://learninglabs.cisco.com

• Network Programmability Springboard• https://learninglabs.cisco.com/springboards

• Devnet• http://developer.cisco.com

• The DevNet Zone Learning Labs• Come see us!

Where to go From Here

34DEVNET-2003

Page 35: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Complete Your Online Session Evaluation

Don’t forget: Cisco Live sessions will be available for viewing on-demand after the event at CiscoLive.com/Online

• Give us your feedback to be entered into a Daily Survey Drawing. A daily winner will receive a $750 Amazon gift card.

• Complete your session surveys through the Cisco Live mobile app or from the Session Catalog on CiscoLive.com/us.

35DEVNET-2003

Page 36: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 36DEVNET-2003

Continue Your Education• Demos in the Cisco campus

• Walk-in Self-Paced Labs

• Lunch & Learn

• Meet the Engineer 1:1 meetings

• Related sessions

Page 37: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public

Please join us for the Service Provider Innovation Talk featuring:

Yvette Kanouff | Senior Vice President and General Manager, SP BusinessJoe Cozzolino | Senior Vice President, Cisco Services

Thursday, July 14th, 201611:30 am - 12:30 pm, In the Oceanside A room

What to expect from this innovation talk• Insights on market trends and forecasts• Preview of key technologies and capabilities • Innovative demonstrations of the latest and greatest products• Better understanding of how Cisco can help you succeed

Register to attend the session live now or watch the broadcast on cisco.com

DEVNET-2003 37

Page 38: Coding 102: REST API Basics using APIC-EM

Thank you

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco PublicDEVNET-2003 38

Page 39: Coding 102: REST API Basics using APIC-EM
Page 40: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 40

DevNet APIC-EM Always On Sandbox

Always there for you to use…

•Register on Cisco DevNet (http://developer.cisco.com ) to get the latest information about APIs as they are release•You can use the Always-On APIC-EM GA Sandbox at anytime•You can use https://learninglabs.cisco.com at anytime•APIC-EM Login: username: admin password: C!sc0123

https://sandboxapic.cisco.com:9443

DEVNET-2003

Page 41: Coding 102: REST API Basics using APIC-EM

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 41

DevNet GitHub

coding-skills-sample-code repository• All the code for this session • All the code for all of the Coding Skills Learning Labs

https://github.com/CiscoDevNet

DEVNET-2003