| 120 |
jeje |
1 |
/**
|
| 337 |
jeje |
2 |
* Elastic Grid
|
|
|
3 |
* Copyright (C) 2008-2009 Elastic Grid, LLC.
|
|
|
4 |
*
|
|
|
5 |
* This program is free software: you can redistribute it and/or modify
|
|
|
6 |
* it under the terms of the GNU Affero General Public License as
|
|
|
7 |
* published by the Free Software Foundation, either version 3 of the
|
|
|
8 |
* License, or (at your option) any later version.
|
|
|
9 |
*
|
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
13 |
* GNU Affero General Public License for more details.
|
|
|
14 |
*
|
|
|
15 |
* You should have received a copy of the GNU Affero General Public License
|
| 182 |
jeje |
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 120 |
jeje |
17 |
*/
|
|
|
18 |
|
|
|
19 |
package com.elasticgrid.rest;
|
|
|
20 |
|
|
|
21 |
import com.elasticgrid.cluster.ClusterManager;
|
|
|
22 |
import com.elasticgrid.model.Cluster;
|
| 135 |
jeje |
23 |
import com.elasticgrid.model.ClusterProvisioning;
|
| 343 |
jeje |
24 |
import com.elasticgrid.model.NodeProfile;
|
| 455 |
jeje |
25 |
import com.elasticgrid.model.NodeProfileInfo;
|
| 343 |
jeje |
26 |
import com.elasticgrid.model.ec2.EC2NodeType;
|
| 159 |
jeje |
27 |
import org.jibx.runtime.JiBXException;
|
| 120 |
jeje |
28 |
import org.restlet.Context;
|
| 159 |
jeje |
29 |
import org.restlet.data.Form;
|
| 120 |
jeje |
30 |
import org.restlet.data.MediaType;
|
|
|
31 |
import org.restlet.data.Request;
|
|
|
32 |
import org.restlet.data.Response;
|
|
|
33 |
import org.restlet.data.Status;
|
|
|
34 |
import org.restlet.ext.jibx.JibxRepresentation;
|
| 135 |
jeje |
35 |
import org.restlet.ext.wadl.DocumentationInfo;
|
| 125 |
jeje |
36 |
import org.restlet.ext.wadl.MethodInfo;
|
| 127 |
jeje |
37 |
import org.restlet.ext.wadl.ParameterInfo;
|
| 138 |
jeje |
38 |
import org.restlet.ext.wadl.ParameterStyle;
|
| 159 |
jeje |
39 |
import org.restlet.ext.wadl.RepresentationInfo;
|
|
|
40 |
import org.restlet.ext.wadl.WadlResource;
|
| 120 |
jeje |
41 |
import org.restlet.resource.Representation;
|
|
|
42 |
import org.restlet.resource.ResourceException;
|
|
|
43 |
import org.restlet.resource.Variant;
|
| 159 |
jeje |
44 |
import java.io.IOException;
|
| 127 |
jeje |
45 |
import java.util.Arrays;
|
| 135 |
jeje |
46 |
import java.util.concurrent.TimeoutException;
|
| 120 |
jeje |
47 |
|
| 121 |
jeje |
48 |
public class ClusterResource extends WadlResource {
|
| 120 |
jeje |
49 |
private String clusterName;
|
|
|
50 |
private ClusterManager clusterManager;
|
|
|
51 |
|
| 131 |
jeje |
52 |
@Override
|
|
|
53 |
public void init(Context context, Request request, Response response) {
|
|
|
54 |
super.init(context, request, response);
|
| 455 |
jeje |
55 |
clusterManager = RestJSB.getClusterManager();
|
| 127 |
jeje |
56 |
// Allow modifications of this resource via POST requests
|
|
|
57 |
setModifiable(true);
|
|
|
58 |
setAutoDescribed(true);
|
|
|
59 |
// Declare the kind of representations supported by this resource
|
|
|
60 |
getVariants().add(new Variant(MediaType.APPLICATION_XML));
|
|
|
61 |
// Extract URI variables
|
| 131 |
jeje |
62 |
clusterName = (String) request.getAttributes().get("clusterName");
|
| 120 |
jeje |
63 |
}
|
| 127 |
jeje |
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Handle GET requests: describe cluster.
|
|
|
67 |
*/
|
| 120 |
jeje |
68 |
@Override
|
|
|
69 |
public Representation represent(Variant variant) throws ResourceException {
|
| 132 |
jeje |
70 |
try {
|
|
|
71 |
Cluster cluster = clusterManager.cluster(clusterName);
|
|
|
72 |
return new JibxRepresentation<Cluster>(MediaType.APPLICATION_XML, cluster, "ElasticGridREST");
|
|
|
73 |
} catch (Exception e) {
|
|
|
74 |
e.printStackTrace();
|
|
|
75 |
throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, e);
|
|
|
76 |
}
|
| 120 |
jeje |
77 |
}
|
|
|
78 |
|
| 127 |
jeje |
79 |
/**
|
|
|
80 |
* Handle POST requests: update cluster.
|
|
|
81 |
*/
|
| 125 |
jeje |
82 |
@Override
|
| 127 |
jeje |
83 |
public void acceptRepresentation(Representation entity) throws ResourceException {
|
| 135 |
jeje |
84 |
String clusterName = null;
|
| 343 |
jeje |
85 |
NodeProfileInfo monitorsInfo = null;
|
|
|
86 |
NodeProfileInfo monitorsAndAgentsInfo = null;
|
|
|
87 |
NodeProfileInfo agentsInfo = null;
|
| 135 |
jeje |
88 |
if (MediaType.APPLICATION_XML.equals(entity.getMediaType())) {
|
|
|
89 |
try {
|
|
|
90 |
JibxRepresentation<ClusterProvisioning> representation =
|
|
|
91 |
new JibxRepresentation<ClusterProvisioning>(entity, ClusterProvisioning.class, "ElasticGridREST");
|
|
|
92 |
ClusterProvisioning clusterProvisioning = representation.getObject();
|
|
|
93 |
clusterName = clusterProvisioning.getClusterName();
|
| 343 |
jeje |
94 |
monitorsInfo = clusterProvisioning.getMonitorsInfo();
|
|
|
95 |
monitorsAndAgentsInfo = clusterProvisioning.getMonitorsAndAgentsInfo();
|
|
|
96 |
agentsInfo = clusterProvisioning.getAgentsInfo();
|
| 135 |
jeje |
97 |
} catch (JiBXException e) {
|
|
|
98 |
e.printStackTrace();
|
|
|
99 |
throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
|
|
|
100 |
} catch (IOException e) {
|
|
|
101 |
e.printStackTrace();
|
|
|
102 |
throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
|
|
|
103 |
}
|
|
|
104 |
} else if (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType())) {
|
|
|
105 |
Form form = new Form(entity);
|
|
|
106 |
clusterName = form.getFirstValue("clusterName");
|
| 343 |
jeje |
107 |
monitorsInfo = new NodeProfileInfo(NodeProfile.MONITOR,
|
|
|
108 |
EC2NodeType.valueOf(form.getFirstValue("monitorNodeType")),
|
|
|
109 |
Integer.parseInt(form.getFirstValue("numberOfMonitors")));
|
|
|
110 |
monitorsAndAgentsInfo = new NodeProfileInfo(NodeProfile.MONITOR_AND_AGENT,
|
|
|
111 |
EC2NodeType.valueOf(form.getFirstValue("monitorAndAgentNodeType")),
|
|
|
112 |
Integer.parseInt(form.getFirstValue("numberOfMonitorsAndAgents")));
|
|
|
113 |
agentsInfo = new NodeProfileInfo(NodeProfile.AGENT,
|
|
|
114 |
EC2NodeType.valueOf(form.getFirstValue("agentNodeType")),
|
|
|
115 |
Integer.parseInt(form.getFirstValue("numberOfAgents")));
|
| 135 |
jeje |
116 |
}
|
|
|
117 |
try {
|
| 343 |
jeje |
118 |
clusterManager.resizeCluster(clusterName, Arrays.asList(monitorsInfo, monitorsAndAgentsInfo, agentsInfo));
|
| 135 |
jeje |
119 |
} catch (TimeoutException e) {
|
|
|
120 |
e.printStackTrace();
|
|
|
121 |
throw new ResourceException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, e);
|
|
|
122 |
} catch (Exception e) {
|
|
|
123 |
e.printStackTrace();
|
|
|
124 |
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
|
|
|
125 |
}
|
| 127 |
jeje |
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Handle DELETE requests: stop the cluster.
|
|
|
130 |
*/
|
|
|
131 |
@Override
|
|
|
132 |
public void removeRepresentations() throws ResourceException {
|
| 135 |
jeje |
133 |
try {
|
|
|
134 |
clusterManager.stopCluster(clusterName);
|
|
|
135 |
} catch (Exception e) {
|
|
|
136 |
e.printStackTrace();
|
|
|
137 |
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Could not stop cluster " + clusterName, e);
|
|
|
138 |
}
|
| 127 |
jeje |
139 |
}
|
|
|
140 |
|
|
|
141 |
@Override
|
| 125 |
jeje |
142 |
protected void describeGet(MethodInfo info) {
|
|
|
143 |
super.describeGet(info);
|
| 127 |
jeje |
144 |
info.setDocumentation("Describe cluster {clusterName}.");
|
|
|
145 |
info.getResponse().setDocumentation("The cluster.");
|
|
|
146 |
RepresentationInfo representation = new RepresentationInfo();
|
|
|
147 |
representation.setDocumentation("This resource exposes cluster {clusterName}.");
|
|
|
148 |
representation.getDocumentations().get(0).setTitle("cluster");
|
|
|
149 |
representation.setMediaType(MediaType.APPLICATION_XML);
|
|
|
150 |
representation.getDocumentations().addAll(Arrays.asList(
|
|
|
151 |
new DocumentationInfo("Example of output:<pre><![CDATA[" +
|
| 143 |
jeje |
152 |
"<cluster name=\"cluster1\" xmlns=\"urn:elastic-grid:eg\">\n" +
|
| 127 |
jeje |
153 |
" <node profile=\"monitor\">ec2-75...</node>\n" +
|
|
|
154 |
" <node profile=\"monitor\">ec2-77...</node>\n" +
|
|
|
155 |
" <node profile=\"agent\">ec2-37...</node>\n" +
|
|
|
156 |
" <node profile=\"agent\">ec2-33...</node>\n" +
|
|
|
157 |
" <node profile=\"agent\">ec2-32...</node>\n" +
|
|
|
158 |
"</cluster>" +
|
|
|
159 |
"]]></pre>")
|
|
|
160 |
));
|
| 143 |
jeje |
161 |
representation.setXmlElement("eg:cluster");
|
| 127 |
jeje |
162 |
info.getResponse().setRepresentations(Arrays.asList(representation));
|
| 125 |
jeje |
163 |
}
|
|
|
164 |
|
| 127 |
jeje |
165 |
@Override
|
|
|
166 |
protected void describePost(MethodInfo info) {
|
|
|
167 |
super.describePut(info);
|
| 135 |
jeje |
168 |
info.setDocumentation("Update Elastic Grid Cluster {clusterName}.");
|
|
|
169 |
info.getRequest().setDocumentation("The cluster to update.");
|
|
|
170 |
RepresentationInfo xmlRepresentation = new RepresentationInfo();
|
|
|
171 |
xmlRepresentation.setDocumentation("This representation exposes a provisioning request for updating an Elastic Grid Cluster.");
|
|
|
172 |
xmlRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
|
|
|
173 |
xmlRepresentation.setMediaType(MediaType.APPLICATION_XML);
|
|
|
174 |
xmlRepresentation.getDocumentations().addAll(Arrays.asList(
|
|
|
175 |
new DocumentationInfo("Example of input:<pre><![CDATA[" +
|
| 143 |
jeje |
176 |
"<cluster-provisioning name=\"my-cluster\" xmlns=\"urn:elastic-grid:eg\">\n" +
|
| 135 |
jeje |
177 |
"\t<!-- Start 2 monitors -->\n" +
|
|
|
178 |
"\t<monitors>2</monitors>\n" +
|
| 337 |
jeje |
179 |
"\t<!-- Start 4 monitors and agents -->\n" +
|
|
|
180 |
"\t<monitors-and-agents>4</monitors-and-agents>\n" +
|
| 135 |
jeje |
181 |
"\t<!-- Start 3 agents -->\n" +
|
|
|
182 |
"\t<agents>3</agents>\n" +
|
|
|
183 |
"</cluster-provisioning>" +
|
|
|
184 |
"]]></pre>")
|
|
|
185 |
));
|
| 143 |
jeje |
186 |
xmlRepresentation.setXmlElement("eg:cluster-provisioning");
|
| 135 |
jeje |
187 |
RepresentationInfo formRepresentation = new RepresentationInfo();
|
|
|
188 |
formRepresentation.setDocumentation("This representation exposes a provisioning request for updating an Elastic Grid Cluster.");
|
|
|
189 |
formRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
|
|
|
190 |
formRepresentation.setMediaType(MediaType.APPLICATION_WWW_FORM);
|
|
|
191 |
formRepresentation.setParameters(Arrays.asList(
|
| 138 |
jeje |
192 |
new ParameterInfo("clusterName", true, "xs:string", ParameterStyle.PLAIN,
|
|
|
193 |
"The name of the Elastic Grid Cluster to start."),
|
|
|
194 |
new ParameterInfo("numberOfMonitors", true, "xs:integer", ParameterStyle.PLAIN,
|
|
|
195 |
"The number of monitors to start in the cluster."),
|
| 337 |
jeje |
196 |
new ParameterInfo("numberOfMonitorsAndAgents", true, "xs:integer", ParameterStyle.PLAIN,
|
|
|
197 |
"The number of 'monitors and agents' to start in the cluster."),
|
| 138 |
jeje |
198 |
new ParameterInfo("numberOfAgents", true, "xs:integer", ParameterStyle.PLAIN,
|
|
|
199 |
"The number of agents to start in the cluster.")
|
| 135 |
jeje |
200 |
));
|
|
|
201 |
info.getRequest().setRepresentations(Arrays.asList(xmlRepresentation, formRepresentation));
|
| 127 |
jeje |
202 |
}
|
|
|
203 |
|
|
|
204 |
@Override
|
|
|
205 |
protected void describeDelete(MethodInfo info) {
|
|
|
206 |
super.describeDelete(info);
|
| 136 |
jeje |
207 |
info.setDocumentations(Arrays.asList(
|
|
|
208 |
new DocumentationInfo("Stop {clusterName} cluster."),
|
|
|
209 |
new DocumentationInfo("All nodes in the cluster will be shut down.")
|
|
|
210 |
));
|
| 127 |
jeje |
211 |
}
|
|
|
212 |
|
|
|
213 |
@Override
|
|
|
214 |
public boolean allowPut() {
|
|
|
215 |
return false;
|
|
|
216 |
}
|
| 131 |
jeje |
217 |
|
| 120 |
jeje |
218 |
}
|