Más contenido relacionado La actualidad más candente (20) Similar a BEST REST in OpenStack (20) Más de Vikram G Hosakote (6) BEST REST in OpenStack2. BEST REST in OpenStack
Vikram Hosakote
Sr. Software Engineer, Cisco Systems
vhosakot@cisco.com
DEVNET-2004
3. • What is a RESTful service?
• REST methods
• REST in OpenStack
• Hands-on exercises
• Troubleshooting / debugging REST APIs
• Q & A
Agenda
5. © 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
7. © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 7DEVNET-2055
REST in OpenStack
9. 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
10. 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
11. © 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
12. 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
13. © 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)
14. 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
15. © 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)
17. © 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
18. © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 18DEVNET-2055
Troubleshooting / debugging REST APIs using
Python
import httplib
import logging
import requests
httplib.HTTPConnection.debuglevel = 1
logging.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/')
19. © 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.com
send: 'GET / HTTP/1.1rnHost: www.cisco.comrnConnection: keep-alivernAccept-Encoding: gzip,
deflaternAccept: */*rnUser-Agent: python-requests/2.10.0rnrn'
reply: 'HTTP/1.1 200 OKrn'
header: Server: Apache
header: ETag: "10839-53789488e5b33"
header: Accept-Ranges: bytes
header: Content-Encoding: gzip
header: CDCHOST: wemxweb-publish-prod1-01
header: Content-Length: 14215
header: Content-Type: text/html
header: Expires: Wed, 13 Jul 2016 20:25:27 GMT
header: Cache-Control: max-age=0, no-cache, no-store
header: Pragma: no-cache
header: Date: Wed, 13 Jul 2016 20:25:27 GMT
header: Connection: keep-alive
header: access-control-allow-origin: *
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 14215
>>> print result
<Response [200]>
20. © 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
26. © 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')
27. © 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
29. © 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-age
max-stale
min-fresh
no-cache
no-store
no-transform
only-if-cached
must-revalidate
Public
private
30. © 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-
proxy-behavior
• Memcached
• http://docs.openstack.org/ha-guide/controller-ha-memcached.html
31. © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Public 31DEVNET-2055
REST API caching and cache-aware REST clients
33. © 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
34. © 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/json
Accept: application/json
{
"networks": [
{
"name": "sample_network_1",
"admin_state_up": false
},
{
"name": "sample_network_2",
"admin_state_up": false
}]
}