# 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/ |
+----------------------------------+-----------+--------------+--------------+---------+-----------+----------------------------+
```

因为我们是使用`curl`或`postman`等工具从外部访问，所以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`字段，是一个类似如下的字符串：

```
gAAAAABcs_WF4UtZ3vamvQGaRDiPHVYY7VyfND1PVRfKJLwjaVihxUgNr-RYLxPPubJPwtUuUKPoQWWJ_0Ha0ITtXPR6l3oRuO4t9oYFlDUFtT4jmEqNOpAxhx6C6oWfa6HPGk245UUlu-6-UeUKMqFMT7kaM5QebA
```

该请求对应的openstack命令为

```
$ openstack token issue
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field      | Value                                                                                                                                                                                   |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| expires    | 2019-04-15T03:27:49+0000                                                                                                                                                                |
| id         | gAAAAABcs-wlsc3GRuTU2PwO1XFybJ6CFrVdHh-TV4Cbq1gxszZpiw0SsLEPV4GelJ9IyTOHOeC45727ubk2W7qMzmGQ8YIcaiW5YpWIv8HbtlA0jJ3cP5zp_w4fcYqlxb9dD5x-zhsWpHbX5auiv1TEAHqTAd8BefPRxMmbWm6PyRSAoZqfgC4 |
| project_id | a190f0f7f1a7416fbb83d1db0594544b                                                                                                                                                        |
| user_id    | 0e785ac45e9f45758ba4ded05edcf8f2                                                                                                                                                        |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
```

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

同样，我们先查看nova服务的`Endpoints`：

```
$ openstack endpoint list --service nova
+----------------------------------+-----------+--------------+--------------+---------+-----------+-----------------------------+
| ID                               | Region    | Service Name | Service Type | Enabled | Interface | URL                         |
+----------------------------------+-----------+--------------+--------------+---------+-----------+-----------------------------+
| 3c8a63c1939d4310bc3270109e4e749b | RegionOne | nova         | compute      | True    | admin     | http://controller:8774/v2.1 |
| 59ed89d1f72b4b9195d60df38a27da6c | RegionOne | nova         | compute      | True    | public    | http://controller:8774/v2.1 |
| 796167511f6449d0845c0b7da583b175 | RegionOne | nova         | compute      | True    | internal  | http://controller:8774/v2.1 |
+----------------------------------+-----------+--------------+--------------+---------+-----------+-----------------------------+
```

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

```
$ curl -H "X-Auth-Token:<token>" http://controller:8774/v2.1/servers
```

返回的结果如下：

```
{
    "servers": [
        {
            "id": "d28580ef-0c1d-4c51-a714-16f8e5eccc05",
            "links": [
                {
                    "href": "http://10.142.232.162:8774/v2.1/servers/d28580ef-0c1d-4c51-a714-16f8e5eccc05",
                    "rel": "self"
                },
                {
                    "href": "http://10.142.232.162:8774/servers/d28580ef-0c1d-4c51-a714-16f8e5eccc05",
                    "rel": "bookmark"
                }
            ],
            "name": "cirror"
        }
    ]
}
```

## Reference

* <https://developer.openstack.org/api-ref/identity/v3/>
* <https://developer.openstack.org/api-ref/compute/>
