Issue 158: Added parent location

This commit is contained in:
Adrian Cole 2011-09-16 17:01:52 -07:00
parent 626b45ee46
commit 89db566131
2 changed files with 19 additions and 1 deletions

View File

@ -18,6 +18,11 @@
*/ */
package org.jclouds.softlayer.compute.functions; package org.jclouds.softlayer.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Inject;
import javax.inject.Provider;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope; import org.jclouds.domain.LocationScope;
@ -25,6 +30,7 @@ import org.jclouds.softlayer.domain.Address;
import org.jclouds.softlayer.domain.Datacenter; import org.jclouds.softlayer.domain.Datacenter;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -32,6 +38,13 @@ import com.google.common.collect.ImmutableSet;
* Converts an Datacenter into a Location. * Converts an Datacenter into a Location.
*/ */
public class DatacenterToLocation implements Function<Datacenter,Location> { public class DatacenterToLocation implements Function<Datacenter,Location> {
private final Provider<Supplier<Location>> provider;
// allow us to lazy discover the provider of a resource
@Inject
public DatacenterToLocation(Provider<Supplier<Location>> provider) {
this.provider = checkNotNull(provider, "provider");
}
@Override @Override
public Location apply(Datacenter datacenter) { public Location apply(Datacenter datacenter) {
@ -40,6 +53,7 @@ public class DatacenterToLocation implements Function<Datacenter,Location> {
.description(datacenter.getLongName()) .description(datacenter.getLongName())
.id(Long.toString(datacenter.getId())) .id(Long.toString(datacenter.getId()))
.iso3166Codes(createIso3166Codes(datacenter.getLocationAddress())) .iso3166Codes(createIso3166Codes(datacenter.getLocationAddress()))
.parent(provider.get().get())
.build(); .build();
} }

View File

@ -24,6 +24,9 @@ import org.jclouds.softlayer.domain.Datacenter;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Suppliers;
import com.google.inject.util.Providers;
import java.util.Set; import java.util.Set;
import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertEquals;
@ -41,7 +44,7 @@ public class DatacenterToLocationTest {
@BeforeMethod @BeforeMethod
public void setup() { public void setup() {
function = new DatacenterToLocation(); function = new DatacenterToLocation(Providers.of(Suppliers.ofInstance((Location) null)));
} }
@Test @Test
@ -52,6 +55,7 @@ public class DatacenterToLocationTest {
.country("US") .country("US")
.state("TX") .state("TX")
.description("This is Texas!").build()).build(); .description("This is Texas!").build()).build();
Location location = function.apply(address); Location location = function.apply(address);
assertEquals(location.getId(), Long.toString(address.getId())); assertEquals(location.getId(), Long.toString(address.getId()));