Upload
vikram-g-hosakote
View
77
Download
0
Embed Size (px)
Citation preview
BEST REST in OpenStackVikram Hosakote
Sr. Software Engineer, Cisco Systems
DEVNET-2004
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 3Presentation ID
• What is a RESTful service?• REST methods• REST in OpenStack• Hands-on exercises• Troubleshooting / debugging REST APIs• Q & A
Agenda
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 4Presentation ID
What is a RESTful service?
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 5DEVNET-2055
What is a RESTful service?
• Representational State Transfer (REST) is a stateless, client-server based, cacheable web service
• Uses HTTP protocol for communication
• Data is considered as a “resource” and accessed using Uniform Resource Identifiers (URIs) that typically look like a web link
• URI is RFC RFC3986 (example: http://www.ciscolive.com/us/learn/sessions/session-catalog)
• Uses HTTP clients like curl, browsers (Google Chrome Postman) and wget
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 6Presentation ID
REST in OpenStack
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 7DEVNET-2055
REST in OpenStack
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 8Presentation ID
Hands-on exercises
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 9Presentation ID
Hands-on exercises
• Run OpenStack Neutron CLIs and analyze REST packets in WireShark
• REST for big data use cases – REST pagination in Neutron
• Implementing a RESTful server using Python Flask
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 10Presentation ID
Hands-on exercises
• Run OpenStack Neutron CLIs and analyze REST packets in WireShark
• REST for big data use cases – REST pagination in Neutron
• Implementing a RESTful server using Python Flask
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 11DEVNET-2055
• start-capture <your first name>-<GET|POST|PUT|DELETE>.pcap
• Run an OpenStack Neutron CLI
• stop-capture
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 12Presentation ID
Hands-on exercises
• Run OpenStack Neutron CLIs and analyze REST packets in WireShark
• REST for big data use cases – REST pagination in Neutron
• Implementing a RESTful server using Python Flask
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 13DEVNET-2055
• Enable neutron pagination and restart neutorn-server
• cd Cisco-Live-Workshop/scripts
• ./big_data_create_networks.py
• Run commands in Cisco-Live-Workshop/scripts/big_data_neutron_curl.txt on GitHub(https://github.com/vhosakot/Cisco-Live-Workshop)
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 14Presentation ID
Hands-on exercises
• Run OpenStack Neutron CLIs and analyze REST packets in WireShark
• REST for big data use cases – REST pagination in Neutron
• Implementing a RESTful server using Python Flask
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 15DEVNET-2055
• cd Cisco-Live-Workshop/scripts
• ./rest_server.py
• Run commands in Cisco-Live-Workshop/scripts/rest_client_steps.txt on GitHub(https://github.com/vhosakot/Cisco-Live-Workshop)
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 16Presentation ID
Troubleshooting / debugging REST APIs
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 17DEVNET-2055
Troubleshooting / debugging REST APIs
• curl -v (verbose mode)
• wget -v (verbose mode)
• wget -d or wget --debug (debug mode)
• neutron -v or neutron -d or neutron --debug (neutron debug mode)
• http://www.httpdebugger.com
• https://www.wireshark.org
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 18DEVNET-2055
Troubleshooting / debugging REST APIs using Python
import httplibimport loggingimport requests
httplib.HTTPConnection.debuglevel = 1logging.basicConfig()logging.getLogger().setLevel(logging.DEBUG)requests_log = logging.getLogger("requests.packages.urllib3")requests_log.setLevel(logging.DEBUG)requests_log.propagate = True
result = requests.get('http://www.cisco.com/')
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 19DEVNET-2055
Troubleshooting / debugging REST APIs using Python
>>> result = requests.get('http://www.cisco.com/')INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): www.cisco.comsend: 'GET / HTTP/1.1\r\nHost: www.cisco.com\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.10.0\r\n\r\n'reply: 'HTTP/1.1 200 OK\r\n'header: Server: Apacheheader: ETag: "10839-53789488e5b33"header: Accept-Ranges: bytesheader: Content-Encoding: gzipheader: CDCHOST: wemxweb-publish-prod1-01header: Content-Length: 14215header: Content-Type: text/htmlheader: Expires: Wed, 13 Jul 2016 20:25:27 GMTheader: Cache-Control: max-age=0, no-cache, no-storeheader: Pragma: no-cacheheader: Date: Wed, 13 Jul 2016 20:25:27 GMTheader: Connection: keep-aliveheader: access-control-allow-origin: *DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 14215
>>> print result<Response [200]>
© 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.
20Presentation ID
Thank you
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 21Presentation ID
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 22Presentation ID
Q & A
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 24Presentation ID
Additional slides
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 25Presentation ID
Mocking a REST server to unit-test REST APIs
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 26DEVNET-2055
Mocking a REST server to unit-test REST APIs
• https://github.com/openstack/requests-mock
>>> import requests>>> import requests_mock
>>> session = requests.Session()>>> adapter = requests_mock.Adapter()>>> session.mount('mock', adapter)
>>> adapter.register_uri('GET', 'mock://test.com', text='data')>>> resp = session.get('mock://test.com')>>> resp.status_code, resp.text(200, 'data')
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 27DEVNET-2055
Mocking a REST server to unit-test REST APIs
• https://pypi.python.org/pypi/mock-server/0.3.7
• https://github.com/gabrielfalcao/HTTPretty
• http://python-mock-tutorial.readthedocs.io/en/latest/mock.html
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 28Presentation ID
REST API caching and cache-aware REST clients
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 29DEVNET-2055
REST API caching and cache-aware REST clients
• RFC 7234 - https://tools.ietf.org/html/rfc7234#section-5
• REST supports HTTP cache-control headers:max-agemax-stalemin-freshno-cacheno-storeno-transformonly-if-cachedmust-revalidatePublicprivate
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 30DEVNET-2055
REST API caching and cache-aware REST clients
• Keystone caching• http://docs.openstack.org/admin-guide/keystone_caching_layer.html• http://docs.openstack.org/developer/keystone/configuration.html#caching-layer
• Glance image caching• http://docs.openstack.org/developer/glance/cache.html
• OSLO caching• http://docs.openstack.org/developer/oslo.cache/api.html• https://specs.openstack.org/openstack/api-wg/guidelines/http.html#http-caching-and-pro
xy-behavior
• Memcached• http://docs.openstack.org/ha-guide/controller-ha-memcached.html
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 31DEVNET-2055
REST API caching and cache-aware REST clients
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 32Presentation ID
Bulk REST operations and REST API batching
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 33DEVNET-2055
Bulk REST operations and REST API batching
• http://developer.openstack.org/api-ref-networking-v2.html#bulkCreateNetwork
• https://wiki.openstack.org/wiki/Neutron/APIv2-specification#Bulk_version
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 34DEVNET-2055
Bulk REST operations and REST API batching
• Bulk REST API JSON request format:
POST v2.0/networks.json
Content-Type: application/jsonAccept: application/json
{"networks": [ { "name": "sample_network_1", "admin_state_up": false }, { "name": "sample_network_2", "admin_state_up": false }]}