手動操作 Neutron REST API

(一) 認證 -> 獲取Token,服務endpoints -> 調用服務API

1. 用戶名、密碼去keystone認證

$ curl -d '{"auth": {"tenantName": "demo", "passwordCredentials": {"username": "demo", "password": "demo"}}}' -H "Content-type: application/json" http://127.0.0.1:35357/v2.0/tokens | python -m json.tool


附:keystone API實驗 http://www.pubyun.com/blog/openstack/openstack-keystone-api-%E5%AE%9E%E9%AA%8Ccurl/

別的blog:http://my.oschina.net/guol/blog/105430

另:PKI token太大,改爲UUID,在/etc/keystone/keystone.conf

[signing]
#Deprecated in favor of provider in the [token] section
#Allowed values are PKI or UUID
#token_format =
token_format =UUID


返回:

{
    "access": {
        "metadata": {
            "is_admin": 0, 
            "roles": [
                "3a497544534648a3baefbbdb1237146f", 
                "4f1d36c63bb841f7846fe90a5ff4cc02"
            ]
        }, 
        "serviceCatalog": [
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:8774/v2/e70c0a66ca274efc9ca32a5d0532c298", 
                        "id": "9b8f6727ced744928719c11dccc6d481", 
                        "internalURL": "http://186.100.253.130:8774/v2/e70c0a66ca274efc9ca32a5d0532c298", 
                        "publicURL": "http://186.100.253.130:8774/v2/e70c0a66ca274efc9ca32a5d0532c298", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "nova", 
                "type": "compute"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:9696/", 
                        "id": "21ee585a72e74dd99eda120390ec5c46", 
                        "internalURL": "http://186.100.253.130:9696/", 
                        "publicURL": "http://186.100.253.130:9696/", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "neutron", 
                "type": "network"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:8776/v2/e70c0a66ca274efc9ca32a5d0532c298", 
                        "id": "7af2c6447d1b462b944c7707396c922d", 
                        "internalURL": "http://186.100.253.130:8776/v2/e70c0a66ca274efc9ca32a5d0532c298", 
                        "publicURL": "http://186.100.253.130:8776/v2/e70c0a66ca274efc9ca32a5d0532c298", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "cinder", 
                "type": "volumev2"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:8774/v3", 
                        "id": "59a10c0e8c704da39cae24dc709df1a6", 
                        "internalURL": "http://186.100.253.130:8774/v3", 
                        "publicURL": "http://186.100.253.130:8774/v3", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "nova", 
                "type": "computev3"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:3333", 
                        "id": "00af69e1b03448acbad75b1ece294762", 
                        "internalURL": "http://186.100.253.130:3333", 
                        "publicURL": "http://186.100.253.130:3333", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "s3", 
                "type": "s3"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:9292", 
                        "id": "0d15a21655b54d019a888c70f3c7f82b", 
                        "internalURL": "http://186.100.253.130:9292", 
                        "publicURL": "http://186.100.253.130:9292", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "glance", 
                "type": "image"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:8776/v1/e70c0a66ca274efc9ca32a5d0532c298", 
                        "id": "0ab5852f1c0e45858e301d97b0cd36ef", 
                        "internalURL": "http://186.100.253.130:8776/v1/e70c0a66ca274efc9ca32a5d0532c298", 
                        "publicURL": "http://186.100.253.130:8776/v1/e70c0a66ca274efc9ca32a5d0532c298", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "cinder", 
                "type": "volume"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:8773/services/Admin", 
                        "id": "39d771d8f0fb4d5b84f70f2ef2696f1a", 
                        "internalURL": "http://186.100.253.130:8773/services/Cloud", 
                        "publicURL": "http://186.100.253.130:8773/services/Cloud", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "ec2", 
                "type": "ec2"
            }, 
            {
                "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:35357/v2.0", 
                        "id": "40757dec7c3043b0a025a86b726364dd", 
                        "internalURL": "http://186.100.253.130:5000/v2.0", 
                        "publicURL": "http://186.100.253.130:5000/v2.0", 
                        "region": "RegionOne"
                    }
                ], 
                "endpoints_links": [], 
                "name": "keystone", 
                "type": "identity"
            }
        ], 
        "token": {
            "expires": "2014-01-19T08:02:48Z", 
            "id": "43c8b3a040864637828690dcb0263691", 
            "issued_at": "2014-01-18T08:02:48.886374", 
            "tenant": {
                "description": null, 
                "enabled": true, 
                "id": "e70c0a66ca274efc9ca32a5d0532c298", 
                "name": "demo"
            }
        }, 
        "user": {
            "id": "bcfca75777ed447297dee807677ee99e", 
            "name": "demo", 
            "roles": [
                {
                    "name": "anotherrole"
                }, 
                {
                    "name": "Member"
                }
            ], 
            "roles_links": [], 
            "username": "demo"
        }
    }
}


2. 提取

(1) token_id 

"id": "43c8b3a040864637828690dcb0263691"

(2) 服務的endpoints的publicURL

               "endpoints": [
                    {
                        "adminURL": "http://186.100.253.130:9696/", 
                        "id": "21ee585a72e74dd99eda120390ec5c46", 
                        "internalURL": "http://186.100.253.130:9696/", 
                        "publicURL": "http://186.100.253.130:9696/", 
                        "region": "RegionOne"
                    }

3. 調用服務API

curl -s -X GET http://186.100.253.130:9696/v2.0/networks -H"X-Auth-Token:af6debf9e18c4e1e9709bb18b564a7ab"-H "Content-type:application/json"  | python -mjson.tool


(二)  用Curl操作Neutron API

1. API接口信息,URI信息,request攜帶數據,response數據:

http://docs.openstack.org/api/openstack-network/2.0/content/ch_preface.html

API quick start, 用處不大,從與keystone交互到調用nova等服務:http://docs.openstack.org/api/quick-start/content/


2. Curl使用

-H <line>

自定義頭信息傳遞給服務器

-i

輸出時包括protocol頭信息

-k

允許不使用證書到SSL站點

-v

顯示詳細信息

-X <command>

指定命令 GET/PUT等

-d <data>

HTTP POST方式傳送數據

 

-D Dump 將HTTP響應頭部到stdout.  使用時 “-D - ”

3. 操作:
# create network
$ curl -s -X POST  -d '{"network":{"name":"test-12","admin_state_up":false}}' http://186.100.253.130:9696/v2.0/networks -H "X-Auth-Token:af6debf9e18c4e1e9709bb18b564a7ab" -H "Content-type:application/json"  | python -mjson.tool


# list networks
$ curl -s -X GET http://186.100.253.130:9696/v2.0/networks -H "X-Auth-Token:af6debf9e18c4e1e9709bb18b564a7ab" -H "Content-type:application/json"  | python -mjson.tool


# show network
# url = http://186.100.253.130:9696/v2.0/networks/{network_id}
$ curl -X GET http://186.100.253.130:9696/v2.0/networks/3c88bce8-3d36-4509-8300-445cfd936841 -H "X-Auth-Token: af6debf9e18c4e1e9709bb18b564a7ab" -H "Content-type: application/json"


# update network
# url = http://186.100.253.130:9696/v2.0/networks/{network_id}


# update case 1 
$ curl -X PUT -d '{"network":{"name":"test-11-v"}}' http://186.100.253.130:9696/v2.0/networks/3c88bce8-3d36-4509-8300-445cfd936841 -H "X-Auth-Token: af6debf9e18c4e1e9709bb18b564a7ab" -H "Content-type: application/json" | python -mjson.tool


# for all non-read-only attributes in network resource, you can update them in one update call.
# update case 2
$ curl -v -X PUT -d '{"network":{"admin_state_up":true, "admin_state_up":true}}' http://186.100.253.130:9696/v2.0/networks/ebfa259d-6d84-49ac-a897-ea8995c666d4 -H "X-Auth-Token: af6debf9e18c4e1e9709bb18b564a7ab" -H "Content-type: application/json"


# network resource
{
        "network": {
                    "admin_state_up": false, 
                    "id": "ebfa259d-6d84-49ac-a897-ea8995c666d4", 
                    "name": "test-12", 
                    "router:external": false, 
                    "shared": false, 
                    "status": "ACTIVE", 
                    "subnets": [], 
                    "tenant_id": "e70c0a66ca274efc9ca32a5d0532c298"
                   }
}

# delete network
# url = http://186.100.253.130:9696/v2.0/networks/{network_id}
$ curl -v -X DELETE http://186.100.253.130:9696/v2.0/networks/3c88bce8-3d36-4509-8300-445cfd936841 -H "X-Auth-Token: af6debf9e18c4e1e9709bb18b564a7ab" -H "Content-type: application/json"



(附) 好用的REST客戶端:

1. GUI:   rest-client

https://code.google.com/p/rest-client/


2. cmd line: curl



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