Search in sources :

Example 56 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cosmic by MissionCriticalCloud.

the class CloudOrchestrator method createVirtualMachine.

@Override
public void createVirtualMachine(final String id, final String owner, final String templateId, final String hostName, final String displayName, final String hypervisor, final int cpu, final long memory, final Long diskSize, final List<String> computeTags, final List<String> rootDiskTags, final Map<String, NicProfile> networkNicMap, final DeploymentPlan plan, final Long rootDiskSize) throws InsufficientCapacityException {
    final LinkedHashMap<NetworkVO, List<? extends NicProfile>> networkIpMap = new LinkedHashMap<>();
    for (final String uuid : networkNicMap.keySet()) {
        final NetworkVO network = _networkDao.findByUuid(uuid);
        if (network != null) {
            networkIpMap.put(network, new ArrayList<>(Arrays.asList(networkNicMap.get(uuid))));
        }
    }
    final VirtualMachineEntityImpl vmEntity = ComponentContext.inject(VirtualMachineEntityImpl.class);
    vmEntity.init(id, owner, hostName, displayName, computeTags, rootDiskTags, new ArrayList<>(networkNicMap.keySet()));
    final HypervisorType hypervisorType = HypervisorType.valueOf(hypervisor);
    // load vm instance and offerings and call virtualMachineManagerImpl
    final VMInstanceVO vm = _vmDao.findByUuid(id);
    // If the template represents an ISO, a disk offering must be passed in, and will be used to create the root disk
    // Else, a disk offering is optional, and if present will be used to create the data disk
    final DiskOfferingInfo rootDiskOfferingInfo = new DiskOfferingInfo();
    final List<DiskOfferingInfo> dataDiskOfferings = new ArrayList<>();
    final ServiceOfferingVO computeOffering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
    rootDiskOfferingInfo.setDiskOffering(computeOffering);
    rootDiskOfferingInfo.setSize(rootDiskSize);
    if (computeOffering.isCustomizedIops() != null && computeOffering.isCustomizedIops()) {
        final Map<String, String> userVmDetails = _userVmDetailsDao.listDetailsKeyPairs(vm.getId());
        if (userVmDetails != null) {
            final String minIops = userVmDetails.get("minIops");
            final String maxIops = userVmDetails.get("maxIops");
            rootDiskOfferingInfo.setMinIops(minIops != null && minIops.trim().length() > 0 ? Long.parseLong(minIops) : null);
            rootDiskOfferingInfo.setMaxIops(maxIops != null && maxIops.trim().length() > 0 ? Long.parseLong(maxIops) : null);
        }
    }
    if (vm.getDiskOfferingId() != null) {
        final DiskOfferingVO diskOffering = _diskOfferingDao.findById(vm.getDiskOfferingId());
        if (diskOffering == null) {
            throw new InvalidParameterValueException("Unable to find disk offering " + vm.getDiskOfferingId());
        }
        Long size = null;
        if (diskOffering.getDiskSize() == 0) {
            size = diskSize;
            if (size == null) {
                throw new InvalidParameterValueException("Disk offering " + diskOffering + " requires size parameter.");
            }
            _volumeMgr.validateVolumeSizeRange(size * 1024 * 1024 * 1024);
        }
        final DiskOfferingInfo dataDiskOfferingInfo = new DiskOfferingInfo();
        dataDiskOfferingInfo.setDiskOffering(diskOffering);
        dataDiskOfferingInfo.setSize(size);
        if (diskOffering.isCustomizedIops() != null && diskOffering.isCustomizedIops()) {
            final Map<String, String> userVmDetails = _userVmDetailsDao.listDetailsKeyPairs(vm.getId());
            if (userVmDetails != null) {
                final String minIops = userVmDetails.get("minIopsDo");
                final String maxIops = userVmDetails.get("maxIopsDo");
                dataDiskOfferingInfo.setMinIops(minIops != null && minIops.trim().length() > 0 ? Long.parseLong(minIops) : null);
                dataDiskOfferingInfo.setMaxIops(maxIops != null && maxIops.trim().length() > 0 ? Long.parseLong(maxIops) : null);
            }
        }
        dataDiskOfferings.add(dataDiskOfferingInfo);
    }
    _itMgr.allocate(vm.getInstanceName(), _templateDao.findById(new Long(templateId)), computeOffering, rootDiskOfferingInfo, dataDiskOfferings, networkIpMap, plan, hypervisorType);
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) NicProfile(com.cloud.vm.NicProfile) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) VirtualMachineEntityImpl(com.cloud.engine.cloud.entity.api.VirtualMachineEntityImpl) LinkedHashMap(java.util.LinkedHashMap) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ArrayList(java.util.ArrayList) List(java.util.List) DiskOfferingInfo(com.cloud.offering.DiskOfferingInfo)

Example 57 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cosmic by MissionCriticalCloud.

the class StorageSystemDataMotionStrategy method handleCreateVolumeFromSnapshotBothOnStorageSystem.

private Void handleCreateVolumeFromSnapshotBothOnStorageSystem(final SnapshotInfo snapshotInfo, VolumeInfo volumeInfo, final AsyncCompletionCallback<CopyCommandResult> callback) {
    try {
        // at this point, the snapshotInfo and volumeInfo should have the same disk offering ID (so either one should be OK to get a DiskOfferingVO instance)
        final DiskOfferingVO diskOffering = _diskOfferingDao.findByIdIncludingRemoved(volumeInfo.getDiskOfferingId());
        final SnapshotVO snapshot = _snapshotDao.findById(snapshotInfo.getId());
        // update the volume's hv_ss_reserve (hypervisor snapshot reserve) from a disk offering (used for managed storage)
        _volumeService.updateHypervisorSnapshotReserveForVolume(diskOffering, volumeInfo.getId(), snapshot.getHypervisorType());
        final AsyncCallFuture<VolumeApiResult> future = _volumeService.createVolumeAsync(volumeInfo, volumeInfo.getDataStore());
        final VolumeApiResult result = future.get();
        if (result.isFailed()) {
            s_logger.debug("Failed to create a volume: " + result.getResult());
            throw new CloudRuntimeException(result.getResult());
        }
    } catch (final Exception ex) {
        throw new CloudRuntimeException(ex.getMessage());
    }
    volumeInfo = _volumeDataFactory.getVolume(volumeInfo.getId(), volumeInfo.getDataStore());
    volumeInfo.processEvent(Event.MigrationRequested);
    volumeInfo = _volumeDataFactory.getVolume(volumeInfo.getId(), volumeInfo.getDataStore());
    final HostVO hostVO = getHost(snapshotInfo.getDataStore().getId());
    final String value = _configDao.getValue(Config.PrimaryStorageDownloadWait.toString());
    final int primaryStorageDownloadWait = NumbersUtil.parseInt(value, Integer.parseInt(Config.PrimaryStorageDownloadWait.getDefaultValue()));
    final CopyCommand copyCommand = new CopyCommand(snapshotInfo.getTO(), volumeInfo.getTO(), primaryStorageDownloadWait, VirtualMachineManager.ExecuteInSequence.value());
    CopyCmdAnswer copyCmdAnswer = null;
    try {
        _volumeService.grantAccess(snapshotInfo, hostVO, snapshotInfo.getDataStore());
        _volumeService.grantAccess(volumeInfo, hostVO, volumeInfo.getDataStore());
        final Map<String, String> srcDetails = getSnapshotDetails(_storagePoolDao.findById(snapshotInfo.getDataStore().getId()), snapshotInfo);
        copyCommand.setOptions(srcDetails);
        final Map<String, String> destDetails = getVolumeDetails(volumeInfo);
        copyCommand.setOptions2(destDetails);
        copyCmdAnswer = (CopyCmdAnswer) _agentMgr.send(hostVO.getId(), copyCommand);
    } catch (final Exception ex) {
        throw new CloudRuntimeException(ex.getMessage());
    } finally {
        try {
            _volumeService.revokeAccess(snapshotInfo, hostVO, snapshotInfo.getDataStore());
        } catch (final Exception ex) {
            s_logger.debug(ex.getMessage(), ex);
        }
        try {
            _volumeService.revokeAccess(volumeInfo, hostVO, volumeInfo.getDataStore());
        } catch (final Exception ex) {
            s_logger.debug(ex.getMessage(), ex);
        }
    }
    String errMsg = null;
    if (copyCmdAnswer == null || !copyCmdAnswer.getResult()) {
        if (copyCmdAnswer != null && copyCmdAnswer.getDetails() != null && !copyCmdAnswer.getDetails().isEmpty()) {
            errMsg = copyCmdAnswer.getDetails();
        } else {
            errMsg = "Unable to perform host-side operation";
        }
    }
    final CopyCommandResult result = new CopyCommandResult(null, copyCmdAnswer);
    result.setResult(errMsg);
    callback.complete(result);
    return null;
}
Also used : CopyCommand(com.cloud.storage.command.CopyCommand) VolumeApiResult(com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HostVO(com.cloud.host.HostVO) SnapshotVO(com.cloud.storage.SnapshotVO) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(com.cloud.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer)

Example 58 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.

the class VirtualMachineManagerImpl method checkDestinationForTags.

private void checkDestinationForTags(StoragePool destPool, VMInstanceVO vm) {
    List<VolumeVO> vols = _volsDao.findUsableVolumesForInstance(vm.getId());
    List<String> storageTags = storageMgr.getStoragePoolTagList(destPool.getId());
    for (Volume vol : vols) {
        DiskOfferingVO diskOffering = _diskOfferingDao.findById(vol.getDiskOfferingId());
        List<String> volumeTags = StringUtils.csvTagsToList(diskOffering.getTags());
        if (!matches(volumeTags, storageTags)) {
            String msg = String.format("destination pool '%s' with tags '%s', does not support the volume diskoffering for volume '%s' (tags: '%s') ", destPool.getName(), StringUtils.listToCsvTags(storageTags), vol.getName(), StringUtils.listToCsvTags(volumeTags));
            throw new CloudRuntimeException(msg);
        }
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) Volume(com.cloud.storage.Volume) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 59 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.

the class CloudOrchestrator method createVirtualMachine.

@Override
public VirtualMachineEntity createVirtualMachine(String id, String owner, String templateId, String hostName, String displayName, String hypervisor, int cpu, int speed, long memory, Long diskSize, List<String> computeTags, List<String> rootDiskTags, Map<String, List<NicProfile>> networkNicMap, DeploymentPlan plan, Long rootDiskSize, Map<String, Map<Integer, String>> extraDhcpOptionMap, Map<Long, DiskOffering> dataDiskTemplateToDiskOfferingMap, Long dataDiskOfferingId, Long rootDiskOfferingId) throws InsufficientCapacityException {
    // VirtualMachineEntityImpl vmEntity = new VirtualMachineEntityImpl(id, owner, hostName, displayName, cpu, speed, memory, computeTags, rootDiskTags, networks,
    // vmEntityManager);
    LinkedHashMap<NetworkVO, List<? extends NicProfile>> networkIpMap = new LinkedHashMap<NetworkVO, List<? extends NicProfile>>();
    for (String uuid : networkNicMap.keySet()) {
        NetworkVO network = _networkDao.findByUuid(uuid);
        if (network != null) {
            networkIpMap.put(network, networkNicMap.get(uuid));
        }
    }
    VirtualMachineEntityImpl vmEntity = ComponentContext.inject(VirtualMachineEntityImpl.class);
    vmEntity.init(id, owner, hostName, displayName, cpu, speed, memory, computeTags, rootDiskTags, new ArrayList<String>(networkNicMap.keySet()));
    HypervisorType hypervisorType = HypervisorType.valueOf(hypervisor);
    // load vm instance and offerings and call virtualMachineManagerImpl
    VMInstanceVO vm = _vmDao.findByUuid(id);
    // If the template represents an ISO, a disk offering must be passed in, and will be used to create the root disk
    // Else, a disk offering is optional, and if present will be used to create the data disk
    DiskOfferingInfo rootDiskOfferingInfo = new DiskOfferingInfo();
    List<DiskOfferingInfo> dataDiskOfferings = new ArrayList<DiskOfferingInfo>();
    ServiceOfferingVO computeOffering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
    DiskOfferingVO rootDiskOffering = _diskOfferingDao.findById(rootDiskOfferingId);
    if (rootDiskOffering == null) {
        throw new InvalidParameterValueException("Unable to find root disk offering " + rootDiskOfferingId);
    }
    rootDiskOfferingInfo.setDiskOffering(rootDiskOffering);
    rootDiskOfferingInfo.setSize(rootDiskSize);
    if (rootDiskOffering.isCustomizedIops() != null && rootDiskOffering.isCustomizedIops()) {
        Map<String, String> userVmDetails = _userVmDetailsDao.listDetailsKeyPairs(vm.getId());
        if (userVmDetails != null) {
            String minIops = userVmDetails.get("minIops");
            String maxIops = userVmDetails.get("maxIops");
            rootDiskOfferingInfo.setMinIops(minIops != null && minIops.trim().length() > 0 ? Long.parseLong(minIops) : null);
            rootDiskOfferingInfo.setMaxIops(maxIops != null && maxIops.trim().length() > 0 ? Long.parseLong(maxIops) : null);
        }
    }
    if (dataDiskOfferingId != null) {
        DiskOfferingVO diskOffering = _diskOfferingDao.findById(dataDiskOfferingId);
        if (diskOffering == null) {
            throw new InvalidParameterValueException("Unable to find data disk offering " + dataDiskOfferingId);
        }
        if (!diskOffering.isComputeOnly()) {
            Long size = null;
            if (diskOffering.getDiskSize() == 0) {
                size = diskSize;
                if (size == null) {
                    throw new InvalidParameterValueException("Disk offering " + diskOffering + " requires size parameter.");
                }
                _volumeMgr.validateVolumeSizeRange(size * 1024 * 1024 * 1024);
            }
            DiskOfferingInfo dataDiskOfferingInfo = new DiskOfferingInfo();
            dataDiskOfferingInfo.setDiskOffering(diskOffering);
            dataDiskOfferingInfo.setSize(size);
            if (diskOffering.isCustomizedIops() != null && diskOffering.isCustomizedIops()) {
                Map<String, String> userVmDetails = _userVmDetailsDao.listDetailsKeyPairs(vm.getId());
                if (userVmDetails != null) {
                    String minIops = userVmDetails.get("minIopsDo");
                    String maxIops = userVmDetails.get("maxIopsDo");
                    dataDiskOfferingInfo.setMinIops(minIops != null && minIops.trim().length() > 0 ? Long.parseLong(minIops) : null);
                    dataDiskOfferingInfo.setMaxIops(maxIops != null && maxIops.trim().length() > 0 ? Long.parseLong(maxIops) : null);
                }
            }
            dataDiskOfferings.add(dataDiskOfferingInfo);
        }
    }
    if (dataDiskTemplateToDiskOfferingMap != null && !dataDiskTemplateToDiskOfferingMap.isEmpty()) {
        for (Entry<Long, DiskOffering> datadiskTemplateToDiskOffering : dataDiskTemplateToDiskOfferingMap.entrySet()) {
            DiskOffering dataDiskOffering = datadiskTemplateToDiskOffering.getValue();
            if (dataDiskOffering == null) {
                throw new InvalidParameterValueException("Unable to find disk offering " + dataDiskOfferingId);
            }
            if (dataDiskOffering.getDiskSize() == 0) {
                // Custom disk offering is not supported for volumes created from datadisk templates
                throw new InvalidParameterValueException("Disk offering " + dataDiskOffering + " requires size parameter.");
            }
        }
    }
    _itMgr.allocate(vm.getInstanceName(), _templateDao.findById(new Long(templateId)), computeOffering, rootDiskOfferingInfo, dataDiskOfferings, networkIpMap, plan, hypervisorType, extraDhcpOptionMap, dataDiskTemplateToDiskOfferingMap);
    return vmEntity;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) DiskOffering(com.cloud.offering.DiskOffering) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) NicProfile(com.cloud.vm.NicProfile) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) VirtualMachineEntityImpl(org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntityImpl) LinkedHashMap(java.util.LinkedHashMap) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ArrayList(java.util.ArrayList) List(java.util.List) DiskOfferingInfo(com.cloud.offering.DiskOfferingInfo)

Example 60 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.

the class UnmanagedVMsManagerImplTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    AccountVO account = new AccountVO("admin", 1L, "", Account.ACCOUNT_TYPE_ADMIN, "uuid");
    UserVO user = new UserVO(1, "adminuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, account);
    UnmanagedInstanceTO instance = new UnmanagedInstanceTO();
    instance.setName("TestInstance");
    instance.setCpuCores(2);
    instance.setCpuCoresPerSocket(1);
    instance.setCpuSpeed(1000);
    instance.setMemory(1024);
    instance.setOperatingSystem("CentOS 7");
    List<UnmanagedInstanceTO.Disk> instanceDisks = new ArrayList<>();
    UnmanagedInstanceTO.Disk instanceDisk = new UnmanagedInstanceTO.Disk();
    instanceDisk.setDiskId("1000-1");
    instanceDisk.setLabel("DiskLabel");
    instanceDisk.setController("scsi");
    instanceDisk.setImagePath("[b6ccf44a1fa13e29b3667b4954fa10ee] TestInstance/ROOT-1.vmdk");
    instanceDisk.setCapacity(5242880L);
    instanceDisk.setDatastoreName("Test");
    instanceDisk.setDatastoreHost("Test");
    instanceDisk.setDatastorePath("Test");
    instanceDisk.setDatastoreType("NFS");
    instanceDisks.add(instanceDisk);
    instance.setDisks(instanceDisks);
    List<UnmanagedInstanceTO.Nic> instanceNics = new ArrayList<>();
    UnmanagedInstanceTO.Nic instanceNic = new UnmanagedInstanceTO.Nic();
    instanceNic.setNicId("NIC 1");
    instanceNic.setAdapterType("VirtualE1000E");
    instanceNic.setMacAddress("02:00:2e:0f:00:02");
    instanceNic.setVlan(1024);
    instanceNics.add(instanceNic);
    instance.setNics(instanceNics);
    instance.setPowerState(UnmanagedInstanceTO.PowerState.PowerOn);
    ClusterVO clusterVO = new ClusterVO(1L, 1L, "Cluster");
    clusterVO.setHypervisorType(Hypervisor.HypervisorType.VMware.toString());
    when(clusterDao.findById(Mockito.anyLong())).thenReturn(clusterVO);
    when(configurationDao.getValue(Mockito.anyString())).thenReturn(null);
    doNothing().when(resourceLimitService).checkResourceLimit(any(Account.class), any(Resource.ResourceType.class), anyLong());
    List<HostVO> hosts = new ArrayList<>();
    HostVO hostVO = Mockito.mock(HostVO.class);
    when(hostVO.isInMaintenanceStates()).thenReturn(false);
    hosts.add(hostVO);
    when(hostVO.checkHostServiceOfferingTags(Mockito.any())).thenReturn(true);
    when(resourceManager.listHostsInClusterByStatus(Mockito.anyLong(), Mockito.any(Status.class))).thenReturn(hosts);
    List<VMTemplateStoragePoolVO> templates = new ArrayList<>();
    when(templatePoolDao.listAll()).thenReturn(templates);
    List<VolumeVO> volumes = new ArrayList<>();
    when(volumeDao.findIncludingRemovedByZone(Mockito.anyLong())).thenReturn(volumes);
    List<VMInstanceVO> vms = new ArrayList<>();
    when(vmDao.listByHostId(Mockito.anyLong())).thenReturn(vms);
    when(vmDao.listByLastHostIdAndStates(Mockito.anyLong())).thenReturn(vms);
    GetUnmanagedInstancesCommand cmd = Mockito.mock(GetUnmanagedInstancesCommand.class);
    HashMap<String, UnmanagedInstanceTO> map = new HashMap<>();
    map.put(instance.getName(), instance);
    Answer answer = new GetUnmanagedInstancesAnswer(cmd, "", map);
    when(agentManager.easySend(Mockito.anyLong(), Mockito.any(GetUnmanagedInstancesCommand.class))).thenReturn(answer);
    DataCenterVO zone = Mockito.mock(DataCenterVO.class);
    when(zone.getId()).thenReturn(1L);
    when(dataCenterDao.findById(Mockito.anyLong())).thenReturn(zone);
    when(accountService.getActiveAccountById(Mockito.anyLong())).thenReturn(Mockito.mock(Account.class));
    List<UserVO> users = new ArrayList<>();
    users.add(Mockito.mock(UserVO.class));
    when(userDao.listByAccount(Mockito.anyLong())).thenReturn(users);
    VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
    when(template.getId()).thenReturn(1L);
    when(template.getName()).thenReturn("Template");
    when(templateDao.findById(Mockito.anyLong())).thenReturn(template);
    when(templateDao.findByName(Mockito.anyString())).thenReturn(template);
    ServiceOfferingVO serviceOffering = Mockito.mock(ServiceOfferingVO.class);
    when(serviceOffering.getId()).thenReturn(1L);
    when(serviceOffering.isDynamic()).thenReturn(false);
    when(serviceOffering.getCpu()).thenReturn(instance.getCpuCores());
    when(serviceOffering.getRamSize()).thenReturn(instance.getMemory());
    when(serviceOffering.getSpeed()).thenReturn(instance.getCpuSpeed());
    when(serviceOfferingDao.findById(Mockito.anyLong())).thenReturn(serviceOffering);
    DiskOfferingVO diskOfferingVO = Mockito.mock(DiskOfferingVO.class);
    when(diskOfferingVO.getTags()).thenReturn("");
    when(diskOfferingVO.isCustomized()).thenReturn(false);
    when(diskOfferingVO.getDiskSize()).thenReturn(Long.MAX_VALUE);
    when(diskOfferingDao.findById(Mockito.anyLong())).thenReturn(diskOfferingVO);
    UserVmVO userVm = Mockito.mock(UserVmVO.class);
    when(userVm.getAccountId()).thenReturn(1L);
    when(userVm.getDataCenterId()).thenReturn(1L);
    when(userVm.getHostName()).thenReturn(instance.getName());
    when(userVm.getTemplateId()).thenReturn(1L);
    when(userVm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.VMware);
    when(userVm.getUuid()).thenReturn("abcd");
    when(userVm.isDisplayVm()).thenReturn(true);
    // Skip usage publishing and resource increment for test
    when(userVm.getType()).thenReturn(VirtualMachine.Type.Instance);
    userVm.setInstanceName(instance.getName());
    userVm.setHostName(instance.getName());
    StoragePoolVO poolVO = Mockito.mock(StoragePoolVO.class);
    when(poolVO.getDataCenterId()).thenReturn(1L);
    when(poolVO.getClusterId()).thenReturn(clusterVO.getId());
    List<StoragePoolVO> pools = new ArrayList<>();
    pools.add(poolVO);
    when(primaryDataStoreDao.listPoolByHostPath(Mockito.anyString(), Mockito.anyString())).thenReturn(pools);
    when(userVmManager.importVM(nullable(DataCenter.class), nullable(Host.class), nullable(VirtualMachineTemplate.class), nullable(String.class), nullable(String.class), nullable(Account.class), nullable(String.class), nullable(Account.class), nullable(Boolean.class), nullable(String.class), nullable(Long.class), nullable(Long.class), nullable(ServiceOffering.class), nullable(String.class), nullable(String.class), nullable(Hypervisor.HypervisorType.class), nullable(Map.class), nullable(VirtualMachine.PowerState.class))).thenReturn(userVm);
    when(volumeApiService.doesTargetStorageSupportDiskOffering(Mockito.any(StoragePool.class), Mockito.anyString())).thenReturn(true);
    NetworkVO networkVO = Mockito.mock(NetworkVO.class);
    when(networkVO.getGuestType()).thenReturn(Network.GuestType.L2);
    when(networkVO.getBroadcastUri()).thenReturn(URI.create(String.format("vlan://%d", instanceNic.getVlan())));
    when(networkVO.getDataCenterId()).thenReturn(1L);
    when(networkDao.findById(Mockito.anyLong())).thenReturn(networkVO);
    List<NetworkVO> networks = new ArrayList<>();
    networks.add(networkVO);
    when(networkDao.listByZone(Mockito.anyLong())).thenReturn(networks);
    doNothing().when(networkModel).checkNetworkPermissions(Mockito.any(Account.class), Mockito.any(Network.class));
    doNothing().when(networkModel).checkRequestedIpAddresses(Mockito.anyLong(), Mockito.any(Network.IpAddresses.class));
    NicProfile profile = Mockito.mock(NicProfile.class);
    Integer deviceId = 100;
    Pair<NicProfile, Integer> pair = new Pair<NicProfile, Integer>(profile, deviceId);
    when(networkOrchestrationService.importNic(nullable(String.class), nullable(Integer.class), nullable(Network.class), nullable(Boolean.class), nullable(VirtualMachine.class), nullable(Network.IpAddresses.class), anyBoolean())).thenReturn(pair);
    when(volumeManager.importVolume(Mockito.any(Volume.Type.class), Mockito.anyString(), Mockito.any(DiskOffering.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.any(VirtualMachine.class), Mockito.any(VirtualMachineTemplate.class), Mockito.any(Account.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyString())).thenReturn(Mockito.mock(DiskProfile.class));
    when(volumeDao.findByInstance(Mockito.anyLong())).thenReturn(volumes);
    List<UserVmResponse> userVmResponses = new ArrayList<>();
    UserVmResponse userVmResponse = new UserVmResponse();
    userVmResponse.setInstanceName(instance.getName());
    userVmResponses.add(userVmResponse);
    when(responseGenerator.createUserVmResponse(Mockito.any(ResponseObject.ResponseView.class), Mockito.anyString(), Mockito.any(UserVm.class))).thenReturn(userVmResponses);
    when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine);
    when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Running);
    when(virtualMachine.getInstanceName()).thenReturn("i-2-7-VM");
    when(virtualMachine.getId()).thenReturn(virtualMachineId);
    VolumeVO volumeVO = mock(VolumeVO.class);
    when(volumeDao.findByInstance(virtualMachineId)).thenReturn(Collections.singletonList(volumeVO));
    when(volumeVO.getInstanceId()).thenReturn(virtualMachineId);
    when(volumeVO.getId()).thenReturn(virtualMachineId);
    when(nicDao.listByVmId(virtualMachineId)).thenReturn(Collections.singletonList(nicVO));
    when(nicVO.getNetworkId()).thenReturn(1L);
    when(networkDao.findById(1L)).thenReturn(networkVO);
}
Also used : Account(com.cloud.user.Account) StoragePool(com.cloud.storage.StoragePool) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AccountVO(com.cloud.user.AccountVO) GetUnmanagedInstancesCommand(com.cloud.agent.api.GetUnmanagedInstancesCommand) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) UserVm(com.cloud.uservm.UserVm) VolumeVO(com.cloud.storage.VolumeVO) GetUnmanagedInstancesAnswer(com.cloud.agent.api.GetUnmanagedInstancesAnswer) Network(com.cloud.network.Network) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Status(com.cloud.host.Status) DataCenterVO(com.cloud.dc.DataCenterVO) NetworkVO(com.cloud.network.dao.NetworkVO) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ServiceOffering(com.cloud.offering.ServiceOffering) VMInstanceVO(com.cloud.vm.VMInstanceVO) DiskProfile(com.cloud.vm.DiskProfile) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) HostVO(com.cloud.host.HostVO) Matchers.anyLong(org.mockito.Matchers.anyLong) Map(java.util.Map) HashMap(java.util.HashMap) VirtualMachine(com.cloud.vm.VirtualMachine) UserVmVO(com.cloud.vm.UserVmVO) DiskOffering(com.cloud.offering.DiskOffering) VMTemplateVO(com.cloud.storage.VMTemplateVO) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) Pair(com.cloud.utils.Pair) ClusterVO(com.cloud.dc.ClusterVO) Host(com.cloud.host.Host) NicProfile(com.cloud.vm.NicProfile) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) Answer(com.cloud.agent.api.Answer) GetUnmanagedInstancesAnswer(com.cloud.agent.api.GetUnmanagedInstancesAnswer) DataCenter(com.cloud.dc.DataCenter) UserVO(com.cloud.user.UserVO) Before(org.junit.Before)

Aggregations

DiskOfferingVO (com.cloud.storage.DiskOfferingVO)86 ArrayList (java.util.ArrayList)34 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)32 VolumeVO (com.cloud.storage.VolumeVO)25 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)24 Account (com.cloud.user.Account)23 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)22 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)18 List (java.util.List)16 StoragePool (com.cloud.storage.StoragePool)15 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)14 DiskProfile (com.cloud.vm.DiskProfile)14 VMInstanceVO (com.cloud.vm.VMInstanceVO)14 HostVO (com.cloud.host.HostVO)13 NetworkVO (com.cloud.network.dao.NetworkVO)13 Pair (com.cloud.utils.Pair)13 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)12 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)12 User (com.cloud.user.User)12 HashMap (java.util.HashMap)12