This commit is contained in:
unknown 2012-05-16 16:36:53 +02:00 committed by Adrian Cole
parent fed9c22a23
commit c4499d8c5e
8 changed files with 405 additions and 23 deletions

View File

@ -21,15 +21,22 @@ package org.jclouds.joyent.sdc.v6_5.features;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.joyent.sdc.v6_5.domain.Machine;
import org.jclouds.joyent.sdc.v6_5.options.CreateServerOptions;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.Headers;
import org.jclouds.rest.annotations.MapBinder;
import org.jclouds.rest.annotations.Payload;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
@ -68,4 +75,65 @@ public interface MachineAsyncClient {
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<Machine> getMachine(@PathParam("id") String id);
/**
* @see MachineClient#createMachine
*/
@POST
@Path("/my/machines")
@Consumes(MediaType.APPLICATION_JSON)
@MapBinder(CreateServerOptions.class)
ListenableFuture<Machine> createMachine(@PayloadParam("name") String name,
@PayloadParam("package") String packageSDC,
@PayloadParam("dataset") String dataset,CreateServerOptions... options);
/**
* @see MachineClient#stopMachine
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/my/machines/{id}")
@Payload("action=stop")
ListenableFuture<Void> stopMachine(@PathParam("id") String id);
/**
* @see MachineClient#startMachine
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/my/machines/{id}")
@Payload("action=start")
ListenableFuture<Void> startMachine(@PathParam("id") String id);
/**
* @see MachineClient#rebootMachine
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/my/machines/{id}")
@Payload("action=reboot")
ListenableFuture<Void> rebootMachine(@PathParam("id") String id);
/**
* @see MachineClient#resizeMachine
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/my/machines/{id}")
@Payload("action=resize&package={package}")
ListenableFuture<Void> resizeMachine(@PathParam("id") String id,@PayloadParam("package") String packageSDC);
/**
* @see MachineClient#deleteMachine
*/
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Path("/my/machines/{id}")
ListenableFuture<Void> deleteMachine(@PathParam("id") String id);
}

View File

@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.joyent.sdc.v6_5.domain.Machine;
import org.jclouds.joyent.sdc.v6_5.options.CreateServerOptions;
/**
* Provides synchronous access to Machine.
@ -38,7 +39,7 @@ public interface MachineClient {
/**
* Lists all machines we have on record for your account.
*
* @return an account's associated machine objects.
* @return an account's associated server objects.
*/
Set<Machine> listMachines();
@ -51,4 +52,60 @@ public interface MachineClient {
*/
Machine getMachine(String id);
/**
*
* @param name
* friendly name for this machine; default is a randomly generated name
* @param packageSDC
* Name of the package to use on provisioning; default is indicated in ListPackages
* @param dataset
* dataset URN; default is indicated in ListDatasets
* @param options
* optional parameters to be passed into the machine creation request
* @return the newly created machine
*/
Machine createMachine(String name, String packageSDC, String dataset, CreateServerOptions... options);
/**
* Allows you to shut down a machine.
*
* @param id
* the id of the machine to stop
*/
void stopMachine(String id);
/**
* Allows you to boot up a machine.
*
* @param id
* the id of the machine to start
*/
void startMachine(String id);
/**
* Allows you to reboot a machine.
*
* @param id
* the id of the machine to reboot
*/
void rebootMachine(String id);
/**
* Allows you to resize a machine. (Works only for smart machines)
*
* @param id
* the id of the machine to resize
* @param packageSDC
* the package to use for the machine
*/
void resizeMachine(String id, String packageSDC);
/**
* Allows you to delete a machine (the machine must be stopped before it can be deleted).
*
* @param id
* the id of the machine to delete
*/
void deleteMachine(String id);
}

View File

@ -0,0 +1,151 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.joyent.sdc.v6_5.options;
import static com.google.common.base.Objects.equal;
import static com.google.common.base.Objects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
import javax.inject.Inject;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.SerializedName;
/**
*
* @author Gerald Pereira
*
*/
public class CreateServerOptions implements MapBinder {
@Inject
private BindToJsonPayload jsonBinder;
private Map<String, String> metadata = ImmutableMap.of();
private Map<String, String> tag = ImmutableMap.of();
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object instanceof CreateServerOptions) {
final CreateServerOptions other = CreateServerOptions.class.cast(object);
return equal(tag, tag) && equal(metadata, other.metadata);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hashCode(tag, metadata);
}
protected ToStringHelper string() {
return toStringHelper("").add("metadata", metadata).add("tag", tag);
}
@Override
public String toString() {
return string().toString();
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
return jsonBinder.bindToRequest(request, input);
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
MachineRequest machine = new MachineRequest(checkNotNull(postParams.get("name"), "name parameter not present")
.toString(), checkNotNull(postParams.get("package"), "package parameter not present").toString(),
checkNotNull(postParams.get("dataset"), "dataset parameter not present").toString());
if (metadata.size() > 0)
machine.metadata = metadata;
if (tag.size() > 0)
machine.tag = tag;
return bindToRequest(request, machine);
}
/**
* An arbitrary set of metadata key/value pairs can be set at provision time, but they must be
* prefixed with "metadata."
*/
public CreateServerOptions metadata(Map<String, String> metadata) {
checkNotNull(metadata, "metadata");
this.metadata = ImmutableMap.copyOf(metadata);
return this;
}
/**
* An arbitrary set of tags can be set at provision time, but they must be prefixed with "tag."
*/
public CreateServerOptions tag(Map<String, String> tag) {
checkNotNull(tag, "tag");
this.tag = ImmutableMap.copyOf(tag);
return this;
}
@SuppressWarnings("unused")
private class MachineRequest {
final String name;
@SerializedName("package")
final String packageSDC;
final String dataset;
Map<String, String> metadata;
Map<String, String> tag;
private MachineRequest(String name, String packageSDC, String dataset) {
this.name = name;
this.packageSDC = packageSDC;
this.dataset = dataset;
}
}
public static class Builder {
/**
* @see CreateServerOptions#metadata(Map<String, String>)
*/
public static CreateServerOptions metadata(Map<String, String> metadata) {
CreateServerOptions options = new CreateServerOptions();
return options.metadata(metadata);
}
/**
* @see CreateServerOptions#tag(Map<String, String>)
*/
public static CreateServerOptions tag(Map<String, String> tag) {
CreateServerOptions options = new CreateServerOptions();
return options.tag(tag);
}
}
}

View File

@ -37,23 +37,19 @@ import com.google.common.collect.ImmutableSet;
*/
@Test(groups = "unit", testName = "DatasetClientExpectTest")
public class DatasetClientExpectTest extends BaseSDCClientExpectTest {
HttpRequest listDatasets = HttpRequest
.builder()
.method("GET")
.endpoint(URI.create("https://api.joyentcloud.com/my/datasets"))
.headers(
ImmutableMultimap.<String, String> builder().put("X-Api-Version", "~6.5")
.put("Accept", "application/json").put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==")
.build()).build();
HttpRequest listDatasets = HttpRequest.builder().method("GET").endpoint(
URI.create("https://api.joyentcloud.com/my/datasets")).headers(
ImmutableMultimap.<String, String> builder().put("X-Api-Version", "~6.5").put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build();
public void testListDatasetsWhenResponseIs2xx() {
HttpResponse listDatasetsResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/dataset_list.json")).build();
HttpResponse listDatasetsResponse = HttpResponse.builder().statusCode(200).payload(
payloadFromResource("/dataset_list.json")).build();
SDCClient clientWhenDatasetsExists = requestSendsResponse(listDatasets, listDatasetsResponse);
assertEquals(clientWhenDatasetsExists.getDatasetClient().listDatasets().toString(), new ParseDatasetListTest()
.expected().toString());
.expected().toString());
}
public void testListDatasetsWhenResponseIs404() {
@ -63,4 +59,15 @@ public class DatasetClientExpectTest extends BaseSDCClientExpectTest {
assertEquals(listDatasetsWhenNone.getDatasetClient().listDatasets(), ImmutableSet.of());
}
// [id=e4cd7b9e-4330-11e1-81cf-3bb50a972bda, name=centos-6, type=VIRTUALMACHINE, version=1.0.1,
// urn=sdc:sdc:centos-6:1.0.1, default=false, created=Mon Feb 13 07:30:33 CET 2012],
// [id=e4cd7b9e-4330-11e1-81cf-3bb50a972bda, name=centos-6, type=VIRTUALMACHINE, version=1.0.1,
// urn=sdc:sdc:centos-6:1.0.1, default=false, created=Mon Feb 13 07:30:33 CET 2012],
//
// [id=e62c30b4-cdda-11e0-9dd4-af4d032032e3, name=nodejs, type=SMARTMACHINE, version=1.2.3,
// urn=sdc:sdc:nodejs:1.2.3, default=false, created=Thu Sep 15 10:15:29 CEST 2011]]
//
// [id=e62c30b4-cdda-11e0-9dd4-af4d032032e3, name=nodejs, type=SMARTMACHINE, version=1.2.3,
// urn=sdc:sdc:nodejs:1.2.3, default=false, created=Thu Sep 15 10:15:29 CEST 2011]] but got
}

View File

@ -26,6 +26,7 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.joyent.sdc.v6_5.SDCClient;
import org.jclouds.joyent.sdc.v6_5.internal.BaseSDCClientExpectTest;
import org.jclouds.joyent.sdc.v6_5.parse.ParseCreatedMachineTest;
import org.jclouds.joyent.sdc.v6_5.parse.ParseMachineListTest;
import org.testng.annotations.Test;
@ -37,18 +38,14 @@ import com.google.common.collect.ImmutableSet;
*/
@Test(groups = "unit", testName = "MachineClientExpectTest")
public class MachineClientExpectTest extends BaseSDCClientExpectTest {
HttpRequest listMachines = HttpRequest
.builder()
.method("GET")
.endpoint(URI.create("https://api.joyentcloud.com/my/machines"))
.headers(
ImmutableMultimap.<String, String> builder().put("X-Api-Version", "~6.5")
.put("Accept", "application/json").put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==")
.build()).build();
HttpRequest listMachines = HttpRequest.builder().method("GET").endpoint(
URI.create("https://api.joyentcloud.com/my/machines")).headers(
ImmutableMultimap.<String, String> builder().put("X-Api-Version", "~6.5").put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()).build();
public void testListMachinesWhenResponseIs2xx() {
HttpResponse listMachinesResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/machine_list.json")).build();
HttpResponse listMachinesResponse = HttpResponse.builder().statusCode(200).payload(
payloadFromResource("/machine_list.json")).build();
SDCClient clientWhenMachinesExists = requestSendsResponse(listMachines, listMachinesResponse);
@ -62,4 +59,27 @@ public class MachineClientExpectTest extends BaseSDCClientExpectTest {
assertEquals(listMachinesWhenNone.getMachineClient().listMachines(), ImmutableSet.of());
}
public void testCreateMachineWhenResponseIs202() throws Exception {
HttpRequest createMachine = HttpRequest
.builder()
.method("POST")
.endpoint(URI.create("https://api.joyentcloud.com/my/machines"))
.headers(
ImmutableMultimap.<String, String> builder().put("X-Api-Version", "~6.5").put("Accept",
"application/json").put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build())
.payload(
payloadFromStringWithContentType(
"{\"name\":\"testJClouds\",\"package\":\"Small 1GB\",\"dataset\":\"sdc:sdc:centos-5.7:1.2.1\"}",
"application/json")).build();
HttpResponse createMachineResponse = HttpResponse.builder().statusCode(202).message("HTTP/1.1 202 Accepted")
.payload(payloadFromResourceWithContentType("/new_machine.json", "application/json; charset=UTF-8"))
.build();
SDCClient clientWithNewMachine = requestSendsResponse(createMachine, createMachineResponse);
assertEquals(clientWithNewMachine.getMachineClient().createMachine("testJClouds", "Small 1GB",
"sdc:sdc:centos-5.7:1.2.1").toString(), new ParseCreatedMachineTest().expected().toString());
}
}

View File

@ -0,0 +1,78 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.joyent.sdc.v6_5.parse;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.joyent.sdc.v6_5.config.SDCParserModule;
import org.jclouds.joyent.sdc.v6_5.domain.Machine;
import org.jclouds.joyent.sdc.v6_5.domain.Type;
import org.jclouds.json.BaseItemParserTest;
import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Gerald Pereira
*/
@Test(groups = "unit", testName = "ParseServerTest")
public class ParseCreatedMachineTest extends BaseItemParserTest<Machine> {
@Override
public String resource() {
return "/new_machine.json";
}
@Override
@Consumes(MediaType.APPLICATION_JSON)
public Machine expected() {
return Machine
.builder()
.id("94eba336-ecb7-49f5-8a27-52f5e4dd57a1")
.name("testJClouds")
.type(Type.VIRTUALMACHINE)
.state(Machine.State.STOPPED)
.dataset("sdc:sdc:centos-5.7:1.2.1")
.ips(ImmutableSet. <String>builder().add("37.153.96.62").add("10.224.0.63").build())
.memorySizeMb(1024)
.diskSizeGb(61440)
.metadata(ImmutableMap. <String,String>builder().put("root_authorized_keys","ssh-rsa XXXXXX== test@xxxx.ovh.net\n").build())
.created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2012-05-09T13:32:46+00:00"))
.updated(new SimpleDateFormatDateService().iso8601SecondsDateParse("2012-05-11T08:44:53+00:00"))
.build();
}
protected Injector injector() {
return Guice.createInjector(new SDCParserModule(), new GsonModule() {
@Override
protected void configure() {
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
super.configure();
}
});
}
}

View File

@ -1 +1 @@
{"id":"94eba336-ecb7-49f5-8a27-52f5e4dd57a1","name":"testJClouds","type":"virtualmachine","state":"stopped","dataset":"sdc:sdc:centos-5.7:1.2.1","ips":["37.153.96.62","10.224.0.63"],"memory":1024,"disk":61440,"metadata":{"root_authorized_keys":"ssh-rsa XXXXXX== test@xxxx.ovh.net"},"created":"2012-05-09T13:32:46+00:00","updated":"2012-05-11T08:44:53+00:00"}
{"id":"94eba336-ecb7-49f5-8a27-52f5e4dd57a1","name":"testJClouds","type":"virtualmachine","state":"stopped","dataset":"sdc:sdc:centos-5.7:1.2.1","ips":["37.153.96.62","10.224.0.63"],"memory":1024,"disk":61440,"metadata":{"root_authorized_keys":"ssh-rsa XXXXXX== test@xxxx.ovh.net"},"created":"2012-05-09T13:32:46+00:00","updated":"2012-05-11T08:44:53+00:00"}

View File

@ -0,0 +1 @@
{"id":"94eba336-ecb7-49f5-8a27-52f5e4dd57a1","name":"testJClouds","type":"virtualmachine","state":"stopped","dataset":"sdc:sdc:centos-5.7:1.2.1","ips":["37.153.96.62","10.224.0.63"],"memory":1024,"disk":61440,"metadata":{"root_authorized_keys":"ssh-rsa XXXXXX== test@xxxx.ovh.net"},"created":"2012-05-09T13:32:46+00:00","updated":"2012-05-11T08:44:53+00:00"}