CLOUDSTACK-6597: Updatevm - root admin should be allowed to change instance name

This commit is contained in:
Nitin Mehta 2014-05-07 14:34:01 -07:00
parent 0e975a773a
commit 5388d349a4
7 changed files with 32 additions and 8 deletions

View File

@ -87,6 +87,9 @@ public class UpdateVMCmd extends BaseCustomIdCmd {
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "new host name of the vm. The VM has to be stopped/started for this update to take affect", since = "4.4")
private String name;
@Parameter(name = ApiConstants.INSTANCE_NAME, type = CommandType.STRING, description = "instance name of the user vm", since = "4.4", authorized = {RoleType.Admin})
private String instanceName;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -123,7 +126,10 @@ public class UpdateVMCmd extends BaseCustomIdCmd {
return name;
}
/////////////////////////////////////////////////////
public String getInstanceName() {
return instanceName;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

View File

@ -281,6 +281,11 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
return instanceName;
}
// Be very careful to use this. This has to be unique for the vm and if changed should be done by root admin only.
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
@Override
public State getState() {
return state;

View File

@ -44,8 +44,9 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long> {
* @param displayVm updates the displayvm attribute signifying whether it has to be displayed to the end user or not.
* @param customId
* @param hostName TODO
* @param instanceName
*/
void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm, boolean isDynamicallyScalable, String customId, String hostName);
void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm, boolean isDynamicallyScalable, String customId, String hostName, String instanceName);
List<UserVmVO> findDestroyedVms(Date date);

View File

@ -216,7 +216,8 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
}
@Override
public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm, boolean isDynamicallyScalable, String customId, String hostName) {
public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm,
boolean isDynamicallyScalable, String customId, String hostName, String instanceName) {
UserVmVO vo = createForUpdate();
vo.setDisplayName(displayName);
vo.setHaEnabled(enable);
@ -230,6 +231,9 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
if (customId != null) {
vo.setUuid(customId);
}
if(instanceName != null){
vo.setInstanceName(instanceName);
}
update(id, vo);
}

View File

@ -1442,7 +1442,7 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic
}
private void validateRootVolumeDetachAttach(VolumeVO volume, UserVmVO vm) {
if (!(vm.getHypervisorType() == HypervisorType.XenServer)) {
if (!(vm.getHypervisorType() == HypervisorType.XenServer || vm.getHypervisorType() == HypervisorType.VMware)) {
throw new InvalidParameterValueException("Root volume detach is allowed for hypervisor type " + HypervisorType.XenServer + " only");
}
if (!(vm.getState() == State.Stopped) || (vm.getState() == State.Destroyed)) {

View File

@ -100,7 +100,7 @@ public interface UserVmManager extends UserVmService {
void collectVmDiskStatistics(UserVmVO userVm);
UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData,
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName) throws ResourceUnavailableException, InsufficientCapacityException;
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName, String instanceName) throws ResourceUnavailableException, InsufficientCapacityException;
//the validateCustomParameters, save and remove CustomOfferingDetils functions can be removed from the interface once we can
//find a common place for all the scaling and upgrading code of both user and systemvms.

View File

@ -1860,7 +1860,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
return updateVirtualMachine(id, displayName, group, ha, isDisplayVm, osTypeId, userData, isDynamicallyScalable, cmd.getHttpMethod(), cmd.getCustomId(), hostName);
return updateVirtualMachine(id, displayName, group, ha, isDisplayVm, osTypeId, userData, isDynamicallyScalable,
cmd.getHttpMethod(), cmd.getCustomId(), hostName, cmd.getInstanceName());
}
private void saveUsageEvent(UserVmVO vm) {
@ -1910,12 +1911,19 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
@Override
public UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData,
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName) throws ResourceUnavailableException, InsufficientCapacityException {
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName, String instanceName) throws ResourceUnavailableException, InsufficientCapacityException {
UserVmVO vm = _vmDao.findById(id);
if (vm == null) {
throw new CloudRuntimeException("Unable to find virual machine with id " + id);
}
if(instanceName != null){
VMInstanceVO vmInstance = _vmInstanceDao.findVMByInstanceName(instanceName);
if(vmInstance != null && vmInstance.getId() != id){
throw new CloudRuntimeException("Instance name : " + instanceName + " is not unique");
}
}
if (vm.getState() == State.Error || vm.getState() == State.Expunging) {
s_logger.error("vm is not in the right state: " + id);
throw new InvalidParameterValueException("Vm with id " + id + " is not in the right state");
@ -1983,7 +1991,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
checkIfHostNameUniqueInNtwkDomain(hostName, vmNtwks);
}
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName);
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName, instanceName);
if (updateUserdata) {
boolean result = updateUserDataInternal(_vmDao.findById(id));