您的位置:首页 > 其它

手动操作 Neutron REST API

2014-01-18 16:22 274 查看
(一) 认证 -> 获取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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: