官网API介绍:
《openstack-bk-api-ref.pdf》
网址:
http://developer.openstack.org/api-ref.html
下载pdf:
http://developer.openstack.org/api-ref-guides/bk-api-ref.pdf
关于RESTful可以参考官方文档:
http://docs.openstack.org/zh_CN/api/quick-start/content/index.html
pdf版本为:
svn://200.200.0.10/vt/vt/VTP/Cloud2.0/Documents/13.预研/01.预研报告/预研文档分享/cinder/openstack-restful-api/api-quick-start-onepager.pdf
实例(这里的python -m json.tool可以让输出看起来是json格式):
# curl -s -X POST http://192.168.15.180:5000/v2.0/tokens -H "Content-Type:application/json" \
-d '{"auth": {"tenantName":"admin","passwordCredentials": \ {"username":"admin","password":"83c53e477779468a"}}}'|python -m json.tool
输出比较多,一般我们只是看token id,这里把其他信息删除:
"token": {
"audit_ids": [
"d5LrCnXNSB-aGwz0x6bSJg"
],
"expires": "2015-08-26T09:24:07Z",
"id": "fc62e669e3b0417faac71e0840c30354",
"issued_at": "2015-08-26T08:24:07.511407",
"tenant": {
"description": "admin tenant",
"enabled": true,
"id": "5f8211463783496b9621c51d5666e70e",
"name": "admin"
}
},
可以通过cinder --debug xxx看到发送的具体的RESTful API
这里发了三条curl命令:
(1)curl -g -i -X GET http://192.168.15.180:5000/v2.0/ -H "Accept: application/json" -H "User-Agent: python-keystoneclient"
(2)curl -g -i -X GET http://192.168.15.180:8776/ -H "Accept: application/json" -H "User-Agent: python-keystoneclient"
(3)curl -g -i -X GET http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/volumes/detail -H "User-Agent: python-cinderclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}58e60a153a105abbc64cd8f28ef8de8f227660fc"
解释:前面两条命令不用管,实际发的是第三条命令。如果自己写API不掉上面两个,实际上也会发上面的两个命令。
实际上List的API只有这一个
curl -g -i -X GET http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/volumes/detail -H "User-Agent: python-cinderclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}58e60a153a105abbc64cd8f28ef8de8f227660fc"
3.1 获取token -- 参考官网文档《http://docs.openstack.org/zh_CN/api/quick-start/content/index.html》给的命令
#curl -s -X POST http://192.168.15.180:5000/v2.0/tokens -H "Content-Type: application/json" -d '{"auth": {"tenantName": "'"$OS_TENANT_NAME"'", "passwordCredentials": {"username": "admin", "password": "83c53e477779468a"}}}' | python -m json.tool
这里会输出很多东西,只需要看token字段:
"token": {
"audit_ids": [
"UGH_HfiYQBy3DleHiZk0nw"
],
"expires": "2015-08-26T10:52:39Z",
"id": "aaa9da01640249abb6032cf013a4095c",
"issued_at": "2015-08-26T09:52:39.549531",
"tenant": {
"description": "admin tenant",
"enabled": true,
"id": "5f8211463783496b9621c51d5666e70e",
"name": "admin"
}
},所以这里获取的toke id是:aaa9da01640249abb6032cf013a4095c
3.2 使用获取的token发送HTTP请求
# curl -g -i -X GET http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/volumes/detail -H "User-Agent: python-cinderclient" -H "Accept: application/json" -H "X-Auth-Token: aaa9da01640249abb6032cf013a4095c"
这里X-Auth-Token后面就是在上面获取的token
而这里的5f8211463783496b9621c51d5666e70e为租户ID,可以通过keystone 的命令来查看:
curl -g -i -X POST http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/volumes -H "User-Agent: python-cinderclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}c88ee47d56a7d428f347fa1d2776783c405d7df9" -d '{"volume": {"status": "creating", "availability_zone": null, "source_volid": null, "display_description": null, "snapshot_id": null, "user_id": null, "size": 1, "display_name": "volume3", "imageRef": null, "attach_status": "detached", "volume_type": null, "project_id": null, "metadata": {}}}'
curl -g -i -X DELETE http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/volumes/73cbde94-c0e6-4b8a-85fb-0ad873d075eb -H "User-Agent: python-cinderclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}74c65bdbf3449ec167c6779cd358e0b9fbaab4d7"
命令:
cinder --debug list
请求:
curl -g -i -X GET http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/volumes/detail -H "User-Agent: python-cinderclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}784a77f98f76124171231264290e63b9546f5b74"
创建命令:
# cinder --debug snapshot-create --display-name snap-volume2 volume2
所发请求:
curl -g -i -X POST http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/snapshots -H "User-Agent: python-cinderclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}00577623ba3ac3e4b349b3f08b3025b68a17e4a7" -d '{"snapshot": {"display_name": "snap-volume2", "force": false, "display_description": null, "volume_id": "40a7c155-30df-4235-93c7-b237616fdd15"}}'
DEBUG:keystoneclient.session:RESP: [200] content-length: 267 x-compute-request-id: req-a9bf8f39-5cf8-4bec-9ec5-d231852f8ae8 connection: keep-alive date: Wed, 26 Aug 2015 10:48:26 GMT content-type: application/json x-openstack-request-id: req-a9bf8f39-5cf8-4bec-9ec5-d231852f8ae8
请求返回:
RESP BODY: {"snapshot": {"status": "creating", "display_name": "snap-volume2", "created_at": "2015-08-26T10:48:26.817644", "display_description": null, "volume_id": "40a7c155-30df-4235-93c7-b237616fdd15", "metadata": {}, "id": "c75753ef-ecc6-400a-b717-bdebc03f4bf2", "size": 1}}
格式化输出:
[root@controller-rdo ~(keystone_admin)]# cat test| awk -F"," '{print $1;print $2;print $3;print $4;print $5;print $6;print $7}'
{"snapshot": {"status": "creating"
"display_name": "snap-volume2"
"created_at": "2015-08-26T10:48:26.817644"
"display_description": null
"volume_id": "40a7c155-30df-4235-93c7-b237616fdd15"
"metadata": {}
"id": "c75753ef-ecc6-400a-b717-bdebc03f4bf2"
命令:
cinder --debug snapshot-delete snap-volume2
请求:
curl -g -i -X DELETE http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/snapshots/c75753ef-ecc6-400a-b717-bdebc03f4bf2 -H "User-Agent: python-cinderclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}e51ce4e914925d9dec15ff307a388b988c792740"
备注:c75753ef-ecc6-400a-b717-bdebc03f4bf2为快照ID
命令:
cinder --debug snapshot-list
请求:
curl -g -i -X GET http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/snapshots/detail -H "User-Agent: python-cinderclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}92d62b8f42ce1dfb102b31419ba8b4e93f538ffd"
返回RESP BODY:
{"snapshots": [{"status": "available", "display_name": "snap-volume2", "created_at": "2015-08-26T10:48:26.000000", "display_description": null, "os-extended-snapshot-attributes:progress": "100%", "volume_id": "40a7c155-30df-4235-93c7-b237616fdd15", "os-extended-snapshot-attributes:project_id": "5f8211463783496b9621c51d5666e70e", "metadata": {}, "id": "c75753ef-ecc6-400a-b717-bdebc03f4bf2", "size": 1}]}
命令:
cinder --debug extend volume2 2
请求:
curl -g -i -X POST http://192.168.15.180:8776/v1/5f8211463783496b9621c51d5666e70e/volumes/40a7c155-30df-4235-93c7-b237616fdd15/action -H "User-Agent: python-cinderclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}5c2b388fa9230f5da4f03d6a5b93b00ac996f6eb" -d '{"os-extend": {"new_size": 2}}'