标签:
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 |
输出结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{ "access" : { "serviceCatalog" : {}, "token" : { "expires" : "2013-01-27T04:55:35Z" , "id" : "a4b6a871ff2f41a797c0b7d45c69f81e" }, "user" : { "id" : "4c1a6122fc874a8bb541a34be3b316ad" , "name" : "guol" , "roles" : [], "roles_links" : [], "username" : "guol" } } } |
然后使用token id调用你使用的api
1
|
curl -X ‘GET‘ -H "X-Auth-Token:a4b6a871ff2f41a797c0b7d45c69f81e" - v http: //10 .1.1.186:5000 /v2 .0 /tenants | python -mjson.tool |
输出的结果如下:
1
2
3
4
5
6
7
8
9
10
11
|
{ "tenants" : [ { "description" : "\u4e91\u5e73\u53f0\u7ba1\u7406\u5458,\u5168\u5c40\u7ba1\u7406\u6240\u6709\u9879\u76ee" , "enabled" : true , "id" : "3a3613f83183435d9c47aa362261f720" , "name" : "admin" } ], "tenants_links" : [] } |
当你按照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 |
返回值如下:
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
{ "access" : { "metadata" : { "is_admin" : 0, "roles" : [ "141927156f7a47d98858e51febaea8cf" ] }, "serviceCatalog" : [ { "endpoints" : [ { "adminURL" : "http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671" , "id" : "e02aaf58a24641049fddfe7385ce9399" , "internalURL" : "http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671" , "publicURL" : "http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671" , "region" : "RegionOne" } ], "endpoints_links" : [], "name" : "nova" , "type" : "compute" }, { "endpoints" : [ { "adminURL" : "http://10.1.1.186:9292/v1" , "id" : "7c48fa4d876d44e0a3cb324a45a14931" , "internalURL" : "http://10.1.1.186:9292/v1" , "publicURL" : "http://10.1.1.186:9292/v1" , "region" : "RegionOne" } ], "endpoints_links" : [], "name" : "glance" , "type" : "image" }, { "endpoints" : [ { "adminURL" : "http://10.1.1.186:8776/v1/170bf7acb88646bc9147085d426c4671" , "id" : "c08114987bc0443d879436db6290dd08" , "internalURL" : "http://10.1.1.186:8776/v1/170bf7acb88646bc9147085d426c4671" , "publicURL" : "http://10.1.1.186:8776/v1/170bf7acb88646bc9147085d426c4671" , "region" : "RegionOne" } ], "endpoints_links" : [], "name" : "volume" , "type" : "volume" }, { "endpoints" : [ { "adminURL" : "http://10.1.1.186:8773/services/Admin" , "id" : "cd6c3c2ac44340d6a78cb903ec9bcab3" , "internalURL" : "http://10.1.1.186:8773/services/Cloud" , "publicURL" : "http://10.1.1.186:8773/services/Cloud" , "region" : "RegionOne" } ], "endpoints_links" : [], "name" : "ec2" , "type" : "ec2" }, { "endpoints" : [ { "adminURL" : "http://10.1.1.186:8080/v1" , "id" : "a75755093500441ea954f4874ccecf15" , "internalURL" : "http://10.1.1.186:8080/v1/AUTH_170bf7acb88646bc9147085d426c4671" , "publicURL" : "http://10.1.1.186:8080/v1/AUTH_170bf7acb88646bc9147085d426c4671" , "region" : "RegionOne" } ], "endpoints_links" : [], "name" : "swift" , "type" : "object-store" }, { "endpoints" : [ { "adminURL" : "http://10.1.1.186:35357/v2.0" , "id" : "196e553f9bcc4c8fa425bc0fa28a144b" , "internalURL" : "http://10.1.1.186:5000/v2.0" , "publicURL" : "http://10.1.1.186:5000/v2.0" , "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
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 |
返回结果如下
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
|
{ "servers" : [ { "id" : "ac561829-dffa-443e-a321-260d48f4cba3" , "links" : [ { "href" : "http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/ac561829-dffa-443e-a321-260d48f4cba3" , "rel" : "self" }, { "href" : "http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/ac561829-dffa-443e-a321-260d48f4cba3" , "rel" : "bookmark" } ], "name" : "VM1" }, { "id" : "e3b84378-7598-48c8-8a1d-bd555c4c1f90" , "links" : [ { "href" : "http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/e3b84378-7598-48c8-8a1d-bd555c4c1f90" , "rel" : "self" }, { "href" : "http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/e3b84378-7598-48c8-8a1d-bd555c4c1f90" , "rel" : "bookmark" } ], "name" : "VM2" }, { "id" : "beafef2e-26c4-4320-9b45-70006e71785c" , "links" : [ { "href" : "http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/beafef2e-26c4-4320-9b45-70006e71785c" , "rel" : "self" }, { "href" : "http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/beafef2e-26c4-4320-9b45-70006e71785c" , "rel" : "bookmark" } ], "name" : "VM3" }, { "id" : "f8450270-9d96-4dba-b1c2-8ebcef1ff012" , "links" : [ { "href" : "http://10.1.1.186:8774/v2/170bf7acb88646bc9147085d426c4671/servers/f8450270-9d96-4dba-b1c2-8ebcef1ff012" , "rel" : "self" }, { "href" : "http://10.1.1.186:8774/170bf7acb88646bc9147085d426c4671/servers/f8450270-9d96-4dba-b1c2-8ebcef1ff012" , "rel" : "bookmark" } ], "name" : "VM4" } ] } |
通过返回值可以看到vm tenant中共包含VM1 VM2 VM3 VM4四台实例。
下面附上一个python调用api获取特定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
|
#!/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‘ ] |
标签:
原文地址:http://www.cnblogs.com/xinhua5146/p/4811640.html