Skip to content

Commit a0eb0aa

Browse files
authored
api,server: fix VM.CREATE events on vm deploy without start (#7421)
Fixes #6697 Allows the server to generate started and completed events for VM.CREATE event type when VM is deployed with startvm=false. Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 8c546bd commit a0eb0aa

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import org.apache.cloudstack.context.CallContext;
5555
import org.apache.commons.collections.CollectionUtils;
5656
import org.apache.commons.collections.MapUtils;
57+
import org.apache.commons.lang3.BooleanUtils;
58+
import org.apache.commons.lang3.StringUtils;
5759
import org.apache.log4j.Logger;
5860

5961
import com.cloud.agent.api.LogLevel;
@@ -75,9 +77,6 @@
7577
import com.cloud.vm.VirtualMachine;
7678
import com.cloud.vm.VmDetailConstants;
7779

78-
import org.apache.commons.lang3.BooleanUtils;
79-
import org.apache.commons.lang3.StringUtils;
80-
8180
@APICommand(name = "deployVirtualMachine", description = "Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted, entityType = {VirtualMachine.class},
8281
requestHasSensitiveInfo = false, responseHasSensitiveInfo = true)
8382
public class DeployVMCmd extends BaseAsyncCreateCustomIdCmd implements SecurityGroupAction, UserCmd {
@@ -766,32 +765,28 @@ public ApiCommandResourceType getApiResourceType() {
766765
public void execute() {
767766
UserVm result;
768767

769-
if (getStartVm()) {
770-
try {
771-
CallContext.current().setEventDetails("Vm Id: " + getEntityUuid());
772-
result = _userVmService.startVirtualMachine(this);
773-
} catch (ResourceUnavailableException ex) {
774-
s_logger.warn("Exception: ", ex);
775-
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
776-
} catch (ResourceAllocationException ex) {
777-
s_logger.warn("Exception: ", ex);
778-
throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
779-
} catch (ConcurrentOperationException ex) {
780-
s_logger.warn("Exception: ", ex);
781-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
782-
} catch (InsufficientCapacityException ex) {
783-
StringBuilder message = new StringBuilder(ex.getMessage());
784-
if (ex instanceof InsufficientServerCapacityException) {
785-
if (((InsufficientServerCapacityException)ex).isAffinityApplied()) {
786-
message.append(", Please check the affinity groups provided, there may not be sufficient capacity to follow them");
787-
}
768+
try {
769+
CallContext.current().setEventDetails("Vm Id: " + getEntityUuid());
770+
result = _userVmService.startVirtualMachine(this);
771+
} catch (ResourceUnavailableException ex) {
772+
s_logger.warn("Exception: ", ex);
773+
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
774+
} catch (ResourceAllocationException ex) {
775+
s_logger.warn("Exception: ", ex);
776+
throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
777+
} catch (ConcurrentOperationException ex) {
778+
s_logger.warn("Exception: ", ex);
779+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
780+
} catch (InsufficientCapacityException ex) {
781+
StringBuilder message = new StringBuilder(ex.getMessage());
782+
if (ex instanceof InsufficientServerCapacityException) {
783+
if (((InsufficientServerCapacityException)ex).isAffinityApplied()) {
784+
message.append(", Please check the affinity groups provided, there may not be sufficient capacity to follow them");
788785
}
789-
s_logger.info(ex);
790-
s_logger.info(message.toString(), ex);
791-
throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, message.toString());
792786
}
793-
} else {
794-
result = _userVmService.getUserVm(getEntityId());
787+
s_logger.info(ex);
788+
s_logger.info(message.toString(), ex);
789+
throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, message.toString());
795790
}
796791

797792
if (result != null) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4810,9 +4810,12 @@ protected String validateUserData(String userData, HTTPMethod httpmethod) {
48104810
}
48114811

48124812
@Override
4813-
@ActionEvent(eventType = EventTypes.EVENT_VM_CREATE, eventDescription = "starting Vm", async = true)
4813+
@ActionEvent(eventType = EventTypes.EVENT_VM_CREATE, eventDescription = "deploying Vm", async = true)
48144814
public UserVm startVirtualMachine(DeployVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException, ConcurrentOperationException, ResourceAllocationException {
48154815
long vmId = cmd.getEntityId();
4816+
if (!cmd.getStartVm()) {
4817+
return getUserVm(vmId);
4818+
}
48164819
Long podId = null;
48174820
Long clusterId = null;
48184821
Long hostId = cmd.getHostId();

0 commit comments

Comments
 (0)