diff --git a/server/src/com/cloud/api/commands/RecoverVMCmd.java b/server/src/com/cloud/api/commands/RecoverVMCmd.java index 0a65792f4d5..f7a7c2fe5ac 100644 --- a/server/src/com/cloud/api/commands/RecoverVMCmd.java +++ b/server/src/com/cloud/api/commands/RecoverVMCmd.java @@ -18,31 +18,18 @@ package com.cloud.api.commands; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.exception.InternalErrorException; -import com.cloud.exception.ResourceAllocationException; -import com.cloud.user.Account; -import com.cloud.utils.Pair; -import com.cloud.vm.UserVmVO; - +import com.cloud.api.BaseCmd.Manager; + +@Implementation(method="recoverVirtualMachine", manager=Manager.UserVmManager) public class RecoverVMCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(RecoverVMCmd.class.getName()); private static final String s_name = "recovervirtualmachineresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -65,44 +52,46 @@ public class RecoverVMCmd extends BaseCmd { public String getName() { return s_name; - } + } + + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - - //if account is removed, return error - if(account!=null && account.getRemoved() != null) - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); - - // Verify input parameters - UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue()); - if (vmInstance == null) { - throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); - } - - if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) { - // the domain in which the VM lives is not in the admin's domain tree - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to recover virtual machine with id " + vmId + ", invalid id given."); - } - - try { - boolean success = getManagementServer().recoverVirtualMachine(vmId.longValue()); - if (success == false) { - throw new ServerApiException(BaseCmd.VM_RECOVER_ERROR, "unable to recover virtual machine with id " + vmId.toString()); - } - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(success).toString())); - return returnValues; - } catch (ResourceAllocationException ex) { - throw new ServerApiException(BaseCmd.VM_RECOVER_ERROR, "Failed to recover virtual machine with id " + vmId + "; " + ex.getMessage()); - } catch (InternalErrorException e) { - throw new ServerApiException(BaseCmd.VM_RECOVER_ERROR, "Failed to recover virtual machine with id " + vmId + "; " + e.getMessage()); - } - } +// @Override +// public List> execute(Map params) { +// Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// +// //if account is removed, return error +// if(account!=null && account.getRemoved() != null) +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); +// +// // Verify input parameters +// UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue()); +// if (vmInstance == null) { +// throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); +// } +// +// if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) { +// // the domain in which the VM lives is not in the admin's domain tree +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to recover virtual machine with id " + vmId + ", invalid id given."); +// } +// +// try { +// boolean success = getManagementServer().recoverVirtualMachine(vmId.longValue()); +// if (success == false) { +// throw new ServerApiException(BaseCmd.VM_RECOVER_ERROR, "unable to recover virtual machine with id " + vmId.toString()); +// } +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(success).toString())); +// return returnValues; +// } catch (ResourceAllocationException ex) { +// throw new ServerApiException(BaseCmd.VM_RECOVER_ERROR, "Failed to recover virtual machine with id " + vmId + "; " + ex.getMessage()); +// } catch (InternalErrorException e) { +// throw new ServerApiException(BaseCmd.VM_RECOVER_ERROR, "Failed to recover virtual machine with id " + vmId + "; " + e.getMessage()); +// } +// } } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 330aa6dbd4b..cb50567ae77 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -18,12 +18,7 @@ package com.cloud.server; import java.math.BigInteger; -import java.net.Inet6Address; -import java.net.InetAddress; -import java.net.URI; -import java.net.URISyntaxException; import java.net.URLEncoder; -import java.net.UnknownHostException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -88,7 +83,6 @@ import com.cloud.api.commands.StopSystemVmCmd; import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateDomainCmd; import com.cloud.api.commands.UpdateIsoPermissionsCmd; -import com.cloud.api.commands.UpdateTemplateCmd; import com.cloud.api.commands.UpdateTemplateOrIsoCmd; import com.cloud.api.commands.UpdateTemplateOrIsoPermissionsCmd; import com.cloud.api.commands.UpdateTemplatePermissionsCmd; @@ -112,19 +106,18 @@ import com.cloud.async.executor.DeployVMParam; import com.cloud.async.executor.DisassociateIpAddressParam; import com.cloud.async.executor.LoadBalancerParam; import com.cloud.async.executor.NetworkGroupIngressParam; -import com.cloud.async.executor.ResetVMPasswordParam; import com.cloud.async.executor.SecurityGroupParam; import com.cloud.async.executor.UpdateLoadBalancerParam; import com.cloud.async.executor.VMOperationParam; -import com.cloud.async.executor.VMOperationParam.VmOp; import com.cloud.async.executor.VolumeOperationParam; +import com.cloud.async.executor.VMOperationParam.VmOp; import com.cloud.async.executor.VolumeOperationParam.VolumeOp; import com.cloud.capacity.CapacityVO; import com.cloud.capacity.dao.CapacityDao; import com.cloud.configuration.ConfigurationManager; import com.cloud.configuration.ConfigurationVO; -import com.cloud.configuration.ResourceCount.ResourceType; import com.cloud.configuration.ResourceLimitVO; +import com.cloud.configuration.ResourceCount.ResourceType; import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.configuration.dao.ResourceLimitDao; import com.cloud.consoleproxy.ConsoleProxyManager; @@ -134,8 +127,8 @@ import com.cloud.dc.DataCenterIpAddressVO; import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; import com.cloud.dc.PodVlanMapVO; -import com.cloud.dc.Vlan.VlanType; import com.cloud.dc.VlanVO; +import com.cloud.dc.Vlan.VlanType; import com.cloud.dc.dao.AccountVlanMapDao; import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; @@ -151,7 +144,6 @@ import com.cloud.event.EventVO; import com.cloud.event.dao.EventDao; import com.cloud.exception.AgentUnavailableException; import com.cloud.exception.ConcurrentOperationException; -import com.cloud.exception.DiscoveryException; import com.cloud.exception.InsufficientAddressCapacityException; import com.cloud.exception.InsufficientStorageCapacityException; import com.cloud.exception.InternalErrorException; @@ -161,7 +153,6 @@ import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceInUseException; import com.cloud.exception.StorageUnavailableException; -import com.cloud.host.Host; import com.cloud.host.HostStats; import com.cloud.host.HostVO; import com.cloud.host.Status; @@ -201,22 +192,21 @@ import com.cloud.storage.GuestOSCategoryVO; import com.cloud.storage.GuestOSVO; import com.cloud.storage.LaunchPermissionVO; import com.cloud.storage.Snapshot; -import com.cloud.storage.Snapshot.SnapshotType; import com.cloud.storage.SnapshotPolicyVO; import com.cloud.storage.SnapshotScheduleVO; import com.cloud.storage.SnapshotVO; import com.cloud.storage.Storage; -import com.cloud.storage.Storage.FileSystem; -import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.StorageManager; import com.cloud.storage.StoragePoolVO; import com.cloud.storage.StorageStats; import com.cloud.storage.VMTemplateHostVO; import com.cloud.storage.VMTemplateStorageResourceAssoc; import com.cloud.storage.VMTemplateVO; -import com.cloud.storage.Volume.VolumeType; import com.cloud.storage.VolumeStats; import com.cloud.storage.VolumeVO; +import com.cloud.storage.Snapshot.SnapshotType; +import com.cloud.storage.Storage.ImageFormat; +import com.cloud.storage.Volume.VolumeType; import com.cloud.storage.dao.DiskOfferingDao; import com.cloud.storage.dao.DiskTemplateDao; import com.cloud.storage.dao.GuestOSCategoryDao; @@ -226,9 +216,9 @@ import com.cloud.storage.dao.SnapshotDao; import com.cloud.storage.dao.SnapshotPolicyDao; import com.cloud.storage.dao.StoragePoolDao; import com.cloud.storage.dao.VMTemplateDao; -import com.cloud.storage.dao.VMTemplateDao.TemplateFilter; import com.cloud.storage.dao.VMTemplateHostDao; import com.cloud.storage.dao.VolumeDao; +import com.cloud.storage.dao.VMTemplateDao.TemplateFilter; import com.cloud.storage.preallocatedlun.PreallocatedLunVO; import com.cloud.storage.preallocatedlun.dao.PreallocatedLunDao; import com.cloud.storage.secondary.SecondaryStorageVmManager; @@ -250,12 +240,12 @@ import com.cloud.user.dao.UserDao; import com.cloud.user.dao.UserStatisticsDao; import com.cloud.uservm.UserVm; import com.cloud.utils.DateUtil; -import com.cloud.utils.DateUtil.IntervalType; import com.cloud.utils.EnumUtils; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; import com.cloud.utils.PasswordGenerator; import com.cloud.utils.StringUtils; +import com.cloud.utils.DateUtil.IntervalType; import com.cloud.utils.component.Adapters; import com.cloud.utils.component.ComponentLocator; import com.cloud.utils.concurrency.NamedThreadFactory; diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java index 6fcdf556ede..03947bc4801 100644 --- a/server/src/com/cloud/vm/UserVmManager.java +++ b/server/src/com/cloud/vm/UserVmManager.java @@ -22,6 +22,7 @@ import java.util.List; import com.cloud.agent.api.VmStatsEntry; import com.cloud.api.ServerApiException; +import com.cloud.api.commands.RecoverVMCmd; import com.cloud.api.commands.ResetVMPasswordCmd; import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StopVMCmd; @@ -193,7 +194,7 @@ public interface UserVmManager extends Manager, VirtualMachineManager boolean rebootVirtualMachine(long userId, long vmId); OperationResponse executeRebootVM(RebootVMExecutor executor, VMOperationParam param); - boolean recoverVirtualMachine(long vmId) throws ResourceAllocationException, InternalErrorException; + boolean recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, InternalErrorException; VMTemplateVO createPrivateTemplateRecord(Long userId, long vmId, String name, String description, long guestOsId, Boolean requiresHvm, Integer bits, Boolean passwordEnabled, boolean isPublic, boolean featured) throws InvalidParameterValueException; diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index d0d38f22ed8..5dfa36cad0d 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -66,6 +66,7 @@ import com.cloud.agent.manager.AgentManager; import com.cloud.alert.AlertManager; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; +import com.cloud.api.commands.RecoverVMCmd; import com.cloud.api.commands.ResetVMPasswordCmd; import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StopVMCmd; @@ -242,7 +243,6 @@ public class UserVmManagerImpl implements UserVmManager { @Inject NetworkGroupManager _networkGroupManager; @Inject ServiceOfferingDao _serviceOfferingDao; @Inject EventDao _eventDao = null; - @Inject UserVmDao _userVmDao = null; private IpAddrAllocator _IpAllocator; ScheduledExecutorService _executor = null; @@ -278,7 +278,7 @@ public class UserVmManagerImpl implements UserVmManager { boolean result = resetVMPasswordInternal(cmd); // Log event - UserVmVO userVm = _userVmDao.findById(cmd.getId()); + UserVmVO userVm = _vmDao.findById(cmd.getId()); if (userVm != null) { if (result) { EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_RESETPASSWORD, "successfully reset password for VM : " + userVm.getName(), null); @@ -302,7 +302,7 @@ public class UserVmManagerImpl implements UserVmManager { String password = null; //Verify input parameters - UserVmVO vmInstance = _userVmDao.findById(id.longValue()); + UserVmVO vmInstance = _vmDao.findById(id.longValue()); if (vmInstance == null) { throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id); } @@ -1169,7 +1169,7 @@ public class UserVmManagerImpl implements UserVmManager { // Verify input parameters - UserVmVO vmInstance = _userVmDao.createForUpdate(virtualMachineId.longValue()); + UserVmVO vmInstance = _vmDao.createForUpdate(virtualMachineId.longValue()); if (vmInstance == null) { throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + virtualMachineId); } @@ -1866,10 +1866,28 @@ public class UserVmManagerImpl implements UserVmManager { } @Override @DB - public boolean recoverVirtualMachine(long vmId) throws ResourceAllocationException, InternalErrorException { - UserVmVO vm = _vmDao.findById(vmId); + public boolean recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, InternalErrorException { + + Long vmId = cmd.getId(); + Account accountHandle = (Account)UserContext.current().getAccountObject(); + + //if account is removed, return error + if(accountHandle!=null && accountHandle.getRemoved() != null) + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + accountHandle.getId()+" is removed"); + + // Verify input parameters + UserVmVO vm = _vmDao.findById(vmId.longValue()); - if (vm == null || vm.getRemoved() != null) { + if (vm == null) { + throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); + } + + if ((accountHandle != null) && !_domainDao.isChildDomain(accountHandle.getDomainId(), vm.getDomainId())) { + // the domain in which the VM lives is not in the admin's domain tree + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to recover virtual machine with id " + vmId + ", invalid id given."); + } + + if (vm.getRemoved() != null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to find vm or vm is removed: " + vmId); } @@ -3132,7 +3150,7 @@ public class UserVmManagerImpl implements UserVmManager { // Verify input parameters try { - vmInstance = _userVmDao.findById(id.longValue()); + vmInstance = _vmDao.findById(id.longValue()); } catch (Exception ex1) { @@ -3160,13 +3178,13 @@ public class UserVmManagerImpl implements UserVmManager { long accountId = vmInstance.getAccountId(); - UserVmVO vm = _userVmDao.findById(id); + UserVmVO vm = _vmDao.findById(id); if (vm == null) { throw new CloudRuntimeException("Unable to find virual machine with id " + id); } boolean haEnabled = vm.isHaEnabled(); - _userVmDao.updateVM(id, displayName, group, ha); + _vmDao.updateVM(id, displayName, group, ha); if (haEnabled != ha) { String description = null; String type = null; @@ -3197,7 +3215,7 @@ public class UserVmManagerImpl implements UserVmManager { if(account!=null && account.getRemoved() != null) throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); - UserVmVO vmInstance = _userVmDao.findById(id.longValue()); + UserVmVO vmInstance = _vmDao.findById(id.longValue()); if (vmInstance == null) { throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id); } @@ -3220,7 +3238,7 @@ public class UserVmManagerImpl implements UserVmManager { if(account!=null && account.getRemoved() != null) throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); - UserVmVO vmInstance = _userVmDao.findById(id.longValue()); + UserVmVO vmInstance = _vmDao.findById(id.longValue()); if (vmInstance == null) { throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id); }