Added VirtualMachineConfigurationOptions, ResourceCapacityRange, Service call and tests

This commit is contained in:
Jason King 2011-11-22 17:08:04 +00:00
parent 2aa0fff801
commit 18f2060502
8 changed files with 505 additions and 10 deletions

View File

@ -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;
/**
* <xs:complexType name="ResourceCapacityRange">
* @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+"]";
}
}

View File

@ -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;
/**
* <xs:complexType name="VirtualMachineConfigurationOptions">
* @author Jason King
*
*/
@XmlRootElement(name = "VirtualMachineConfigurationOptions")
public class VirtualMachineConfigurationOptions extends BaseResource<VirtualMachineConfigurationOptions> {
@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<VirtualMachineConfigurationOptions> {
//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<VirtualMachineConfigurationOptions> 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;
}
}

View File

@ -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<AssignedIpAddresses> getAssignedIpAddresses(@EndpointParam URI uri);
/**
* @see VirtualMachineClient#getVirtualMachineConfigurationOptions
*/
@GET
@Consumes("application/vnd.tmrk.cloud.virtualMachineConfigurationOptions")
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<VirtualMachineConfigurationOptions> getVirtualMachineConfigurationOptions(@EndpointParam URI uri);
}

View File

@ -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);
}

View File

@ -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<RestAnnotationProcessor<VirtualMachineAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<VirtualMachineAsyncClient>>() {

View File

@ -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<String> 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");
}
}

View File

@ -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<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo",
"credentialFoo", String.class, Integer.class,
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new AbstractModule() {
@Override
protected void configure() {}
@SuppressWarnings("unused")
@Provides
@Named("exception")
Set<String> 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<HttpResponse, VirtualMachineConfigurationOptions> parser = (Function<HttpResponse, VirtualMachineConfigurationOptions>) 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());
}
}

View File

@ -0,0 +1,73 @@
<VirtualMachineConfigurationOptions
href="/cloudapi/ecloud/virtualmachines/5504/configurationoptions"
type="application/vnd.tmrk.cloud.virtualMachineConfigurationOptions"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Processor>
<Minimum>1</Minimum>
<Maximum>8</Maximum>
<StepFactor>1</StepFactor>
</Processor>
<Memory>
<MinimumSize>
<Unit>MB</Unit>
<Value>256</Value>
</MinimumSize>
<MaximumSize>
<Unit>MB</Unit>
<Value>261120</Value>
</MaximumSize>
<StepFactor>
<Unit>MB</Unit>
<Value>4</Value>
</StepFactor>
</Memory>
<Disk>
<Minimum>1</Minimum>
<Maximum>15</Maximum>
<SystemDisk>
<ResourceCapacityRange>
<MinimumSize>
<Unit>GB</Unit>
<Value>1</Value>
</MinimumSize>
<MaximumSize>
<Unit>GB</Unit>
<Value>512</Value>
</MaximumSize>
<StepFactor>
<Unit>GB</Unit>
<Value>1</Value>
</StepFactor>
</ResourceCapacityRange>
<MonthlyCost>0</MonthlyCost>
</SystemDisk>
<DataDisk>
<ResourceCapacityRange>
<MinimumSize>
<Unit>GB</Unit>
<Value>1</Value>
</MinimumSize>
<MaximumSize>
<Unit>GB</Unit>
<Value>512</Value>
</MaximumSize>
<StepFactor>
<Unit>GB</Unit>
<Value>1</Value>
</StepFactor>
</ResourceCapacityRange>
<MonthlyCost>0</MonthlyCost>
</DataDisk>
</Disk>
<NetworkAdapter>
<Minimum>1</Minimum>
<Maximum>4</Maximum>
<StepFactor>1</StepFactor>
</NetworkAdapter>
<Customization>
<Type>Linux</Type>
<CanPowerOn>false</CanPowerOn>
<PasswordRequired>false</PasswordRequired>
<SshKeyRequired>true</SshKeyRequired>
</Customization>
</VirtualMachineConfigurationOptions>