Openstack api 學習文檔 & restclient使用文檔

轉載自http://www.cnblogs.com/juandx/p/4943409.html



一、使用curl命名來使用openstack的api

 

當然,首先你得有個openstack的環境,我是自己搭建的kilo,我們先用curl來使用api,注意自己改一下ip還有用戶名和密碼,下面這個命令就會得到一個令牌(token)來使用,以後調用api就需要使用這個令牌。

1
curl -k -X 'POST' -v http://controller_ip:5000/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "ADMIN", "password":"MY_PASSWORD"}}}' -H 'Content-type: application/json' | python -mjson.tool

這樣後會得到如下的json數據

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
    "access": {
        "metadata": {
            "is_admin": 0,
            "roles": []
        },
        "serviceCatalog": [],
        "token": {
            "audit_ids": [
                "WPon6i60TamUfqcsPV2oVg"
            ],
            "expires""2015-11-06T11:19:19Z",
            "id""bc422dc370324535a64699640f50a3ab",//這個id就是令牌(token)
            "issued_at""2015-11-06T10:19:19.559266"
        },
        "user": {
            "id""9c20d9fe21a14741ac1486b2db058bd5",
            "name""admin",
            "roles": [],
            "roles_links": [],
            "username""admin"
        }
    }
}

 

下面得到這個用戶的所有項目(tenant),注意下面的token就是剛剛得到的那個令牌

1
curl -X 'GET' -H  "X-Auth-Token:bc422dc370324535a64699640f50a3ab" -v http://controller_ip:5000/v2.0/tenants | python -mjson.tool

 

這裏面得到這個用戶的項目(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
31
32
33
34
35
{
    "tenants": [
        {
            "description"null,
            "enabled"true,
            "id""92afd13a680645c191a2aab7422a7a3b",
            "name""AIM"
        },
        {
            "description"null,
            "enabled"true,
            "id""b3457999de4d4add99e89f4f299f5857",
            "name""ECM"
        },
        {
            "description""Admin Project",
            "enabled"true,
            "id""de8f43bfe4244a1aa315a70a43248d60",
            "name""admin"
        },
        {
            "description"null,
            "enabled"true,
            "id""f3c1015ff67e4cd18b022b9b51eb1123",
            "name""IM"
        },
        {
            "description"null,
            "enabled"true,
            "id""fae79b8f91b944b985d2b35373cf9bb2",
            "name""Commerce"
        }
    ],
    "tenants_links": []
}

可以看到我這裏ADMIN用戶總共有5個tenant,我們選取一個即可,就選取名爲admin的項目,id看到爲de8f43bfe4244a1aa315a70a43248d60

 

接下來我們去一個項目(tenant)看看它的虛擬機、等信息。

 

首先需要得到這個tenant的令牌信息,注意這個token和上面的那個不一樣

1
curl -k -X 'POST' -v http://controller_ip:5000/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "ADMIN", "password":"MY_PASSWORD"},"tenantId":"de8f43bfe4244a1aa315a70a43248d60"}}' -H 'Content-type: application/json' | python -mjson.tool

注意這裏看到用戶名和密碼還是和之前一樣,但是多了一個tenantId的字段,這裏說明了我們需要的是ADMIN用戶下的這個項目的令牌,得到如下信息:

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
{
    "access": {
        "metadata": {
            "is_admin": 0,
            "roles": [
                "cf87d1b66b044cdd94972d72d6e4d474"
            ]
        },
        "serviceCatalog": [
            {
                "endpoints": [
                    {
                        "adminURL""http://controller:9292",
                        "id""5f892fde3a2241c989355cdd6beea4b5",
                        "internalURL""http://controller:9292",
                        "publicURL""http://controller:9292",
                        "region""RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name""glance",
                "type""image"
            },
            {
                "endpoints": [
                    {
                        "adminURL""http://controller:8774/v2/de8f43bfe4244a1aa315a70a43248d60",
                        "id""0c52d8a741f740a6b83be51ae7082b62",
                        "internalURL""http://controller:8774/v2/de8f43bfe4244a1aa315a70a43248d60",
                        "publicURL""http://controller:8774/v2/de8f43bfe4244a1aa315a70a43248d60",
                        "region""RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name""nova",
                "type""compute"
            },
            {
                "endpoints": [
                    {
                        "adminURL""http://controller:9696",
                        "id""3d70017df8ee40d698988ad52b25cc8e",
                        "internalURL""http://controller:9696",
                        "publicURL""http://controller:9696",
                        "region""RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name""neutron",
                "type""network"
            },
            {
                "endpoints": [
                    {
                        "adminURL""http://controller:35357/v2.0",
                        "id""23bb295b14554d2b89535d67632e7c60",
                        "internalURL""http://controller:5000/v2.0",
                        "publicURL""http://controller:5000/v2.0",
                        "region""RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name""keystone",
                "type""identity"
            }
        ],
        "token": {
            "audit_ids": [
                "XNBLV1ohSXensbuHqwTo3Q"
            ],
            "expires""2015-11-06T11:29:22Z",
            "id""b1470e1f13284a8b8f979eb652e29db5",
            "issued_at""2015-11-06T10:29:22.423828",
            "tenant": {
                "description""Admin Project",
                "enabled"true,
                "id""de8f43bfe4244a1aa315a70a43248d60",
                "name""admin"
            }
        },
        "user": {
            "id""9c20d9fe21a14741ac1486b2db058bd5",
            "name""admin",
            "roles": [
                {
                    "name""admin"
                }
            ],
            "roles_links": [],
            "username""admin"
        }
    }
}

這裏面有很多關於這個tenant的信息,其中最重要的是token的id,爲b1470e1f13284a8b8f979eb652e29db5,這是我們後面調取api需要的,還有注意de8f43bfe4244a1aa315a70a43248d60是tenant_id。

接下來我們開始看看這個項目裏面的虛擬機

1
curl -v -H "X-Auth-Token:b1470e1f13284a8b8f979eb652e29db5" http://controller_ip:8774/v2/tenant_id/servers

把得到的令牌寫好,執行後得到如下信息:

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
{
    "servers": [
        {
            "id""098982e0-251d-4306-9171-f4d52674fde3",
            "links": [
                {
                    "href""http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/servers/098982e0-251d-4306-9171-f4d52674fde3",
                    "rel""self"
                },
                {
                    "href""http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/servers/098982e0-251d-4306-9171-f4d52674fde3",
                    "rel""bookmark"
                }
            ],
            "name""vm2"
        },
        {
            "id""86baaddc-3c3e-4687-87bc-2e22f53eb7ba",
            "links": [
                {
                    "href""http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/servers/86baaddc-3c3e-4687-87bc-2e22f53eb7ba",
                    "rel""self"
                },
                {
                    "href""http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/servers/86baaddc-3c3e-4687-87bc-2e22f53eb7ba",
                    "rel""bookmark"
                }
            ],
            "name""vm1"
        }
    ]
}

這裏可以明顯看到有兩個虛擬機,分別爲vm1和vm2,這都是我之前創建的。

 

再看看我的image有哪些

1
curl -v -H "X-Auth-Token:b1470e1f13284a8b8f979eb652e29db5" http://controller_ip:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images | 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
{
    "images": [
        {
            "id""cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
            "links": [
                {
                    "href""http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images/cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
                    "rel""self"
                },
                {
                    "href""http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/images/cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
                    "rel""bookmark"
                },
                {
                    "href""http://controller:9292/images/cfa488bb-f5aa-4f67-830c-6d84a1b0f062",
                    "rel""alternate",
                    "type""application/vnd.openstack.image"
                }
            ],
            "name""windows7-x86_64-bpm20150724"
        },
        {
            "id""5c997a18-3f93-41bb-8ce2-724b10d0d418",
            "links": [
                {
                    "href""http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images/5c997a18-3f93-41bb-8ce2-724b10d0d418",
                    "rel""self"
                },
                {
                    "href""http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/images/5c997a18-3f93-41bb-8ce2-724b10d0d418",
                    "rel""bookmark"
                },
                {
                    "href""http://controller:9292/images/5c997a18-3f93-41bb-8ce2-724b10d0d418",
                    "rel""alternate",
                    "type""application/vnd.openstack.image"
                }
            ],
            "name""rhel7.1"
        },
        {
            "id""e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
            "links": [
                {
                    "href""http://9.111.222.23:8774/v2/de8f43bfe4244a1aa315a70a43248d60/images/e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
                    "rel""self"
                },
                {
                    "href""http://9.111.222.23:8774/de8f43bfe4244a1aa315a70a43248d60/images/e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
                    "rel""bookmark"
                },
                {
                    "href""http://controller:9292/images/e1c3cc65-54ae-4a76-9646-2d4dc3b7473b",
                    "rel""alternate",
                    "type""application/vnd.openstack.image"
                }
            ],
            "name""cirros-0.3.4-x86_64"
        }
    ]
}

可以看到我一共有3個鏡像

 

至此,其他api查看文檔進行查看即可,都是相似的方法,這裏僅作一個簡單介紹。

 

 

二、使用火狐firefox 的 restclient 工具來 使用 openstack的api

下面我還嘗試了一些Firefox的restclient,來調取api

當然需要打開火狐瀏覽器安裝restclient的插件https://addons.mozilla.org/en-US/firefox/addon/restclient/,當然使用java的客戶端也可以http://code.google.com/p/rest-client/

 

這裏演示兩個rest的方法來使用,get和post

 

先看看post:

我們來取一個ADMIN用戶的admin的tenant的令牌。

對比下curl的命令

1
curl -k -X 'POST' -v http://controller_ip:5000/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "ADMIN", "password":"MY_PASSWORD"},"tenantId":"de8f43bfe4244a1aa315a70a43248d60"}}' -H 'Content-type: application/json' | python -mjson.tool

先設置http的header ,name爲 Content-Type ,value爲 application/json

 

設置好後輸入網址http://controller_ip:5000/v2.0/tokens,並且把要post的json的數據寫入到body裏面,最後點擊按鈕SEND即可,可以得到和curl一樣的json數據

 

接下來試試使用get的方法

我們得到了tenant的token,就可以使用get方法獲取其虛擬機的信息

注意要先設置token在header中,name爲X-Auth-Token,value爲token,然後輸入網址,選擇爲get方法,點擊SEND即可得到:

 

其他的api幾乎都是get和post的方法,這裏簡單演示了firefox的restclient的用法,希望大家多多指教

 

參考資料:

openstack quick start api:http://docs.openstack.org/zh_CN/api/quick-start/content/index.html

openstack api document:http://developer.openstack.org/api-ref.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章