diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 466d0ea53c0..5a3b885d08d 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1045,16 +1045,27 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir } NicProfile guestNic = null; + boolean cleanUp = true; try { guestNic = _itMgr.addVmToNetwork(vmInstance, network, profile); + cleanUp = false; } catch (ResourceUnavailableException e) { throw new CloudRuntimeException("Unable to add NIC to " + vmInstance + ": " + e); } catch (InsufficientCapacityException e) { throw new CloudRuntimeException("Insufficient capacity when adding NIC to " + vmInstance + ": " + e); } catch (ConcurrentOperationException e) { throw new CloudRuntimeException("Concurrent operations on adding NIC to " + vmInstance + ": " + e); + } finally { + if(cleanUp) { + try { + _itMgr.removeVmFromNetwork(vmInstance, network, null); + } catch (ResourceUnavailableException e) { + throw new CloudRuntimeException("Error while cleaning up NIC " + e); + } + } } + if (guestNic == null) { throw new CloudRuntimeException("Unable to add NIC to " + vmInstance); }