Openstack api 学习文档 & restclient使用文档

Openstack api 学习文档 & restclient使用文档

转载请注明http://www.cnblogs.com/juandx/p/4943409.html

这篇文档总结一下我初步学习Openstack的api,并且趁这个机会学习一下Firefox的restclient客户端。

一、使用curl命名来使用openstack的api

当然,首先你得有个openstack的环境,我是自己搭建的kilo,我们先用curl来使用api,注意自己改一下ip还有用户名和密码,下面这个命令就会得到一个令牌(token)来使用,以后调用api就需要使用这个令牌。

curl -k -X 'POST' -v http://controller_ip:5000/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "ADMIN", "password":"MY_PASSWORD"}}}' -H 'Content-type: application/json' | python -mjson.tool

这样后会得到如下的json数据

{
"access": {
"metadata": {
"is_admin": 0,
"roles": []
},
"serviceCatalog": [],
"token": {
"audit_ids": [
"WPon6i60TamUfqcsPV2oVg"
],
"expires": "2015-11-06T11:19:19Z",
"id": "bc422dc370324535a64699640f50a3ab",//这个id就是令牌(token)
"issued_at": "2015-11-06T10:19:19.559266"
},
"user": {
"id": "9c20d9fe21a14741ac1486b2db058bd5",
"name": "admin",
"roles": [],
"roles_links": [],
"username": "admin"
}
}
}

下面得到这个用户的所有项目(tenant),注意下面的token就是刚刚得到的那个令牌

curl -X 'GET' -H  "X-Auth-Token:bc422dc370324535a64699640f50a3ab" -v http://controller_ip:5000/v2.0/tenants | python -mjson.tool

这里面得到这个用户的项目(tenant)

{
"tenants": [
{
"description": null,
"enabled": true,
"id": "92afd13a680645c191a2aab7422a7a3b",
"name": "AIM"
},
{
"description": null,
"enabled": true,
"id": "b3457999de4d4add99e89f4f299f5857",
"name": "ECM"
},
{
"description": "Admin Project",
"enabled": true,
"id": "de8f43bfe4244a1aa315a70a43248d60",
"name": "admin"
},
{
"description": null,
"enabled": true,
"id": "f3c1015ff67e4cd18b022b9b51eb1123",
"name": "IM"
},
{
"description": null,
"enabled": true,
"id": "fae79b8f91b944b985d2b35373cf9bb2",
"name": "Commerce"
}
],
"tenants_links": []
}

可以看到我这里ADMIN用户总共有5个tenant,我们选取一个即可,就选取名为admin的项目,id看到为de8f43bfe4244a1aa315a70a43248d60

接下来我们去一个项目(tenant)看看它的虚拟机、等信息。

首先需要得到这个tenant的令牌信息,注意这个token和上面的那个不一样

curl -k -X 'POST' -v http://controller_ip:5000/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "ADMIN", "password":"MY_PASSWORD"},"tenantId":"de8f43bfe4244a1aa315a70a43248d60"}}' -H 'Content-type: application/json' | python -mjson.tool

注意这里看到用户名和密码还是和之前一样,但是多了一个tenantId的字段,这里说明了我们需要的是ADMIN用户下的这个项目的令牌,得到如下信息:

{
"access": {
"metadata": {
"is_admin": 0,
"roles": [
"cf87d1b66b044cdd94972d72d6e4d474"
]
},
"serviceCatalog": [
{
"endpoints": [
{
"adminURL": "http://controller:9292",
"id": "5f892fde3a2241c989355cdd6beea4b5",
"internalURL": "http://controller:9292",
"publicURL": "http://controller:9292",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "glance",
"type": "image"
},
{
"endpoints": [
{
"adminURL": "http://controller:8774/v2/de8f43bfe4244a1aa315a70a43248d60",
"id": "0c52d8a741f740a6b83be51ae7082b62",
"internalURL": "http://controller:8774/v2/de8f43bfe4244a1aa315a70a43248d60",
"publicURL": "http://controller:8774/v2/de8f43bfe4244a1aa315a70a43248d60",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "nova",
"type": "compute"
},
{
"endpoints": [
{
"adminURL": "http://controller:9696",
"id": "3d70017df8ee40d698988ad52b25cc8e",
"internalURL": "http://controller:9696",
"publicURL": "http://controller:9696",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "neutron",
"type": "network"
},
{
"endpoints": [
{
"adminURL": "http://controller:35357/v2.0",
"id": "23bb295b14554d2b89535d67632e7c60",
"internalURL": "http://controller:5000/v2.0",
"publicURL": "http://controller:5000/v2.0",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "keystone",
"type": "identity"
}
],
"token": {
"audit_ids": [
"XNBLV1ohSXensbuHqwTo3Q"
],
"expires": "2015-11-06T11:29:22Z",
"id": "b1470e1f13284a8b8f979eb652e29db5",
"issued_at": "2015-11-06T10:29:22.423828",
"tenant": {
"description": "Admin Project",
"enabled": true,
"id": "de8f43bfe4244a1aa315a70a43248d60",
"name": "admin"
}
},
"user": {
"id": "9c20d9fe21a14741ac1486b2db058bd5",
"name": "admin",
"roles": [
{
"name": "admin"
}
],
"roles_links": [],
"username": "admin"
}
}
}

这里面有很多关于这个tenant的信息,其中最重要的是token的id,为b1470e1f13284a8b8f979eb652e29db5,这是我们后面调取api需要的,还有注意de8f43bfe4244a1aa315a70a43248d60是tenant_id。

接下来我们开始看看这个项目里面的虚拟机

curl -v -H "X-Auth-Token:b1470e1f13284a8b8f979eb652e29db5" http://controller_ip:8774/v2/tenant_id/servers

把得到的令牌写好,执行后得到如下信息:

{
"servers": [
{
"id": "098982e0-251d-4306-9171-f4d52674fde3",
"links": [
{
"href": "http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/servers/098982e0-251d-4306-9171-f4d52674fde3",
"rel": "self"
},
{
"href": "http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/servers/098982e0-251d-4306-9171-f4d52674fde3",
"rel": "bookmark"
}
],
"name": "vm2"
},
{
"id": "86baaddc-3c3e-4687-87bc-2e22f53eb7ba",
"links": [
{
"href": "http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/servers/86baaddc-3c3e-4687-87bc-2e22f53eb7ba",
"rel": "self"
},
{
"href": "http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/servers/86baaddc-3c3e-4687-87bc-2e22f53eb7ba",
"rel": "bookmark"
}
],
"name": "vm1"
}
]
}

这里可以明显看到有两个虚拟机,分别为vm1和vm2,这都是我之前创建的。

再看看我的image有哪些

curl -v -H "X-Auth-Token:b1470e1f13284a8b8f979eb652e29db5" http://controller_ip:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images | python -mjson.tool

执行后得到以下信息

{
"images": [
{
"id": "cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
"links": [
{
"href": "http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images/cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
"rel": "self"
},
{
"href": "http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/images/cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
"rel": "bookmark"
},
{
"href": "http://controller:9292/images/cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
"rel": "alternate",
"type": "application/vnd.openstack.image"
}
],
"name": "windows7-x86_64-bpm20150724"
},
{
"id": "5c997a18-3f93-41bb-8ce2-724b10d0d418",
"links": [
{
"href": "http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images/5c997a18-3f93-41bb-8ce2-724b10d0d418",
"rel": "self"
},
{
"href": "http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/images/5c997a18-3f93-41bb-8ce2-724b10d0d418",
"rel": "bookmark"
},
{
"href": "http://controller:9292/images/5c997a18-3f93-41bb-8ce2-724b10d0d418",
"rel": "alternate",
"type": "application/vnd.openstack.image"
}
],
"name": "rhel7.1"
},
{
"id": "e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
"links": [
{
"href": "http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images/e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
"rel": "self"
},
{
"href": "http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/images/e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
"rel": "bookmark"
},
{
"href": "http://controller:9292/images/e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
"rel": "alternate",
"type": "application/vnd.openstack.image"
}
],
"name": "cirros-0.3.4-x86_64"
}
]
}

可以看到我一共有3个镜像

至此,其他api查看文档进行查看即可,都是相似的方法,这里仅作一个简单介绍。

二、使用火狐firefox 的 restclient 工具来 使用 openstack的api

下面我还尝试了一些Firefox的restclient,来调取api

当然需要打开火狐浏览器安装restclient的插件https://addons.mozilla.org/en-US/firefox/addon/restclient/,当然使用java的客户端也可以http://code.google.com/p/rest-client/

这里演示两个rest的方法来使用,get和post

先看看post:

我们来取一个ADMIN用户的admin的tenant的令牌。

对比下curl的命令

curl -k -X 'POST' -v http://controller_ip:5000/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "ADMIN", "password":"MY_PASSWORD"},"tenantId":"de8f43bfe4244a1aa315a70a43248d60"}}' -H 'Content-type: application/json' | python -mjson.tool

先设置http的header ,name为 Content-Type ,value为 application/json

Openstack api 学习文档 & restclient使用文档

设置好后输入网址http://controller_ip:5000/v2.0/tokens,并且把要post的json的数据写入到body里面,最后点击按钮SEND即可,可以得到和curl一样的json数据

Openstack api 学习文档 & restclient使用文档

接下来试试使用get的方法

我们得到了tenant的token,就可以使用get方法获取其虚拟机的信息

注意要先设置token在header中,name为X-Auth-Token,value为token,然后输入网址,选择为get方法,点击SEND即可得到:

Openstack api 学习文档 & restclient使用文档

其他的api几乎都是get和post的方法,这里简单演示了firefox的restclient的用法,希望大家多多指教

参考资料:

openstack quick start api:http://docs.openstack.org/zh_CN/api/quick-start/content/index.html

openstack api document:http://developer.openstack.org/api-ref.html

上一篇:ASP.NET MVC5+EF6+EasyUI 后台管理系统(90)-EF 扩展操作


下一篇:细说RESTful API之文档管理