28
开发训练营 梁博 [email protected] OPENSTACK API DEVELOPMENT WORKSHOP

how to use openstack api

Embed Size (px)

Citation preview

Page 1: how to use openstack api

开发训练营梁博 [email protected]

OPENSTACK API DEVELOPMENT WORKSHOP

Page 2: how to use openstack api

Conveniently procrastinate client-centric technology via highly efficient manufactured

Conveniently procrastinate client-centric technology via highly efficient manufactured products. Seamlessly productivate go forward

benefits

COURSE INTRODUCTION

DAY 01• Before Developing• Developing OpenStack • Developer’s Tools• OpenStack API (RESTful API)• How to use OpenStack API• OpenStack API Binding

DAY 02• Workshop (Use OpenStack API)• OpenStack API SDKs• Workshop (A tiny project)

Page 3: how to use openstack api

梁博 在云计算领域有8年以上的项⺫⽬目经验,熟悉OpenStack架构设计,具有在中⼤大型集成项⺫⽬目应⽤用OpenStack及相关架构设计和项⺫⽬目实施经验,曾负责设计实施过国内⾸首个基于OpenStack的公有云平台,并在OpenStack⾹香港峰会上做过专题报告,有丰富的培训和咨询服务经验。

[email protected]://github.com/onetownhttp://liangbo.me

Infrastructure 90%

Storage, Network 40%

Front-end, HTML, CSS, Javascript 60%

Python, C Sharp, Go-lang Development 90%Liang Bo (TRYSTACK.CN架构师)

Page 4: how to use openstack api

Talk is cheap, show me the code.

Page 5: how to use openstack api

Python – Before Developing

Page 6: how to use openstack api

Python For BeginnersAn experienced programmer in any programming language(whatever it may be ) can pick up Python very quickly.

03 Looking for Something Specific

The Python Package Index provides many libraries for particular application, you just need to know how to find it.

04 Looking for Help

Use Google instead any other search engines.

02Learning

Before getting started, you may want to find out which IDEs or Text Editors are tailored to make Python editing easy.

01Installing

Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python.

Page 7: how to use openstack api

OpenStack – Developer’s tools

!

Page 8: how to use openstack api

Libraries• Cliff• Debtcollector• Keystonemiddleware• Pbr• PyCADF• Pylockfile• Stevedore• TaskFlow• Tooz

• Oslo.concurrency• Oslo.config• Oslo.db• Oslo.i18n• Oslo.log• Oslo.policy• Oslo.rootwrap• Oslo.serialization• Oslo.utils• oslo.sphinx

Page 9: how to use openstack api

IDEs & Text EditorsIt is necessary to have effective tools to do good work.

� � � �

IDE

PycharmText Editor

Sublime TextIDE

EclipseText Editor

Vim

Page 10: how to use openstack api

OpenStack SDKs

OpenStack provides a lot of SDKs to make programming cloud application easy. You don’t need deal with RESTful APIs.

PHPPhp-

opencloud .NetOpenstack.net

JavajClouds Golang

gophercloud"

It’s Your Style !

Pick any programming language you like.

AROUND THE WORLD

Most OpenStack Application made by python

Most OpenStack

Page 11: how to use openstack api

Portfolio

You can use ANY LANGUAGE, ANY TOOLS to build your own cloud application

UISpecific UI Integration

Skills Needed: • CSS• HTML• Javascript• Web Development• RESTful API

ApplicationSpecific Function Integration

Skills Needed:• Python• TaskFlow• RESTful API

Page 12: how to use openstack api

RESTful API

#

Page 13: how to use openstack api

$API

%JAVA

Your System

REQUEST:  curl  -­‐i -­‐X  GET  http://172.16.103.7:35357/v2.0/users  -­‐H  "User-­‐Agent:  python-­‐keystoneclient"  -­‐H  "X-­‐Auth-­‐Token:  TOKEN_REDACTED"

HEAD:{'date':   'Mon,  18  May  2015  04:30:52  GMT',  'content-­‐type':   'application/json',   'content-­‐length':   '2038',  

'vary':  'X-­‐Auth-­‐Token'}BODY:{"users":  [{"username":  "liangbo",   "name":  

"liangbo",   "id":  "00a6d76201b7461490cfa96794ce3d7b",  "enabled":  true,   "email":  "[email protected]",  

"tenantId":  "76e9492941f34034b380ec936490adcb"},

Use RESTful APIs

Page 14: how to use openstack api

$API

%JAVA

Your System

REQUEST:  curl  -­‐i -­‐X  GET  http://172.16.103.7:35357/v2.0/users  -­‐H  "User-­‐Agent:  python-­‐keystoneclient"  -­‐H  "X-­‐Auth-­‐Token:  TOKEN_REDACTED"

HEAD:{'date':   'Mon,  18  May  2015  04:30:52  GMT',  'content-­‐type':   'application/json',   'content-­‐length':   '2038',  

'vary':  'X-­‐Auth-­‐Token'}BODY:{"users":  [{"username":  "liangbo",   "name":  

"liangbo",   "id":  "00a6d76201b7461490cfa96794ce3d7b",  "enabled":  true,   "email":  "[email protected]",  

"tenantId":  "76e9492941f34034b380ec936490adcb"},

SDK

Keystone.userlist(); Use SDKs

Page 15: how to use openstack api

Auth Token Requestcurl -i 'http://cloud.trystack.cn:5000/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth":{"tenantName": "99cloud",

"passwordCredentials": {"username":"liangbo","password":"lb23zx"}

}}’

HTTP/1.1 200 OKVary: X-Auth-TokenContent-Type: application/jsonContent-Length: 2881Date: Mon, 18 May 2015 05:04:30 GMT

Page 16: how to use openstack api

Auth Token Response{"access": {"token": {"issued_at": "2015-05-18T05:04:30.749551", "expires": "2015-05-18T06:04:30Z", "id": "c1eddcbe9b39413aa9838957d2c6912f", "tenant": {"description": "\u4e5d\u5dde\u4e91\u5728\u7ebf\u670d\u52a1\u63d0\u4f9b", "enabled": true, "id": "76e9492941f34034b380ec936490adcb", "name": "99cloud"}, "audit_ids": ["08I-DEsQTtyN8oECsZzhCQ"]}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://172.16.103.7:8774/v2/76e9492941f34034b380ec936490adcb", "region": "regionOne", "internalURL": "http://172.16.103.7:8774/v2/76e9492941f34034b380ec936490adcb", "id": "00fb6ad4d7814ce9b82a9be2f585f05c", "publicURL": "http://172.16.103.7:8774/v2/76e9492941f34034b380ec936490adcb"}], "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://172.16.103.7:9696", "region": "regionOne", "internalURL": "http://172.16.103.7:9696", "id": "44ee9426b83b4f57b0b25b5eeeb3840a", "publicURL": "http://172.16.103.7:9696"}], "endpoints_links": [], "type": "network", "name": "neutron"}, {"endpoints": [{"adminURL": "http://172.16.103.7:9292", "region": "regionOne", "internalURL": "http://172.16.103.7:9292", "id": "aac4cad8e89d49ba83a7c37ac52499c5", "publicURL": "http://172.16.103.7:9292"}], "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": "http://172.16.103.7:8000/v1", "region": "regionOne", "internalURL": "http://172.16.103.7:8000/v1", "id": "04b4d9582fc8474f94488c6dfa7faba7", "publicURL": "http://172.16.103.7:8000/v1"}], "endpoints_links": [], "type": "cloudformation", "name": "heat-cfn"}, {"endpoints": [{"adminURL": "http://172.16.103.7:8776/v1/76e9492941f34034b380ec936490adcb", "region": "regionOne", "internalURL": "http://172.16.103.7:8776/v1/76e9492941f34034b380ec936490adcb", "id": "110c884741dc4817b2ff025729994781", "publicURL": "http://172.16.103.7:8776/v1/76e9492941f34034b380ec936490adcb"}], "endpoints_links": [], "type": "volume", "name": "cinder"}, {"endpoints": [{"adminURL": "http://172.16.103.7:8004/v1/76e9492941f34034b380ec936490adcb", "region": "regionOne", "internalURL": "http://172.16.103.7:8004/v1/76e9492941f34034b380ec936490adcb", "id": "2b17c5290b044743ab5871c2761a37fe", "publicURL": "http://172.16.103.7:8004/v1/76e9492941f34034b380ec936490adcb"}], "endpoints_links": [], "type": "orchestration", "name": "heat"}, {"endpoints": [{"adminURL": "http://172.16.103.7:35357/v2.0", "region": "regionOne", "internalURL": "http://172.16.103.7:5000/v2.0", "id": "5daf9e9e4cf24bf6876547dd8f8be78b", "publicURL": "http://172.16.103.7:5000/v2.0"}], "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": "liangbo", "roles_links": [], "id": "00a6d76201b7461490cfa96794ce3d7b", "roles": [{"name": "admin"}, {"name": "_member_"}], "name": "liangbo"}, "metadata": {"is_admin": 0, "roles": ["7a38637226bd4ab293f1cdeb9d8892a7", "e5a8b1766760491caf66eff2f8f8d654"]}}}

Page 17: how to use openstack api

User

Keystone

Nova

Glance

Neutron

Credentials

Token

Toekn + request for VM

Verify token

Token + request for images

Verify token

Images

Token + request to plug VIF into net

Verify token

Token + verify user access to VIF

Successful response

Successful response

Keystone flowchart

Page 18: how to use openstack api

Use Python Requests>>> import requests>>> r = requests.get('https://api.github.com/user',auth=('[email protected]', ‘password'))>>> r.status_code200>>> r.headers['content-type']'application/json; charset=utf-8’>>> r.textu'{"login":"onetown","id":62006,"avatar_url":"https://avatars.githubusercontent.com/u/62006?v=3","gravatar_id":"","url":"https://api.github.com/users/onetown","html_url":"https://github.com/onetown","followers_url":"https://api.github.com/users/onetown/followers","following_url":"https://api.github.com/users/onetown/following{/other_user}","gists_url":"https://api.github.com/users/onetown/gists{/gist_id}","starred_url":"https://api.github.com/users/onetown/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/onetown/subscriptions","organizations_url":"https://api.github.com/users/onetown/orgs","repos_url":"https://api.github.com/users/onetown/repos","events_url":"https://api.github.com/users/onetown/events{/privacy}","received_events_url":"https://api.github.com/users/onetown/received_events","type":"User","site_admin":false,"name":"oNeToWn","company":null,"blog":"http://liangbo.me","location":”…>>>

Page 19: how to use openstack api

Use Python JSON>>> import json>>> data = [{'a':"A",'b':(2,4),'c':3.0}]>>> data_string = json.dumps(data)>>> print data_string[{"a": "A", "c": 3.0, "b": [2, 4]}]>>> decoded = json.loads(data_string)>>> decoded[{u'a': u'A', u'c': 3.0, u'b': [2, 4]}]>>>

Page 20: how to use openstack api

Launch an Instance?>>> import os>>> creds = {}>>> creds['username']='liangbo‘>>> creds['api_key'] = ‚‘********‘>>> creds['auth_url']='http://cloud.trystack.cn:5000/v2.0‘>>> creds['project_id']='99cloud‘>>> nova = client.Client(**creds)>>> nova.security_groups.list()[<SecurityGroup description=DMZ Security Group, id=366dc2df-07af-409f-9841-d8b0dd75f379, name=DMZ, rules=[{u'from_port': 3389, u'group': {}, u'ip_protocol': u'tcp', u'to_port': 3389, u'parent_group_id': u'366dc2df-07af-409f-9841-d8b0dd75f379', u'ip_range': {u'cidr': u'0.0.0.0/0'}, u'id': u'122a4212-a1a6-44d4-a924-b433612cda42'}, {u'from_port': 22, u'group': {}, u'ip_protocol': u'tcp', u'to_port': 22, u'parent_group_id': u'366dc2df-07af-409f-9841-d8b0dd75f379', u'ip_range': {u'cidr': u'0.0.0.0/0'}, u'id': u'7685cebf-e1ee-4e5a-a32e-7a4ab897febc'}, {u'from_port': 80, u'group': {}, u'ip_protocol': u'tcp', u'to_port': 80, u'parent_group_id': u'366dc2df-07af-409f-9841-d8b0dd75f379', u'ip_range': {u'cidr': u'0.0.0.0/0'}, u'id': u'80ef4de3-0cf9-479c-8d06-73eef4cdea6e'}, {u'from_port': -1, u'group': {}, ...

Page 21: how to use openstack api

Ready to go!!!!

Make Your Own

Page 22: how to use openstack api

Design  Your  Own

• Python• Flask• A  micro  python  web  framework

Page 23: how to use openstack api

Design Your Own

One Single App.py

Fully functional Read-only OpenStack

& Static folder for javascripts, css and fonts

♥ Templates folder for rendered html

( App.py is the real web application

IMAGE INFO : SimpleSmart Design Office

Page 24: how to use openstack api

It should looks like this

Page 25: how to use openstack api

With This Demo

Image

Compute

Network

!

$

"Image Service

This is a client for the OpenStack Images API. There’s a Python API (the glanceclient module) and a command-line script (installed as glance).

Network Service

In order to use the python neutron client directly, you must first obtain an auth token and identify which endpoint you wish to speak to. Once you have done so, you can use the API like so:

Compute ServiceThis is a client for OpenStack Nova API. There’s a Python API (the

novaclient module), and a command-line script (installed as nova). Each implements the entire OpenStack Nova API.

Page 26: how to use openstack api

Coding2  hours

Page 27: how to use openstack api
Page 28: how to use openstack api

Questions?