| 119 |
jeje |
1 |
/**
|
|
|
2 |
* Copyright (C) 2007-2008 Elastic Grid, LLC.
|
|
|
3 |
*
|
|
|
4 |
* This file is part of Elastic Grid.
|
|
|
5 |
*
|
|
|
6 |
* Elastic Grid is free software: you can redistribute it and/or modify
|
|
|
7 |
* it under the terms of the GNU Lesser General Public License as
|
|
|
8 |
* published by the Free Software Foundation, either version 3 of the
|
|
|
9 |
* License, or any later version.
|
|
|
10 |
*
|
|
|
11 |
* Elastic Grid is distributed in the hope that it will be useful,
|
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
14 |
* GNU General Public License for more details.
|
|
|
15 |
*
|
|
|
16 |
* You should have received a copy of the GNU Lesser General Public License
|
|
|
17 |
* along with Elastic Grid. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
package com.elasticgrid.rest;
|
|
|
21 |
|
| 127 |
jeje |
22 |
import com.elasticgrid.cluster.ClusterManager;
|
|
|
23 |
import com.elasticgrid.model.ec2.impl.EC2ClusterImpl;
|
|
|
24 |
import com.elasticgrid.model.internal.Clusters;
|
|
|
25 |
import org.restlet.Context;
|
| 119 |
jeje |
26 |
import org.restlet.data.MediaType;
|
| 127 |
jeje |
27 |
import org.restlet.data.Request;
|
| 119 |
jeje |
28 |
import org.restlet.data.Response;
|
| 127 |
jeje |
29 |
import org.restlet.data.Reference;
|
|
|
30 |
import org.restlet.data.Status;
|
| 119 |
jeje |
31 |
import org.restlet.ext.jibx.JibxRepresentation;
|
| 127 |
jeje |
32 |
import org.restlet.ext.wadl.MethodInfo;
|
|
|
33 |
import org.restlet.ext.wadl.RepresentationInfo;
|
| 121 |
jeje |
34 |
import org.restlet.ext.wadl.WadlResource;
|
| 125 |
jeje |
35 |
import org.restlet.ext.wadl.DocumentationInfo;
|
| 127 |
jeje |
36 |
import org.restlet.resource.Representation;
|
|
|
37 |
import org.restlet.resource.ResourceException;
|
|
|
38 |
import org.restlet.resource.Variant;
|
|
|
39 |
import org.apache.commons.lang.StringUtils;
|
|
|
40 |
import java.util.Arrays;
|
|
|
41 |
import java.util.HashMap;
|
| 119 |
jeje |
42 |
|
| 121 |
jeje |
43 |
public class ClustersResource extends WadlResource {
|
| 119 |
jeje |
44 |
private ClusterManager clusterManager;
|
|
|
45 |
|
|
|
46 |
public ClustersResource(Context context, Request request, Response response) {
|
|
|
47 |
super(context, request, response);
|
| 125 |
jeje |
48 |
// Allow modifications of this resource via POST requests
|
|
|
49 |
setModifiable(true);
|
|
|
50 |
// Declare the kind of representations supported by this resource
|
| 119 |
jeje |
51 |
getVariants().add(new Variant(MediaType.APPLICATION_XML));
|
|
|
52 |
}
|
|
|
53 |
|
| 125 |
jeje |
54 |
/**
|
|
|
55 |
* Handle GET requests: describe all clusters.
|
|
|
56 |
*/
|
| 119 |
jeje |
57 |
@Override
|
|
|
58 |
public Representation represent(Variant variant) throws ResourceException {
|
|
|
59 |
Clusters clusters = new Clusters();
|
|
|
60 |
clusters.addCluster(new EC2ClusterImpl().name("cluster1"));
|
|
|
61 |
clusters.addCluster(new EC2ClusterImpl().name("cluster2"));
|
|
|
62 |
clusters.addCluster(new EC2ClusterImpl().name("cluster3"));
|
|
|
63 |
return new JibxRepresentation<Clusters>(MediaType.APPLICATION_XML, clusters, "ElasticGridREST");
|
|
|
64 |
// List<Cluster> clusters = null;
|
|
|
65 |
// try {
|
| 120 |
jeje |
66 |
// clusters = clusterManager.findClusters();
|
|
|
67 |
// return new JibxRepresentation<Clusters>(MediaType.APPLICATION_XML, new Clusters(clusters), "ElasticGridREST");
|
| 119 |
jeje |
68 |
// } catch (Exception e) {
|
| 120 |
jeje |
69 |
// throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, e);
|
| 119 |
jeje |
70 |
// }
|
|
|
71 |
}
|
|
|
72 |
|
| 125 |
jeje |
73 |
/**
|
| 127 |
jeje |
74 |
* Handle PUT requests: start a new cluster.
|
| 125 |
jeje |
75 |
*/
|
|
|
76 |
@Override
|
| 127 |
jeje |
77 |
public void storeRepresentation(Representation entity) throws ResourceException {
|
| 125 |
jeje |
78 |
super.acceptRepresentation(entity);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
@Override
|
|
|
82 |
protected void describeGet(MethodInfo info) {
|
|
|
83 |
super.describeGet(info);
|
|
|
84 |
info.setDocumentation("Describe all Elastic Grid clusters.");
|
| 127 |
jeje |
85 |
info.getResponse().setDocumentation("The clusters.");
|
|
|
86 |
RepresentationInfo representation = new RepresentationInfo();
|
|
|
87 |
representation.setDocumentation("This representation exposes all running Elastic Grid Clusters.");
|
|
|
88 |
representation.getDocumentations().get(0).setTitle("clusters");
|
|
|
89 |
representation.setMediaType(MediaType.APPLICATION_XML);
|
|
|
90 |
representation.getDocumentations().addAll(Arrays.asList(
|
|
|
91 |
new DocumentationInfo("Example of output:<pre><![CDATA[" +
|
|
|
92 |
"<clusters>\n" +
|
|
|
93 |
" <cluster name=\"cluster1\">\n" +
|
|
|
94 |
" <node profile=\"monitor\">ec2-75...</node>\n" +
|
|
|
95 |
" <node profile=\"monitor\">ec2-77...</node>\n" +
|
|
|
96 |
" <node profile=\"agent\">ec2-37...</node>\n" +
|
|
|
97 |
" </cluster>\n" +
|
|
|
98 |
" <cluster name=\"cluster2\">\n" +
|
|
|
99 |
" <node profile=\"monitor\">ec2-57...</node>\n" +
|
|
|
100 |
" <node profile=\"monitor\">ec2-63...</node>\n" +
|
|
|
101 |
" <node profile=\"agent\">ec2-31...</node>\n" +
|
|
|
102 |
" </cluster>\n" +
|
|
|
103 |
"</clusters>" +
|
|
|
104 |
"]]></pre>")
|
|
|
105 |
));
|
|
|
106 |
info.getResponse().setRepresentations(Arrays.asList(representation));
|
| 125 |
jeje |
107 |
}
|
|
|
108 |
|
|
|
109 |
@Override
|
| 127 |
jeje |
110 |
protected void describePut(MethodInfo info) {
|
| 125 |
jeje |
111 |
super.describePost(info);
|
|
|
112 |
info.setDocumentation("Start a new cluster.");
|
| 127 |
jeje |
113 |
info.getRequest().setDocumentation("The cluster to start.");
|
|
|
114 |
RepresentationInfo representation = new RepresentationInfo();
|
|
|
115 |
representation.setDocumentation("This representation exposes a request for starting a new Elastic Grid Cluster.");
|
|
|
116 |
representation.getDocumentations().get(0).setTitle("cluster-request");
|
|
|
117 |
representation.setMediaType(MediaType.APPLICATION_XML);
|
|
|
118 |
representation.getDocumentations().addAll(Arrays.asList(
|
|
|
119 |
new DocumentationInfo("Example of input:<pre><![CDATA[" +
|
|
|
120 |
"<cluster name=\"my-cluster\">\n" +
|
|
|
121 |
" <provisioning>\n" +
|
|
|
122 |
" <!-- Start 2 monitors -->\n" +
|
|
|
123 |
" <monitors>2</monitors>\n" +
|
|
|
124 |
" <!-- Start 3 agents -->\n" +
|
|
|
125 |
" <agents>3</agents>\n" +
|
|
|
126 |
" </provisioning>\n" +
|
|
|
127 |
"</cluster>" +
|
|
|
128 |
"]]></pre>")
|
|
|
129 |
));
|
|
|
130 |
info.getRequest().setRepresentations(Arrays.asList(representation));
|
| 125 |
jeje |
131 |
}
|
| 127 |
jeje |
132 |
|
|
|
133 |
@Override
|
|
|
134 |
public boolean allowDelete() {
|
|
|
135 |
return false;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
@Override
|
|
|
139 |
public boolean allowPost() {
|
|
|
140 |
return false;
|
|
|
141 |
}
|
| 119 |
jeje |
142 |
}
|