diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/config/SoftLayerComputeServiceContextModule.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/config/SoftLayerComputeServiceContextModule.java index 0bc9dfd027..29a7d88906 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/config/SoftLayerComputeServiceContextModule.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/config/SoftLayerComputeServiceContextModule.java @@ -59,6 +59,7 @@ public class SoftLayerComputeServiceContextModule extends @Override protected void configure() { super.configure(); + install(new SoftLayerParserModule()); bind(new TypeLiteral, ProductItem, Datacenter>>() {}) .to(SoftLayerComputeServiceAdapter.class); bind(new TypeLiteral>() {}) diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/config/SoftLayerParserModule.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/config/SoftLayerParserModule.java new file mode 100644 index 0000000000..b2fde38269 --- /dev/null +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/config/SoftLayerParserModule.java @@ -0,0 +1,114 @@ +/** + * 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.softlayer.compute.config; + +import java.lang.reflect.Type; +import java.util.Date; +import java.util.Map; + +import javax.inject.Singleton; + +import org.jclouds.softlayer.domain.Datacenter; +import org.jclouds.softlayer.domain.OperatingSystem; +import org.jclouds.softlayer.domain.PowerState; +import org.jclouds.softlayer.domain.VirtualGuest; + +import com.google.common.collect.ImmutableMap; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.inject.AbstractModule; +import com.google.inject.TypeLiteral; + +/** + * + * @author Adrian Cole + */ +public class SoftLayerParserModule extends AbstractModule { + + @Singleton + public static class VirtualGuestAdapter implements JsonSerializer, JsonDeserializer { + + public JsonElement serialize(VirtualGuest src, Type typeOfSrc, JsonSerializationContext context) { + return context.serialize(src); + } + + public VirtualGuest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + return apply(context. deserialize(json, VirtualGuestInternal.class)); + } + + public VirtualGuest apply(VirtualGuestInternal in) { + return in; + } + + /** + * Internal class that flattens billingItem into billingItemId + */ + public static class VirtualGuestInternal extends VirtualGuest { + private BillingItem billingItem; + + public VirtualGuestInternal(int accountId, Date createDate, boolean dedicatedAccountHostOnly, String domain, + String fullyQualifiedDomainName, String hostname, int id, Date lastVerifiedDate, int maxCpu, + String maxCpuUnits, int maxMemory, Date metricPollDate, Date modifyDate, String notes, + boolean privateNetworkOnly, int startCpus, int statusId, String uuid, String primaryBackendIpAddress, + String primaryIpAddress, int billingItemId, OperatingSystem operatingSystem, Datacenter datacenter, + PowerState powerState) { + super(accountId, createDate, dedicatedAccountHostOnly, domain, fullyQualifiedDomainName, hostname, id, + lastVerifiedDate, maxCpu, maxCpuUnits, maxMemory, metricPollDate, modifyDate, notes, + privateNetworkOnly, startCpus, statusId, uuid, primaryBackendIpAddress, primaryIpAddress, + billingItemId, operatingSystem, datacenter, powerState); + } + + @Override + public int getBillingItemId() { + return billingItem != null ? billingItem.id : -1; + } + } + + public static class BillingItem { + + private int id = -1; + + // for deserializer + BillingItem() { + + } + + public BillingItem(int id) { + this.id = id; + } + + @Override + public String toString() { + return "[id=" + id + "]"; + } + } + } + + @Override + protected void configure() { + bind(new TypeLiteral>() { + }).toInstance(ImmutableMap. of(VirtualGuest.class, new VirtualGuestAdapter())); + } + +} diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java index 4923d161d6..2b56e9fff3 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadata.java @@ -29,8 +29,6 @@ import javax.inject.Singleton; import org.jclouds.collect.FindResourceInSet; import org.jclouds.collect.Memoized; -import org.jclouds.compute.domain.Hardware; -import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadataBuilder; import org.jclouds.compute.domain.NodeState; @@ -51,25 +49,18 @@ import com.google.common.collect.ImmutableSet; public class VirtualGuestToNodeMetadata implements Function { public static final Map serverStateToNodeState = ImmutableMap - . builder() - .put(VirtualGuest.State.HALTED, NodeState.PENDING) - .put(VirtualGuest.State.PAUSED, NodeState.SUSPENDED) - .put(VirtualGuest.State.RUNNING, NodeState.RUNNING) - .put(VirtualGuest.State.UNRECOGNIZED, NodeState.UNRECOGNIZED) - .build(); + . builder().put(VirtualGuest.State.HALTED, NodeState.PENDING).put( + VirtualGuest.State.PAUSED, NodeState.SUSPENDED).put(VirtualGuest.State.RUNNING, NodeState.RUNNING) + .put(VirtualGuest.State.UNRECOGNIZED, NodeState.UNRECOGNIZED).build(); - private final FindHardwareForVirtualGuest findHardwareForVirtualGuest; - private final FindLocationForVirtualGuest findLocationForVirtualGuest; - private final FindImageForVirtualGuest findImageForVirtualGuest; private final Map credentialStore; + private final FindLocationForVirtualGuest findLocationForVirtualGuest; @Inject - VirtualGuestToNodeMetadata(Map credentialStore, FindHardwareForVirtualGuest findHardwareForVirtualGuest, - FindLocationForVirtualGuest findLocationForVirtualGuest, FindImageForVirtualGuest findImageForVirtualGuest) { + VirtualGuestToNodeMetadata(Map credentialStore, + FindLocationForVirtualGuest findLocationForVirtualGuest) { this.credentialStore = checkNotNull(credentialStore, "credentialStore"); - this.findHardwareForVirtualGuest = checkNotNull(findHardwareForVirtualGuest, "findHardwareForVirtualGuest"); this.findLocationForVirtualGuest = checkNotNull(findLocationForVirtualGuest, "findLocationForVirtualGuest"); - this.findImageForVirtualGuest = checkNotNull(findImageForVirtualGuest, "findImageForVirtualGuest"); } @Override @@ -77,67 +68,40 @@ public class VirtualGuestToNodeMetadata implements Function of(from.getPrimaryIpAddress())); - if (from.getPrimaryBackendIpAddress()!=null)builder.privateAddresses(ImmutableSet. of(from.getPrimaryBackendIpAddress())); + if (from.getPrimaryIpAddress() != null) + builder.publicAddresses(ImmutableSet. of(from.getPrimaryIpAddress())); + if (from.getPrimaryBackendIpAddress() != null) + builder.privateAddresses(ImmutableSet. of(from.getPrimaryBackendIpAddress())); - builder.credentials(credentialStore.get("node#"+ from.getId())); + builder.credentials(credentialStore.get("node#" + from.getId())); return builder.build(); } - @Singleton - public static class FindHardwareForVirtualGuest extends FindResourceInSet { - - @Inject - public FindHardwareForVirtualGuest(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(VirtualGuest from, Hardware input) { - //return input.getProviderId().equals(from.getId() + ""); - return false; - } - } - - @Singleton - public static class FindImageForVirtualGuest extends FindResourceInSet { - - @Inject - public FindImageForVirtualGuest(@Memoized Supplier> hardware) { - super(hardware); - } - - @Override - public boolean matches(VirtualGuest from, Image input) { - // TODO determine the price list from the virtual guest which would have the image in it. - return false; - } - } - @Singleton public static class FindLocationForVirtualGuest extends FindResourceInSet { @Inject - public FindLocationForVirtualGuest(@Memoized Supplier> hardware) { - super(hardware); + public FindLocationForVirtualGuest(@Memoized Supplier> location) { + super(location); } @Override public boolean matches(VirtualGuest from, Location input) { Datacenter dc = from.getDatacenter(); - if (dc == null) return false; + if (dc == null) + return false; return input.getId().equals(Integer.toString(dc.getId())); } } diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java index 860663a08d..edcb6ddd08 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/compute/strategy/SoftLayerComputeServiceAdapter.java @@ -196,11 +196,10 @@ public class SoftLayerComputeServiceAdapter implements if (guest == null) return; - BillingItemVirtualGuest billingItem = guest.getBillingItem(); - if (billingItem == null) + if (guest.getBillingItemId() == -1) return; - client.getVirtualGuestClient().cancelService(billingItem.getId()); + client.getVirtualGuestClient().cancelService(guest.getBillingItemId()); } @Override diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/BillingItemVirtualGuest.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/BillingItemVirtualGuest.java deleted file mode 100644 index 42f47817fe..0000000000 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/BillingItemVirtualGuest.java +++ /dev/null @@ -1,101 +0,0 @@ -/** - * 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.softlayer.domain; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Strings.emptyToNull; - -/** - * - * @author Jason King - * @see - */ -public class BillingItemVirtualGuest implements Comparable { - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - private int id = -1; - - public Builder id(int id) { - this.id = id; - return this; - } - - public static Builder fromBillingItemVirtualGuest(BillingItemVirtualGuest in) { - return BillingItemVirtualGuest.builder().id(in.getId()); - } - } - - private int id = -1; - - // for deserializer - BillingItemVirtualGuest() { - - } - - public BillingItemVirtualGuest(int id) { - this.id = id; - } - - @Override - public int compareTo(BillingItemVirtualGuest arg0) { - return new Integer(id).compareTo(arg0.getId()); - } - - /** - * @return The unique identifier for this billing item. - */ - public int getId() { - return id; - } - - public Builder toBuilder() { - return Builder.fromBillingItemVirtualGuest(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (id ^ (id >>> 32)); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - BillingItemVirtualGuest other = (BillingItemVirtualGuest) obj; - if (id != other.id) - return false; - return true; - } - - @Override - public String toString() { - return "[id=" + id + "]"; - } -} diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/OperatingSystem.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/OperatingSystem.java index 053c740e36..0b10bee531 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/OperatingSystem.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/OperatingSystem.java @@ -132,6 +132,6 @@ public class OperatingSystem implements Comparable { @Override public String toString() { - return "OperatingSystem [id=" + id + ", passwords=" + passwords + "]"; + return "[id=" + id + ", passwords=" + passwords + "]"; } } diff --git a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/VirtualGuest.java b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/VirtualGuest.java index 11cc3afbf1..4f7445b461 100644 --- a/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/VirtualGuest.java +++ b/sandbox-providers/softlayer/src/main/java/org/jclouds/softlayer/domain/VirtualGuest.java @@ -74,7 +74,7 @@ public class VirtualGuest implements Comparable { private String uuid; private String primaryBackendIpAddress; private String primaryIpAddress; - private BillingItemVirtualGuest billingItem; + private int billingItemId; private OperatingSystem operatingSystem; private Datacenter datacenter; private PowerState powerState; @@ -179,8 +179,8 @@ public class VirtualGuest implements Comparable { return this; } - public Builder billingItem(BillingItemVirtualGuest billingItem) { - this.billingItem = billingItem; + public Builder billingItemId(int billingItemId) { + this.billingItemId = billingItemId; return this; } @@ -204,7 +204,7 @@ public class VirtualGuest implements Comparable { fullyQualifiedDomainName, hostname, id, lastVerifiedDate, maxCpu, maxCpuUnits, maxMemory, metricPollDate, modifyDate, notes, privateNetworkOnly, startCpus, statusId, uuid, primaryBackendIpAddress, - primaryIpAddress,billingItem,operatingSystem,datacenter,powerState); + primaryIpAddress, billingItemId,operatingSystem,datacenter,powerState); } public static Builder fromVirtualGuest(VirtualGuest in) { @@ -229,7 +229,7 @@ public class VirtualGuest implements Comparable { .uuid(in.getUuid()) .primaryBackendIpAddress(in.getPrimaryBackendIpAddress()) .primaryIpAddress(in.getPrimaryIpAddress()) - .billingItem(in.getBillingItem()) + .billingItemId(in.getBillingItemId()) .operatingSystem(in.getOperatingSystem()) .datacenter(in.getDatacenter()) .powerState(in.getPowerState()); @@ -283,7 +283,7 @@ public class VirtualGuest implements Comparable { private String primaryBackendIpAddress; private String primaryIpAddress; - private BillingItemVirtualGuest billingItem; + private int billingItemId = -1; private OperatingSystem operatingSystem; private Datacenter datacenter; private PowerState powerState; @@ -297,7 +297,7 @@ public class VirtualGuest implements Comparable { String fullyQualifiedDomainName, String hostname, int id, Date lastVerifiedDate, int maxCpu, String maxCpuUnits, int maxMemory, Date metricPollDate, Date modifyDate, String notes, boolean privateNetworkOnly, int startCpus, int statusId, String uuid, String primaryBackendIpAddress, - String primaryIpAddress,BillingItemVirtualGuest billingItem, OperatingSystem operatingSystem, Datacenter datacenter, PowerState powerState) { + String primaryIpAddress,int billingItemId, OperatingSystem operatingSystem, Datacenter datacenter, PowerState powerState) { this.accountId = accountId; this.createDate = createDate; this.dedicatedAccountHostOnly = dedicatedAccountHostOnly; @@ -318,7 +318,7 @@ public class VirtualGuest implements Comparable { this.uuid = uuid; this.primaryBackendIpAddress = primaryBackendIpAddress; this.primaryIpAddress = primaryIpAddress; - this.billingItem = billingItem; + this.billingItemId = billingItemId; this.operatingSystem = operatingSystem; this.datacenter = datacenter; this.powerState = powerState; @@ -475,8 +475,8 @@ public class VirtualGuest implements Comparable { /** * @return The billing item for a CloudLayer Compute Instance. */ - public BillingItemVirtualGuest getBillingItem() { - return billingItem; + public int getBillingItemId() { + return billingItemId; } /** @@ -525,7 +525,7 @@ public class VirtualGuest implements Comparable { result = prime * result + startCpus; result = prime * result + statusId; result = prime * result + ((uuid == null) ? 0 : uuid.hashCode()); - result = prime * result + ((billingItem == null) ? 0 : billingItem.hashCode()); + result = prime * result + (billingItemId ^ (billingItemId >>> 32)); result = prime * result + ((operatingSystem == null) ? 0 : operatingSystem.hashCode()); result = prime * result + ((datacenter == null) ? 0 : datacenter.hashCode()); result = prime * result + ((powerState == null) ? 0 : powerState.hashCode()); @@ -617,10 +617,7 @@ public class VirtualGuest implements Comparable { return false; } else if (!uuid.equals(other.uuid)) return false; - if (billingItem == null) { - if (other.billingItem != null) - return false; - } else if (!billingItem.equals(other.billingItem)) + if (billingItemId != other.billingItemId) return false; if (operatingSystem == null) { if (other.operatingSystem != null) @@ -649,7 +646,7 @@ public class VirtualGuest implements Comparable { + ", metricPollDate=" + metricPollDate + ", modifyDate=" + modifyDate + ", notes=" + notes + ", primaryBackendIpAddress=" + primaryBackendIpAddress + ", primaryIpAddress=" + primaryIpAddress + ", privateNetworkOnly=" + privateNetworkOnly + ", startCpus=" + startCpus + ", statusId=" + statusId - + ", uuid=" + uuid + ", billingItem="+billingItem+", operatingSystem="+operatingSystem+", datacenter="+datacenter + + ", uuid=" + uuid + ", billingItemId="+billingItemId+", operatingSystem="+operatingSystem+", datacenter="+datacenter + ", powerState="+powerState+"]"; } 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 520e3aaf2e..6bb8e8761e 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 @@ -28,7 +28,6 @@ import org.jclouds.domain.Location; import org.jclouds.location.suppliers.JustProvider; import org.jclouds.softlayer.domain.Address; import org.jclouds.softlayer.domain.Datacenter; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @@ -41,24 +40,14 @@ import com.google.common.collect.ImmutableSet; @Test(singleThreaded = true, groups = "unit") public class DatacenterToLocationTest { - private DatacenterToLocation function; - - @BeforeMethod - public void setup() { - function = new DatacenterToLocation(new JustProvider(ImmutableSet.of(), "softlayer", URI.create("foo"))); - } + static DatacenterToLocation function = new DatacenterToLocation(new JustProvider(ImmutableSet. of(), "softlayer", + URI.create("foo")));; @Test public void testDatacenterToLocation() { - Address address = Address.builder().country("US") - .state("TX") - .description("This is Texas!") - .build(); + Address address = Address.builder().country("US").state("TX").description("This is Texas!").build(); - Datacenter datacenter = Datacenter.builder().id(1) - .longName("Texas Datacenter") - .locationAddress(address) - .build(); + Datacenter datacenter = Datacenter.builder().id(1).longName("Texas Datacenter").locationAddress(address).build(); Location location = function.apply(datacenter); @@ -70,9 +59,7 @@ public class DatacenterToLocationTest { @Test public void testGetIso3166CodeNoCountryAndState() { - Datacenter datacenter = Datacenter.builder().id(1) - .longName("Nowhere") - .build(); + Datacenter datacenter = Datacenter.builder().id(1).longName("Nowhere").build(); Location location = function.apply(datacenter); @@ -83,14 +70,9 @@ public class DatacenterToLocationTest { @Test public void testGetIso3166CodeCountryOnly() { - Address address = Address.builder().country("US") - .description("This is North America!") - .build(); + Address address = Address.builder().country("US").description("This is North America!").build(); - Datacenter datacenter = Datacenter.builder().id(1) - .longName("Nowhere") - .locationAddress(address) - .build(); + Datacenter datacenter = Datacenter.builder().id(1).longName("Nowhere").locationAddress(address).build(); Location location = function.apply(datacenter); @@ -102,15 +84,10 @@ public class DatacenterToLocationTest { @Test public void testGetIso3166CodeWhitespaceTrimmer() { - Address address = Address.builder().country(" US ") - .state(" TX ") - .description("This is spaced out Texas") - .build(); + Address address = Address.builder().country(" US ").state(" TX ").description("This is spaced out Texas") + .build(); - Datacenter datacenter = Datacenter.builder().id(1) - .longName("Nowhere") - .locationAddress(address) - .build(); + Datacenter datacenter = Datacenter.builder().id(1).longName("Nowhere").locationAddress(address).build(); Location location = function.apply(datacenter); diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java new file mode 100644 index 0000000000..7cf4b7948c --- /dev/null +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java @@ -0,0 +1,75 @@ +/** + * 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.softlayer.compute.functions; + +import static org.testng.Assert.assertEquals; + +import java.net.UnknownHostException; +import java.util.Map; +import java.util.Set; + +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadataBuilder; +import org.jclouds.compute.domain.NodeState; +import org.jclouds.domain.Credentials; +import org.jclouds.domain.Location; +import org.jclouds.softlayer.compute.functions.VirtualGuestToNodeMetadata.FindLocationForVirtualGuest; +import org.jclouds.softlayer.domain.VirtualGuest; +import org.jclouds.softlayer.parse.ParseVirtualGuestWithNoPasswordTest; +import org.testng.annotations.Test; + +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "VirtualGuestToNodeMetadataTest") +public class VirtualGuestToNodeMetadataTest { + + @Test + public void testApplyWhereVirtualGuestWithNoPassword() throws UnknownHostException { + + // notice if we've already parsed this properly here, we can rely on it. + VirtualGuest guest = new ParseVirtualGuestWithNoPasswordTest().expected(); + + // note we are testing when no credentials are here. otherwise would be ("node#416696", new + // Credentials("root", "password")) + Map credentialStore = ImmutableMap. of(); + + // setup so that we have an expected Location to be parsed from the guest. + Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter()); + Supplier> locationSupplier = Suppliers.> ofInstance(ImmutableSet + . of(expectedLocation)); + + VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(credentialStore, + new FindLocationForVirtualGuest(locationSupplier)); + + NodeMetadata node = parser.apply(guest); + + assertEquals(node, new NodeMetadataBuilder().ids("416696").name("node-246105155").group("node").location( + expectedLocation).state(NodeState.PENDING).publicAddresses(ImmutableSet.of("173.192.29.186")) + .privateAddresses(ImmutableSet.of("10.37.102.194")).build()); + + // because it wasn't present in the credential store. + assertEquals(node.getCredentials(), null); + } +} diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientLiveTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientLiveTest.java index d8f3444567..575ceeb1e2 100644 --- a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientLiveTest.java +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientLiveTest.java @@ -73,9 +73,8 @@ public class VirtualGuestClientLiveTest extends BaseSoftLayerClientLiveTest { // objectMask: virtualGuests.activeTransaction for( VirtualGuest guest: client.listVirtualGuests()) { if (guest.getHostname().startsWith(TEST_HOSTNAME_PREFIX)) { - BillingItemVirtualGuest billingItem = guest.getBillingItem(); - if(billingItem!=null) { - client.cancelService(billingItem.getId()); + if(guest.getBillingItemId()!=-1) { + client.cancelService(guest.getBillingItemId()); } } } @@ -125,8 +124,7 @@ public class VirtualGuestClientLiveTest extends BaseSoftLayerClientLiveTest { private void checkVirtualGuest(VirtualGuest vg) { - if (vg.getBillingItem()==null) return;//Quotes and shutting down guests - checkBillingItem(vg.getBillingItem()); + if (vg.getBillingItemId()==-1) return;//Quotes and shutting down guests assert vg.getAccountId() > 0 : vg; assert vg.getCreateDate() != null : vg; @@ -146,9 +144,4 @@ public class VirtualGuestClientLiveTest extends BaseSoftLayerClientLiveTest { assert vg.getPrimaryIpAddress() != null : vg; } - private void checkBillingItem(BillingItemVirtualGuest billingItem) { - assert null != billingItem; - assert billingItem.getId() > 0 : billingItem; - } - } diff --git a/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ParseVirtualGuestWithNoPasswordTest.java b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ParseVirtualGuestWithNoPasswordTest.java new file mode 100644 index 0000000000..b7811a9072 --- /dev/null +++ b/sandbox-providers/softlayer/src/test/java/org/jclouds/softlayer/parse/ParseVirtualGuestWithNoPasswordTest.java @@ -0,0 +1,80 @@ +/** + * 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.softlayer.parse; + +import javax.ws.rs.Consumes; +import javax.ws.rs.core.MediaType; + +import org.jclouds.date.internal.SimpleDateFormatDateService; +import org.jclouds.json.BaseItemParserTest; +import org.jclouds.json.config.GsonModule; +import org.jclouds.softlayer.compute.config.SoftLayerParserModule; +import org.jclouds.softlayer.domain.Datacenter; +import org.jclouds.softlayer.domain.OperatingSystem; +import org.jclouds.softlayer.domain.PowerState; +import org.jclouds.softlayer.domain.VirtualGuest; +import org.testng.annotations.Test; + +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ParseVirtualGuestWithNoPasswordTest") +public class ParseVirtualGuestWithNoPasswordTest extends BaseItemParserTest { + + @Override + public String resource() { + return "/virtual_guest_no_password.json"; + } + + @Override + @Consumes(MediaType.APPLICATION_JSON) + public VirtualGuest expected() { + return VirtualGuest + .builder() + //TODO: why is there no billingItem in the json? + .id(416696).accountId(93750).billingItemId(0) + .createDate(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-10-01T11:10:16-08:00")) + .dedicatedAccountHostOnly(true).domain("me.org").fullyQualifiedDomainName("node-246105155.me.org") + .hostname("node-246105155").maxCpu(1).maxCpuUnits("CORE").maxMemory(1024) + .modifyDate(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-10-01T11:11:31-08:00")) + .primaryBackendIpAddress("10.37.102.194").primaryIpAddress("173.192.29.186").startCpus(1).statusId(1001) + .uuid("694d34c3-9c36-b590-4ba7-4ec58954b9dc") + .operatingSystem(OperatingSystem.builder().id(913812).build()) + .datacenter(Datacenter.builder().id(3).name("dal01").longName("Dallas").build()) + //TODO: maybe powerState can be flattened like billingItemId + .powerState(new PowerState(VirtualGuest.State.HALTED)).build(); + } + + protected Injector injector() { + return Guice.createInjector(new SoftLayerParserModule(), new GsonModule() { + + @Override + protected void configure() { + bind(DateAdapter.class).to(Iso8601DateAdapter.class); + super.configure(); + } + + }); + } + +}