Issue 695: Implemented createVirtualMachineFromTemplate

This commit is contained in:
Jason King 2011-12-15 23:10:05 +00:00
parent d241db7280
commit 24d3bd5d04
8 changed files with 405 additions and 27 deletions

View File

@ -0,0 +1,167 @@
/**
* 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.binders;
import com.jamesmurty.utils.XMLBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
import org.jclouds.rest.binders.BindToStringPayload;
import org.jclouds.tmrk.enterprisecloud.domain.internal.AnonymousResource;
import org.jclouds.tmrk.enterprisecloud.domain.layout.LayoutRequest;
import org.jclouds.tmrk.enterprisecloud.domain.network.LinuxCustomization;
import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkAdapterSetting;
import org.jclouds.tmrk.enterprisecloud.domain.network.WindowsCustomization;
import org.jclouds.tmrk.enterprisecloud.domain.vm.CreateVirtualMachine;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.util.Properties;
import java.util.Set;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* For use with {@see VirtualMachineClient#createVirtualMachineFromTemplate}
* @author Jason King
*/
@Singleton
public class BindCreateVirtualMachineKeyToXmlPayload implements Binder {
private final BindToStringPayload stringBinder;
@Inject
BindCreateVirtualMachineKeyToXmlPayload(BindToStringPayload stringBinder) {
this.stringBinder = stringBinder;
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object key) {
checkArgument(checkNotNull(key, "key") instanceof CreateVirtualMachine, "this binder is only valid for CreateOsTemplateVirtualMachineRequest instances!");
checkNotNull(request, "request");
CreateVirtualMachine vmData = CreateVirtualMachine.class.cast(key);
String payload = createXMLPayload(vmData);
return stringBinder.bindToRequest(request, payload);
}
private String createXMLPayload(CreateVirtualMachine vmData) {
try {
Properties outputProperties = new Properties();
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
final String name = vmData.getName();
final String processorCount = Integer.toString(vmData.getProcessorCount());
final String memoryUnit = vmData.getMemory().getUnit();
final int memoryValue = ((Double)(vmData.getMemory().getValue())).intValue();
final Set<String> tags = vmData.getTags();
final String description = vmData.getDescription();
final LayoutRequest layout = vmData.getLayout();
final String poweredOn = Boolean.toString(vmData.isPoweredOn());
final AnonymousResource template = vmData.getTemplate();
XMLBuilder builder = XMLBuilder.create("CreateVirtualMachine").a("name",name)
.e("ProcessorCount").t(processorCount).up()
.e("Memory").e("Unit").t(memoryUnit).up()
.e("Value").t(Integer.toString(memoryValue)).up().up();
builder = layout(builder,layout);
builder.e("Description").t(description).up();
builder = tags(builder,tags);
builder = linuxCustomization(builder, vmData);
builder = windowsCustomization(builder, vmData);
builder.e("PoweredOn").t(poweredOn).up()
.e("Template").a("href",template.getHref().toString())
.a("type", template.getType()).up();
return builder.asString(outputProperties);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (TransformerException t) {
throw new RuntimeException(t);
}
}
private XMLBuilder layout(XMLBuilder in, LayoutRequest layout) {
in = in.e("Layout");
if(layout.getGroup()!=null) {
in = in.e("Group").a("href",layout.getGroup().getHref().toString())
.a("type",layout.getGroup().getType()).up();
} else if (layout.getRow()!=null) {
checkNotNull(layout.getNewGroup(),"newGroup");
in = in.e("Row").a("href",layout.getRow().getHref().toString())
.a("type", layout.getRow().getType()).up()
.e("NewGroup").t(layout.getNewGroup()).up();
} else {
checkNotNull(layout.getNewRow(),"newRow");
checkNotNull(layout.getNewGroup(), "newGroup");
in = in.e("NewRow").t(layout.getNewRow()).up()
.e("NewGroup").t(layout.getNewGroup()).up();
}
return in.up();
}
private XMLBuilder tags(XMLBuilder in, Set<String> tags ) {
checkNotNull(tags,"tags");
in = in.e("Tags");
for(String tag: tags) {
in = in.e("Tag").t(tag).up();
}
return in.up();
}
private XMLBuilder linuxCustomization(XMLBuilder in, CreateVirtualMachine vmData) {
LinuxCustomization linuxCustomization = vmData.getLinuxCustomization();
if(linuxCustomization==null) return in;
if(vmData.getWindowsCustomization()!=null) throw new IllegalStateException("Cannot have linux and windows customizations");
in = in.e("LinuxCustomization")
.e("NetworkSettings")
.e("NetworkAdapterSettings");
for(NetworkAdapterSetting setting:linuxCustomization.getNetworkSettings().getNetworkAdapterSettings().getNetworkAdapterSettings()) {
in = networkAdapterSetting(in,setting);
}
//TODO DNS Settings
String href = linuxCustomization.getSshKey().getHref().toString();
String type = linuxCustomization.getSshKey().getType();
return in.up().up().e("SshKey").a("href",href).a("type",type).up().up();
}
private XMLBuilder networkAdapterSetting(XMLBuilder builder, NetworkAdapterSetting setting) {
String href = setting.getNetwork().getHref().toString();
String name = setting.getNetwork().getName();
String type = setting.getNetwork().getType();
builder.e("NetworkAdapter")
.e("Network").a("href",href).a("name",name).a("type",type).up()
.e("IpAddress").t(setting.getIpAddress()).up();
return builder;
}
private XMLBuilder windowsCustomization(XMLBuilder builder, CreateVirtualMachine vmData) {
WindowsCustomization windowsCustomization = vmData.getWindowsCustomization();
if(windowsCustomization==null) return builder;
if(vmData.getLinuxCustomization()!=null) throw new IllegalStateException("Cannot have linux and windows customizations");
//TODO: Not implemented yet
throw new UnsupportedOperationException("windowsCustomization has not been implemented yet");
//return builder;
}
}

View File

@ -40,7 +40,7 @@ public class LinuxCustomization {
public static class Builder {
private NetworkSettings networkSettings;
private NetworkSettings networkSettings = NetworkSettings.builder().build();
private AnonymousResource sshKey;
/**

View File

@ -39,7 +39,7 @@ public class NetworkSettings {
public static class Builder {
private NetworkAdapterSettings networkAdapterSettings;
private NetworkAdapterSettings networkAdapterSettings = NetworkAdapterSettings.builder().build();
private DnsSettings dnsSettings;
/**

View File

@ -19,6 +19,7 @@
package org.jclouds.tmrk.enterprisecloud.domain.vm;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.tmrk.enterprisecloud.domain.internal.AnonymousResource;
import org.jclouds.tmrk.enterprisecloud.domain.layout.LayoutRequest;
import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity;
import org.jclouds.tmrk.enterprisecloud.domain.network.LinuxCustomization;
@ -29,12 +30,14 @@ import javax.xml.bind.annotation.XmlRootElement;
import java.util.Set;
/**
* <xs:complexType name="CreateOsTemplateVirtualMachineRequestType">
* <xs:complexType name="CreateVirtualMachineType">
* @author Jason King
*
*/
@XmlRootElement(name = "CreateVirtualMachineRequest")
public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineRequest {
public class CreateVirtualMachine extends CreateVirtualMachineRequest {
// Note that this class collapses both
// CreateOsTemplateVirtualMachineRequestType and CreateVirtualMachineType
// into the same class as the separate classes are not needed.
@SuppressWarnings("unchecked")
public static Builder builder() {
@ -49,14 +52,15 @@ public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineR
return new Builder().fromCreateOsTemplateVirtualMachineRequest(this);
}
public static class Builder extends CreateVirtualMachineRequest.Builder<CreateOsTemplateVirtualMachineRequest> {
public static class Builder extends CreateVirtualMachineRequest.Builder<CreateVirtualMachine> {
private LinuxCustomization linuxCustomization;
private WindowsCustomization windowsCustomization;
private boolean poweredOn;
private AnonymousResource template;
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.vm.CreateOsTemplateVirtualMachineRequest#getLinuxCustomization
* @see CreateVirtualMachine#getLinuxCustomization
*/
public Builder linuxCustomization(LinuxCustomization linuxCustomization) {
this.linuxCustomization = linuxCustomization;
@ -64,7 +68,7 @@ public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineR
}
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.vm.CreateOsTemplateVirtualMachineRequest#getWindowsCustomization
* @see CreateVirtualMachine#getWindowsCustomization
*/
public Builder windowsCustomization(WindowsCustomization windowsCustomization) {
this.windowsCustomization = windowsCustomization;
@ -73,13 +77,21 @@ public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineR
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.vm.CreateOsTemplateVirtualMachineRequest#isPoweredOn
* @see CreateVirtualMachine#isPoweredOn
*/
public Builder poweredOn(boolean poweredOn) {
this.poweredOn = poweredOn;
return this;
}
/**
* @see CreateVirtualMachine#getTemplate
*/
public Builder template(AnonymousResource template) {
this.template = template;
return this;
}
/**
* @see CreateVirtualMachineRequest#getName()
*/
@ -127,17 +139,18 @@ public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineR
}
@Override
public CreateOsTemplateVirtualMachineRequest build() {
return new CreateOsTemplateVirtualMachineRequest(name, processorCount,memory,
public CreateVirtualMachine build() {
return new CreateVirtualMachine(name, processorCount,memory,
description,layout,tags,
linuxCustomization,windowsCustomization,poweredOn);
linuxCustomization,windowsCustomization,poweredOn, template);
}
public Builder fromCreateOsTemplateVirtualMachineRequest(CreateOsTemplateVirtualMachineRequest in) {
public Builder fromCreateOsTemplateVirtualMachineRequest(CreateVirtualMachine in) {
return fromCreateVirtualMachineRequest(in)
.linuxCustomization(in.getLinuxCustomization())
.windowsCustomization(in.getWindowsCustomization())
.poweredOn(in.isPoweredOn());
.poweredOn(in.isPoweredOn())
.template(in.getTemplate());
}
}
@ -150,17 +163,22 @@ public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineR
@XmlElement(name = "PoweredOn", required = false)
private boolean poweredOn;
@XmlElement(name = "Template", required = false)
private AnonymousResource template;
private CreateOsTemplateVirtualMachineRequest(String name, int processorCount, ResourceCapacity memory,
private CreateVirtualMachine(String name, int processorCount, ResourceCapacity memory,
@Nullable String description, @Nullable LayoutRequest layout, @Nullable Set<String> tags,
@Nullable LinuxCustomization linuxCustomization, @Nullable WindowsCustomization windowsCustomization, boolean poweredOn) {
@Nullable LinuxCustomization linuxCustomization, @Nullable WindowsCustomization windowsCustomization,
boolean poweredOn, @Nullable AnonymousResource template) {
super(name,processorCount,memory,description,layout,tags);
this.linuxCustomization = linuxCustomization;
this.windowsCustomization = windowsCustomization;
this.poweredOn = poweredOn;
this.template = template;
}
protected CreateOsTemplateVirtualMachineRequest() {
protected CreateVirtualMachine() {
//For JAXB
}
@ -176,16 +194,22 @@ public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineR
return poweredOn;
}
public AnonymousResource getTemplate() {
return template;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CreateOsTemplateVirtualMachineRequest that = (CreateOsTemplateVirtualMachineRequest) o;
CreateVirtualMachine that = (CreateVirtualMachine) o;
if (poweredOn != that.poweredOn) return false;
if (linuxCustomization != null ? !linuxCustomization.equals(that.linuxCustomization) : that.linuxCustomization != null)
return false;
if (template != null ? !template.equals(that.template) : that.template != null)
return false;
if (windowsCustomization != null ? !windowsCustomization.equals(that.windowsCustomization) : that.windowsCustomization != null)
return false;
@ -197,11 +221,12 @@ public class CreateOsTemplateVirtualMachineRequest extends CreateVirtualMachineR
int result = linuxCustomization != null ? linuxCustomization.hashCode() : 0;
result = 31 * result + (windowsCustomization != null ? windowsCustomization.hashCode() : 0);
result = 31 * result + (poweredOn ? 1 : 0);
result = 31 * result + (template != null ? template.hashCode() : 0);
return result;
}
@Override
public String string() {
return super.string()+", linuxCustomization="+linuxCustomization+", windowsCustomization="+windowsCustomization+", poweredOn="+poweredOn;
return super.string()+", linuxCustomization="+linuxCustomization+", windowsCustomization="+windowsCustomization+", poweredOn="+poweredOn+", template="+template;
}
}

View File

@ -22,15 +22,18 @@ import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.rest.annotations.*;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.tmrk.enterprisecloud.binders.BindCreateVirtualMachineKeyToXmlPayload;
import org.jclouds.tmrk.enterprisecloud.domain.Task;
import org.jclouds.tmrk.enterprisecloud.domain.hardware.HardwareConfiguration;
import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses;
import org.jclouds.tmrk.enterprisecloud.domain.vm.CreateVirtualMachine;
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.jclouds.tmrk.enterprisecloud.functions.ReturnEmptyVirtualMachinesOnNotFoundOr404;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.net.URI;
/**
@ -92,6 +95,15 @@ public interface VirtualMachineAsyncClient {
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<HardwareConfiguration> getHardwareConfiguration(@EndpointParam URI uri);
/**
* @see VirtualMachineClient#createVirtualMachineFromTemplate
*/
@POST
@Consumes("application/vnd.tmrk.cloud.virtualMachine")
@Produces(MediaType.APPLICATION_XML)
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<VirtualMachine> createVirtualMachineFromTemplate(@EndpointParam URI uri, @BinderParam(BindCreateVirtualMachineKeyToXmlPayload.class)CreateVirtualMachine request);
/**
* @see VirtualMachineClient#powerOn
*/

View File

@ -22,9 +22,7 @@ import org.jclouds.concurrent.Timeout;
import org.jclouds.tmrk.enterprisecloud.domain.Task;
import org.jclouds.tmrk.enterprisecloud.domain.hardware.HardwareConfiguration;
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 org.jclouds.tmrk.enterprisecloud.domain.vm.*;
import java.net.URI;
import java.util.concurrent.TimeUnit;
@ -81,6 +79,15 @@ public interface VirtualMachineClient {
*/
HardwareConfiguration getHardwareConfiguration(URI uri);
/**
* The Create Virtual Machines from Template call creates a new virtual machine from a template in the compute pool.
* If successful, the call returns information regarding the virtual machine that was created.
* @param uri the uri to create a virtual machine based upon the compute pool
* e.g. /cloudapi/ecloud/virtualMachines/computePools/{compute pool identifier}/action/createVirtualMachine
* @return the created virtual machine
*/
VirtualMachine createVirtualMachineFromTemplate(URI uri,CreateVirtualMachine request);
/**
* The Action Virtual Machines Power On call powers on a specified virtual machine.
* If successful, the call returns the task that powered on the virtual machine.

View File

@ -0,0 +1,119 @@
/**
* 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.binders;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.http.HttpRequest;
import org.jclouds.tmrk.enterprisecloud.domain.NamedResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.AnonymousResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity;
import org.jclouds.tmrk.enterprisecloud.domain.layout.LayoutRequest;
import org.jclouds.tmrk.enterprisecloud.domain.network.LinuxCustomization;
import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkAdapterSetting;
import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkAdapterSettings;
import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkSettings;
import org.jclouds.tmrk.enterprisecloud.domain.vm.CreateVirtualMachine;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code BindCreateVirtualMachineKeyToXmlPayload}
* @author Jason King
*/
@Test(groups = "unit", testName = "BindCreateVirtualMachineKeyToXmlPayloadTest")
public class BindCreateVirtualMachineKeyToXmlPayloadTest {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
}
});
public void testPayloadXmlContent() throws IOException {
String expected =
"<CreateVirtualMachine name='VirtualMachine2'>" +
"<ProcessorCount>2</ProcessorCount>" +
"<Memory><Unit>MB</Unit><Value>1024</Value></Memory>" +
"<Layout><Group href='/cloudapi/ecloud/layoutgroups/308' type='application/vnd.tmrk.cloud.layoutGroup'/></Layout>" +
"<Description>This is my first VM</Description>" +
"<Tags><Tag>Web</Tag></Tags>" +
"<LinuxCustomization>" +
"<NetworkSettings>" +
"<NetworkAdapterSettings>" +
"<NetworkAdapter>" +
"<Network href='/cloudapi/ecloud/networks/3936' name='10.146.204.64/28' type='application/vnd.tmrk.cloud.network'/>" +
"<IpAddress>10.146.204.68</IpAddress>" +
"</NetworkAdapter>" +
"</NetworkAdapterSettings>" +
"</NetworkSettings>" +
"<SshKey href='/cloudapi/ecloud/admin/sshkeys/77' type='application/vnd.tmrk.cloud.admin.sshKey'/>" +
"</LinuxCustomization>" +
"<PoweredOn>false</PoweredOn>" +
"<Template href='/cloudapi/ecloud/templates/6/computepools/89' type='application/vnd.tmrk.cloud.template'/>" +
"</CreateVirtualMachine>";
HttpRequest request = new HttpRequest("GET", URI.create("http://test"));
BindCreateVirtualMachineKeyToXmlPayload binder = injector
.getInstance(BindCreateVirtualMachineKeyToXmlPayload.class);
CreateVirtualMachine.Builder builder = CreateVirtualMachine.builder();
builder.name("VirtualMachine2")
.processorCount(2)
.memory(ResourceCapacity.builder().value(1024).unit("MB").build());
AnonymousResource group = AnonymousResource.builder().href(URI.create("/cloudapi/ecloud/layoutgroups/308")).type("application/vnd.tmrk.cloud.layoutGroup").build();
builder.layout(LayoutRequest.builder().group(group).build());
builder.description("This is my first VM");
builder.tags(ImmutableSet.of("Web"));
AnonymousResource sshKey = AnonymousResource.builder().href(URI.create("/cloudapi/ecloud/admin/sshkeys/77")).type("application/vnd.tmrk.cloud.admin.sshKey").build();
NamedResource network = NamedResource.builder()
.href(URI.create("/cloudapi/ecloud/networks/3936"))
.name("10.146.204.64/28")
.type("application/vnd.tmrk.cloud.network")
.build();
NetworkAdapterSetting adapterSetting = NetworkAdapterSetting.builder()
.network(network)
.ipAddress("10.146.204.68")
.build();
NetworkAdapterSettings adapterSettings = NetworkAdapterSettings.builder()
.addNetworkAdapterSetting(adapterSetting).build();
NetworkSettings networkSettings = NetworkSettings.builder().networkAdapterSettings(adapterSettings).build();
LinuxCustomization linuxCustomization = LinuxCustomization.builder()
.sshKey(sshKey)
.networkSettings(networkSettings)
.build();
builder.linuxCustomization(linuxCustomization);
AnonymousResource template = AnonymousResource.builder().href(URI.create("/cloudapi/ecloud/templates/6/computepools/89")).type("application/vnd.tmrk.cloud.template").build();
builder.template(template);
binder.bindToRequest(request, builder.build());
assertEquals(request.getPayload().getRawContent(), expected.replaceAll("'","\""));
}
}

View File

@ -18,10 +18,15 @@
*/
package org.jclouds.tmrk.enterprisecloud.features;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.jclouds.tmrk.enterprisecloud.domain.NamedResource;
import org.jclouds.tmrk.enterprisecloud.domain.hardware.HardwareConfiguration;
import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses;
import org.jclouds.tmrk.enterprisecloud.domain.network.DeviceNetwork;
import org.jclouds.tmrk.enterprisecloud.domain.internal.AnonymousResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity;
import org.jclouds.tmrk.enterprisecloud.domain.layout.LayoutRequest;
import org.jclouds.tmrk.enterprisecloud.domain.network.*;
import org.jclouds.tmrk.enterprisecloud.domain.vm.CreateVirtualMachine;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions;
import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachines;
@ -101,4 +106,47 @@ public class VirtualMachineClientLiveTest extends BaseTerremarkEnterpriseCloudCl
assertNull(result);
}
public void testCreateVirtualMachineFromTemplate() throws Exception {
CreateVirtualMachine.Builder builder = CreateVirtualMachine.builder();
builder.name("VirtualMachine2")
.processorCount(2)
.memory(ResourceCapacity.builder().value(1024).unit("MB").build());
AnonymousResource group = AnonymousResource.builder().href(URI.create("/cloudapi/ecloud/layoutgroups/308")).type("application/vnd.tmrk.cloud.layoutGroup").build();
builder.layout(LayoutRequest.builder().group(group).build());
builder.description("This is my first VM");
builder.tags(ImmutableSet.of("Web"));
AnonymousResource sshKey = AnonymousResource.builder().href(URI.create("/cloudapi/ecloud/admin/sshkeys/77")).type("application/vnd.tmrk.cloud.admin.sshKey").build();
NamedResource network = NamedResource.builder()
.href(URI.create("/cloudapi/ecloud/networks/3936"))
.name("10.146.204.64/28")
.type("application/vnd.tmrk.cloud.network")
.build();
NetworkAdapterSetting adapterSetting = NetworkAdapterSetting.builder()
.network(network)
.ipAddress("10.146.204.68")
.build();
NetworkAdapterSettings adapterSettings = NetworkAdapterSettings.builder()
.addNetworkAdapterSetting(adapterSetting).build();
NetworkSettings networkSettings = NetworkSettings.builder().networkAdapterSettings(adapterSettings).build();
LinuxCustomization linuxCustomization = LinuxCustomization.builder()
.sshKey(sshKey)
.networkSettings(networkSettings)
.build();
builder.linuxCustomization(linuxCustomization);
AnonymousResource template = AnonymousResource.builder().href(URI.create("/cloudapi/ecloud/templates/6/computepools/89")).type("application/vnd.tmrk.cloud.template").build();
builder.template(template);
VirtualMachine vm = client.createVirtualMachineFromTemplate(URI.create("/cloudapi/ecloud/virtualMachines/computePools/89/action/createVirtualMachine"), builder.build());
assertNotNull(vm);
// TODO: Check that the VM is created OK.
// TODO: DNSSettings are missing
//client.remove(vm.getHref()); //remove once verified - there needs to be no running tasks.
}
}