use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.
the class VMSnapshotManagerImpl method revertToVMSnapshotThroughJobQueue.
public Outcome<VMSnapshot> revertToVMSnapshotThroughJobQueue(final Long vmId, final Long vmSnapshotId) {
final CallContext context = CallContext.current();
final User callingUser = context.getCallingUser();
final Account callingAccount = context.getCallingAccount();
final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
workJob.setCmd(VmWorkRevertToVMSnapshot.class.getName());
workJob.setAccountId(callingAccount.getId());
workJob.setUserId(callingUser.getId());
workJob.setStep(VmWorkJobVO.Step.Starting);
workJob.setVmType(VirtualMachine.Type.Instance);
workJob.setVmInstanceId(vm.getId());
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
// save work context info (there are some duplications)
VmWorkRevertToVMSnapshot workInfo = new VmWorkRevertToVMSnapshot(callingUser.getId(), callingAccount.getId(), vm.getId(), VMSnapshotManagerImpl.VM_WORK_JOB_HANDLER, vmSnapshotId);
workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
_jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
return new VmJobVMSnapshotOutcome(workJob, vmSnapshotId);
}
use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.
the class BareMetalGuru method implement.
@Override
public VirtualMachineTO implement(VirtualMachineProfile vm) {
VirtualMachineTO to = toVirtualMachineTO(vm);
VMInstanceVO vo = _vmDao.findById(vm.getId());
if (vo.getLastHostId() == null) {
to.setBootArgs(BaremetalManager.DO_PXE);
}
Map<String, String> details = new HashMap<String, String>();
details.put("template", vm.getTemplate().getUrl());
to.setDetails(details);
// Determine the VM's OS description
GuestOSVO guestOS = _guestOsDao.findById(vm.getVirtualMachine().getGuestOSId());
to.setOs(guestOS.getDisplayName());
return to;
}
use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.
the class FloatingIpModel method update.
@Override
public void update(ModelController controller) throws InternalErrorException, IOException {
assert _initialized;
ApiConnector api = controller.getApiAccessor();
ContrailManager manager = controller.getManager();
FloatingIp fip = _fip;
if (_fip == null) {
_fip = fip = (FloatingIp) controller.getApiAccessor().findById(FloatingIp.class, _uuid);
if (fip == null) {
fip = new FloatingIp();
fip.setUuid(_uuid);
fip.setAddress(_addr);
fip.setName(_name);
fip.setParent(_fipPoolModel.getFloatingIpPool());
}
}
IPAddressVO ipAddrVO = controller.getIPAddressDao().findById(_id);
assert ipAddrVO != null : "can not find address object in db";
Long vmId = ipAddrVO.getAssociatedWithVmId();
Long networkId = ipAddrVO.getAssociatedWithNetworkId();
if (vmId == null || networkId == null) {
s_logger.debug("Floating ip is not yet associated to either vm or network");
return;
}
NicVO nic = controller.getNicDao().findByNtwkIdAndInstanceId(networkId, vmId);
assert nic != null : "can not find nic for the given network and vm in db";
VMInstanceVO vm = controller.getVmDao().findById(vmId);
assert vm != null : "can not find vm in db";
VirtualMachineModel vmModel = manager.getDatabase().lookupVirtualMachine(vm.getUuid());
assert vmModel != null : "can not find vm model";
VMInterfaceModel vmiModel = vmModel.getVMInterface(nic.getUuid());
assert vmiModel != null && vmiModel.getVMInterface() != null : "can not find virtual machine interface";
fip.setVirtualMachineInterface(vmiModel.getVMInterface());
if (_fip == null) {
try {
api.create(fip);
} catch (Exception ex) {
s_logger.debug("floating ip create", ex);
throw new CloudRuntimeException("Failed to create floating ip", ex);
}
_fip = fip;
} else {
try {
api.update(fip);
} catch (IOException ex) {
s_logger.warn("floating ip update", ex);
throw new CloudRuntimeException("Unable to update floating ip object", ex);
}
}
addToVMInterface(vmiModel);
for (ModelObject successor : successors()) {
successor.update(controller);
}
}
use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.
the class InstanceIpModelTest method testCreateInstanceIp.
@Test
public void testCreateInstanceIp() throws IOException {
ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
ModelController controller = mock(ModelController.class);
ApiConnector api = new ApiConnectorMock(null, 0);
when(controller.getApiAccessor()).thenReturn(api);
when(controller.getManager()).thenReturn(contrailMgr);
// Create Virtual-Network (VN)
NetworkVO network = mock(NetworkVO.class);
when(network.getName()).thenReturn("testnetwork");
when(network.getState()).thenReturn(Network.State.Implemented);
when(network.getGateway()).thenReturn("10.1.1.1");
when(network.getCidr()).thenReturn("10.1.1.0/24");
when(network.getPhysicalNetworkId()).thenReturn(42L);
when(network.getDomainId()).thenReturn(10L);
when(network.getAccountId()).thenReturn(42L);
NetworkDao networkDao = mock(NetworkDao.class);
when(networkDao.findById(anyLong())).thenReturn(network);
when(controller.getNetworkDao()).thenReturn(networkDao);
when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
VirtualNetworkModel vnModel = new VirtualNetworkModel(network, UUID.randomUUID().toString(), "test", TrafficType.Guest);
vnModel.build(controller, network);
try {
vnModel.update(controller);
} catch (Exception ex) {
fail("virtual-network update failed ");
}
// Create Virtual-Machine (VM)
VMInstanceVO vm = mock(VMInstanceVO.class);
when(vm.getInstanceName()).thenReturn("testVM1");
when(vm.getState()).thenReturn(VirtualMachine.State.Running);
when(vm.getDomainId()).thenReturn(10L);
when(vm.getAccountId()).thenReturn(42L);
UserVmDao VmDao = mock(UserVmDao.class);
when(VmDao.findById(anyLong())).thenReturn(null);
when(controller.getVmDao()).thenReturn(VmDao);
VirtualMachineModel vmModel = new VirtualMachineModel(vm, UUID.randomUUID().toString());
vmModel.build(controller, vm);
try {
vmModel.update(controller);
} catch (Exception ex) {
fail("virtual-machine update failed ");
}
// Create Virtual=Machine-Interface (VMInterface)
NicVO nic = mock(NicVO.class);
when(nic.getIPv4Address()).thenReturn("10.1.1.2");
when(nic.getMacAddress()).thenReturn("00:01:02:03:04:05");
when(nic.getDeviceId()).thenReturn(100);
when(nic.getState()).thenReturn(NicVO.State.Allocated);
when(nic.getNetworkId()).thenReturn(10L);
when(contrailMgr.getVifNameByVmName(anyString(), anyInt())).thenReturn("testVM1-100");
VMInterfaceModel vmiModel = new VMInterfaceModel(UUID.randomUUID().toString());
vmiModel.addToVirtualMachine(vmModel);
vmiModel.addToVirtualNetwork(vnModel);
try {
vmiModel.build(controller, vm, nic);
vmiModel.setActive();
} catch (Exception ex) {
fail("vm-interface build failed ");
}
try {
vmiModel.update(controller);
} catch (Exception ex) {
fail("vm-interface update failed ");
}
InstanceIpModel ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
ipModel.addToVMInterface(vmiModel);
ipModel.setAddress(nic.getIPv4Address());
try {
ipModel.update(controller);
} catch (Exception ex) {
fail("ipInstance update failed ");
}
}
use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.
the class VMInterfaceModelTest method testCreateVMInterface.
@Test
public void testCreateVMInterface() throws IOException {
String uuid;
ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
ModelController controller = mock(ModelController.class);
ApiConnector api = new ApiConnectorMock(null, 0);
when(controller.getManager()).thenReturn(contrailMgr);
when(controller.getApiAccessor()).thenReturn(api);
// Create Virtual-Network (VN)
NetworkVO network = mock(NetworkVO.class);
when(network.getName()).thenReturn("testnetwork");
when(network.getState()).thenReturn(Network.State.Implemented);
when(network.getGateway()).thenReturn("10.1.1.1");
when(network.getCidr()).thenReturn("10.1.1.0/24");
when(network.getPhysicalNetworkId()).thenReturn(42L);
when(network.getDomainId()).thenReturn(10L);
when(network.getAccountId()).thenReturn(42L);
NetworkDao networkDao = mock(NetworkDao.class);
when(networkDao.findById(anyLong())).thenReturn(network);
when(controller.getNetworkDao()).thenReturn(networkDao);
when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
VirtualNetworkModel vnModel = new VirtualNetworkModel(network, UUID.randomUUID().toString(), "test", TrafficType.Guest);
vnModel.build(controller, network);
try {
vnModel.update(controller);
} catch (Exception ex) {
fail("virtual-network update failed ");
}
// Create Virtual-Machine (VM)
VMInstanceVO vm = mock(VMInstanceVO.class);
when(vm.getInstanceName()).thenReturn("testVM1");
when(vm.getState()).thenReturn(VirtualMachine.State.Running);
when(vm.getDomainId()).thenReturn(10L);
when(vm.getAccountId()).thenReturn(42L);
UserVmDao VmDao = mock(UserVmDao.class);
when(VmDao.findById(anyLong())).thenReturn(null);
when(controller.getVmDao()).thenReturn(VmDao);
VirtualMachineModel vmModel = new VirtualMachineModel(vm, UUID.randomUUID().toString());
vmModel.build(controller, vm);
try {
vmModel.update(controller);
} catch (Exception ex) {
fail("virtual-machine update failed ");
}
// Create Virtual=Machine-Interface (VMInterface)
NicVO nic = mock(NicVO.class);
when(nic.getIPv4Address()).thenReturn("10.1.1.2");
when(nic.getMacAddress()).thenReturn("00:01:02:03:04:05");
when(nic.getDeviceId()).thenReturn(100);
when(nic.getState()).thenReturn(NicVO.State.Allocated);
when(nic.getNetworkId()).thenReturn(10L);
when(contrailMgr.getVifNameByVmName(anyString(), anyInt())).thenReturn("testVM1-100");
uuid = UUID.randomUUID().toString();
VMInterfaceModel vmiModel = new VMInterfaceModel(uuid);
assertEquals(uuid, vmiModel.getUuid());
vmiModel.addToVirtualMachine(vmModel);
vmiModel.addToVirtualNetwork(vnModel);
try {
vmiModel.build(controller, vm, nic);
vmiModel.setActive();
} catch (Exception ex) {
fail("vm-interface build failed ");
}
try {
vmiModel.update(controller);
} catch (Exception ex) {
fail("vm-interface update failed ");
}
// Verify virtual-machine-interface creation.
try {
VirtualMachineInterface vmi = (VirtualMachineInterface) api.findById(VirtualMachineInterface.class, vmiModel.getUuid());
assertNotNull(vmi);
} catch (Exception ex) {
fail("vm-interface verify failed ");
}
}
Aggregations