Skip to content

Commit abb37ac

Browse files
author
Abhinandan Prateek
committed
bug 8962: added usage related and resource count related code, changed the API name as suggested. Incorporated relevant review comments
1 parent 932e3c3 commit abb37ac

4 files changed

Lines changed: 35 additions & 121 deletions

File tree

‎api/src/com/cloud/api/commands/MoveUserVMCmd.java‎

Lines changed: 0 additions & 109 deletions
This file was deleted.

‎api/src/com/cloud/vm/UserVmService.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import javax.naming.InsufficientResourcesException;
2424

25+
import com.cloud.api.commands.AssignVMCmd;
2526
import com.cloud.api.commands.AttachVolumeCmd;
2627
import com.cloud.api.commands.CreateTemplateCmd;
2728
import com.cloud.api.commands.CreateVMGroupCmd;
@@ -30,7 +31,6 @@
3031
import com.cloud.api.commands.DestroyVMCmd;
3132
import com.cloud.api.commands.DetachVolumeCmd;
3233
import com.cloud.api.commands.ListVMsCmd;
33-
import com.cloud.api.commands.MoveUserVMCmd;
3434
import com.cloud.api.commands.RebootVMCmd;
3535
import com.cloud.api.commands.RecoverVMCmd;
3636
import com.cloud.api.commands.ResetVMPasswordCmd;
@@ -378,7 +378,7 @@ UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityExceptio
378378
*/
379379
VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException;
380380

381-
UserVm moveVMToUser(MoveUserVMCmd moveUserVMCmd) throws ResourceAllocationException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException ;
381+
UserVm moveVMToUser(AssignVMCmd moveUserVMCmd) throws ResourceAllocationException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException ;
382382

383383
VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool);
384384

‎client/tomcatconf/commands.properties.in‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ recoverVirtualMachine=com.cloud.api.commands.RecoverVMCmd;7
4848
listVirtualMachines=com.cloud.api.commands.ListVMsCmd;15
4949
getVMPassword=com.cloud.api.commands.GetVMPasswordCmd;15
5050
migrateVirtualMachine=com.cloud.api.commands.MigrateVMCmd;1
51-
moveVirtualMachine=com.cloud.api.commands.MoveUserVMCmd;15
51+
assignVirtualMachine=com.cloud.api.commands.AssignVMCmd;15
5252
restoreVm=com.cloud.api.commands.RestoreVMCmd;15
5353

5454
#### snapshot commands

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import com.cloud.api.commands.DestroyVMCmd;
6464
import com.cloud.api.commands.DetachVolumeCmd;
6565
import com.cloud.api.commands.ListVMsCmd;
66-
import com.cloud.api.commands.MoveUserVMCmd;
66+
import com.cloud.api.commands.AssignVMCmd;
6767
import com.cloud.api.commands.RebootVMCmd;
6868
import com.cloud.api.commands.RecoverVMCmd;
6969
import com.cloud.api.commands.ResetVMPasswordCmd;
@@ -3290,17 +3290,17 @@ public VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) thr
32903290
return migratedVm;
32913291
}
32923292

3293-
3293+
@DB
32943294
@Override
32953295
@ActionEvent(eventType = EventTypes.EVENT_VM_MOVE, eventDescription = "move VM to another user", async = false)
3296-
public UserVm moveVMToUser(MoveUserVMCmd cmd) throws ResourceAllocationException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
3296+
public UserVm moveVMToUser(AssignVMCmd cmd) throws ResourceAllocationException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
32973297
// VERIFICATIONS and VALIDATIONS
32983298

32993299
//VV 1: verify the two users
33003300
Account oldAccount = UserContext.current().getCaller();
33013301
Account newAccount = _accountService.getAccount(cmd.getAccountId());
33023302
if (newAccount == null || newAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) {
3303-
throw new InvalidParameterValueException("Unable to find account " + newAccount + " in domain " + oldAccount.getDomainId());
3303+
throw new InvalidParameterValueException("Invalid accountid=" + cmd.getAccountId() + " in domain " + oldAccount.getDomainId());
33043304
}
33053305

33063306
//don't allow to move the vm from the project
@@ -3314,8 +3314,9 @@ public UserVm moveVMToUser(MoveUserVMCmd cmd) throws ResourceAllocationException
33143314
//get the VM
33153315
UserVmVO vm = _vmDao.findById(cmd.getVmId());
33163316

3317-
// VV 3: check if vm is running
3318-
if (vm.getState() == State.Running) {
3317+
if (vm == null){
3318+
throw new InvalidParameterValueException("There is no vm by that id " + cmd.getVmId());
3319+
} else if (vm.getState() == State.Running) { // VV 3: check if vm is running
33193320
if (s_logger.isDebugEnabled()) {
33203321
s_logger.debug("VM is Running, unable to move the vm " + vm);
33213322
}
@@ -3334,21 +3335,41 @@ public UserVm moveVMToUser(MoveUserVMCmd cmd) throws ResourceAllocationException
33343335
_accountMgr.checkAccess(newAccount, domain);
33353336

33363337
DataCenterVO zone = _dcDao.findById(vm.getDataCenterIdToDeployIn());
3338+
3339+
//check is zone networking is advanced
3340+
if (zone.getNetworkType() != NetworkType.Advanced) {
3341+
throw new InvalidParameterValueException("Assing virtual machine to another account is only available for advanced networking " + vm);
3342+
}
3343+
33373344
VMInstanceVO vmoi = _itMgr.findByIdAndType(vm.getType(), vm.getId());
33383345
VirtualMachineProfileImpl<VMInstanceVO> vmOldProfile = new VirtualMachineProfileImpl<VMInstanceVO>(vmoi);
33393346

3340-
3347+
Transaction txn = Transaction.currentTxn();
3348+
txn.start();
3349+
//generate destory vm event for usage
3350+
_usageEventDao.persist(new UsageEventVO(EventTypes.EVENT_VM_DESTROY, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(),
3351+
vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString()));
3352+
// update resource counts
3353+
_resourceLimitMgr.decrementResourceCount(vm.getAccountId(), ResourceType.user_vm);
3354+
33413355
// OWNERSHIP STEP 1: update the vm owner
33423356
vm.setAccountId(newAccount.getAccountId());
3343-
vm.setAccountId(newAccount.getId());
33443357
_vmDao.persist(vm);
3345-
33463358
// OS 2: update volume
33473359
List<VolumeVO> volumes = _volsDao.findByInstance(cmd.getVmId());
33483360
for (VolumeVO volume : volumes) {
33493361
volume.setAccountId(cmd.getAccountId());
3362+
_resourceLimitMgr.decrementResourceCount(vm.getAccountId(), ResourceType.volume, new Long(volumes.size()));
33503363
_volsDao.persist(volume);
3364+
_resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.volume, new Long(volumes.size()));
33513365
}
3366+
3367+
_resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.user_vm);
3368+
//generate usage evenst to account for this change
3369+
_usageEventDao.persist(new UsageEventVO(EventTypes.EVENT_VM_CREATE, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(),
3370+
vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString()));
3371+
3372+
33523373

33533374
// OS 3: update the network
33543375
if (zone.getNetworkType() == NetworkType.Advanced) {
@@ -3404,6 +3425,8 @@ public UserVm moveVMToUser(MoveUserVMCmd cmd) throws ResourceAllocationException
34043425
}
34053426
}
34063427

3428+
txn.commit();
3429+
34073430
return vm;
34083431
}
34093432

0 commit comments

Comments
 (0)