From 89db5661315da5825537f79b5e118e68ae41c714 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 16 Sep 2011 17:01:52 -0700 Subject: [PATCH] Issue 158: Added parent location --- .../compute/functions/DatacenterToLocation.java | 14 ++++++++++++++ .../functions/DatacenterToLocationTest.java | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/DatacenterToLocation.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/DatacenterToLocation.java index f0a08cf30c..e390c5a73b 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/DatacenterToLocation.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/DatacenterToLocation.java @@ -18,6 +18,11 @@ */ 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.LocationBuilder; import org.jclouds.domain.LocationScope; @@ -25,6 +30,7 @@ import org.jclouds.softlayer.domain.Address; import org.jclouds.softlayer.domain.Datacenter; import com.google.common.base.Function; +import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -32,7 +38,14 @@ import com.google.common.collect.ImmutableSet; * Converts an Datacenter into a Location. */ public class DatacenterToLocation implements Function { + private final Provider> provider; + // allow us to lazy discover the provider of a resource + @Inject + public DatacenterToLocation(Provider> provider) { + this.provider = checkNotNull(provider, "provider"); + } + @Override public Location apply(Datacenter datacenter) { return new LocationBuilder().scope(LocationScope.ZONE) @@ -40,6 +53,7 @@ public class DatacenterToLocation implements Function { .description(datacenter.getLongName()) .id(Long.toString(datacenter.getId())) .iso3166Codes(createIso3166Codes(datacenter.getLocationAddress())) + .parent(provider.get().get()) .build(); } diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/DatacenterToLocationTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/DatacenterToLocationTest.java index 56b1c55d9c..5c3e51b92c 100644 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/DatacenterToLocationTest.java +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/DatacenterToLocationTest.java @@ -24,6 +24,9 @@ import org.jclouds.softlayer.domain.Datacenter; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import com.google.common.base.Suppliers; +import com.google.inject.util.Providers; + import java.util.Set; import static org.testng.AssertJUnit.assertEquals; @@ -41,7 +44,7 @@ public class DatacenterToLocationTest { @BeforeMethod public void setup() { - function = new DatacenterToLocation(); + function = new DatacenterToLocation(Providers.of(Suppliers.ofInstance((Location) null))); } @Test @@ -52,6 +55,7 @@ public class DatacenterToLocationTest { .country("US") .state("TX") .description("This is Texas!").build()).build(); + Location location = function.apply(address); assertEquals(location.getId(), Long.toString(address.getId()));