Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2427533
  • 博文数量: 293
  • 博客积分: 2660
  • 博客等级: 少校
  • 技术积分: 3632
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-03 17:50
文章分类

全部博文(293)

文章存档

2015年(13)

2014年(58)

2013年(73)

2012年(25)

2011年(30)

2010年(86)

2009年(8)

分类: 云计算

2014-03-02 11:03:22

 http://my.oschina.net/guol/blog/105430
openstack官方有提供api供开发者使用,可以使用api做一些外围的小工具,用来简化对openstack的管理。本篇主要讲解api中使用GET方法的接口,其余的POST、DELETE方法以后介绍。

    api-quickstart:

    api-doc:

    环境:ubuntu12.10(10.1.1.186)

    如果你看了官方的quickstart,他会让你先生成一个X-Auth-Token值,然后使用这个认证的值调用相关的api接口。如下过程:
curl -k -X 'POST' -v -d '{"auth":{"passwordCredentials":{"username": "guol", "password":"123456"}}}' -H 'Content-type: application/json' | python -mjson.tool
输出结果如下:

{
    "access": {
        "serviceCatalog": {},
        "token": {
            "expires": "2013-01-27T04:55:35Z",
            "id": "a4b6a871ff2f41a797c0b7d45c69f81e"
        },
        "user": {
            "id": "4c1a6122fc874a8bb541a34be3b316ad",
            "name": "guol",
            "roles": [],
            "roles_links": [],
            "username": "guol"
        }
    }
}然后使用token id调用你使用的api
curl -X 'GET' -H  "X-Auth-Token:a4b6a871ff2f41a797c0b7d45c69f81e" -v | python -mjson.tool
输出的结果如下:{
    "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值

curl -X POST -d  '{"auth": {"tenantName": "VM", "passwordCredentials":{"username": "admin", "password": "123456"}}}' -H "Content-type: application/json" | python -mjson.tool

返回值如下:
{
    "access": {
        "metadata": {
            "is_admin": 0,
            "roles": [
                "141927156f7a47d98858e51febaea8cf"
            ]
        },
        "serviceCatalog": [
            {
                "endpoints": [
                    {
                        "adminURL": "",
                        "id": "e02aaf58a24641049fddfe7385ce9399",
                        "internalURL": "",
                        "publicURL": "",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "nova",
                "type": "compute"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "",
                        "id": "7c48fa4d876d44e0a3cb324a45a14931",
                        "internalURL": "",
                        "publicURL": "",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "glance",
                "type": "image"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "",
                        "id": "c08114987bc0443d879436db6290dd08",
                        "internalURL": "",
                        "publicURL": "",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "volume",
                "type": "volume"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "",
                        "id": "cd6c3c2ac44340d6a78cb903ec9bcab3",
                        "internalURL": "",
                        "publicURL": "",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "ec2",
                "type": "ec2"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "",
                        "id": "a75755093500441ea954f4874ccecf15",
                        "internalURL": "",
                        "publicURL": "",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "swift",
                "type": "object-store"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "",
                        "id": "196e553f9bcc4c8fa425bc0fa28a144b",
                        "internalURL": "",
                        "publicURL": "",
                        "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
curl -X GET -H "X-Auth-Token:71fb5b7816804febbd89ed315fe67c94" -H "Content-type: application/json" | python -mjson.tool

返回结果如下
{
    "servers": [
        {
            "id": "ac561829-dffa-443e-a321-260d48f4cba3",
            "links": [
                {
                    "href": "",
                    "rel": "self"
                },
                {
                    "href": "",
                    "rel": "bookmark"
                }
            ],
            "name": "VM1"
        },
        {
            "id": "e3b84378-7598-48c8-8a1d-bd555c4c1f90",
            "links": [
                {
                    "href": "",
                    "rel": "self"
                },
                {
                    "href": "",
                    "rel": "bookmark"
                }
            ],
            "name": "VM2"
        },
        {
            "id": "beafef2e-26c4-4320-9b45-70006e71785c",
            "links": [
                {
                    "href": "",
                    "rel": "self"
                },
                {
                    "href": "",
                    "rel": "bookmark"
                }
            ],
            "name": "VM3"
        },
        {
            "id": "f8450270-9d96-4dba-b1c2-8ebcef1ff012",
            "links": [
                {
                    "href": "",
                    "rel": "self"
                },
                {
                    "href": "",
                    "rel": "bookmark"
                }
            ],
            "name": "VM4"
        }
    ]
}
通过返回值可以看到vm tenant中共包含VM1 VM2 VM3 VM4四台实例。

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

#!/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']

 

阅读(1195) | 评论(0) | 转发(0) |
0

上一篇:openstack学习_5

下一篇:MongoDB

给主人留下些什么吧!~~