RackSpace提供基于RESTful方式访问的API。通过结合jersey可以方便的操作。
具体的API参见:http://docs.rackspacecloud.com/servers/api/v1.0/cs-devguide-20091015.pdf.
介绍几个常用的API:
返回的格式有两种,1、JSON; 2、XML.这里都返回Json
一、认证
通过RESTful方式访问云,首先要进行身份验证。
提供APIkey,通过验证后云会返回给你一个AuthToken和ServerManagementUrl。
AuthToken的有效期只有24个小时,过期后需要重新认证。
1 private final String BASE_URI = "https://auth.api.rackspacecloud.com/v1.0";
2 private String serverManagementUrl = null;
3 private String authToken = null;
4
5 public void auth() throws Exception {
6 Client client = new Client();
7 WebResource webResource = client.resource(BASE_URI);
8 ClientResponse response = null;
9
10 try {
11 response = webResource.header("X-Auth-User", "alps").
12 header("X-Auth-Key", "21fee63xxxxxxxx0").get(ClientResponse.class);
13 } catch (UniformInterfaceException ex) {
14 response = ex.getResponse();
15 }
16
17 int status = response.getStatus();
18 switch (status) {
19 case 204:
20 MultivaluedMap<String, String> headers = response.getHeaders();
21 serverManagementUrl = headers.get("X-Server-Management-Url").get(0);
22 authToken = headers.get("X-Auth-Token").get(0);
23 break;
24 default:
25 throw new UniformInterfaceException(response);
26 }
BASE_URL,是进行身份认证的一个url。如果认证通过response code是204.没有通过则返回401.
请求这个url时,需要传入两个参数
1)X-Auth-User:用户名(RackSpace帐号)
2)X-Auth-Key:API Key(点击Control Panel的Your Account菜单下的API Access获得)
如果验证成功,返回的Response如下:
HTTP/1.1 204 No Content
Date: Mon, 12 Nov 2007 15:32:21 GMT
Server: Apache
X-Server-Management-Url: https://servers.api.rackspacecloud.com/v1.0/35428
X-Storage-Url: https://storage.clouddrive.com/v1/CloudFS_9c83b-5ed4
X-CDN-Management-Url: https://cdn.clouddrive.com/v1/CloudFS_9c83b-5ed4
X-Auth-Token: eaaafd18-0fed-4b3a-81b4-663c99ec1cbb
Content-Length: 0
Content-Type: text/plain; charset=UTF-8
两个有用的信息是
1)X-Server-Management-Url:通过这个url可以对云进行操作。
2)X-Auth-Token:身份令牌,每次操作时都要当参入传入,注意有时限,有效期为24小时。
二、获得云里的服务器信息
Verb |
URL |
Desc |
Get |
/servers |
列出所有的server(只有id和name) |
Get |
/servers/detail |
列出所有的server(详细信息) |
正常的Response Code:200,203
错误的Response Code:cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest (400), overLimit (413)
查询详细信息
1 public void getAllserverInfo() throws Exception {
2 Client client = new Client();
3 WebResource webResource = client.resource("https://servers.api.rackspacecloud.com/v1.0/35428/servers/detail");
4 ClientResponse response = null;
5 try {
6 response = webResource.header("X-Auth-Token", authToken).get(ClientResponse.class);
7 } catch (Exception e) {
8 e.printStackTrace();
9 }
10
11 int status = response.getStatus();
12 switch (status) {
13 case 200:
14 case 203:
15 System.out.println(IOUtils.toString(response.getEntityInputStream()));
16 break;
17 default:
18 throw new UniformInterfaceException(response);
19 }
20 }
Sample JSON Response (detail)
1{
2"servers" :
3 [
4 {
5 "id" : 1234,
6 "name" : "sample-server",
7 "imageId" : 2,
8 "flavorId" : 1,
9 "hostId" : "e4d909c290d0fb1ca068ffaddf22cbd0",
10 "status" : "BUILD",
11 "progress" : 60,
12 "addresses" : {
13 "public" : [
14 "67.23.10.132",
15 "67.23.10.131"
16 ],
17 "private" : [
18 "10.176.42.16"
19 ]
20 },
21 "metadata" : {
22 "Server Label" : "Web Head 1",
23 "Image Version" : "2.1"
24 }
25 }
26 ]
27}
三、创建云中的服务器
Verb |
URI |
Desc |
Post |
/servers |
创建新服务器 |
正常的Response Code:200
错误的Response Code:cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest (400), overLimit (413)
1 public void creatServerVersion() throws Exception {
2 Client client = new Client();
3 WebResource webResource = client.resource("https://servers.api.rackspacecloud.com/v1.0/35428/servers");
4 ClientResponse response = null;
5 String creatServer = "{"
6 + "\"server\" : {"
7 + "\"name\" : \"CreateByAPP\","
8 + "\"imageId\" : 51,"
9 + "\"flavorId\" : 1,"
10 + "\"metadata\" : "
11 + "{\"My Server Name\" : \"Apache1\"}"
12 + "}" + "}";
13 try {
14 response = webResource.header("X-Auth-Token", authToken).
15 entity(creatServer, MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class);
16 } catch (Exception e) {
17 e.printStackTrace();
18 }
19 int status = response.getStatus();
20 switch (status) {
21 case 200:
22 System.out.println(IOUtils.toString(response.getEntityInputStream()));
23 break;
24 default:
25 throw new UniformInterfaceException(response);
26 }
27 }
主要参数说明:
1)name:server的name
2)imageId:基于那个image创建的instance
3)flavorId:硬件配置,每一个flavor都有唯一的内存、硬盘大小(这里flavorId=1,代表内存256M,硬盘10G)。
Sample JSON Response
1{
2 "server" : {
3 "id" : 1235,
4 "name" : "new-server-test",
5 "imageId" : 2,
6 "flavorId" : 1,
7 "hostId" : "e4d909c290d0fb1ca068ffaddf22cbd0",
8 "progress" : 0,
9 "status" : "BUILD",
10 "adminPass" : "GFf1j9aP",
11 "metadata" : {
12 "My Server Name" : "Apache1"
13 },
14 "addresses" : {
15 "public" : [
16 "67.23.10.138"
17 ],
18 "private" : [
19 "10.176.42.19"
20 ]
21 }
22 }
23}
四、删除服务器
Verb |
URI |
Desc |
Delete |
/servers/id |
根据serverid,删除指定的server |
正常的Response Code:202
错误的Response Code:cloudServersFault (400, 500), serviceUnavailable (503), unauthorized (401), badRequest (400), overLimit (413)
关键代码
ClientResponse response = webResource.header("X-Auth-Token", authToken).delete(ClientResponse.class);
更多的操作,可以去查询API
2010-09-05
Alps
|
|
CALENDER
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|
公告
留言簿(5)
随笔分类
随笔档案
文章分类
最新评论
阅读排行榜
评论排行榜
Powered By: 博客园 模板提供:沪江博客
|