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接口。如下过程:

1curl -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}
然后使用token id调用你使用的api

1curl -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值

1curl -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

1curl -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}
通过返回值可以看到vm tenant中共包含VM1 VM2 VM3 VM4四台实例。


下面附上一个python调用api获取特定tenant下运行实例的脚本

view source
print?
01#!/usr/bin/python
02import base64,urllib,httplib,json,os
03from urlparse import urlparse
04 
05url1="10.1.1.186:35357"
06params1 = '{"auth": {"tenantName": "tenant-name", "passwordCredentials":{"username": "admin", "password": "123456"}}}'
07headers1 = {"Content-Type"'application/json'}
08conn1 = httplib.HTTPConnection(url1)
09conn1.request("POST","/v2.0/tokens",params1,headers1)
10response1 = conn1.getresponse()
11data1 = response1.read()
12dd1 = json.loads(data1)
13conn1.close()
14 
15apitoken = dd1['access']['token']['id']
16apitenant= dd1['access']['token']['tenant']['id']
17apiurl = dd1['access']['serviceCatalog'][0]['endpoints'][0]['publicURL']
18apiurlt = urlparse(dd1['access']['serviceCatalog'][0]['endpoints'][0]['publicURL'])
19 
20url2 = apiurlt[1]
21params2 = urllib.urlencode({})
22headers2 = "X-Auth-Token":apitoken, "Content-type":"application/json" }
23conn2 = httplib.HTTPConnection(url2)
24conn2.request("GET""%s/servers" % apiurlt[2], params2, headers2)
25response2 = conn2.getresponse()
26data2 = response2.read()
27dd2 = json.loads(data2)
28conn2.close()
29for in range(len(dd2['servers'])):
30    print dd2['servers'][i]['name']
0 0