openstack restful api 使用

Openstack每个项目的接口遵循REST原则,通常也被称为RESTful接口,如何使用restful api

第一件事情你必须使用你的证书来认证来获取authentication token。证书包含了用户名、密码和tenant(项目)。你可以openrc.sh文件中得到他们。token允许你不需要再次认证就能和其他的service endpoints交互。Tokens通常存在24小时比较好,当token过期后,你将会收到一个401(未被授权的)响应,然后你可以再请求一个token。


openstack 相关环境信息为:
Keystone IP: 192.168.12.1
用户名:admin
密码:password
租户ID: admin


验证一下,API是否正常工作.使用Curl工具验证,并获取token

curl -k -X 'POST' -v http://192.168.12.1:5000/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "admin", "password":"password"}, "tenantname":"admin"}}' -H 'Content-type: application/json'

返回

openstack restful api 使用_第1张图片

获取前面几行,其中的ID一项,即为我们以后认证需要的token,记下来:

X-Auth-Token: MIIMUAYJKoZIhvcNAQcCoIIMQTCCDD0CAQExDTALBglghkgBZQMEAgEwggqeBgkqhkiG9w0BBwGgggqPBIIKi3siYWNjZXNzIjogeyJ0b2tlbiI6IHsiaXNzdWVkX2F0IjogIjIwMTUtMTItMjNUMDI6NTA6MjcuMzc4NTkyIiwgImV4cGlyZXMiOiAiMjAxNS0xMi0yM1QwMzo1MDoyN1oiLCAiaWQiOiAicGxhY2Vob2xkZXIiLCAidGVuYW50IjogeyJkZXNjcmlwdGlvbiI6ICJBZG1pbiBUZW5hbnQiLCAiZW5hYmxlZCI6IHRydWUsICJpZCI6ICIyOTk2ZTQ2YzI1MTk0MTVkOGRlMmIxNDFkNmM2MDdiYSIsICJuYW1lIjogImFkbWluIn19LCAic2VydmljZUNhdGFsb2ciOiBbeyJlbmRwb2ludHMiOiBbeyJhZG1pblVSTCI6ICJodHRwOi8vMTkyLjE2OC4xMi4xOjg3NzQvdjIvMjk5NmU0NmMyNTE5NDE1ZDhkZTJiMTQxZDZjNjA3YmEiLCAicmVnaW9uIjogInJlZ2lvbk9uZSIsICJpbnRlcm5hbFVSTCI6ICJodHRwOi8vMTkyLjE2OC4xMi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EAwwPd3d3LmV4YW1wbGUuY29tAgEBMAsGCWCGSAFlAwQCATANBgkqhkiG9w0BAQEFAASCAQDbzMul-Fxn-lu7E2w-w+lUcetiH2wOLQw68Cv36H3bvLDljJXb8QECSMT1k0+-hVvnxPt2-h3CvG3Mzp7h5KHUVsRL2NNOFJln7M64rjvegmzd1U5YT5lumiUMC4hlSSC23Y9nkAl+aw7Z16pD3nQdE9cY338dDlt3pHrSLNHd1nLoSh2Su6t3Y0YH70nyhRn7uIdqW6iSxGoAFB+rky3GSLH2HWNeoYrg3hCOzsV76XLgbybCkOPdPyAhu59G6NvwYyVgBXGfwznJGYekuw-d4mq4Sp5GlR2WKF9R9MxdCAOaGMmUpxCgz1C2XSmQWqHBBhwtX+w8VOw-z9aSV8P4
获得正确的的tokens后,便可以参考官方的api参考页面,测试其它的api了.官方参考  http://developer.openstack.org/api-ref-compute-v2.1.html  
如instance  列表:

curl -v -H "X-Auth-Token: MIIMUAYJKoZIhvcNAQcCoIIMQTCCDD0CAQExDTALBglghkgBZQMEAgEwggqeBgkqhkiG9w0BBwGgggqPBIIKi3siYWNjZXNzIjogeyJ0b2tlbiI6IHsiaXNzdWVkX2F0IjogIjIwMTUtMTItMjNUMDI6NTA6MjcuMzc4NTkyIiwgImV4cGlyZXMiOiAiMjAxNS0xMi0yM1QwMzo1MDoyN1oiLCAiaWQiOiAicGxhY2Vob2xkZXIiLCAidGVuYW50IjogeyJkZXNjcmlwdGlvbiI6ICJBZG1pbiBUZW5hbnQiLCAiZW5hYmxlZCI6IHRydWUsICJpZCI6ICIyOTk2ZTQ2YzI1MTk0MTVkOGRlMmIxNDFkNmM2MDdiYSIsICJuYW1lIjogImFkbWluIn19LCAic2VydmljZUNhdGFsb2ciOiBbeyJlbmRwb2ludHMiOiBbeyJhZG1pblVSTCI6ICJodHRwOi8vMTkyLjE2OC4xMi4xOjg3NzQvdjIvMjk5NmU0NmMyNTE5NDE1ZDhkZTJiMTQxZDZjNjA3YmEiLCAicmVnaW9u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ECAwFVW5zZXQxDjAMBgNVBAcMBVVuc2V0MQ4wDAYDVQQKDAVVbnNldDEYMBYGA1UEAwwPd3d3LmV4YW1wbGUuY29tAgEBMAsGCWCGSAFlAwQCATANBgkqhkiG9w0BAQEFAASCAQDbzMul-Fxn-lu7E2w-w+lUcetiH2wOLQw68Cv36H3bvLDljJXb8QECSMT1k0+-hVvnxPt2-h3CvG3Mzp7h5KHUVsRL2NNOFJln7M64rjvegmzd1U5YT5lumiUMC4hlSSC23Y9nkAl+aw7Z16pD3nQdE9cY338dDlt3pHrSLNHd1nLoSh2Su6t3Y0YH70nyhRn7uIdqW6iSxGoAFB+rky3GSLH2HWNeoYrg3hCOzsV76XLgbybCkOPdPyAhu59G6NvwYyVgBXGfwznJGYekuw-d4mq4Sp5GlR2WKF9R9MxdCAOaGMmUpxCgz1C2XSmQWqHBBhwtX+w8VOw-z9aSV8P4" http://192.168.12.1:8774/v2/2996e46c2519415d8de2b141d6c607ba/servers
返回instance 列表:

openstack restful api 使用_第2张图片

curl毕竟是命令行,比较麻烦,推荐使用rest客户端去操作。这里推存一个JAVA的: https://code.google.com/p/rest-client/

填入,X-Auth-Token 和 URL部分

openstack restful api 使用_第3张图片

Status 返回200 即成功。调整返回JSON格式:

openstack restful api 使用_第4张图片

openstack restful api 使用_第5张图片

你可能感兴趣的:(REST,openstack,Restful,curl,AP,restclient-ui)