}
@POST
@Produces({ JSON })
public Response launchServer(WrappedServer request) throws CloudException {
StartInstancesAction action = startInstancesActionProvider.get();
// action.user = getUser();
action.project = getProject();
action.auth = getAuth();
FlavorData flavor;
{
long flavorId = OpenstackIds.toFlavorId(request.server.flavorRef);
flavor = flavors.find(flavorId);
if (flavor == null) {
throw new IllegalArgumentException();
}
}
action.minCount = request.server.minCount;
if (action.minCount == 0) {
action.minCount = 1;
}
action.maxCount = request.server.maxCount;
if (action.maxCount == 0) {
action.maxCount = 1;
}
if (action.maxCount != 1) {
// Not clear what the response should be in this case...
throw new UnsupportedOperationException();
}
ImageService.Image image;
{
long imageId = OpenstackIds.toImageId(request.server.imageRef);
image = imageService.findImage(getProject(), imageId);
if (image == null) {
throw new IllegalArgumentException();
}
}
{
ReservationData.Builder reservation = ReservationData.newBuilder();
// TODO: Copy image?
reservation.setImageId(image.getId());
action.reservationTemplate = reservation.build();
}
{
InstanceData.Builder instance = InstanceData.newBuilder();
instance.setName(request.server.name);
if (request.server.keyName != null) {
KeyPairData keypair = keypairs.findKeyPair(getProject(), request.server.keyName);
if (keypair == null) {
throw new IllegalArgumentException();
}
instance.setKeyPair(keypair);
}
instance.setImageId(image.getId());
instance.setFlavor(flavor);
if (request.server.securityGroups != null && !request.server.securityGroups.isEmpty()) {
SecurityGroupDictionary dictionary = new SecurityGroupDictionary(securityGroups.list(getProject()));
for (SecurityGroup securityGroup : request.server.securityGroups) {
String name = securityGroup.name;
SecurityGroupData data = dictionary.getByName(name);
if (data == null) {
throw new IllegalArgumentException("Security group not found: " + name);
}
instance.addSecurityGroupId(data.getId());
}
}
action.instanceTemplate = instance.build();
}
StartInstancesAction.Result result = action.go();
WrappedServer response = new WrappedServer();
if (result.instances.size() != 1) {
throw new IllegalStateException();