diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java new file mode 100644 index 0000000000..08d2d25668 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java @@ -0,0 +1,67 @@ +/* + * 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.admin; + + +import com.google.common.base.Function; +import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.logging.Logger; +import org.jclouds.virtualbox.domain.ErrorCode; +import org.virtualbox_4_1.CleanupMode; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.VBoxException; +import org.virtualbox_4_1.VirtualBoxManager; + +import javax.annotation.Nullable; +import javax.annotation.Resource; +import javax.inject.Named; + +public class UnregisterMachineIfExists implements Function { + + @Resource + @Named(ComputeServiceConstants.COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; + + private VirtualBoxManager manager; + private CleanupMode mode; + + public UnregisterMachineIfExists(VirtualBoxManager manager, CleanupMode mode) { + this.manager = manager; + this.mode = mode; + } + + @Override + public Void apply(@Nullable String vmName) { + try { + IMachine machine = manager.getVBox().findMachine(vmName); + machine.unregister(mode); + } catch (VBoxException e) { + ErrorCode errorCode = ErrorCode.valueOf(e); + switch (errorCode) { + case VBOX_E_OBJECT_NOT_FOUND: + logger.debug("Machine %s does not exists, cannot unregister", vmName); + break; + default: + throw e; + } + } + return null; + } +} diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java index eb7286d382..0f5d194a21 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java @@ -36,8 +36,12 @@ import org.jclouds.domain.Credentials; import org.jclouds.json.Json; import org.jclouds.json.config.GsonModule; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; +import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.VirtualBoxManager; @@ -69,9 +73,9 @@ public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest { public void setUp() throws Exception { identity = "toor"; credential = "password"; + new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); } - @Test public void testCreateImageMachineFromIso() throws Exception { VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi(); diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java new file mode 100644 index 0000000000..e0c3628c4d --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java @@ -0,0 +1,56 @@ +/* + * 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.admin; + +import org.testng.annotations.Test; +import org.virtualbox_4_1.*; + +import java.util.Collections; +import java.util.List; + +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.expect; +import static org.easymock.classextension.EasyMock.*; + +@Test(groups = "unit", testName = "UnregisterMachineIfExistsTest") +public class UnregisterMachineIfExistsTest { + + @Test + public void testUnregisterExistingMachine() throws Exception { + VirtualBoxManager manager = createMock(VirtualBoxManager.class); + IVirtualBox vBox = createMock(IVirtualBox.class); + IMachine registeredMachine = createMock(IMachine.class); + List mediums = Collections.emptyList(); + + CleanupMode mode = CleanupMode.Full; + String vmName = "jclouds-image-example-machine"; + + expect(manager.getVBox()).andReturn(vBox).anyTimes(); + expect(vBox.findMachine(vmName)).andReturn(registeredMachine); + + expect(registeredMachine.unregister(mode)).andReturn(mediums); + expectLastCall().anyTimes(); + + replay(manager, vBox, registeredMachine); + + new UnregisterMachineIfExists(manager, mode).apply(vmName); + } + +}