@Test public void testSuccess() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException { UserVmService userVmService = Mockito.mock(UserVmService.class); updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class); UserVm userVm = Mockito.mock(UserVm.class); Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(userVm); updateVmNicIpCmd._userVmService = userVmService; responseGenerator = Mockito.mock(ResponseGenerator.class); List<UserVmResponse> list = new LinkedList<UserVmResponse>(); UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class); list.add(userVmResponse); Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn(list); updateVmNicIpCmd._responseGenerator = responseGenerator; updateVmNicIpCmd.execute(); }
@Test public void testCreateFailure() { UserVmService userVmService = Mockito.mock(UserVmService.class); try { Mockito.when(userVmService.upgradeVirtualMachine(scaleVMCmd)).thenReturn(null); } catch (Exception e) { Assert.fail("Received exception when success expected " + e.getMessage()); } scaleVMCmd._userVmService = userVmService; try { scaleVMCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to scale vm", exception.getDescription()); } } }
@Override public void execute() { CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getVmId()) + " Network Id: " + this._uuidMgr.getUuid(Network.class, getNetworkId())); UserVm result = _userVmService.addNicToVirtualMachine(this); ArrayList<VMDetails> dc = new ArrayList<VMDetails>(); dc.add(VMDetails.valueOf("nics")); EnumSet<VMDetails> details = EnumSet.copyOf(dc); if (result != null){ UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, result).get(0); response.setResponseName(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add NIC to vm. Refer to server logs for details."); } } }
try { CallContext.current().setEventDetails("Vm Id: " + getEntityUuid()); result = _userVmService.startVirtualMachine(this); } catch (ResourceUnavailableException ex) { s_logger.warn("Exception: ", ex); result = _userVmService.getUserVm(getEntityId());
@Override public void execute() { UserVm userVm = _userVmService.getUserVm(getVirtualMachineId()); if (userVm == null) { throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId()); VirtualMachine migratedVm = _userVmService.migrateVirtualMachineWithVolume(getVirtualMachineId(), destinationHost, getVolumeToPool()); if (migratedVm != null) { UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm)migratedVm).get(0);
UserVm userVm = _userVmService.getUserVm(getVirtualMachineId()); if (userVm == null) { throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId()); VirtualMachine migratedVm = null; if (getHostId() != null) { migratedVm = _userVmService.migrateVirtualMachine(getVirtualMachineId(), destinationHost); } else if (getStoragePoolId() != null) { migratedVm = _userVmService.vmStorageMigration(getVirtualMachineId(), destStoragePool);
IpAddresses addrs = new IpAddresses(null, null); if (zone.getNetworkType() == NetworkType.Basic) { vm = _userVmService.createBasicSecurityGroupVirtualMachine(zone, serviceOffering, template, null, owner, "autoScaleVm-" + asGroup.getId() + "-" + getCurrentTimeStampString(), "autoScaleVm-" + asGroup.getId() + "-" + getCurrentTimeStampString(), null, null, null, HypervisorType.XenServer, HTTPMethod.GET, null, null, null, } else { if (zone.isSecurityGroupEnabled()) { vm = _userVmService.createAdvancedSecurityGroupVirtualMachine(zone, serviceOffering, template, null, null, owner, "autoScaleVm-" + asGroup.getId() + "-" + getCurrentTimeStampString(), "autoScaleVm-" + asGroup.getId() + "-" + getCurrentTimeStampString(), null, null, null, HypervisorType.XenServer, HTTPMethod.GET, null, null, vm = _userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, null, owner, "autoScaleVm-" + asGroup.getId() + "-" + getCurrentTimeStampString(), "autoScaleVm-" + asGroup.getId() + "-" + getCurrentTimeStampString(), null, null, null, HypervisorType.XenServer, HTTPMethod.GET, null, null, null, addrs, true, null, null, null, null, null, null);
@Override public void execute() throws ResourceUnavailableException, ConcurrentOperationException{ CallContext.current().setEventDetails("Vm Id: "+this._uuidMgr.getUuid(VirtualMachine.class, getId())); UserVm result = _userVmService.destroyVm(this); UserVmResponse response = new UserVmResponse(); if (result != null) { List<UserVmResponse> responses = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result); if (responses != null && !responses.isEmpty()) { response = responses.get(0); } response.setResponseName("virtualmachine"); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy vm"); } } }
@Override public void execute() { InstanceGroup result = _userVmService.createVmGroup(this); if (result != null) { InstanceGroupResponse response = _responseGenerator.createInstanceGroupResponse(result); response.setResponseName(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create vm instance group"); } } }
@Override public void execute() { boolean result = _userVmService.deleteVmGroup(this); if (result) { SuccessResponse response = new SuccessResponse(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete vm group"); } } }
@Override public long getEntityOwnerId() { UserVm userVM = _userVmService.getUserVm(vmId); return userVM.getAccountId(); }
@Override public void execute() { String userData = _userVmService.getVmUserData(getId()); VMUserDataResponse resp = new VMUserDataResponse(); resp.setVmId(_entityMgr.findById(UserVm.class, getId()).getUuid()); resp.setUserData(userData); resp.setObjectName("virtualmachineuserdata"); resp.setResponseName(getCommandName()); this.setResponseObject(resp); }
@Override public void execute() throws ResourceUnavailableException, ConcurrentOperationException { CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId())); try { UserVm result = _userVmService.expungeVm(this.getId()); if (result != null) { SuccessResponse response = new SuccessResponse(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to expunge vm"); } } catch (InvalidParameterValueException ipve) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, ipve.getMessage()); } catch (CloudRuntimeException cre) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, cre.getMessage()); } } }
final UserVm vm = _userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, getNetworkIds(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), getHttpMethod(), userData, sshKeyPairName, getIpToNetworkMap(), addrs, displayVm, keyboard, getAffinityGroupIdList(), getDetails(), getCustomId(), getDiskController(), getBootMenuTimeout(), getMaintenancePolicy(), getOptimiseFor(), getManufacturerString());
final UserVm userVm = _userVmService.getUserVm(getVirtualMachineId()); if (userVm == null) { throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId()); VirtualMachine migratedVm = null; if (getHostId() != null) { migratedVm = _userVmService.migrateVirtualMachine(getVirtualMachineId(), destinationHost); } else if (getStoragePoolId() != null) { migratedVm = _userVmService.vmStorageMigration(getVirtualMachineId(), destStoragePool);
try { CallContext.current().setEventDetails("Vm Id: " + getEntityUuid()); result = _userVmService.startVirtualMachine(this); } catch (ResourceUnavailableException ex) { s_logger.warn("Exception: ", ex); result = _userVmService.getUserVm(getEntityId());
@Override public void execute() { final UserVm userVm = _userVmService.getUserVm(getVirtualMachineId()); if (userVm == null) { throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId()); } final Host destinationHost = _resourceService.getHost(getHostId()); if (destinationHost == null) { throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id =" + getHostId()); } try { final VirtualMachine migratedVm = _userVmService.migrateVirtualMachineWithVolume(getVirtualMachineId(), destinationHost, getVolumeToPool()); if (migratedVm != null) { final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm) migratedVm).get(0); response.setResponseName(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm"); } } catch (final ResourceUnavailableException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); } catch (final ConcurrentOperationException | ManagementServerException | VirtualMachineMigrationException e) { s_logger.warn("Exception: ", e); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); } }
@Override public void execute() throws ResourceUnavailableException, ConcurrentOperationException { CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId())); UserVm result = _userVmService.destroyVm(this); UserVmResponse response = new UserVmResponse(); if (result != null) { List<UserVmResponse> responses = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result); if (responses != null && !responses.isEmpty()) { response = responses.get(0); } response.setResponseName("virtualmachine"); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy vm"); } } }
@Override public void execute() { final InstanceGroup result = _userVmService.createVmGroup(this); if (result != null) { final InstanceGroupResponse response = _responseGenerator.createInstanceGroupResponse(result); response.setResponseName(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create vm instance group"); } }
@Override public void execute() { final boolean result = _userVmService.deleteVmGroup(this); if (result) { final SuccessResponse response = new SuccessResponse(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to delete vm group"); } }