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就需要使用这个令牌。
1 | 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数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | { "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就是刚刚得到的那个令牌
1 | curl -X 'GET' -H "X-Auth-Token:bc422dc370324535a64699640f50a3ab" -v http: //controller_ip:5000/v2.0/tenants | python -mjson.tool |
这里面得到这个用户的项目(tenant)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | { "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和上面的那个不一样
1 | 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用户下的这个项目的令牌,得到如下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | { "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。
接下来我们开始看看这个项目里面的虚拟机
1 | curl -v -H "X-Auth-Token:b1470e1f13284a8b8f979eb652e29db5" http: //controller_ip:8774/v2/tenant_id/servers |
把得到的令牌写好,执行后得到如下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | { "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有哪些
1 | curl -v -H "X-Auth-Token:b1470e1f13284a8b8f979eb652e29db5" http: //controller_ip:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images | python -mjson.tool |
执行后得到以下信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | { "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的命令
1 | 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
设置好后输入网址http://controller_ip:5000/v2.0/tokens,并且把要post的json的数据写入到body里面,最后点击按钮SEND即可,可以得到和curl一样的json数据
接下来试试使用get的方法
我们得到了tenant的token,就可以使用get方法获取其虚拟机的信息
注意要先设置token在header中,name为X-Auth-Token,value为token,然后输入网址,选择为get方法,点击SEND即可得到:
其他的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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现