6363import com .cloud .api .commands .DestroyVMCmd ;
6464import com .cloud .api .commands .DetachVolumeCmd ;
6565import com .cloud .api .commands .ListVMsCmd ;
66- import com .cloud .api .commands .MoveUserVMCmd ;
66+ import com .cloud .api .commands .AssignVMCmd ;
6767import com .cloud .api .commands .RebootVMCmd ;
6868import com .cloud .api .commands .RecoverVMCmd ;
6969import 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