private void generateUsageEvent(Long serviceOfferingId, VirtualMachine vm, String eventType){ boolean displayVm = true; if(vm.getType() == VirtualMachine.Type.User){ UserVmVO uservm = _userVmDao.findById(vm.getId()); displayVm = uservm.isDisplayVm(); } _userVmMgr.generateUsageEvent(vm, displayVm, eventType); }
@Override public List<? extends NicSecondaryIp> listVmNicSecondaryIps(ListNicsCmd cmd) { Account caller = CallContext.current().getCallingAccount(); Long nicId = cmd.getNicId(); long vmId = cmd.getVmId(); String keyword = cmd.getKeyword(); UserVmVO userVm = _userVmDao.findById(vmId); if (userVm == null || (!userVm.isDisplayVm() && caller.getType() == Account.ACCOUNT_TYPE_NORMAL)) { throwInvalidIdException("Virtual machine id does not exist", Long.valueOf(vmId).toString(), "vmId"); } _accountMgr.checkAccess(caller, null, true, userVm); return _nicSecondaryIpDao.listSecondaryIpUsingKeyword(nicId, keyword); }
@Override public List<? extends Nic> listNics(ListNicsCmd cmd) { Account caller = CallContext.current().getCallingAccount(); Long nicId = cmd.getNicId(); long vmId = cmd.getVmId(); String keyword = cmd.getKeyword(); Long networkId = cmd.getNetworkId(); UserVmVO userVm = _userVmDao.findById(vmId); if (userVm == null || (!userVm.isDisplayVm() && caller.getType() == Account.ACCOUNT_TYPE_NORMAL)) { throwInvalidIdException("Virtual machine id does not exist", Long.valueOf(vmId).toString(), "vmId"); } _accountMgr.checkAccess(caller, null, true, userVm); return _networkMgr.listVmNics(vmId, nicId, networkId, keyword); }
private void updateVmStateForFailedVmCreation(Long vmId, Long hostId) { UserVmVO vm = _vmDao.findById(vmId); if (vm != null) { if (vm.getState().equals(State.Stopped)) { s_logger.debug("Destroying vm " + vm + " as it failed to create on Host with Id:" + hostId); try { _itMgr.stateTransitTo(vm, VirtualMachine.Event.OperationFailedToError, null); } catch (NoTransitionException e1) { s_logger.warn(e1.getMessage()); } // destroy associated volumes for vm in error state // get all volumes in non destroyed state List<VolumeVO> volumesForThisVm = _volsDao.findUsableVolumesForInstance(vm.getId()); for (VolumeVO volume : volumesForThisVm) { if (volume.getState() != Volume.State.Destroy) { volumeMgr.destroyVolume(volume); } } String msg = "Failed to deploy Vm with Id: " + vmId + ", on Host with Id: " + hostId; _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_USERVM, vm.getDataCenterId(), vm.getPodIdToDeployIn(), msg, msg); // Get serviceOffering for Virtual Machine ServiceOfferingVO offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId()); // Update Resource Count for the given account resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize())); } } }
vm.getHypervisorType().toString(), VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplayVm()); resourceCountDecrement(oldAccount.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize())); resourceCountIncrement(newAccount.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize())); VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplayVm());
resourceLimitCheck(account, vm.isDisplayVm(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize())); resourceCountIncrement(account.getId(), vm.isDisplayVm(), new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize()));
hypervisorType.toString(), VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplayVm()); } else { UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), vm.getHostName(), offering.getId(), template.getId(), hypervisorType.toString(), VirtualMachine.class.getName(), vm.getUuid(), customParameters, vm.isDisplayVm());
@Override @ActionEvent(eventType = EventTypes.EVENT_VM_UPGRADE, eventDescription = "Upgrading VM", async = true) public UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException { Long vmId = cmd.getId(); Long newServiceOfferingId = cmd.getServiceOfferingId(); VirtualMachine vm = (VirtualMachine) this._entityMgr.findById(VirtualMachine.class, vmId); if (vm == null) { throw new InvalidParameterValueException("Unable to find VM's UUID"); } CallContext.current().setEventDetails("Vm Id: " + vm.getUuid()); boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId, cmd.getDetails()); if (result) { UserVmVO vmInstance = _vmDao.findById(vmId); if (vmInstance.getState().equals(State.Stopped)) { // Generate usage event for VM upgrade generateUsageEvent(vmInstance, vmInstance.isDisplayVm(), EventTypes.EVENT_VM_UPGRADE); } if (vmInstance.getState().equals(State.Running)) { // Generate usage event for Dynamic scaling of VM generateUsageEvent( vmInstance, vmInstance.isDisplayVm(), EventTypes.EVENT_VM_DYNAMIC_SCALE); } return vmInstance; } else { throw new CloudRuntimeException("Failed to scale the VM"); } }
isDisplayVmEnabled = vm.isDisplayVm();
resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
private void saveUsageEvent(UserVmVO vm) { // If vm not destroyed if( vm.getState() != State.Destroyed && vm.getState() != State.Expunging && vm.getState() != State.Error){ if(vm.isDisplayVm()){ //1. Allocated VM Usage Event generateUsageEvent(vm, true, EventTypes.EVENT_VM_CREATE); if(vm.getState() == State.Running || vm.getState() == State.Stopping){ //2. Running VM Usage Event generateUsageEvent(vm, true, EventTypes.EVENT_VM_START); // 3. Network offering usage generateNetworkUsageForVm(vm, true, EventTypes.EVENT_NETWORK_OFFERING_ASSIGN); } }else { //1. Allocated VM Usage Event generateUsageEvent(vm, true, EventTypes.EVENT_VM_DESTROY); if(vm.getState() == State.Running || vm.getState() == State.Stopping){ //2. Running VM Usage Event generateUsageEvent(vm, true, EventTypes.EVENT_VM_STOP); // 3. Network offering usage generateNetworkUsageForVm(vm, true, EventTypes.EVENT_NETWORK_OFFERING_REMOVE); } } } }
resourceLimitCheck(newAccount, vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
generateUsageEvent( userVm, userVm.isDisplayVm(), EventTypes.EVENT_VM_UPGRADE);
vol.setDisplayVolume(userVm.isDisplayVm());
vol.setDisplayVolume(userVm.isDisplayVm());
CountByAccount.and("type", CountByAccount.entity().getType(), SearchCriteria.Op.EQ); CountByAccount.and("state", CountByAccount.entity().getState(), SearchCriteria.Op.NIN); CountByAccount.and("displayVm", CountByAccount.entity().isDisplayVm(), SearchCriteria.Op.EQ); CountByAccount.done();