diff --git a/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterAsyncClient.java b/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterAsyncClient.java index f189732787..0c549137eb 100644 --- a/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterAsyncClient.java +++ b/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterAsyncClient.java @@ -18,9 +18,22 @@ */ package org.jclouds.joyent.sdc.v6_5.features; +import java.net.URI; +import java.util.Map; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + import org.jclouds.http.filters.BasicAuthentication; +import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.rest.annotations.Headers; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.SkipEncoding; +import org.jclouds.rest.functions.ReturnEmptyMapOnNotFoundOr404; + +import com.google.common.util.concurrent.ListenableFuture; /** * Datacenter Services @@ -30,7 +43,16 @@ import org.jclouds.rest.annotations.SkipEncoding; * @see api doc */ @SkipEncoding( { '/', '=' }) +@Headers(keys="X-Api-Version", values="{jclouds.api-version}") @RequestFilters(BasicAuthentication.class) public interface DatacenterAsyncClient { - + + /** + * @see DatacenterClient#getDatacenters + */ + @GET + @Path("/my/datacenters") + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnEmptyMapOnNotFoundOr404.class) + ListenableFuture> getDatacenters(); } diff --git a/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClient.java b/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClient.java index e1735c4d1e..c0224194c7 100644 --- a/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClient.java +++ b/labs/joyent-sdc/src/main/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClient.java @@ -18,6 +18,8 @@ */ package org.jclouds.joyent.sdc.v6_5.features; +import java.net.URI; +import java.util.Map; import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; @@ -31,5 +33,11 @@ import org.jclouds.concurrent.Timeout; */ @Timeout(duration = 30, timeUnit = TimeUnit.SECONDS) public interface DatacenterClient { - + + /** + * Provides a list of all datacenters this cloud is aware of. + * + * @return keys are the datacenter name, and the value is the URL. + */ + Map getDatacenters(); } diff --git a/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientExpectTest.java b/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientExpectTest.java index 86928f54b0..fd4711e884 100644 --- a/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientExpectTest.java +++ b/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientExpectTest.java @@ -18,13 +18,53 @@ */ package org.jclouds.joyent.sdc.v6_5.features; +import static org.testng.Assert.assertEquals; + +import java.net.URI; + +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.testng.annotations.Test; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMultimap; + /** * @author Adrian Cole */ @Test(groups = "unit", testName = "DatacenterClientExpectTest") public class DatacenterClientExpectTest extends BaseSDCClientExpectTest { + HttpRequest getDatacenters = HttpRequest.builder() + .method("GET") + .endpoint(URI.create("https://api.joyentcloud.com/my/datacenters")) + .headers(ImmutableMultimap. builder() + .put("X-Api-Version", "~6.5") + .put("Accept", "application/json") + .put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==").build()) + .build(); + public void testGetDatacentersWhenResponseIs2xx() { + HttpResponse getDatacentersResponse = HttpResponse.builder() + .statusCode(200) + .payload(payloadFromResource("/datacenters.json")).build(); + + SDCClient clientWhenDatacentersExists = requestSendsResponse(getDatacenters, getDatacentersResponse); + + assertEquals(clientWhenDatacentersExists.getDatacenterClient().getDatacenters(), + ImmutableMap. builder() + .put("us-east-1", URI.create("https://us-east-1.api.joyentcloud.com")) + .put("us-west-1", URI.create("https://us-west-1.api.joyentcloud.com")) + .put("us-sw-1", URI.create("https://us-sw-1.api.joyentcloud.com")) + .put("eu-ams-1", URI.create("https://eu-ams-1.api.joyentcloud.com")).build()); + } + + public void testGetDatacentersWhenResponseIs404() { + HttpResponse getDatacentersResponse = HttpResponse.builder().statusCode(404).build(); + + SDCClient getDatacentersWhenNone = requestSendsResponse(getDatacenters, getDatacentersResponse); + + assertEquals(getDatacentersWhenNone.getDatacenterClient().getDatacenters(), ImmutableMap.of()); + } } diff --git a/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientLiveTest.java b/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientLiveTest.java index 24b9847966..7d14711b3b 100644 --- a/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientLiveTest.java +++ b/labs/joyent-sdc/src/test/java/org/jclouds/joyent/sdc/v6_5/features/DatacenterClientLiveTest.java @@ -18,6 +18,12 @@ */ package org.jclouds.joyent.sdc.v6_5.features; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.net.URI; +import java.util.Map; + import org.jclouds.joyent.sdc.v6_5.internal.BaseSDCClientLiveTest; import org.testng.annotations.Test; @@ -27,4 +33,9 @@ import org.testng.annotations.Test; @Test(groups = "live", testName = "DatacenterClientLiveTest") public class DatacenterClientLiveTest extends BaseSDCClientLiveTest { + public void testGetDatacenters(){ + Map dcs = sdcContext.getApi().getDatacenterClient().getDatacenters(); + assertNotNull(dcs); + assertTrue(dcs.size() > 0); + } } diff --git a/labs/joyent-sdc/src/test/resources/datacenters.json b/labs/joyent-sdc/src/test/resources/datacenters.json new file mode 100644 index 0000000000..7f4cdc3783 --- /dev/null +++ b/labs/joyent-sdc/src/test/resources/datacenters.json @@ -0,0 +1 @@ +{"us-east-1":"https://us-east-1.api.joyentcloud.com","us-west-1":"https://us-west-1.api.joyentcloud.com","us-sw-1":"https://us-sw-1.api.joyentcloud.com","eu-ams-1":"https://eu-ams-1.api.joyentcloud.com"} \ No newline at end of file