Skip to content

Commit b0d2a9d

Browse files
committed
Fix VM import DB sequence issue on import failure
1 parent 0cbebbd commit b0d2a9d

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import javax.xml.parsers.ParserConfigurationException;
5656

5757
import com.cloud.network.NetworkService;
58+
import com.cloud.network.addr.PublicIp;
59+
import com.cloud.utils.db.TransactionCallback;
5860
import org.apache.cloudstack.acl.ControlledEntity;
5961
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
6062
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -8995,28 +8997,30 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach
89958997
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKeys,
89968998
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
89978999
final VirtualMachine.PowerState powerState, final LinkedHashMap<String, List<NicProfile>> networkNicMap) throws InsufficientCapacityException {
8998-
if (zone == null) {
8999-
throw new InvalidParameterValueException("Unable to import virtual machine with invalid zone");
9000-
}
9001-
if (host == null && hypervisorType == HypervisorType.VMware) {
9002-
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
9003-
}
9000+
return Transaction.execute((TransactionCallbackWithException<UserVm, InsufficientCapacityException>) status -> {
9001+
if (zone == null) {
9002+
throw new InvalidParameterValueException("Unable to import virtual machine with invalid zone");
9003+
}
9004+
if (host == null && hypervisorType == HypervisorType.VMware) {
9005+
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
9006+
}
90049007

9005-
final long id = _vmDao.getNextInSequence(Long.class, "id");
9008+
final long id = _vmDao.getNextInSequence(Long.class, "id");
90069009

9007-
if (hostName != null) {
9008-
// Check is hostName is RFC compliant
9009-
checkNameForRFCCompliance(hostName);
9010-
}
9010+
if (hostName != null) {
9011+
// Check is hostName is RFC compliant
9012+
checkNameForRFCCompliance(hostName);
9013+
}
90119014

9012-
final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
9013-
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
9014-
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
9015-
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
9016-
null, null, userData, null, null, isDisplayVm, keyboard,
9017-
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
9018-
id, instanceName, uuidName, hypervisorType, customParameters,
9019-
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null);
9015+
final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
9016+
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
9017+
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
9018+
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
9019+
null, null, userData, null, null, isDisplayVm, keyboard,
9020+
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
9021+
id, instanceName, uuidName, hypervisorType, customParameters,
9022+
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null);
9023+
});
90209024
}
90219025

90229026
@Override

0 commit comments

Comments
 (0)