mirror of https://github.com/apache/jclouds.git
Added AttachHardDiskToMachineIfNotAlreadyAttached and used it from IsoToIMachine.
This commit is contained in:
parent
6d4a605f3f
commit
b64fee01a8
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* *
|
||||
* * 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.*;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.virtualbox_4_1.DeviceType.HardDisk;
|
||||
|
||||
public class AttachHardDiskToMachineIfNotAlreadyAttached implements Function<IMachine, Void> {
|
||||
|
||||
private String controllerIDE;
|
||||
private IMedium hardDisk;
|
||||
private String adminDiskPath;
|
||||
private String diskFormat;
|
||||
private VirtualBoxManager manager;
|
||||
|
||||
public AttachHardDiskToMachineIfNotAlreadyAttached(String controllerIDE, IMedium hardDisk, VirtualBoxManager manager) {
|
||||
this.controllerIDE = controllerIDE;
|
||||
this.hardDisk = hardDisk;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void apply(@Nullable IMachine machine) {
|
||||
|
||||
// Create and attach hard disk
|
||||
int controllerPort = 0;
|
||||
int device = 1;
|
||||
try {
|
||||
machine.attachDevice(controllerIDE, controllerPort, device, HardDisk, hardDisk);
|
||||
machine.saveSettings();
|
||||
} catch (VBoxException e) {
|
||||
if (!alreadyAttached(e))
|
||||
throw e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean alreadyAttached(VBoxException e) {
|
||||
return e.getMessage().indexOf("is already attached to port") != -1;
|
||||
}
|
||||
|
||||
}
|
|
@ -60,7 +60,6 @@ import org.virtualbox_4_1.ISession;
|
|||
import org.virtualbox_4_1.LockType;
|
||||
import org.virtualbox_4_1.VBoxException;
|
||||
import org.virtualbox_4_1.VirtualBoxManager;
|
||||
import org.virtualbox_4_1.jaxws.MediumVariant;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.inject.Inject;
|
||||
|
@ -141,22 +140,14 @@ public class IsoToIMachine implements Function<String, IMachine> {
|
|||
new File(adminDiskPath).delete();
|
||||
}
|
||||
|
||||
// Create and attach hard disk
|
||||
final IMedium hd = manager.getVBox().createHardDisk(diskFormat, adminDiskPath);
|
||||
long size = 4L * 1024L * 1024L * 1024L - 4L;
|
||||
IProgress storageCreation = hd.createBaseStorage(size, (long) MediumVariant.STANDARD.ordinal());
|
||||
IProgress storageCreation = hd.createBaseStorage(size, (long) org.virtualbox_4_1.jaxws.MediumVariant.STANDARD.ordinal());
|
||||
storageCreation.waitForCompletion(-1);
|
||||
|
||||
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
|
||||
|
||||
@Override
|
||||
public Void apply(IMachine machine) {
|
||||
machine.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
|
||||
machine.saveSettings();
|
||||
return null;
|
||||
}
|
||||
|
||||
});
|
||||
// Attach hard disk to machine
|
||||
lockMachineAndApply(manager, Write, vmName,
|
||||
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hd, manager));
|
||||
|
||||
// NAT
|
||||
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
|
||||
|
|
|
@ -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.virtualbox.functions;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
import org.virtualbox_4_1.*;
|
||||
|
||||
import static org.easymock.classextension.EasyMock.*;
|
||||
|
||||
@Test(groups = "unit", testName = "AttachHardDiskToMachineIfNotAlreadyAttachedTest")
|
||||
public class AttachHardDiskToMachineIfNotAlreadyAttachedTest {
|
||||
|
||||
@Test
|
||||
public void testAttachHardDiskIfNotAttached() throws Exception {
|
||||
|
||||
String controllerIDE = "IDE Controller";
|
||||
String adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
|
||||
String diskFormat = "vdi";
|
||||
int controllerPort = 0;
|
||||
int device = 1;
|
||||
|
||||
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
|
||||
IMachine machine = createMock(IMachine.class);
|
||||
IVirtualBox vBox = createMock(IVirtualBox.class);
|
||||
IMedium hardDisk = createNiceMock(IMedium.class);
|
||||
IProgress progress = createNiceMock(IProgress.class);
|
||||
|
||||
expect(manager.getVBox()).andReturn(vBox).anyTimes();
|
||||
expect(vBox.createHardDisk(diskFormat, adminDiskPath)).andReturn(hardDisk);
|
||||
expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
|
||||
|
||||
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
|
||||
machine.saveSettings();
|
||||
replay(manager, machine, vBox, hardDisk);
|
||||
|
||||
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
|
||||
|
||||
verify(machine);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoNothingIfAlreadyAttachedAttachHardDisk() throws Exception {
|
||||
|
||||
String controllerIDE = "IDE Controller";
|
||||
int controllerPort = 0;
|
||||
int device = 1;
|
||||
|
||||
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
|
||||
IMachine machine = createMock(IMachine.class);
|
||||
IVirtualBox vBox = createMock(IVirtualBox.class);
|
||||
IMedium hardDisk = createNiceMock(IMedium.class);
|
||||
|
||||
final StringBuilder errorBuilder = new StringBuilder();
|
||||
errorBuilder.append("VirtualBox error: ");
|
||||
errorBuilder.append("Medium '/Users/mattias/jclouds-virtualbox-test/testadmin.vdi' ");
|
||||
errorBuilder.append("is already attached to port 0, device 1 of controller 'IDE Controller' ");
|
||||
errorBuilder.append("of this virtual machine (0x80BB000C)");
|
||||
String isoAlreadyAttachedException = errorBuilder.toString();
|
||||
|
||||
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
|
||||
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
|
||||
expectLastCall().andThrow(isoAttachedException);
|
||||
|
||||
replay(manager, machine, vBox, hardDisk);
|
||||
|
||||
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
|
||||
|
||||
verify(machine);
|
||||
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = VBoxException.class)
|
||||
public void testFailOnOtherVBoxError() throws Exception {
|
||||
|
||||
String controllerIDE = "IDE Controller";
|
||||
int controllerPort = 0;
|
||||
int device = 1;
|
||||
|
||||
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
|
||||
IMachine machine = createMock(IMachine.class);
|
||||
IVirtualBox vBox = createMock(IVirtualBox.class);
|
||||
IMedium hardDisk = createNiceMock(IMedium.class);
|
||||
|
||||
final StringBuilder errorBuilder = new StringBuilder();
|
||||
errorBuilder.append("VirtualBox error: ");
|
||||
errorBuilder.append("Some other VBox error");
|
||||
String isoAlreadyAttachedException = errorBuilder.toString();
|
||||
|
||||
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
|
||||
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.HardDisk, hardDisk);
|
||||
expectLastCall().andThrow(isoAttachedException);
|
||||
|
||||
replay(manager, machine, vBox, hardDisk);
|
||||
|
||||
new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager).apply(machine);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue