added datacenter awareness to joyent-sdc

This commit is contained in:
Adrian Cole 2012-06-13 19:12:32 -04:00
parent ff2c2fb3e3
commit 0ec72c4476
16 changed files with 321 additions and 85 deletions

View File

@ -22,6 +22,7 @@ import java.net.URI;
import java.util.Properties;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.joyent.sdc.v6_5.config.DatacentersAreZonesModule;
import org.jclouds.joyent.sdc.v6_5.config.SDCRestClientModule;
import org.jclouds.rest.RestContext;
import org.jclouds.rest.internal.BaseRestApiMetadata;
@ -66,10 +67,15 @@ public class SDCApiMetadata extends BaseRestApiMetadata {
protected Builder() {
super(SDCClient.class, SDCAsyncClient.class);
id("joyent-sdc").name("Joyent SDC API").identityName("username").credentialName("password")
.documentation(URI.create("http://sdc.joyent.org/sdcapi.html")).version("~6.5")
.defaultEndpoint("https://api.joyentcloud.com").defaultProperties(SDCApiMetadata.defaultProperties())
.defaultModules(ImmutableSet.<Class<? extends Module>> of(SDCRestClientModule.class));
id("joyent-sdc")
.name("Joyent SDC API")
.identityName("username")
.credentialName("password")
.documentation(URI.create("http://sdc.joyent.org/sdcapi.html"))
.version("~6.5")
.defaultEndpoint("https://api.joyentcloud.com")
.defaultProperties(SDCApiMetadata.defaultProperties())
.defaultModules(ImmutableSet.<Class<? extends Module>> of(DatacentersAreZonesModule.class, SDCRestClientModule.class));
}
@Override

View File

@ -18,11 +18,19 @@
*/
package org.jclouds.joyent.sdc.v6_5;
import java.util.Set;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.joyent.sdc.v6_5.features.DatacenterAsyncClient;
import org.jclouds.joyent.sdc.v6_5.features.DatasetAsyncClient;
import org.jclouds.joyent.sdc.v6_5.features.MachineAsyncClient;
import org.jclouds.joyent.sdc.v6_5.features.PackageAsyncClient;
import org.jclouds.location.Zone;
import org.jclouds.location.functions.ZoneToEndpoint;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.annotations.EndpointParam;
import com.google.inject.Provides;
/**
* Provides asynchronous access to SDC via their REST API.
@ -33,7 +41,15 @@ import org.jclouds.rest.annotations.Delegate;
* @author Adrian Cole
*/
public interface SDCAsyncClient {
/**
*
* @return the datacenter codes configured
*/
@Provides
@Zone
Set<String> getConfiguredDatacenters();
/**
* Provides asynchronous access to Datacenter features.
*/
@ -44,17 +60,20 @@ public interface SDCAsyncClient {
* Provides asynchronous access to Machine features.
*/
@Delegate
MachineAsyncClient getMachineClient();
MachineAsyncClient getMachineClientForDatacenter(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String datacenter);
/**
* Provides asynchronous access to Dataset features.
*/
@Delegate
DatasetAsyncClient getDatasetClient();
DatasetAsyncClient getDatasetClientForDatacenter(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String datacenter);
/**
* Provides asynchronous access to Package features.
*/
@Delegate
PackageAsyncClient getPackageClient();
PackageAsyncClient getPackageClientForDatacenter(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String datacenter);
}

View File

@ -18,14 +18,21 @@
*/
package org.jclouds.joyent.sdc.v6_5;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.joyent.sdc.v6_5.features.DatacenterClient;
import org.jclouds.joyent.sdc.v6_5.features.DatasetClient;
import org.jclouds.joyent.sdc.v6_5.features.MachineClient;
import org.jclouds.joyent.sdc.v6_5.features.PackageClient;
import org.jclouds.location.Zone;
import org.jclouds.location.functions.ZoneToEndpoint;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.annotations.EndpointParam;
import com.google.inject.Provides;
/**
* Provides synchronous access to SDC.
@ -38,6 +45,14 @@ import org.jclouds.rest.annotations.Delegate;
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface SDCClient {
/**
*
* @return the datacenter codes configured
*/
@Provides
@Zone
Set<String> getConfiguredDatacenters();
/**
* Provides synchronous access to Datacenter features.
*/
@ -48,17 +63,20 @@ public interface SDCClient {
* Provides synchronous access to Machine features.
*/
@Delegate
MachineClient getMachineClient();
MachineClient getMachineClientForDatacenter(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String datacenter);
/**
* Provides synchronous access to Dataset features.
*/
@Delegate
DatasetClient getDatasetClient();
DatasetClient getDatasetClientForDatacenter(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String datacenter);
/**
* Provides synchronous access to Package features.
*/
@Delegate
PackageClient getPackageClient();
PackageClient getPackageClientForDatacenter(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String datacenter);
}

View File

@ -0,0 +1,49 @@
/**
* 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.config;
import static org.jclouds.rest.config.BinderUtils.bindClientAndAsyncClient;
import org.jclouds.joyent.sdc.v6_5.features.DatacenterAsyncClient;
import org.jclouds.joyent.sdc.v6_5.features.DatacenterClient;
import org.jclouds.joyent.sdc.v6_5.suppliers.ZoneIdToURIFromDatacentersClient;
import org.jclouds.location.suppliers.ImplicitLocationSupplier;
import org.jclouds.location.suppliers.ZoneIdToURISupplier;
import org.jclouds.location.suppliers.ZoneIdsSupplier;
import org.jclouds.location.suppliers.derived.ZoneIdsFromZoneIdToURIKeySet;
import org.jclouds.location.suppliers.implicit.OnlyLocationOrFirstZone;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
/**
*
* @author Adrian Cole
*/
public class DatacentersAreZonesModule extends AbstractModule {
@Override
protected void configure() {
// datacenter client is needed for obtaining zone ids
bindClientAndAsyncClient(binder(), DatacenterClient.class, DatacenterAsyncClient.class);
bind(ImplicitLocationSupplier.class).to(OnlyLocationOrFirstZone.class).in(Scopes.SINGLETON);
bind(ZoneIdToURISupplier.class).to(ZoneIdToURIFromDatacentersClient.class).in(Scopes.SINGLETON);
bind(ZoneIdsSupplier.class).to(ZoneIdsFromZoneIdToURIKeySet.class).in(Scopes.SINGLETON);
}
}

View File

@ -50,8 +50,10 @@ import com.google.common.collect.ImmutableMap;
@ConfiguresRestClient
public class SDCRestClientModule extends RestClientModule<SDCClient, SDCAsyncClient> {
public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()
.put(DatacenterClient.class, DatacenterAsyncClient.class).put(MachineClient.class, MachineAsyncClient.class)
.put(DatasetClient.class, DatasetAsyncClient.class).put(PackageClient.class, PackageAsyncClient.class).build();
.put(DatacenterClient.class, DatacenterAsyncClient.class)
.put(MachineClient.class, MachineAsyncClient.class)
.put(DatasetClient.class, DatasetAsyncClient.class)
.put(PackageClient.class, PackageAsyncClient.class).build();
public SDCRestClientModule() {
super(DELEGATE_MAP);
@ -70,4 +72,5 @@ public class SDCRestClientModule extends RestClientModule<SDCClient, SDCAsyncCli
bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(SDCErrorHandler.class);
bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(SDCErrorHandler.class);
}
}

View File

@ -0,0 +1,35 @@
package org.jclouds.joyent.sdc.v6_5.suppliers;
import java.net.URI;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.joyent.sdc.v6_5.features.DatacenterClient;
import org.jclouds.location.suppliers.ZoneIdToURISupplier;
import org.jclouds.util.Suppliers2;
import com.google.common.base.Supplier;
import com.google.common.collect.Maps;
@Singleton
public class ZoneIdToURIFromDatacentersClient implements ZoneIdToURISupplier {
private final DatacenterClient client;
@Inject
public ZoneIdToURIFromDatacentersClient(DatacenterClient client) {
this.client = client;
}
@Override
public Map<String, Supplier<URI>> get() {
return Maps.transformValues(client.getDatacenters(), Suppliers2.<URI> ofInstanceFunction());
}
@Override
public String toString() {
return "getDatacenters()";
}
}

View File

@ -0,0 +1,47 @@
/**
* 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
*
* Unles 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 expres or implied. See the License for the
* specific language governing permisions and limitations
* under the License.
*/
package org.jclouds.joyent.sdc.v6_5;
import static org.testng.Assert.assertEquals;
import org.jclouds.joyent.sdc.v6_5.internal.BaseSDCClientExpectTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "SDCClientExpectTest")
public class SDCClientExpectTest extends BaseSDCClientExpectTest {
public void testGetConfiguredDatacenters() {
SDCClient clientWhenDatacentersExists = requestSendsResponse(getDatacenters, getDatacentersResponse);
assertEquals(
clientWhenDatacentersExists.getConfiguredDatacenters(),
ImmutableSet.<String> builder()
.add("us-east-1")
.add("us-west-1")
.add("us-sw-1")
.add("eu-ams-1").build());
}
}

View File

@ -0,0 +1,38 @@
/**
* 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;
import static org.testng.Assert.assertEquals;
import java.util.Set;
import org.jclouds.joyent.sdc.v6_5.internal.BaseSDCClientLiveTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "live", testName = "SDCClientLiveTest")
public class SDCClientLiveTest extends BaseSDCClientLiveTest {
public void testGetDatacenters() {
Set<String> dcs = sdcContext.getApi().getConfiguredDatacenters();
assertEquals(dcs, sdcContext.getApi().getDatacenterClient().getDatacenters().keySet());
}
}

View File

@ -22,32 +22,20 @@ 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.<String, String> 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);

View File

@ -38,7 +38,7 @@ 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(
URI.create("https://us-sw-1.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();
@ -46,18 +46,18 @@ public class DatasetClientExpectTest extends BaseSDCClientExpectTest {
HttpResponse listDatasetsResponse = HttpResponse.builder().statusCode(200).payload(
payloadFromResource("/dataset_list.json")).build();
SDCClient clientWhenDatasetsExists = requestSendsResponse(listDatasets, listDatasetsResponse);
SDCClient clientWhenDatasetsExists = requestsSendResponses(getDatacenters, getDatacentersResponse, listDatasets, listDatasetsResponse);
assertEquals(clientWhenDatasetsExists.getDatasetClient().listDatasets().toString(), new ParseDatasetListTest()
assertEquals(clientWhenDatasetsExists.getDatasetClientForDatacenter("us-sw-1").listDatasets().toString(), new ParseDatasetListTest()
.expected().toString());
}
public void testListDatasetsWhenResponseIs404() {
HttpResponse listDatasetsResponse = HttpResponse.builder().statusCode(404).build();
SDCClient listDatasetsWhenNone = requestSendsResponse(listDatasets, listDatasetsResponse);
SDCClient listDatasetsWhenNone = requestsSendResponses(getDatacenters, getDatacentersResponse, listDatasets, listDatasetsResponse);
assertEquals(listDatasetsWhenNone.getDatasetClient().listDatasets(), ImmutableSet.of());
assertEquals(listDatasetsWhenNone.getDatasetClientForDatacenter("us-sw-1").listDatasets(), ImmutableSet.of());
}
// [id=e4cd7b9e-4330-11e1-81cf-3bb50a972bda, name=centos-6, type=VIRTUALMACHINE, version=1.0.1,

View File

@ -18,9 +18,7 @@
*/
package org.jclouds.joyent.sdc.v6_5.features;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.Assert.assertEquals;
import java.util.Set;
@ -34,16 +32,22 @@ import org.testng.annotations.Test;
@Test(groups = "live", testName = "DatasetClientLiveTest")
public class DatasetClientLiveTest extends BaseSDCClientLiveTest {
public void testListDatasets() {
Set<Dataset> datasets = sdcContext.getApi().getDatasetClient().listDatasets();
assertNotNull(datasets);
assertTrue(datasets.size() > 0);
}
public void testGetDataset() {
final String id = "e4cd7b9e-4330-11e1-81cf-3bb50a972bda";
Dataset dataset = sdcContext.getApi().getDatasetClient().getDataset(id);
assertNotNull(dataset);
assertEquals(dataset.getId(), id);
@Test
public void testListAndGetDatasets() throws Exception {
for (String datacenterId : sdcContext.getApi().getConfiguredDatacenters()) {
DatasetClient client = sdcContext.getApi().getDatasetClientForDatacenter(datacenterId);
Set<Dataset> response = client.listDatasets();
assert null != response;
for (Dataset dataset : response) {
Dataset newDetails = client.getDataset(dataset.getId());
assertEquals(newDetails.getId(), dataset.getId());
assertEquals(newDetails.getName(), dataset.getName());
assertEquals(newDetails.getType(), dataset.getType());
assertEquals(newDetails.getVersion(), dataset.getVersion());
assertEquals(newDetails.getUrn(), dataset.getUrn());
assertEquals(newDetails.getCreated(), dataset.getCreated());
assertEquals(newDetails.isDefault(), dataset.isDefault());
}
}
}
}

View File

@ -39,7 +39,7 @@ 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(
URI.create("https://us-sw-1.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();
@ -47,24 +47,24 @@ public class MachineClientExpectTest extends BaseSDCClientExpectTest {
HttpResponse listMachinesResponse = HttpResponse.builder().statusCode(200).payload(
payloadFromResource("/machine_list.json")).build();
SDCClient clientWhenMachinesExists = requestSendsResponse(listMachines, listMachinesResponse);
SDCClient clientWhenMachinesExists = requestsSendResponses(getDatacenters, getDatacentersResponse, listMachines, listMachinesResponse);
assertEquals(clientWhenMachinesExists.getMachineClient().listMachines(), new ParseMachineListTest().expected());
assertEquals(clientWhenMachinesExists.getMachineClientForDatacenter("us-sw-1").listMachines(), new ParseMachineListTest().expected());
}
public void testListMachinesWhenResponseIs404() {
HttpResponse listMachinesResponse = HttpResponse.builder().statusCode(404).build();
SDCClient listMachinesWhenNone = requestSendsResponse(listMachines, listMachinesResponse);
SDCClient listMachinesWhenNone = requestsSendResponses(getDatacenters, getDatacentersResponse, listMachines, listMachinesResponse);
assertEquals(listMachinesWhenNone.getMachineClient().listMachines(), ImmutableSet.of());
assertEquals(listMachinesWhenNone.getMachineClientForDatacenter("us-sw-1").listMachines(), ImmutableSet.of());
}
public void testCreateMachineWhenResponseIs202() throws Exception {
HttpRequest createMachine = HttpRequest
.builder()
.method("POST")
.endpoint(URI.create("https://api.joyentcloud.com/my/machines"))
.endpoint(URI.create("https://us-sw-1.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())
@ -77,9 +77,9 @@ public class MachineClientExpectTest extends BaseSDCClientExpectTest {
.payload(payloadFromResourceWithContentType("/new_machine.json", "application/json; charset=UTF-8"))
.build();
SDCClient clientWithNewMachine = requestSendsResponse(createMachine, createMachineResponse);
SDCClient clientWithNewMachine = requestsSendResponses(getDatacenters, getDatacentersResponse, createMachine, createMachineResponse);
assertEquals(clientWithNewMachine.getMachineClient().createMachine("testJClouds", "Small 1GB",
assertEquals(clientWithNewMachine.getMachineClientForDatacenter("us-sw-1").createMachine("testJClouds", "Small 1GB",
"sdc:sdc:centos-5.7:1.2.1").toString(), new ParseCreatedMachineTest().expected().toString());
}
}

View File

@ -19,8 +19,6 @@
package org.jclouds.joyent.sdc.v6_5.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.util.Set;
@ -34,16 +32,26 @@ import org.testng.annotations.Test;
@Test(groups = "live", testName = "MachineClientLiveTest")
public class MachineClientLiveTest extends BaseSDCClientLiveTest {
public void testListMachines() {
Set<Machine> machines = sdcContext.getApi().getMachineClient().listMachines();
assertNotNull(machines);
assertTrue(machines.size() > 0);
}
public void testGetMachine() {
final String id = "d73cb0b0-7d1f-44ef-8c40-e040eef0f726";
Machine machine = sdcContext.getApi().getMachineClient().getMachine(id);
assertNotNull(machine);
assertEquals(machine.getId(), id);
@Test
public void testListAndGetMachines() throws Exception {
for (String datacenterId : sdcContext.getApi().getConfiguredDatacenters()) {
MachineClient client = sdcContext.getApi().getMachineClientForDatacenter(datacenterId);
Set<Machine> response = client.listMachines();
assert null != response;
for (Machine machine : response) {
Machine newDetails = client.getMachine(machine.getId());
assertEquals(newDetails.getId(), machine.getId());
assertEquals(newDetails.getName(), machine.getName());
assertEquals(newDetails.getType(), machine.getType());
assertEquals(newDetails.getState(), machine.getState());
assertEquals(newDetails.getDataset(), machine.getDataset());
assertEquals(newDetails.getMemorySizeMb(), machine.getMemorySizeMb());
assertEquals(newDetails.getDiskSizeGb(), machine.getDiskSizeGb());
assertEquals(newDetails.getIps(), machine.getIps());
assertEquals(newDetails.getCreated(), machine.getCreated());
assertEquals(newDetails.getUpdated(), machine.getUpdated());
assertEquals(newDetails.getMetadata(), machine.getMetadata());
}
}
}
}

View File

@ -40,7 +40,7 @@ public class PackageClientExpectTest extends BaseSDCClientExpectTest {
HttpRequest listPackages = HttpRequest
.builder()
.method("GET")
.endpoint(URI.create("https://api.joyentcloud.com/my/packages"))
.endpoint(URI.create("https://us-sw-1.api.joyentcloud.com/my/packages"))
.headers(
ImmutableMultimap.<String, String> builder().put("X-Api-Version", "~6.5")
.put("Accept", "application/json").put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==")
@ -50,16 +50,16 @@ public class PackageClientExpectTest extends BaseSDCClientExpectTest {
HttpResponse listPackagesResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/package_list.json")).build();
SDCClient clientWhenPackagesExists = requestSendsResponse(listPackages, listPackagesResponse);
SDCClient clientWhenPackagesExists = requestsSendResponses(getDatacenters, getDatacentersResponse, listPackages, listPackagesResponse);
assertEquals(clientWhenPackagesExists.getPackageClient().listPackages(), new ParsePackageListTest().expected());
assertEquals(clientWhenPackagesExists.getPackageClientForDatacenter("us-sw-1").listPackages(), new ParsePackageListTest().expected());
}
public void testListPackagesWhenResponseIs404() {
HttpResponse listPackagesResponse = HttpResponse.builder().statusCode(404).build();
SDCClient listPackagesWhenNone = requestSendsResponse(listPackages, listPackagesResponse);
SDCClient listPackagesWhenNone = requestsSendResponses(getDatacenters, getDatacentersResponse, listPackages, listPackagesResponse);
assertEquals(listPackagesWhenNone.getPackageClient().listPackages(), ImmutableSet.of());
assertEquals(listPackagesWhenNone.getPackageClientForDatacenter("us-sw-1").listPackages(), ImmutableSet.of());
}
}

View File

@ -18,12 +18,11 @@
*/
package org.jclouds.joyent.sdc.v6_5.features;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.Assert.assertEquals;
import java.util.Set;
import org.jclouds.joyent.sdc.v6_5.domain.Package;
import org.jclouds.joyent.sdc.v6_5.internal.BaseSDCClientLiveTest;
import org.testng.annotations.Test;
@ -33,16 +32,20 @@ import org.testng.annotations.Test;
@Test(groups = "live", testName = "PackageClientLiveTest")
public class PackageClientLiveTest extends BaseSDCClientLiveTest {
public void testListPackages() {
Set<org.jclouds.joyent.sdc.v6_5.domain.Package> packages = sdcContext.getApi().getPackageClient().listPackages();
assertNotNull(packages);
assertTrue(packages.size() > 0);
}
public void testGetPackage() {
final String name = "Small 1GB";
org.jclouds.joyent.sdc.v6_5.domain.Package packageSDC = sdcContext.getApi().getPackageClient().getPackage(name);
assertNotNull(packageSDC);
assertEquals(packageSDC.getName(), name);
@Test
public void testListAndGetPackages() throws Exception {
for (String datacenterId : sdcContext.getApi().getConfiguredDatacenters()) {
PackageClient client = sdcContext.getApi().getPackageClientForDatacenter(datacenterId);
Set<Package> response = client.listPackages();
assert null != response;
for (Package pkg : response) {
Package newDetails = client.getPackage(pkg.getName());
assertEquals(newDetails.getName(), pkg.getName());
assertEquals(newDetails.getMemorySizeMb(), pkg.getMemorySizeMb());
assertEquals(newDetails.getDiskSizeGb(), pkg.getDiskSizeGb());
assertEquals(newDetails.getSwapSizeMb(), pkg.getSwapSizeMb());
assertEquals(newDetails.isDefault(), pkg.isDefault());
}
}
}
}

View File

@ -18,13 +18,31 @@
*/
package org.jclouds.joyent.sdc.v6_5.internal;
import java.net.URI;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.joyent.sdc.v6_5.SDCClient;
import com.google.common.collect.ImmutableMultimap;
/**
* Base class for writing KeyStone Rest Client Expect tests
*
* @author Adrian Cole
*/
public class BaseSDCClientExpectTest extends BaseSDCExpectTest<SDCClient> {
protected HttpRequest getDatacenters = HttpRequest
.builder()
.method("GET")
.endpoint(URI.create("https://api.joyentcloud.com/my/datacenters"))
.headers(
ImmutableMultimap.<String, String> builder()
.put("X-Api-Version", "~6.5")
.put("Accept", "application/json")
.put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==")
.build()).build();
protected HttpResponse getDatacentersResponse = HttpResponse.builder().statusCode(200)
.payload(payloadFromResource("/datacenters.json")).build();
}