diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/ResourceCapacityRange.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/ResourceCapacityRange.java new file mode 100644 index 0000000000..1515cd8426 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/ResourceCapacityRange.java @@ -0,0 +1,140 @@ +/** + * 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.tmrk.enterprisecloud.domain; + +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity; + +import javax.xml.bind.annotation.XmlElement; + +/** + * + * @author Jason King + */ +public class ResourceCapacityRange { + + @SuppressWarnings("unchecked") + public static Builder builder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder().fromResourceCapacityRange(this); + } + + public static class Builder { + + private ResourceCapacity minimumSize; + private ResourceCapacity maximumSize; + private ResourceCapacity stepFactor; + + /** + * @see ResourceCapacityRange#getMinimumSize + */ + public Builder minimumSize(ResourceCapacity minimumSize) { + this.minimumSize = minimumSize; + return this; + } + + /** + * @see ResourceCapacityRange#getMaximumSize + */ + public Builder maximumSize(ResourceCapacity maximumSize) { + this.maximumSize = maximumSize; + return this; + } + + /** + * @see ResourceCapacityRange#getStepFactor + */ + public Builder stepFactor(ResourceCapacity stepFactor) { + this.stepFactor = stepFactor; + return this; + } + + public ResourceCapacityRange build() { + return new ResourceCapacityRange(minimumSize,maximumSize,stepFactor); + } + + public Builder fromResourceCapacityRange(ResourceCapacityRange in) { + return minimumSize(in.getMinimumSize()).maximumSize(in.getMaximumSize()).stepFactor(in.getStepFactor()); + } + } + + @XmlElement(name = "MinimumSize", required = false) + private ResourceCapacity minimumSize; + + @XmlElement(name = "MaximumSize", required = false) + private ResourceCapacity maximumSize; + + @XmlElement(name = "StepFactor", required = false) + private ResourceCapacity stepFactor; + + private ResourceCapacityRange(@Nullable ResourceCapacity minimumSize, @Nullable ResourceCapacity maximumSize, @Nullable ResourceCapacity stepFactor) { + this.minimumSize = minimumSize; + this.maximumSize = maximumSize; + this.stepFactor = stepFactor; + } + + private ResourceCapacityRange() { + //For JAXB + } + + public ResourceCapacity getMinimumSize() { + return minimumSize; + } + + public ResourceCapacity getMaximumSize() { + return maximumSize; + } + + public ResourceCapacity getStepFactor() { + return stepFactor; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ResourceCapacityRange that = (ResourceCapacityRange) o; + + if (maximumSize != null ? !maximumSize.equals(that.maximumSize) : that.maximumSize != null) + return false; + if (minimumSize != null ? !minimumSize.equals(that.minimumSize) : that.minimumSize != null) + return false; + if (stepFactor != null ? !stepFactor.equals(that.stepFactor) : that.stepFactor != null) + return false; + + return true; + } + + @Override + public int hashCode() { + int result = minimumSize != null ? minimumSize.hashCode() : 0; + result = 31 * result + (maximumSize != null ? maximumSize.hashCode() : 0); + result = 31 * result + (stepFactor != null ? stepFactor.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "[minimumSize="+ minimumSize +", maximumSize="+maximumSize+", stepFactor="+stepFactor+"]"; + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/vm/VirtualMachineConfigurationOptions.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/vm/VirtualMachineConfigurationOptions.java new file mode 100644 index 0000000000..261db65e9e --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/vm/VirtualMachineConfigurationOptions.java @@ -0,0 +1,142 @@ +/** + * 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.tmrk.enterprisecloud.domain.vm; + +import org.jclouds.javax.annotation.Nullable; +import org.jclouds.tmrk.enterprisecloud.domain.ResourceCapacityRange; +import org.jclouds.tmrk.enterprisecloud.domain.internal.BaseResource; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.net.URI; + +/** + * + * @author Jason King + * + */ +@XmlRootElement(name = "VirtualMachineConfigurationOptions") +public class VirtualMachineConfigurationOptions extends BaseResource { + + @SuppressWarnings("unchecked") + public static Builder builder() { + return new Builder(); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder toBuilder() { + return new Builder().fromVirtualMachineConfigurationOptions(this); + } + + public static class Builder extends BaseResource.Builder { + //TODO There are additional fields + protected ResourceCapacityRange memory; + + /** + * @see VirtualMachineConfigurationOptions#getMemory + */ + public Builder memory(ResourceCapacityRange memory) { + this.memory = memory; + return this; + } + + @Override + public VirtualMachineConfigurationOptions build() { + return new VirtualMachineConfigurationOptions(href, type, memory); + } + + public Builder fromVirtualMachineConfigurationOptions(VirtualMachineConfigurationOptions in) { + return fromResource(in).memory(in.getMemory()); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder fromResource(BaseResource in) { + return Builder.class.cast(super.fromResource(in)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder type(String type) { + return Builder.class.cast(super.type(type)); + } + + /** + * {@inheritDoc} + */ + @Override + public Builder href(URI href) { + return Builder.class.cast(super.href(href)); + } + + } + + @XmlElement(name = "Memory", required = false) + private ResourceCapacityRange memory; + + private VirtualMachineConfigurationOptions(URI href, String type, @Nullable ResourceCapacityRange memory) { + super(href, type); + this.memory = memory; + } + + private VirtualMachineConfigurationOptions() { + //For JAXB + } + + /** + * + * @return memory capacity range + */ + public ResourceCapacityRange getMemory() { + return memory; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + VirtualMachineConfigurationOptions that = (VirtualMachineConfigurationOptions) o; + + if (memory != null ? !memory.equals(that.memory) : that.memory != null) + return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (memory != null ? memory.hashCode() : 0); + return result; + } + + @Override + public String string() { + return super.string()+", memory="+memory; + } +} \ No newline at end of file diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java index 1fdb2f38ce..31518b6f36 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java @@ -24,6 +24,7 @@ import org.jclouds.rest.annotations.*; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine; +import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines; import javax.ws.rs.Consumes; @@ -71,4 +72,12 @@ public interface VirtualMachineAsyncClient { @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture getAssignedIpAddresses(@EndpointParam URI uri); + /** + * @see VirtualMachineClient#getVirtualMachineConfigurationOptions + */ + @GET + @Consumes("application/vnd.tmrk.cloud.virtualMachineConfigurationOptions") + @JAXBResponseParser + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getVirtualMachineConfigurationOptions(@EndpointParam URI uri); } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java index 49f743e800..8734ee572c 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java @@ -21,6 +21,7 @@ package org.jclouds.tmrk.enterprisecloud.features; import org.jclouds.concurrent.Timeout; import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine; +import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines; import java.net.URI; @@ -62,4 +63,12 @@ public interface VirtualMachineClient { */ AssignedIpAddresses getAssignedIpAddresses(URI uri); + /** + * The Get Virtual Machines Configuration Options call returns information + * regarding the configuration options of a specified virtual machine in a compute pool. + * @param uri the uri for the configuration options. e.g. /cloudapi/ecloud/virtualmachines/{id}/configurationoptions + * @return the configuration options + */ + VirtualMachineConfigurationOptions getVirtualMachineConfigurationOptions(URI uri); + } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java index c16e0250f8..8ffcf2dbeb 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java @@ -80,6 +80,20 @@ public class VirtualMachineAsyncClientTest extends BaseTerremarkEnterpriseCloudA checkFilters(httpRequest); } + public void testGetVirtualMachineConfigurationOptions() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + Method method = VirtualMachineAsyncClient.class.getMethod("getVirtualMachineConfigurationOptions", URI.class); + HttpRequest httpRequest = processor.createRequest(method,new URI("/cloudapi/ecloud/virtualmachines/5504/configurationoptions")); + + assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/virtualmachines/5504/configurationoptions HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.virtualMachineConfigurationOptions\nx-tmrk-version: 2011-07-01\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + @Override protected TypeLiteral> createTypeLiteral() { return new TypeLiteral>() { diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java index 2d56910add..d59642c1a8 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java @@ -22,6 +22,7 @@ import com.google.common.collect.Iterables; import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses; import org.jclouds.tmrk.enterprisecloud.domain.network.DeviceNetwork; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine; +import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; @@ -30,11 +31,12 @@ import java.net.URI; import java.util.Set; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; /** * Tests behavior of {@code VirtualMachineClient} - * + * TODO: don't hard-code uri's it should be possible to determine them but that means chaining the tests potentially. * @author Jason King */ @Test(groups = "live", testName = "VirtualMachineClientLiveTest") @@ -47,31 +49,31 @@ public class VirtualMachineClientLiveTest extends BaseTerremarkEnterpriseCloudCl private VirtualMachineClient client; - @Test public void testGetVirtualMachines() throws Exception { - // TODO: don't hard-code uri VirtualMachines virtualMachines = client.getVirtualMachines(new URI("/cloudapi/ecloud/virtualMachines/computePools/89")); for( VirtualMachine vm : virtualMachines.getVirtualMachines()) { VirtualMachine virtualMachine = client.getVirtualMachine(vm.getHref()); - assert null != virtualMachine; + assertNotNull(virtualMachine,"virtualMachine should not be null"); assertEquals(virtualMachine.getStatus(),VirtualMachine.VirtualMachineStatus.DEPLOYED); } } - @Test public void testGetVirtualMachine() throws Exception { - // TODO: don't hard-code uri VirtualMachine virtualMachine = client.getVirtualMachine(new URI("/cloudapi/ecloud/virtualMachines/5504")); - assert null != virtualMachine; + assertNotNull(virtualMachine,"virtualMachine should not be null"); assertEquals(virtualMachine.getStatus(), VirtualMachine.VirtualMachineStatus.DEPLOYED); } - @Test public void testGetAssignedIpAddresses() throws Exception { AssignedIpAddresses assignedIpAddresses = client.getAssignedIpAddresses(new URI("/cloudapi/ecloud/virtualMachines/5504/assignedips")); - assert null != assignedIpAddresses; + assertNotNull(assignedIpAddresses,"assignedIpAddresses should not be null"); DeviceNetwork network = Iterables.getOnlyElement(assignedIpAddresses.getNetworks().getDeviceNetworks()); Set ipAddresses = network.getIpAddresses().getIpAddresses(); assertTrue(ipAddresses.size()>0, "vm has no assigned ip addresses"); - } + } + + public void testGetVirtualMachineConfigurationOptions() throws Exception { + VirtualMachineConfigurationOptions virtualMachineConfigurationOptions = client.getVirtualMachineConfigurationOptions(new URI("/cloudapi/ecloud/virtualmachines/5504/configurationoptions")); + assertNotNull(virtualMachineConfigurationOptions,"options should not be null"); + } } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/VirtualMachineConfigurationOptionsJAXBParsingTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/VirtualMachineConfigurationOptionsJAXBParsingTest.java new file mode 100644 index 0000000000..0237be11d0 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/VirtualMachineConfigurationOptionsJAXBParsingTest.java @@ -0,0 +1,106 @@ +/** + * 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.tmrk.enterprisecloud.xml; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; +import com.google.inject.AbstractModule; +import com.google.inject.Module; +import com.google.inject.Provides; +import org.jclouds.crypto.Crypto; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.http.functions.ParseXMLWithJAXB; +import org.jclouds.logging.config.NullLoggingModule; +import org.jclouds.rest.AuthorizationException; +import org.jclouds.rest.BaseRestClientTest; +import org.jclouds.rest.RestContextSpec; +import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.jclouds.tmrk.enterprisecloud.domain.ResourceCapacityRange; +import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity; +import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions; +import org.jclouds.tmrk.enterprisecloud.features.VirtualMachineAsyncClient; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.inject.Named; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.URI; +import java.util.Set; + +import static org.jclouds.io.Payloads.newInputStreamPayload; +import static org.jclouds.rest.RestContextFactory.contextSpec; +import static org.jclouds.rest.RestContextFactory.createContextBuilder; +import static org.testng.Assert.assertEquals; + +/** + * Tests behavior of JAXB parsing for VirtualMachineConfigurationOptions + * + * @author Jason King + */ +@Test(groups = "unit", testName = "VirtualMachineConfigurationOptionsJAXBParsingTest") +public class VirtualMachineConfigurationOptionsJAXBParsingTest extends BaseRestClientTest { + + @BeforeClass + void setupFactory() { + RestContextSpec contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo", + "credentialFoo", String.class, Integer.class, + ImmutableSet. of(new MockModule(), new NullLoggingModule(), new AbstractModule() { + + @Override + protected void configure() {} + + @SuppressWarnings("unused") + @Provides + @Named("exception") + Set exception() { + throw new AuthorizationException(); + } + + })); + + injector = createContextBuilder(contextSpec).buildInjector(); + parserFactory = injector.getInstance(ParseSax.Factory.class); + crypto = injector.getInstance(Crypto.class); + } + + @Test + public void testParseVirtualMachineWithJAXB() throws Exception { + + Method method = VirtualMachineAsyncClient.class.getMethod("getVirtualMachineConfigurationOptions", URI.class); + HttpRequest request = factory(VirtualMachineAsyncClient.class).createRequest(method,new URI("/1")); + assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class); + + Function parser = (Function) RestAnnotationProcessor + .createResponseParser(parserFactory, injector, method, request); + + InputStream is = getClass().getResourceAsStream("/virtualMachineConfigurationOptions.xml"); + VirtualMachineConfigurationOptions virtualMachineConfigurationOptions = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is))); + + assertMemoryOptions(virtualMachineConfigurationOptions.getMemory()); + } + + private void assertMemoryOptions(ResourceCapacityRange memory) { + assertEquals(memory.getMinimumSize(), ResourceCapacity.builder().value(256).unit("MB").build()); + assertEquals(memory.getMaximumSize(), ResourceCapacity.builder().value(261120).unit("MB").build()); + assertEquals(memory.getStepFactor(), ResourceCapacity.builder().value(4).unit("MB").build()); + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/resources/virtualMachineConfigurationOptions.xml b/sandbox-providers/tmrk-enterprisecloud/src/test/resources/virtualMachineConfigurationOptions.xml new file mode 100644 index 0000000000..a566b12a97 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/resources/virtualMachineConfigurationOptions.xml @@ -0,0 +1,73 @@ + + + 1 + 8 + 1 + + + + MB + 256 + + + MB + 261120 + + + MB + 4 + + + + 1 + 15 + + + + GB + 1 + + + GB + 512 + + + GB + 1 + + + 0 + + + + + GB + 1 + + + GB + 512 + + + GB + 1 + + + 0 + + + + 1 + 4 + 1 + + + Linux + false + false + true + + \ No newline at end of file