Added function ApplyMemoryToMachine for virtualbox.

commit e52df20b9010ebac266e2b593628ea1876a578f9
Author: Mattias Holmqvist <mattias.holmqvist@gmail.com>
Date:   Sun Oct 23 23:59:25 2011 +0200

    Added function ApplyMemoryToMachine for virtualbox.

commit a43e189b306fddbdc5ee5930355f20d90d148854
Merge: 3f6de26 041360e
Author: Mattias Holmqvist <mattias.holmqvist@gmail.com>
Date:   Sun Oct 23 23:26:42 2011 +0200

    Merge branch 'master' of https://github.com/jclouds/jclouds into functionalize

    Conflicts:
    	sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachine.java

commit 3f6de265d7342676fdeab99badd31f31cb43aadc
Author: Mattias Holmqvist <mattias.holmqvist@gmail.com>
Date:   Sun Oct 23 23:23:49 2011 +0200

    Added AttachNATRedirectRuleToMachine to virtualbox.
This commit is contained in:
Mattias Holmqvist 2011-10-23 23:59:54 +02:00
parent 041360e2e7
commit 394bb1b602
3 changed files with 125 additions and 15 deletions

View File

@ -0,0 +1,44 @@
/*
* 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.virtualbox.functions;
import com.google.common.base.Function;
import org.virtualbox_4_1.IMachine;
import javax.annotation.Nullable;
/**
* @author Mattias Holmqvist
*/
public class ApplyMemoryToMachine implements Function<IMachine, Object> {
private long memorySize;
public ApplyMemoryToMachine(long memorySize) {
this.memorySize = memorySize;
}
@Override
public Object apply(@Nullable IMachine machine) {
machine.setMemorySize(memorySize);
machine.saveSettings();
return null;
}
}

View File

@ -105,16 +105,7 @@ public class IsoToIMachine implements Function<String, IMachine> {
String workingDir = System.getProperty(VIRTUALBOX_WORKINGDIR, defaultWorkingDir);
// Change RAM
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override
public Void apply(IMachine machine) {
machine.setMemorySize(1024l);
machine.saveSettings();
return null;
}
});
ensureMachineHasMemory(vmName, 1024l);
// IDE Controller
ensureMachineHasIDEControllerNamed(vmName, controllerIDE);
@ -128,12 +119,8 @@ public class IsoToIMachine implements Function<String, IMachine> {
new File(adminDiskPath).delete();
}
// Create hard disk
IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath);
// Attach hard disk to machine
lockMachineAndApply(manager, Write, vmName,
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager));
ensureMachineHasHardDiskAttached(vmName, adminDiskPath);
// NAT
ensureNATNetworkingIsAppliedToMachine(vmName);
@ -197,6 +184,17 @@ public class IsoToIMachine implements Function<String, IMachine> {
return vm;
}
private void ensureMachineHasHardDiskAttached(String vmName, String path) {
// Create hard disk
IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(path);
lockMachineAndApply(manager, Write, vmName,
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager));
}
private void ensureMachineHasMemory(String vmName, final long memorySize) {
lockMachineAndApply(manager, Write, vmName, new ApplyMemoryToMachine(memorySize));
}
private void ensureNATNetworkingIsAppliedToMachine(String vmName) {
lockMachineAndApply(manager, Write, vmName, new AttachNATRedirectRuleToMachine(0l));
}

View File

@ -0,0 +1,68 @@
/*
* 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.virtualbox.functions;
import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VBoxException;
import static org.easymock.classextension.EasyMock.*;
/**
* @author Mattias Holmqvist
*/
@Test(groups = "unit", testName = "ApplyMemoryToMachineTest")
public class ApplyMemoryToMachineTest {
@Test
public void testSetRAMSizeSuccessful() throws Exception {
long memorySize = 1024l;
IMachine machine = createMock(IMachine.class);
machine.setMemorySize(memorySize);
machine.saveSettings();
replay(machine);
new ApplyMemoryToMachine(memorySize).apply(machine);
verify(machine);
}
@Test(expectedExceptions = VBoxException.class)
public void testRethrowInvalidRamSizeError() throws Exception {
// Mainly here for documentation purposes
final String error = "VirtualBox error: Invalid RAM size: " +
"3567587327 MB (must be in range [4, 2097152] MB) (0x80070057)";
long memorySize = 1024l;
IMachine machine = createMock(IMachine.class);
VBoxException invalidRamSizeException = new VBoxException(createNiceMock(Throwable.class), error);
machine.setMemorySize(memorySize);
expectLastCall().andThrow(invalidRamSizeException);
machine.saveSettings();
replay(machine);
new ApplyMemoryToMachine(memorySize).apply(machine);
}
}