Skip to content

Commit 5388d34

Browse files
committed
CLOUDSTACK-6597: Updatevm - root admin should be allowed to change instance name
1 parent 0e975a7 commit 5388d34

7 files changed

Lines changed: 32 additions & 8 deletions

File tree

api/src/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public class UpdateVMCmd extends BaseCustomIdCmd {
8787
@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")
8888
private String name;
8989

90+
@Parameter(name = ApiConstants.INSTANCE_NAME, type = CommandType.STRING, description = "instance name of the user vm", since = "4.4", authorized = {RoleType.Admin})
91+
private String instanceName;
92+
9093
/////////////////////////////////////////////////////
9194
/////////////////// Accessors ///////////////////////
9295
/////////////////////////////////////////////////////
@@ -123,7 +126,10 @@ public String getHostName() {
123126
return name;
124127
}
125128

126-
/////////////////////////////////////////////////////
129+
public String getInstanceName() {
130+
return instanceName;
131+
}
132+
/////////////////////////////////////////////////////
127133
/////////////// API Implementation///////////////////
128134
/////////////////////////////////////////////////////
129135

engine/schema/src/com/cloud/vm/VMInstanceVO.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ public String getInstanceName() {
281281
return instanceName;
282282
}
283283

284+
// Be very careful to use this. This has to be unique for the vm and if changed should be done by root admin only.
285+
public void setInstanceName(String instanceName) {
286+
this.instanceName = instanceName;
287+
}
288+
284289
@Override
285290
public State getState() {
286291
return state;

engine/schema/src/com/cloud/vm/dao/UserVmDao.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long> {
4444
* @param displayVm updates the displayvm attribute signifying whether it has to be displayed to the end user or not.
4545
* @param customId
4646
* @param hostName TODO
47+
* @param instanceName
4748
*/
48-
void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm, boolean isDynamicallyScalable, String customId, String hostName);
49+
void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm, boolean isDynamicallyScalable, String customId, String hostName, String instanceName);
4950

5051
List<UserVmVO> findDestroyedVms(Date date);
5152

engine/schema/src/com/cloud/vm/dao/UserVmDaoImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ public List<UserVmVO> listByAccountAndDataCenter(long accountId, long dcId) {
216216
}
217217

218218
@Override
219-
public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm, boolean isDynamicallyScalable, String customId, String hostName) {
219+
public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData, boolean displayVm,
220+
boolean isDynamicallyScalable, String customId, String hostName, String instanceName) {
220221
UserVmVO vo = createForUpdate();
221222
vo.setDisplayName(displayName);
222223
vo.setHaEnabled(enable);
@@ -230,6 +231,9 @@ public void updateVM(long id, String displayName, boolean enable, Long osTypeId,
230231
if (customId != null) {
231232
vo.setUuid(customId);
232233
}
234+
if(instanceName != null){
235+
vo.setInstanceName(instanceName);
236+
}
233237

234238
update(id, vo);
235239
}

server/src/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ else if (jobResult instanceof Long) {
14421442
}
14431443

14441444
private void validateRootVolumeDetachAttach(VolumeVO volume, UserVmVO vm) {
1445-
if (!(vm.getHypervisorType() == HypervisorType.XenServer)) {
1445+
if (!(vm.getHypervisorType() == HypervisorType.XenServer || vm.getHypervisorType() == HypervisorType.VMware)) {
14461446
throw new InvalidParameterValueException("Root volume detach is allowed for hypervisor type " + HypervisorType.XenServer + " only");
14471447
}
14481448
if (!(vm.getState() == State.Stopped) || (vm.getState() == State.Destroyed)) {

server/src/com/cloud/vm/UserVmManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ boolean upgradeVirtualMachine(Long id, Long serviceOfferingId, Map<String, Strin
100100
void collectVmDiskStatistics(UserVmVO userVm);
101101

102102
UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData,
103-
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName) throws ResourceUnavailableException, InsufficientCapacityException;
103+
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName, String instanceName) throws ResourceUnavailableException, InsufficientCapacityException;
104104

105105
//the validateCustomParameters, save and remove CustomOfferingDetils functions can be removed from the interface once we can
106106
//find a common place for all the scaling and upgrading code of both user and systemvms.

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,8 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableEx
18601860

18611861
}
18621862

1863-
return updateVirtualMachine(id, displayName, group, ha, isDisplayVm, osTypeId, userData, isDynamicallyScalable, cmd.getHttpMethod(), cmd.getCustomId(), hostName);
1863+
return updateVirtualMachine(id, displayName, group, ha, isDisplayVm, osTypeId, userData, isDynamicallyScalable,
1864+
cmd.getHttpMethod(), cmd.getCustomId(), hostName, cmd.getInstanceName());
18641865
}
18651866

18661867
private void saveUsageEvent(UserVmVO vm) {
@@ -1910,12 +1911,19 @@ private void generateNetworkUsageForVm(VirtualMachine vm, boolean isDisplay, Str
19101911

19111912
@Override
19121913
public UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData,
1913-
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName) throws ResourceUnavailableException, InsufficientCapacityException {
1914+
Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName, String instanceName) throws ResourceUnavailableException, InsufficientCapacityException {
19141915
UserVmVO vm = _vmDao.findById(id);
19151916
if (vm == null) {
19161917
throw new CloudRuntimeException("Unable to find virual machine with id " + id);
19171918
}
19181919

1920+
if(instanceName != null){
1921+
VMInstanceVO vmInstance = _vmInstanceDao.findVMByInstanceName(instanceName);
1922+
if(vmInstance != null && vmInstance.getId() != id){
1923+
throw new CloudRuntimeException("Instance name : " + instanceName + " is not unique");
1924+
}
1925+
}
1926+
19191927
if (vm.getState() == State.Error || vm.getState() == State.Expunging) {
19201928
s_logger.error("vm is not in the right state: " + id);
19211929
throw new InvalidParameterValueException("Vm with id " + id + " is not in the right state");
@@ -1983,7 +1991,7 @@ public UserVm updateVirtualMachine(long id, String displayName, String group, Bo
19831991
checkIfHostNameUniqueInNtwkDomain(hostName, vmNtwks);
19841992
}
19851993

1986-
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName);
1994+
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName, instanceName);
19871995

19881996
if (updateUserdata) {
19891997
boolean result = updateUserDataInternal(_vmDao.findById(id));

0 commit comments

Comments
 (0)