API

本文将给出一个简单例子,如何通过API获取实例列表与实例详情。

首先,使用用户名与密码获取token。在获取token前,我们先要知道keystone的URL,我们使用以下的命令获取:

$ openstack endpoint list --service keystone
+----------------------------------+-----------+--------------+--------------+---------+-----------+----------------------------+
| ID                               | Region    | Service Name | Service Type | Enabled | Interface | URL                        |
+----------------------------------+-----------+--------------+--------------+---------+-----------+----------------------------+
| 185e37babfb945b4b988e255d6587d6d | RegionOne | keystone     | identity     | True    | internal  | http://controller:5000/v3/ |
| 2678b37c0a8f4803a4acdeb09cce0f81 | RegionOne | keystone     | identity     | True    | admin     | http://controller:5000/v3/ |
| b3bc9d01d9cd4faa98dc3d8655714a15 | RegionOne | keystone     | identity     | True    | public    | http://controller:5000/v3/ |
+----------------------------------+-----------+--------------+--------------+---------+-----------+----------------------------+

因为我们是使用curlpostman等工具从外部访问,所以URL要选择public的。接下来,我们便可以使用用户名、密码等信息获取一个token了(API详情见https://developer.openstack.org/api-ref/identity/v3/)。

请求的URL为

POST http://controller:5000/v3/auth/tokens

需要的body参数如下(该参数有问题,得到的token权限还不够):

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "name": "admin",
                    "password": "123456",
                    "domain": {
                        "name": "Default"
                    }
                }
            }
        }
    }
}

请求成功后,Token包含在Response的Header中的x-subject-token字段,是一个类似如下的字符串:

该请求对应的openstack命令为

接下来,我们来使用这个token,获取实例列表。

同样,我们先查看nova服务的Endpoints

然后获取实例列表(API详情见https://developer.openstack.org/api-ref/compute/

返回的结果如下:

Reference

Last updated

Was this helpful?