diff --git a/api/src/com/cloud/api/commands/UpdateHostCmd.java b/api/src/com/cloud/api/commands/UpdateHostCmd.java old mode 100644 new mode 100755 index 8648e1164d3..a1901490bce --- a/api/src/com/cloud/api/commands/UpdateHostCmd.java +++ b/api/src/com/cloud/api/commands/UpdateHostCmd.java @@ -32,6 +32,7 @@ import com.cloud.api.BaseCmd.CommandType; import com.cloud.api.response.HostResponse; import com.cloud.host.Host; import com.cloud.user.Account; +import com.cloud.utils.fsm.NoTransitionException; @Implementation(description="Updates a host.", responseObject=HostResponse.class) public class UpdateHostCmd extends BaseCmd { @@ -50,7 +51,7 @@ public class UpdateHostCmd extends BaseCmd { @Parameter(name=ApiConstants.OS_CATEGORY_ID, type=CommandType.LONG, description="the id of Os category to update the host with") private Long osCategoryId; - @Parameter(name=ApiConstants.ALLOCATION_STATE, type=CommandType.STRING, description="Allocation state of this Host for allocation of new resources") + @Parameter(name=ApiConstants.ALLOCATION_STATE, type=CommandType.STRING, description="Change resource state of host, valid values are [Enable, Disable]. Operation may failed if host in states not allowing Enable/Disable") private String allocationState; @Parameter(name=ApiConstants.HOST_TAGS, type=CommandType.LIST, collectionType=CommandType.STRING, description="list of tags to be added to the host") @@ -103,13 +104,15 @@ public class UpdateHostCmd extends BaseCmd { @Override public void execute(){ - Host result = _resourceService.updateHost(this); - if (result != null) { + Host result; + try { + result = _resourceService.updateHost(this); HostResponse hostResponse = _responseGenerator.createHostResponse(result); hostResponse.setResponseName(getCommandName()); this.setResponseObject(hostResponse); - } else { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update host"); - } + } catch (Exception e) { + s_logger.debug("Failed to update host:" + getId(), e); + throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update host:" + getId() + "," + e.getMessage()); + } } } diff --git a/api/src/com/cloud/resource/ResourceService.java b/api/src/com/cloud/resource/ResourceService.java index 3cd544f4d5d..51ab776b060 100755 --- a/api/src/com/cloud/resource/ResourceService.java +++ b/api/src/com/cloud/resource/ResourceService.java @@ -36,6 +36,7 @@ import com.cloud.host.Host; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.org.Cluster; import com.cloud.storage.Swift; +import com.cloud.utils.fsm.NoTransitionException; public interface ResourceService { /** @@ -44,8 +45,9 @@ public interface ResourceService { * @param cmd * - the command specifying hostId * @return hostObject + * @throws NoTransitionException */ - Host updateHost(UpdateHostCmd cmd); + Host updateHost(UpdateHostCmd cmd) throws NoTransitionException; Host cancelMaintenance(CancelMaintenanceCmd cmd); diff --git a/api/src/com/cloud/resource/ResourceState.java b/api/src/com/cloud/resource/ResourceState.java index ecdcfa069c5..71888da8e2b 100755 --- a/api/src/com/cloud/resource/ResourceState.java +++ b/api/src/com/cloud/resource/ResourceState.java @@ -3,6 +3,7 @@ package com.cloud.resource; import java.util.List; import java.util.Set; +import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.StateMachine; public enum ResourceState { @@ -40,6 +41,16 @@ public enum ResourceState { public String getDescription() { return this.comment; } + + public static Event toEvent(String e) { + if (Enable.toString().equals(e)) { + return Enable; + } else if (Disable.toString().equals(e)) { + return Disable; + } + + return null; + } } public ResourceState getNextState(Event a) { diff --git a/server/src/com/cloud/resource/ResourceManagerImpl.java b/server/src/com/cloud/resource/ResourceManagerImpl.java index acb22213ba1..f4ab83c9b6d 100755 --- a/server/src/com/cloud/resource/ResourceManagerImpl.java +++ b/server/src/com/cloud/resource/ResourceManagerImpl.java @@ -1119,18 +1119,26 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma } @Override - public Host updateHost(UpdateHostCmd cmd) { + public Host updateHost(UpdateHostCmd cmd) throws NoTransitionException { Long hostId = cmd.getId(); Long guestOSCategoryId = cmd.getOsCategoryId(); + // Verify that the host exists + HostVO host = _hostDao.findById(hostId); + if (host == null) { + throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist"); + } + + if (cmd.getAllocationState() != null) { + ResourceState.Event resourceEvent = ResourceState.Event.toEvent(cmd.getAllocationState()); + if (resourceEvent != ResourceState.Event.Enable && resourceEvent != ResourceState.Event.Disable) { + throw new CloudRuntimeException("Invalid allocation state:" + cmd.getAllocationState() + ", only Enable/Disable are allowed"); + } + + resourceStateTransitTo(host, resourceEvent, _nodeId); + } + if (guestOSCategoryId != null) { - - // Verify that the host exists - HostVO host = _hostDao.findById(hostId); - if (host == null) { - throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist"); - } - // Verify that the guest OS Category exists if (guestOSCategoryId > 0) { if (_guestOSCategoryDao.findById(guestOSCategoryId) == null) { @@ -1150,40 +1158,9 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma } _hostDetailsDao.persist(hostId, hostDetails); } - - /* - String allocationState = cmd.getAllocationState(); - if (allocationState != null) { - // Verify that the host exists - HostVO host = _hostDao.findById(hostId); - if (host == null) { - throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist"); - } - - try { - HostAllocationState newAllocationState = Host.HostAllocationState.valueOf(allocationState); - if (newAllocationState == null) { - s_logger.error("Unable to resolve " + allocationState + " to a valid supported allocation State"); - throw new InvalidParameterValueException("Unable to resolve " + allocationState + " to a supported state"); - } else { - host.setHostAllocationState(newAllocationState); - } - } catch (IllegalArgumentException ex) { - s_logger.error("Unable to resolve " + allocationState + " to a valid supported allocation State"); - throw new InvalidParameterValueException("Unable to resolve " + allocationState + " to a supported state"); - } - - _hostDao.update(hostId, host); - } - */ List hostTags = cmd.getHostTags(); if (hostTags != null) { - // Verify that the host exists - HostVO host = _hostDao.findById(hostId); - if (host == null) { - throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist"); - } if(s_logger.isDebugEnabled()){ s_logger.debug("Updating Host Tags to :"+hostTags); }