use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class VirtualRouterElement method savePassword.
@Override
public boolean savePassword(final Network network, final NicProfile nic, final VirtualMachineProfile vm) throws ResourceUnavailableException {
if (!canHandle(network, null)) {
return false;
}
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Can't find virtual router element in network " + network.getId());
return true;
}
final VirtualMachineProfile uservm = vm;
final Zone zone = zoneRepository.findOne(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(zone);
// save the password in DB
for (final VirtualRouter router : routers) {
if (router.getState() == State.Running) {
return networkTopology.savePasswordToRouter(network, nic, uservm, router);
}
}
final String password = (String) uservm.getParameter(VirtualMachineProfile.Param.VmPassword);
final String password_encrypted = DBEncryptionUtil.encrypt(password);
final UserVmVO userVmVO = _userVmDao.findById(vm.getId());
_userVmDao.loadDetails(userVmVO);
userVmVO.setDetail("password", password_encrypted);
_userVmDao.saveDetails(userVmVO);
userVmVO.setUpdateParameters(true);
_userVmDao.update(userVmVO.getId(), userVmVO);
return true;
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class XenServerGuru method implement.
@Override
public VirtualMachineTO implement(final VirtualMachineProfile vm) {
BootloaderType bt = BootloaderType.PyGrub;
if (vm.getBootLoaderType() == BootloaderType.CD) {
bt = vm.getBootLoaderType();
}
final VirtualMachineTO to = toVirtualMachineTO(vm);
final UserVmVO userVmVO = _userVmDao.findById(vm.getId());
if (userVmVO != null) {
final HostVO host = hostDao.findById(userVmVO.getHostId());
if (host != null) {
to.setVcpuMaxLimit(MaxNumberOfVCPUSPerVM.valueIn(host.getClusterId()));
}
}
to.setBootloader(bt);
// Determine the VM's OS description
final GuestOSVO guestOS = _guestOsDao.findByIdIncludingRemoved(vm.getVirtualMachine().getGuestOSId());
to.setOs(guestOS.getDisplayName());
final HostVO host = hostDao.findById(vm.getVirtualMachine().getHostId());
GuestOSHypervisorVO guestOsMapping = null;
if (host != null) {
guestOsMapping = _guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), getHypervisorType().toString(), host.getHypervisorVersion());
}
if (guestOsMapping == null || host == null) {
to.setPlatformEmulator(null);
} else {
to.setPlatformEmulator(guestOsMapping.getGuestOsName());
}
return to;
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class VMSnapshotStrategyTest method testCreateVMSnapshot.
@Test
public void testCreateVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
final Long hostId = 1L;
final Long vmId = 1L;
final Long guestOsId = 1L;
final HypervisorType hypervisorType = HypervisorType.Any;
final String hypervisorVersion = "default";
final String guestOsName = "Other";
final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
final HostVO hostVO = Mockito.mock(HostVO.class);
Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
Exception e = null;
try {
vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
} catch (final CloudRuntimeException e1) {
e = e1;
}
assertNotNull(e);
final CreateVMSnapshotAnswer answer = Mockito.mock(CreateVMSnapshotAnswer.class);
Mockito.when(answer.getResult()).thenReturn(true);
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
VMSnapshot snapshot = null;
snapshot = vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
assertNotNull(snapshot);
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class VMSnapshotStrategyTest method testRevertSnapshot.
@Test
public void testRevertSnapshot() throws AgentUnavailableException, OperationTimedoutException {
final Long hostId = 1L;
final Long vmId = 1L;
final Long guestOsId = 1L;
final HypervisorType hypervisorType = HypervisorType.Any;
final String hypervisorVersion = "default";
final String guestOsName = "Other";
final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
final VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
Mockito.when(vmSnapshot.getId()).thenReturn(1L);
Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
final HostVO hostVO = Mockito.mock(HostVO.class);
Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
Exception e = null;
try {
vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
} catch (final CloudRuntimeException e1) {
e = e1;
}
assertNotNull(e);
final RevertToVMSnapshotAnswer answer = Mockito.mock(RevertToVMSnapshotAnswer.class);
Mockito.when(answer.getResult()).thenReturn(Boolean.TRUE);
Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
final boolean result = vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
assertTrue(result);
}
use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.
the class AffinityGroupServiceImpl method updateVMAffinityGroups.
@Override
public UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds) {
// Verify input parameters
final UserVmVO vmInstance = _userVmDao.findById(vmId);
if (vmInstance == null) {
throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
}
final Account caller = CallContext.current().getCallingAccount();
final Account owner = _accountMgr.getAccount(vmInstance.getAccountId());
// check that the affinity groups exist
for (final Long affinityGroupId : affinityGroupIds) {
final AffinityGroupVO AffinityGroupVO = _affinityGroupDao.findById(affinityGroupId);
if (AffinityGroupVO == null) {
throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId);
} else {
// verify permissions (same as when deploying VM)
_accountMgr.checkAccess(caller, null, false, owner, AffinityGroupVO);
// Only Explicit Dedication can be handled in non-Stopped state
if ("ExplicitDedication".equals(AffinityGroupVO.getType())) {
// Check if VM is currently running on host Explicitly Dedicated to this domain
Boolean VmRunsOnDedicatedHost = false;
final Pair<List<DedicatedResourceVO>, Integer> result = _dedicatedDao.searchDedicatedHosts(vmInstance.getHostId(), vmInstance.getDomainId(), null, affinityGroupId);
final List<DedicatedResourceVO> DedicatedResourceList = result.first();
for (final DedicatedResourceVO dedicatedResourceVO : DedicatedResourceList) {
if (dedicatedResourceVO.getHostId() != null && dedicatedResourceVO.getHostId().equals(vmInstance.getHostId())) {
VmRunsOnDedicatedHost = true;
}
}
if (!VmRunsOnDedicatedHost && !State.Stopped.equals(vmInstance.getState())) {
throw new InvalidParameterValueException("Unable update Explicit Dedication affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is either stopped or running on a host that is part of the " + "Explicit Dedication Affinity Group.");
}
} else {
if (!State.Stopped.equals(vmInstance.getState())) {
throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is stopped and not in an error state before updating.");
}
}
// owner of these entities is same
if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) {
if (!_affinityGroupService.isAffinityGroupAvailableInDomain(AffinityGroupVO.getId(), owner.getDomainId())) {
throw new PermissionDeniedException("Affinity Group " + AffinityGroupVO + " does not belong to the VM's domain");
}
}
}
}
_affinityGroupVMMapDao.updateMap(vmId, affinityGroupIds);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Updated VM :" + vmId + " affinity groups to =" + affinityGroupIds);
}
// APIResponseHelper will pull out the updated affinitygroups.
return vmInstance;
}
Aggregations