培训-UOS API简单示例

获取 uosrc 文件

登录Dashboard -> 个人设置 -> API信息 -> 下载UOSRC

打开uosrc文件,找到 OS_PROJECT_ID 和 OS_USER_ID,下面的操作会用到。

在下面的curl命令中,用OS_PROJECT_ID的值去替换{YOUR-PROJECT-ID},用OS_USER_ID的值去替换{YOUR-USER-ID},用你账号的密码的值去替换{YOUR-ACCOUNT-PASSWORD}。

名词解释:

  • token: 令牌
  • flavor: 虚拟机配置
  • image: 操作系统镜像
  • network: 隔离的二层广播域
  • subnet: IP地址段
  • instance: 虚拟机
  • server: 虚拟机
  • floatingip: 公网IP
  • uuid: 唯一id
  • port: 网络接口(网卡)

获取 Token

执行以下操作获得你账号的Token值,URL的前缀是 identity,更详细信息请参考 http://docs.ustack.com/api_doc/identity_api.html 或 OpenStack API。

curl -i -X POST https://identity.api.ustack.com/v3/auth/tokens  \
 -H "Accept: application/json" \
 -H "Content-Type: application/json" \
 -d '{"auth": {"scope": {"project": {"id": "{YOUR-PROJECT-ID}"}},
               "identity": {"password": {"user":{ "password": "{YOUR-ACCOUNT-PASSWORD}",
                                                  "id": "{YOUR-USER-ID}"}},
                                         "methods": ["password"]}}}'

在curl命令的返回的Response Header中有一个X-Subject-Token,它的值就是你的Token值。

HTTP/1.1 201 Created
Server: ngx_openresty
Date: Mon, 15 Dec 2014 09:38:51 GMT
Content-Type: application/json
Content-Length: 1664
Connection: keep-alive
X-Subject-Token: 3449bf20fdde48d4a6b1933b30de64c2
Vary: X-Auth-Token,Accept-Encoding

{"token": ......}                                     

下面的curl操作的X-Auth-Token请填上你的Token的值。Token的有效期是1天

获取 Flavor 列表

执行以下操作,你可以获得Flavor(虚拟机配置类型列表),URL的前缀是 bj1.compute,更详细信息请参考 http://docs.ustack.com/api_doc/compute_api.html#flavorshttp://docs.ustack.com/price_n_quota.html#flavor-price-list 或 OpenStack API。

curl -i -s -X GET https://bj1.compute.api.ustack.com/v2/{YOUR-PROJECT-ID}/flavors \
 -H 'Content-Type: application/json' \
 -H 'Accept: application/json' \
 -H 'X-Auth-Token: 3449bf20fdde48d4a6b1933b30de64c2'

在curl返回的Response Body中,我们找到micro-1配置类型的uuid是 8abaa0f9-30e1-4509-8ba5-5c97366029df,创建虚拟机的时候需要用到这个值。

HTTP/1.1 200 OK
Server: ngx_openresty
Date: Mon, 15 Dec 2014 09:49:39 GMT
Content-Type: application/json
Content-Length: 6253
Connection: keep-alive
X-Compute-Request-Id: req-e3748591-3e1b-4b50-ba6a-401faf2c6940

{"flavors": ......}

获取 Image 列表

执行以下操作,你可以获得Image(虚拟机镜像列表),URL的前缀是 bj1.image,更详细信息请参考 http://docs.ustack.com/api_doc/image_api.html 或 OpenStack API。

你也可以在Dashboard中的镜像列表中找到你所需要镜像的UUID,比如我想使用CentOS 6.6 64bit镜像,这个镜像的uuid是 54cdd4d5-f570-4668-a4ae-ab7e8e398244 。

curl -i -s -X GET https://bj1.image.api.ustack.com/v2/images \
 -H "Content-Type: application/json" \
 -H 'X-Auth-Token: 3449bf20fdde48d4a6b1933b30de64c2'


HTTP/1.1 200 OK
Server: ngx_openresty
Date: Mon, 15 Dec 2014 10:04:32 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 23334
Connection: keep-alive
X-Openstack-Request-Id: req-e9e39802-7ef8-4e5b-a4bb-4647722b45a0

{"images": ......}

获取 Network 列表

创建虚拟机需要指定的Network和Subnet。现在的Network有两种:基础网络(共享网络)、私有网络。更多详细信息请参考 http://docs.ustack.com/api_doc/network_api.html 或 OpenStack API。

执行以下操作,你可以获得Network列表,URL的前缀是 bj1.network 。

curl -i -s -X GET https://bj1.network.api.ustack.com/v2.0/networks \
 -H "Content-Type: application/json" \
 -H "Accept: application/json" \
 -H 'X-Auth-Token: 3449bf20fdde48d4a6b1933b30de64c2'

HTTP/1.1 200 OK
Server: ngx_openresty
Date: Mon, 15 Dec 2014 10:09:04 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 1903
Connection: keep-alive

{"networks": ......}

在curl返回的Response Body中,我们找到名字为 shared 的Network,它的uuid是 ba05e057-1eb8-4cb2-af58-9b99a72df5ed 。在这个Network下选择一个Subnet,这个Subnet的uuid是 3b426a5f-db56-4e56-a57d-5e493c5957ff 。

我们还找到了一个名字为 BGP 的Network,它的uuid是 ddabcc63-8617-465c-8a6d-f534000887e3,我们需要用它来创建FloatingIP(公网IP)。

创建 Instance

创建虚拟机需要指定Flavor、Image、Network、Subnet的UUID,还需要指定虚拟机的root密码(也可以指定ssh keypair)。更多相信信息请参考 http://docs.ustack.com/api_doc/compute_api.html 或 OpenStack API。

执行以下操作,你可以创建1台虚拟机,URL的前缀是 bj1.compute 。

curl -i -X POST https://bj1.compute.api.ustack.com/v2/{YOUR-PROJECT-ID}/servers  \
 -H "Content-Type: application/json" \
 -H "Accept: application/json" \
 -H "X-Auth-Token: 3449bf20fdde48d4a6b1933b30de64c2" \
 -d '{"server": {"name": "helloword",
                 "imageRef": "54cdd4d5-f570-4668-a4ae-ab7e8e398244",
                 "adminPass": "Pa55Mord#V045$",
                 "flavorRef": "8abaa0f9-30e1-4509-8ba5-5c97366029df",
                 "max_count": 1,
                 "min_count": 1,
                 "networks": [{"uuid": "ba05e057-1eb8-4cb2-af58-9b99a72df5ed",
                               "subnet_id":"3b426a5f-db56-4e56-a57d-5e493c5957ff"}]}}'

HTTP/1.1 202 Accepted
Server: ngx_openresty
Date: Mon, 15 Dec 2014 10:16:41 GMT
Content-Type: application/json
Content-Length: 460
Connection: keep-alive
Location: http://bj1.compute.api.ustack.com/v2/1a5d983149cc49618ed851bd1b2acc0f/servers/8f56869d-f6e7-417a-9355-423f0e2349f9
X-Compute-Request-Id: req-364b7343-54fd-4e30-9928-b01bf55bffc1

{"server": {"security_groups": [{"name": "default"}], 
            "OS-DCF:diskConfig": "MANUAL", 
            "id": "8f56869d-   f6e7-417a-9355-423f0e2349f9", 
            "links": [{"href": "http://bj1.compute.api.ustack.com/v2/1a5d983149cc49618ed851bd1b2acc0f/    servers/8f56869d-f6e7-417a-9355-423f0e2349f9", "rel": "self"}, {"href": "http://bj1.compute.api.ustack.com/ 1a5d983149cc49618ed851bd1b2acc0f/servers/8f56869d-f6e7-417a-9355-423f0e2349f9", "rel": "bookmark"}], 
            "adminPass":   "Pa55Mord#V045$"}}  

在curl返回的Response Body中,我们可以得到虚拟机的uuid是 8f56869d-f6e7-417a-9355-423f0e2349f9 。

创建 FloatingIP

创建公网IP你需要指定带宽大小、所在网络(一般都是BGP网络)、名字。更多相信信息请参考 http://docs.ustack.com/api_doc/network_api.html#ip-floating-ips 或 OpenStack API。

执行以下操作,你可以创建1个公网IP,URL的前缀是 bj1.network 。

curl -i -X POST https://bj1.network.api.ustack.com/v2.0/floatingips  \
 -H "Content-Type: application/json" \
 -H "Accept: application/json" \
 -H "X-Auth-Token: 3449bf20fdde48d4a6b1933b30de64c2" \
 -d '{"floatingip":{"rate_limit":2048,
                    "floating_network_id":"ddabcc63-8617-465c-8a6d-f534000887e3",
                    "uos:name":"2Mb-fip"}}'


HTTP/1.1 201 Created
Server: ngx_openresty
Date: Mon, 15 Dec 2014 10:33:50 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 464
Connection: keep-alive

{"floatingip": {"router_id": null, 
                "status": "DOWN", 
                "port_id": null,
                "created_at": "2014-12-15T10:33:50.472334", 
                "rate_limit": 2048,       
                "uos:name": "2Mb-fip",
                "uos:registerno": "",
                "floating_network_id": "ddabcc63-8617-465c-8a6d-f534000887e3",
                "fixed_ip_address": null,
                "floating_ip_address": "42.62.101.89", 
                "tenant_id":    "1a5d983149cc49618ed851bd1b2acc0f",
                "floating_subnet_id": "f9eafd3e-e6f6-4c20-9a8a-15ad061f2840",
                "id": "b8c57425-804a-4248-88fe-0a9ce61b662c"}}

在curl返回的Response Body中,我们知道这个公网IP的地址是 42.62.101.89 ,它的uuid是 b8c57425-804a-4248-88fe-0a9ce61b662c 。

获取 Instance 的 Port

刚创建的虚拟机都只有1块网卡(port),我们需要获得这个网卡(port)的uuid,以便在上面绑定公网IP。

你需要在URL上指定虚拟机的UUID,才能执行以下操作,URL的前缀是 bj1.network 。注意 URL 最后面的是虚拟机的uuid。

curl -i -s -X GET https://bj1.network.api.ustack.com/v2.0/ports?device_id=8f56869d-f6e7-417a-9355-423f0e2349f9 \
 -H 'Content-Type: application/json' \
 -H 'Accept: application/json' \
 -H 'X-Auth-Token: 3449bf20fdde48d4a6b1933b30de64c2'


 HTTP/1.1 200 OK
Server: ngx_openresty
Date: Mon, 15 Dec 2014 10:41:09 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 639
Connection: keep-alive

{"ports": [{"status": "ACTIVE", 
            "created_at": "2014-12-15T10:16:42.000000", 
            "name": "nic-2c3f15ed", 
            "allowed_address_pairs":    [], 
            "admin_state_up": true, 
            "network_id": "ba05e057-1eb8-4cb2-af58-9b99a72df5ed", 
            "tenant_id":  "1a5d983149cc49618ed851bd1b2acc0f", 
            "extra_dhcp_opts": [], 
            "binding:vnic_type": "normal", 
            "device_owner": "compute:None",   
            "mac_address": "fa:16:3e:35:33:6a", 
            "fixed_ips": [{"subnet_id": "3b426a5f-db56-4e56-a57d-5e493c5957ff", 
                           "ip_address":   "10.250.9.198"}], 
                           "id": "2c3f15ed-9a57-442f-8a93-92ce232bc6e2", 
                           "security_groups": ["4bbab7ac-b333-4182-8f25-af5f322efb31"],    
                           "device_id": "8f56869d-f6e7-417a-9355-423f0e2349f9"}]}

在curl返回的Response Body中,我们知道这个虚拟机网卡的uuid是 2c3f15ed-9a57-442f-8a93-92ce232bc6e2 。

绑定 FloatingIP 到 Instance 的 Port 上

绑定公网IP到虚拟机的网卡(port),需要指定公网IP的uuid和网卡的uuid。更多详细信息请参考 http://docs.ustack.com/api_doc/network_api.html#ip-floating-ips

执行以下操作,你可以绑定公网IP,URL的前缀是 bj1.network 。注意 URL 最后面的是公网IP的uuid。

curl -i -s -X PUT https://bj1.network.api.ustack.com/v2.0/floatingips/b8c57425-804a-4248-88fe-0a9ce61b662c \
 -H "Content-Type: application/json" \
 -H "Accept: application/json" \
 -H "X-Auth-Token: 3449bf20fdde48d4a6b1933b30de64c2" \
 -d '{"floatingip": {"port_id": "2c3f15ed-9a57-442f-8a93-92ce232bc6e2"}}'


HTTP/1.1 200 OK
Server: ngx_openresty
Date: Mon, 15 Dec 2014 10:45:43 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 542
Connection: keep-alive

{"floatingip": {"router_id": "63dbb014-e030-4560-9bd5-e1454a6fb19c", 
                "status": "DOWN", 
                "port_id":   "2c3f15ed-9a57-442f-8a93-92ce232bc6e2", 
                "created_at": "2014-12-15T10:33:50.000000", 
                "rate_limit": 2048, "uos:name": "2Mb-fip", 
                "uos:registerno": "", 
                "floating_network_id": "ddabcc63-8617-465c-8a6d-f534000887e3", 
                "fixed_ip_address":  "10.250.9.198", 
                "floating_ip_address": "42.62.101.89", 
                "tenant_id": "1a5d983149cc49618ed851bd1b2acc0f",     
                "floating_subnet_id": "f9eafd3e-e6f6-4c20-9a8a-15ad061f2840", 
                "id": "b8c57425-804a-4248-88fe-0a9ce61b662c"}}

通过 FloatingIP 访问 Instance

可以通过以下命令使用ssh登录instance,假如有问题,请查看安全组规则是否设置正常。

ssh  root@42.62.101.89

Leave a Reply

Your email address will not be published. Required fields are marked *