分类: 云计算
2014-03-02 11:03:22
api-quickstart:
api-doc:
环境:ubuntu12.10(10.1.1.186)
如果你看了官方的quickstart,他会让你先生成一个X-Auth-Token值,然后使用这个认证的值调用相关的api接口。如下过程:
curl -k -X 'POST' -v -d '{"auth":{"passwordCredentials":{"username": "guol", "password":"123456"}}}' -H 'Content-type: application/json' | python -mjson.tool
输出结果如下:
获取认证token值
curl -X POST -d '{"auth": {"tenantName": "VM", "passwordCredentials":{"username": "admin", "password": "123456"}}}' -H "Content-type: application/json" | python -mjson.tool
返回值如下:
{
"access": {
"metadata": {
"is_admin": 0,
"roles": [
"141927156f7a47d98858e51febaea8cf"
]
},
"serviceCatalog": [
{
"endpoints": [
{
"adminURL": "",
"id": "e02aaf58a24641049fddfe7385ce9399",
"internalURL": "",
"publicURL": "",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "nova",
"type": "compute"
},
{
"endpoints": [
{
"adminURL": "",
"id": "7c48fa4d876d44e0a3cb324a45a14931",
"internalURL": "",
"publicURL": "",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "glance",
"type": "image"
},
{
"endpoints": [
{
"adminURL": "",
"id": "c08114987bc0443d879436db6290dd08",
"internalURL": "",
"publicURL": "",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "volume",
"type": "volume"
},
{
"endpoints": [
{
"adminURL": "",
"id": "cd6c3c2ac44340d6a78cb903ec9bcab3",
"internalURL": "",
"publicURL": "",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "ec2",
"type": "ec2"
},
{
"endpoints": [
{
"adminURL": "",
"id": "a75755093500441ea954f4874ccecf15",
"internalURL": "",
"publicURL": "",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "swift",
"type": "object-store"
},
{
"endpoints": [
{
"adminURL": "",
"id": "196e553f9bcc4c8fa425bc0fa28a144b",
"internalURL": "",
"publicURL": "",
"region": "RegionOne"
}
],
"endpoints_links": [],
"name": "keystone",
"type": "identity"
}
],
"token": {
"expires": "2013-01-27T05:06:50Z",
"id": "71fb5b7816804febbd89ed315fe67c94",
"tenant": {
"description": "\u6240\u6709\u516c\u53f8\u81ea\u4e3b\u6e38\u620f\u7684\u524d\u7aef\u53d1\u5e03\u673a,\u5982\u8d5b\u5c14\u53f7,\u529f\u592b\u7b49",
"enabled": true,
"id": "170bf7acb88646bc9147085d426c4671",
"name": "VM"
}
},
"user": {
"id": "24a2251c32504957ad2647c2448ffac3",
"name": "admin",
"roles": [
{
"name": "\u524d\u7aef\u6e38\u620f"
}
],
"roles_links": [],
"username": "admin"
}
}
}
使用返回的token id和tenant id调用api
返回该tenant下的servers
curl -X GET -H "X-Auth-Token:71fb5b7816804febbd89ed315fe67c94" -H "Content-type: application/json" | python -mjson.tool
返回结果如下
{
"servers": [
{
"id": "ac561829-dffa-443e-a321-260d48f4cba3",
"links": [
{
"href": "",
"rel": "self"
},
{
"href": "",
"rel": "bookmark"
}
],
"name": "VM1"
},
{
"id": "e3b84378-7598-48c8-8a1d-bd555c4c1f90",
"links": [
{
"href": "",
"rel": "self"
},
{
"href": "",
"rel": "bookmark"
}
],
"name": "VM2"
},
{
"id": "beafef2e-26c4-4320-9b45-70006e71785c",
"links": [
{
"href": "",
"rel": "self"
},
{
"href": "",
"rel": "bookmark"
}
],
"name": "VM3"
},
{
"id": "f8450270-9d96-4dba-b1c2-8ebcef1ff012",
"links": [
{
"href": "",
"rel": "self"
},
{
"href": "",
"rel": "bookmark"
}
],
"name": "VM4"
}
]
}
通过返回值可以看到vm tenant中共包含VM1 VM2 VM3 VM4四台实例。
下面附上一个python调用api获取特定tenant下运行实例的脚本
#!/usr/bin/python
import base64,urllib,httplib,json,os
from urlparse import urlparse
url1="10.1.1.186:35357"
params1 = '{"auth": {"tenantName": "tenant-name", "passwordCredentials":{"username": "admin", "password": "123456"}}}'
headers1 = {"Content-Type": 'application/json'}
conn1 = httplib.HTTPConnection(url1)
conn1.request("POST","/v2.0/tokens",params1,headers1)
response1 = conn1.getresponse()
data1 = response1.read()
dd1 = json.loads(data1)
conn1.close()
apitoken = dd1['access']['token']['id']
apitenant= dd1['access']['token']['tenant']['id']
apiurl = dd1['access']['serviceCatalog'][0]['endpoints'][0]['publicURL']
apiurlt = urlparse(dd1['access']['serviceCatalog'][0]['endpoints'][0]['publicURL'])
url2 = apiurlt[1]
params2 = urllib.urlencode({})
headers2 = { "X-Auth-Token":apitoken, "Content-type":"application/json" }
conn2 = httplib.HTTPConnection(url2)
conn2.request("GET", "%s/servers" % apiurlt[2], params2, headers2)
response2 = conn2.getresponse()
data2 = response2.read()
dd2 = json.loads(data2)
conn2.close()
for i in range(len(dd2['servers'])):
print dd2['servers'][i]['name']