OpenStack api使用记录
来源:互联网 发布:去码软件下载 编辑:程序博客网 时间:2024/05/17 00:19
openstack官方有提供api供开发者使用,可以使用api做一些外围的小工具,用来简化对openstack的管理。本篇主要讲解api中使用GET方法的接口,其余的POST、DELETE方法以后介绍。
api-quickstart:http://docs.openstack.org/api/quick-start/content/index.html
api-doc:http://api.openstack.org/api-ref.html
环境:ubuntu12.10(10.1.1.186)
如果你看了官方的quickstart,他会让你先生成一个X-Auth-Token值,然后使用这个认证的值调用相关的api接口。如下过程:
1
curl -k -X
'POST'
-
v
http://10.1.1.186:5000/v2.0/tokens -d
'{"auth":{"passwordCredentials":{"username": "guol", "password":"123456"}}}'
-H
'Content-type: application/json'
| python -mjson.tool
01
{
02
"access"
: {
03
"serviceCatalog"
: {},
04
"token"
: {
05
"expires"
:
"2013-01-27T04:55:35Z"
,
06
"id"
:
"a4b6a871ff2f41a797c0b7d45c69f81e"
07
},
08
"user"
: {
09
"id"
:
"4c1a6122fc874a8bb541a34be3b316ad"
,
10
"name"
:
"guol"
,
11
"roles"
: [],
12
"roles_links"
: [],
13
"username"
:
"guol"
14
}
15
}
16
}
1
curl -X
'GET'
-H
"X-Auth-Token:a4b6a871ff2f41a797c0b7d45c69f81e"
-
v
http://10.1.1.186:5000/v2.0/tenants | python -mjson.tool
01
{
02
"tenants"
: [
03
{
04
"description"
:
"\u4e91\u5e73\u53f0\u7ba1\u7406\u5458,\u5168\u5c40\u7ba1\u7406\u6240\u6709\u9879\u76ee"
,
05
"enabled"
:
true
,
06
"id"
:
"3a3613f83183435d9c47aa362261f720"
,
07
"name"
:
"admin"
08
}
09
],
10
"tenants_links"
: []
11
}
当你按照api-doc接着调用compute、image、volume等下面的api接口时,会发现统统没有返回值,经过一段时间的摸索,发现对下剩下的api接口是需要使用认证过的token才能使用的,下面的方法才是王道。
获取认证token值
1
curl -X POST -d
'{"auth": {"tenantName": "VM", "passwordCredentials":{"username": "admin", "password": "123456"}}}'
-H
"Content-type: application/json"
http://10.1.1.186:35357/v2.0/tokens | python -mjson.tool
返回值如下:
001
{
002
"access"
: {
003
"metadata"
: {
004
"is_admin"
: 0,
005
"roles"
: [
006
"141927156f7a47d98858e51febaea8cf"
007
]
008
},
009
"serviceCatalog"
: [
010
{
011
"endpoints"
: [
012
{
013
"adminURL"
:
"http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671"
,
014
"id"
:
"e02aaf58a24641049fddfe7385ce9399"
,
015
"internalURL"
:
"http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671"
,
016
"publicURL"
:
"http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671"
,
017
"region"
:
"RegionOne"
018
}
019
],
020
"endpoints_links"
: [],
021
"name"
:
"nova"
,
022
"type"
:
"compute"
023
},
024
{
025
"endpoints"
: [
026
{
027
"adminURL"
:
"http://10.1.1.186:9292/v1"
,
028
"id"
:
"7c48fa4d876d44e0a3cb324a45a14931"
,
029
"internalURL"
:
"http://10.1.1.186:9292/v1"
,
030
"publicURL"
:
"http://10.1.1.186:9292/v1"
,
031
"region"
:
"RegionOne"
032
}
033
],
034
"endpoints_links"
: [],
035
"name"
:
"glance"
,
036
"type"
:
"image"
037
},
038
{
039
"endpoints"
: [
040
{
041
"adminURL"
:
"http://10.1.1.186:8776/v1/170bf7acb88646bc9147085d426c4671"
,
042
"id"
:
"c08114987bc0443d879436db6290dd08"
,
043
"internalURL"
:
"http://10.1.1.186:8776/v1/170bf7acb88646bc9147085d426c4671"
,
044
"publicURL"
:
"http://10.1.1.186:8776/v1/170bf7acb88646bc9147085d426c4671"
,
045
"region"
:
"RegionOne"
046
}
047
],
048
"endpoints_links"
: [],
049
"name"
:
"volume"
,
050
"type"
:
"volume"
051
},
052
{
053
"endpoints"
: [
054
{
055
"adminURL"
:
"http://10.1.1.186:8773/services/Admin"
,
056
"id"
:
"cd6c3c2ac44340d6a78cb903ec9bcab3"
,
057
"internalURL"
:
"http://10.1.1.186:8773/services/Cloud"
,
058
"publicURL"
:
"http://10.1.1.186:8773/services/Cloud"
,
059
"region"
:
"RegionOne"
060
}
061
],
062
"endpoints_links"
: [],
063
"name"
:
"ec2"
,
064
"type"
:
"ec2"
065
},
066
{
067
"endpoints"
: [
068
{
069
"adminURL"
:
"http://10.1.1.186:8080/v1"
,
070
"id"
:
"a75755093500441ea954f4874ccecf15"
,
071
"internalURL"
:
"http://10.1.1.186:8080/v1/AUTH_170bf7acb88646bc9147085d426c4671"
,
072
"publicURL"
:
"http://10.1.1.186:8080/v1/AUTH_170bf7acb88646bc9147085d426c4671"
,
073
"region"
:
"RegionOne"
074
}
075
],
076
"endpoints_links"
: [],
077
"name"
:
"swift"
,
078
"type"
:
"object-store"
079
},
080
{
081
"endpoints"
: [
082
{
083
"adminURL"
:
"http://10.1.1.186:35357/v2.0"
,
084
"id"
:
"196e553f9bcc4c8fa425bc0fa28a144b"
,
085
"internalURL"
:
"http://10.1.1.186:5000/v2.0"
,
086
"publicURL"
:
"http://10.1.1.186:5000/v2.0"
,
087
"region"
:
"RegionOne"
088
}
089
],
090
"endpoints_links"
: [],
091
"name"
:
"keystone"
,
092
"type"
:
"identity"
093
}
094
],
095
"token"
: {
096
"expires"
:
"2013-01-27T05:06:50Z"
,
097
"id"
:
"71fb5b7816804febbd89ed315fe67c94"
,
098
"tenant"
: {
099
"description"
:
"\u6240\u6709\u516c\u53f8\u81ea\u4e3b\u6e38\u620f\u7684\u524d\u7aef\u53d1\u5e03\u673a,\u5982\u8d5b\u5c14\u53f7,\u529f\u592b\u7b49"
,
100
"enabled"
:
true
,
101
"id"
:
"170bf7acb88646bc9147085d426c4671"
,
102
"name"
:
"VM"
103
}
104
},
105
"user"
: {
106
"id"
:
"24a2251c32504957ad2647c2448ffac3"
,
107
"name"
:
"admin"
,
108
"roles"
: [
109
{
110
"name"
:
"\u524d\u7aef\u6e38\u620f"
111
}
112
],
113
"roles_links"
: [],
114
"username"
:
"admin"
115
}
116
}
117
}
使用返回的token id和tenant id调用api返回该tenant下的servers
1
curl -X GET -H
"X-Auth-Token:71fb5b7816804febbd89ed315fe67c94"
-H
"Content-type: application/json"
http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers | python -mjson.tool
01
{
02
"servers"
: [
03
{
04
"id"
:
"ac561829-dffa-443e-a321-260d48f4cba3"
,
05
"links"
: [
06
{
07
"href"
:
"http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/ac561829-dffa-443e-a321-260d48f4cba3"
,
08
"rel"
:
"self"
09
},
10
{
11
"href"
:
"http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/ac561829-dffa-443e-a321-260d48f4cba3"
,
12
"rel"
:
"bookmark"
13
}
14
],
15
"name"
:
"VM1"
16
},
17
{
18
"id"
:
"e3b84378-7598-48c8-8a1d-bd555c4c1f90"
,
19
"links"
: [
20
{
21
"href"
:
"http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/e3b84378-7598-48c8-8a1d-bd555c4c1f90"
,
22
"rel"
:
"self"
23
},
24
{
25
"href"
:
"http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/e3b84378-7598-48c8-8a1d-bd555c4c1f90"
,
26
"rel"
:
"bookmark"
27
}
28
],
29
"name"
:
"VM2"
30
},
31
{
32
"id"
:
"beafef2e-26c4-4320-9b45-70006e71785c"
,
33
"links"
: [
34
{
35
"href"
:
"http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/beafef2e-26c4-4320-9b45-70006e71785c"
,
36
"rel"
:
"self"
37
},
38
{
39
"href"
:
"http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/beafef2e-26c4-4320-9b45-70006e71785c"
,
40
"rel"
:
"bookmark"
41
}
42
],
43
"name"
:
"VM3"
44
},
45
{
46
"id"
:
"f8450270-9d96-4dba-b1c2-8ebcef1ff012"
,
47
"links"
: [
48
{
49
"href"
:
"http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/f8450270-9d96-4dba-b1c2-8ebcef1ff012"
,
50
"rel"
:
"self"
51
},
52
{
53
"href"
:
"http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/f8450270-9d96-4dba-b1c2-8ebcef1ff012"
,
54
"rel"
:
"bookmark"
55
}
56
],
57
"name"
:
"VM4"
58
}
59
]
60
}
下面附上一个python调用api获取特定tenant下运行实例的脚本
01
#!/usr/bin/python
02
import
base64,urllib,httplib,json,os
03
from
urlparse
import
urlparse
04
05
url1
=
"10.1.1.186:35357"
06
params1
=
'{"auth": {"tenantName": "tenant-name", "passwordCredentials":{"username": "admin", "password": "123456"}}}'
07
headers1
=
{
"Content-Type"
:
'application/json'
}
08
conn1
=
httplib.HTTPConnection(url1)
09
conn1.request(
"POST"
,
"/v2.0/tokens"
,params1,headers1)
10
response1
=
conn1.getresponse()
11
data1
=
response1.read()
12
dd1
=
json.loads(data1)
13
conn1.close()
14
15
apitoken
=
dd1[
'access'
][
'token'
][
'id'
]
16
apitenant
=
dd1[
'access'
][
'token'
][
'tenant'
][
'id'
]
17
apiurl
=
dd1[
'access'
][
'serviceCatalog'
][
0
][
'endpoints'
][
0
][
'publicURL'
]
18
apiurlt
=
urlparse(dd1[
'access'
][
'serviceCatalog'
][
0
][
'endpoints'
][
0
][
'publicURL'
])
19
20
url2
=
apiurlt[
1
]
21
params2
=
urllib.urlencode({})
22
headers2
=
{
"X-Auth-Token"
:apitoken,
"Content-type"
:
"application/json"
}
23
conn2
=
httplib.HTTPConnection(url2)
24
conn2.request(
"GET"
,
"%s/servers"
%
apiurlt[
2
], params2, headers2)
25
response2
=
conn2.getresponse()
26
data2
=
response2.read()
27
dd2
=
json.loads(data2)
28
conn2.close()
29
for
i
in
range
(
len
(dd2[
'servers'
])):
30
print
dd2[
'servers'
][i][
'name'
]
0 0
- OpenStack api使用记录
- openstack restful api 使用
- 使用RESTClient调试openstack api
- openstack,使用devstack安装记录
- Windows API使用记录
- gitlab api使用记录
- 如何开始使用OpenStack命令行和API
- Openstack api 学习文档 & restclient使用文档
- Openstack api 学习文档 & restclient使用文档
- Openstack api 学习文档 & restclient使用文档
- openstack API
- openstack API
- Openstack API
- Sqlite3数据库api使用记录
- 百度地图API使用记录
- 【OpenStack】【转载】OpenStack extension api
- 使用cURL操作Openstack对象存储的ReST API
- OpenStack使用 devstack安装 g-api did not start
- 浏览csdn随感
- c/c++内存对齐技术之图文终极解释[原创]
- 【工作精华】月末营业日的问题
- 算法导论系列文章之快速排序
- linux menuconfig 详解
- OpenStack api使用记录
- linux中断子系统
- git android sdk 源代码 遇到不知道 如何下载 tag
- myisamchk修复表
- php创建zip压缩文件
- 微信公众平台开发(八) 自定义菜单功能开发
- 第十七周上机实践项目——三色球的问题
- [回溯]Graph Coloring UVA193
- mysql语句记录