Search in sources :

Example 91 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UserVmManagerTest method testScaleVMF4.

// Test scaleVm for Running vm. Full positive test.
public void testScaleVMF4() throws Exception {
    ScaleVMCmd cmd = new ScaleVMCmd();
    Class<?> _class = cmd.getClass();
    Field idField = _class.getDeclaredField("id");
    idField.setAccessible(true);
    idField.set(cmd, 1L);
    Field serviceOfferingIdField = _class.getDeclaredField("serviceOfferingId");
    serviceOfferingIdField.setAccessible(true);
    serviceOfferingIdField.set(cmd, 1L);
    // UserContext.current().setEventDetails("Vm Id: "+getId());
    // Account account = (Account) new AccountVO("testaccount", 1L, "networkdomain", (short) 0, 1);
    // AccountVO(String accountName, long domainId, String networkDomain, short type, int regionId)
    // UserContext.registerContext(1, account, null, true);
    when(_vmInstanceDao.findById(anyLong())).thenReturn(_vmInstance);
    doReturn(Hypervisor.HypervisorType.XenServer).when(_vmInstance).getHypervisorType();
    ServiceOffering so1 = getSvcoffering(512);
    ServiceOffering so2 = getSvcoffering(256);
    when(_entityMgr.findById(eq(ServiceOffering.class), anyLong())).thenReturn(so2);
    when(_entityMgr.findById(ServiceOffering.class, 1L)).thenReturn(so1);
    doReturn(VirtualMachine.State.Running).when(_vmInstance).getState();
    // when(ApiDBUtils.getCpuOverprovisioningFactor()).thenReturn(3f);
    when(_capacityMgr.checkIfHostHasCapacity(anyLong(), anyInt(), anyLong(), anyBoolean(), anyFloat(), anyFloat(), anyBoolean())).thenReturn(false);
    when(_itMgr.reConfigureVm(_vmInstance.getUuid(), so2, so1, new HashMap<String, String>(), false)).thenReturn(_vmInstance);
    doReturn(true).when(_itMgr).upgradeVmDb(anyLong(), so1, so2);
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, UUID.randomUUID().toString());
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, account);
    try {
        _userVmMgr.upgradeVirtualMachine(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) ServiceOffering(com.cloud.offering.ServiceOffering) ScaleVMCmd(org.apache.cloudstack.api.command.user.vm.ScaleVMCmd) Matchers.anyString(org.mockito.Matchers.anyString) AccountVO(com.cloud.user.AccountVO)

Example 92 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class ConfigurationManagerImpl method updateServiceOffering.

@Override
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_OFFERING_EDIT, eventDescription = "updating service offering")
public ServiceOffering updateServiceOffering(final UpdateServiceOfferingCmd cmd) {
    final String displayText = cmd.getDisplayText();
    final Long id = cmd.getId();
    final String name = cmd.getServiceOfferingName();
    final Integer sortKey = cmd.getSortKey();
    Long userId = CallContext.current().getCallingUserId();
    final List<Long> domainIds = cmd.getDomainIds();
    final List<Long> zoneIds = cmd.getZoneIds();
    String storageTags = cmd.getStorageTags();
    String hostTags = cmd.getHostTags();
    if (userId == null) {
        userId = Long.valueOf(User.UID_SYSTEM);
    }
    // Verify input parameters
    final ServiceOffering offeringHandle = _entityMgr.findById(ServiceOffering.class, id);
    if (offeringHandle == null) {
        throw new InvalidParameterValueException("unable to find service offering " + id);
    }
    List<Long> existingDomainIds = _serviceOfferingDetailsDao.findDomainIds(id);
    Collections.sort(existingDomainIds);
    List<Long> existingZoneIds = _serviceOfferingDetailsDao.findZoneIds(id);
    Collections.sort(existingZoneIds);
    // check if valid domain
    if (CollectionUtils.isNotEmpty(domainIds)) {
        for (final Long domainId : domainIds) {
            if (_domainDao.findById(domainId) == null) {
                throw new InvalidParameterValueException("Please specify a valid domain id");
            }
        }
    }
    // check if valid zone
    if (CollectionUtils.isNotEmpty(zoneIds)) {
        for (Long zoneId : zoneIds) {
            if (_zoneDao.findById(zoneId) == null)
                throw new InvalidParameterValueException("Please specify a valid zone id");
        }
    }
    final User user = _userDao.findById(userId);
    if (user == null || user.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find active user by id " + userId);
    }
    final Account account = _accountDao.findById(user.getAccountId());
    // Filter child domains when both parent and child domains are present
    List<Long> filteredDomainIds = filterChildSubDomains(domainIds);
    Collections.sort(filteredDomainIds);
    List<Long> filteredZoneIds = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(zoneIds)) {
        filteredZoneIds.addAll(zoneIds);
    }
    Collections.sort(filteredZoneIds);
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
        if (!filteredZoneIds.equals(existingZoneIds)) {
            // Domain-admins cannot update zone(s) for offerings
            throw new InvalidParameterValueException(String.format("Unable to update zone(s) for service offering: %s by admin: %s as it is domain-admin", offeringHandle.getUuid(), user.getUuid()));
        }
        if (existingDomainIds.isEmpty()) {
            throw new InvalidParameterValueException(String.format("Unable to update public service offering: %s by user: %s because it is domain-admin", offeringHandle.getUuid(), user.getUuid()));
        } else {
            if (filteredDomainIds.isEmpty()) {
                throw new InvalidParameterValueException(String.format("Unable to update service offering: %s to a public offering by user: %s because it is domain-admin", offeringHandle.getUuid(), user.getUuid()));
            }
        }
        List<Long> nonChildDomains = new ArrayList<>();
        for (Long domainId : existingDomainIds) {
            if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                if (name != null || displayText != null || sortKey != null) {
                    // Domain-admins cannot update name, display text, sort key for offerings with domain which are not child domains for domain-admin
                    throw new InvalidParameterValueException(String.format("Unable to update service offering: %s as it has linked domain(s) which are not child domain for domain-admin: %s", offeringHandle.getUuid(), user.getUuid()));
                }
                nonChildDomains.add(domainId);
            }
        }
        for (Long domainId : filteredDomainIds) {
            if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                Domain domain = _entityMgr.findById(Domain.class, domainId);
                throw new InvalidParameterValueException(String.format("Unable to update service offering: %s by domain-admin: %s with domain: %3$s which is not a child domain", offeringHandle.getUuid(), user.getUuid(), domain.getUuid()));
            }
        }
        // Final list must include domains which were not child domain for domain-admin but specified for this offering prior to update
        filteredDomainIds.addAll(nonChildDomains);
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException(String.format("Unable to update service offering: %s by id user: %s because it is not root-admin or domain-admin", offeringHandle.getUuid(), user.getUuid()));
    }
    final boolean updateNeeded = name != null || displayText != null || sortKey != null || storageTags != null || hostTags != null;
    final boolean detailsUpdateNeeded = !filteredDomainIds.equals(existingDomainIds) || !filteredZoneIds.equals(existingZoneIds);
    if (!updateNeeded && !detailsUpdateNeeded) {
        return _serviceOfferingDao.findById(id);
    }
    ServiceOfferingVO offering = _serviceOfferingDao.createForUpdate(id);
    if (name != null) {
        offering.setName(name);
    }
    if (displayText != null) {
        offering.setDisplayText(displayText);
    }
    if (sortKey != null) {
        offering.setSortKey(sortKey);
    }
    DiskOfferingVO diskOffering = _diskOfferingDao.findById(offeringHandle.getDiskOfferingId());
    updateOfferingTagsIfIsNotNull(storageTags, diskOffering);
    _diskOfferingDao.update(diskOffering.getId(), diskOffering);
    updateServiceOfferingHostTagsIfNotNull(hostTags, offering);
    if (updateNeeded && !_serviceOfferingDao.update(id, offering)) {
        return null;
    }
    List<ServiceOfferingDetailsVO> detailsVO = new ArrayList<>();
    if (detailsUpdateNeeded) {
        SearchBuilder<ServiceOfferingDetailsVO> sb = _serviceOfferingDetailsDao.createSearchBuilder();
        sb.and("offeringId", sb.entity().getResourceId(), SearchCriteria.Op.EQ);
        sb.and("detailName", sb.entity().getName(), SearchCriteria.Op.EQ);
        sb.done();
        SearchCriteria<ServiceOfferingDetailsVO> sc = sb.create();
        sc.setParameters("offeringId", String.valueOf(id));
        if (!filteredDomainIds.equals(existingDomainIds)) {
            sc.setParameters("detailName", ApiConstants.DOMAIN_ID);
            _serviceOfferingDetailsDao.remove(sc);
            for (Long domainId : filteredDomainIds) {
                detailsVO.add(new ServiceOfferingDetailsVO(id, ApiConstants.DOMAIN_ID, String.valueOf(domainId), false));
            }
        }
        if (!filteredZoneIds.equals(existingZoneIds)) {
            sc.setParameters("detailName", ApiConstants.ZONE_ID);
            _serviceOfferingDetailsDao.remove(sc);
            for (Long zoneId : filteredZoneIds) {
                detailsVO.add(new ServiceOfferingDetailsVO(id, ApiConstants.ZONE_ID, String.valueOf(zoneId), false));
            }
        }
    }
    if (!detailsVO.isEmpty()) {
        for (ServiceOfferingDetailsVO detailVO : detailsVO) {
            _serviceOfferingDetailsDao.persist(detailVO);
        }
    }
    offering = _serviceOfferingDao.findById(id);
    CallContext.current().setEventDetails("Service offering id=" + offering.getId());
    return offering;
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) ServiceOffering(com.cloud.offering.ServiceOffering) ArrayList(java.util.ArrayList) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) Domain(com.cloud.domain.Domain) ActionEvent(com.cloud.event.ActionEvent)

Example 93 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class ApiResponseHelper method createAutoScaleVmProfileResponse.

@Override
public AutoScaleVmProfileResponse createAutoScaleVmProfileResponse(AutoScaleVmProfile profile) {
    AutoScaleVmProfileResponse response = new AutoScaleVmProfileResponse();
    response.setId(profile.getUuid());
    if (profile.getZoneId() != null) {
        DataCenter zone = ApiDBUtils.findZoneById(profile.getZoneId());
        if (zone != null) {
            response.setZoneId(zone.getUuid());
        }
    }
    if (profile.getServiceOfferingId() != null) {
        ServiceOffering so = ApiDBUtils.findServiceOfferingById(profile.getServiceOfferingId());
        if (so != null) {
            response.setServiceOfferingId(so.getUuid());
        }
    }
    if (profile.getTemplateId() != null) {
        VMTemplateVO template = ApiDBUtils.findTemplateById(profile.getTemplateId());
        if (template != null) {
            response.setTemplateId(template.getUuid());
        }
    }
    response.setOtherDeployParams(profile.getOtherDeployParams());
    response.setCounterParams(profile.getCounterParams());
    response.setDestroyVmGraceperiod(profile.getDestroyVmGraceperiod());
    User user = ApiDBUtils.findUserById(profile.getAutoScaleUserId());
    if (user != null) {
        response.setAutoscaleUserId(user.getUuid());
    }
    response.setObjectName("autoscalevmprofile");
    // Populates the account information in the response
    populateOwner(response, profile);
    return response;
}
Also used : DataCenter(com.cloud.dc.DataCenter) User(com.cloud.user.User) VpnUser(com.cloud.network.VpnUser) ServiceOffering(com.cloud.offering.ServiceOffering) AutoScaleVmProfileResponse(org.apache.cloudstack.api.response.AutoScaleVmProfileResponse) VMTemplateVO(com.cloud.storage.VMTemplateVO)

Example 94 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class StorageManagerImpl method getDiskWithThrottling.

@Override
public DiskTO getDiskWithThrottling(final DataTO volTO, final Volume.Type volumeType, final long deviceId, final String path, final long offeringId, final long diskOfferingId) {
    DiskTO disk = null;
    if (volTO != null && volTO instanceof VolumeObjectTO) {
        VolumeObjectTO volumeTO = (VolumeObjectTO) volTO;
        ServiceOffering offering = _entityMgr.findById(ServiceOffering.class, offeringId);
        DiskOffering diskOffering = _entityMgr.findById(DiskOffering.class, diskOfferingId);
        if (volumeType == Volume.Type.ROOT) {
            setVolumeObjectTOThrottling(volumeTO, offering, diskOffering);
        } else {
            setVolumeObjectTOThrottling(volumeTO, null, diskOffering);
        }
        disk = new DiskTO(volumeTO, deviceId, path, volumeType);
    } else {
        disk = new DiskTO(volTO, deviceId, path, volumeType);
    }
    return disk;
}
Also used : DiskOffering(com.cloud.offering.DiskOffering) ServiceOffering(com.cloud.offering.ServiceOffering) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 95 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class FirstFitAllocator method allocateTo.

@Override
public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo, boolean considerReservedCapacity) {
    long dcId = plan.getDataCenterId();
    Long podId = plan.getPodId();
    Long clusterId = plan.getClusterId();
    ServiceOffering offering = vmProfile.getServiceOffering();
    VMTemplateVO template = (VMTemplateVO) vmProfile.getTemplate();
    Account account = vmProfile.getOwner();
    boolean isVMDeployedWithUefi = false;
    UserVmDetailVO userVmDetailVO = _userVmDetailsDao.findDetail(vmProfile.getId(), "UEFI");
    if (userVmDetailVO != null) {
        if ("secure".equalsIgnoreCase(userVmDetailVO.getValue()) || "legacy".equalsIgnoreCase(userVmDetailVO.getValue())) {
            isVMDeployedWithUefi = true;
        }
    }
    s_logger.info(" Guest VM is requested with Custom[UEFI] Boot Type " + isVMDeployedWithUefi);
    if (type == Host.Type.Storage) {
        // FirstFitAllocator should be used for user VMs only since it won't care whether the host is capable of routing or not
        return new ArrayList<Host>();
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Looking for hosts in dc: " + dcId + "  pod:" + podId + "  cluster:" + clusterId);
    }
    String hostTagOnOffering = offering.getHostTag();
    String hostTagOnTemplate = template.getTemplateTag();
    String hostTagUefi = "UEFI";
    boolean hasSvcOfferingTag = hostTagOnOffering != null ? true : false;
    boolean hasTemplateTag = hostTagOnTemplate != null ? true : false;
    List<HostVO> clusterHosts = new ArrayList<HostVO>();
    List<HostVO> hostsMatchingUefiTag = new ArrayList<HostVO>();
    if (isVMDeployedWithUefi) {
        hostsMatchingUefiTag = _hostDao.listByHostCapability(type, clusterId, podId, dcId, Host.HOST_UEFI_ENABLE);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Hosts with tag '" + hostTagUefi + "' are:" + hostsMatchingUefiTag);
        }
    }
    String haVmTag = (String) vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
    if (haVmTag != null) {
        clusterHosts = _hostDao.listByHostTag(type, clusterId, podId, dcId, haVmTag);
    } else {
        if (hostTagOnOffering == null && hostTagOnTemplate == null) {
            clusterHosts = _resourceMgr.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId);
        } else {
            List<HostVO> hostsMatchingOfferingTag = new ArrayList<HostVO>();
            List<HostVO> hostsMatchingTemplateTag = new ArrayList<HostVO>();
            if (hasSvcOfferingTag) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Looking for hosts having tag specified on SvcOffering:" + hostTagOnOffering);
                }
                hostsMatchingOfferingTag = _hostDao.listByHostTag(type, clusterId, podId, dcId, hostTagOnOffering);
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Hosts with tag '" + hostTagOnOffering + "' are:" + hostsMatchingOfferingTag);
                }
            }
            if (hasTemplateTag) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Looking for hosts having tag specified on Template:" + hostTagOnTemplate);
                }
                hostsMatchingTemplateTag = _hostDao.listByHostTag(type, clusterId, podId, dcId, hostTagOnTemplate);
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Hosts with tag '" + hostTagOnTemplate + "' are:" + hostsMatchingTemplateTag);
                }
            }
            if (hasSvcOfferingTag && hasTemplateTag) {
                hostsMatchingOfferingTag.retainAll(hostsMatchingTemplateTag);
                clusterHosts = _hostDao.listByHostTag(type, clusterId, podId, dcId, hostTagOnTemplate);
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Found " + hostsMatchingOfferingTag.size() + " Hosts satisfying both tags, host ids are:" + hostsMatchingOfferingTag);
                }
                clusterHosts = hostsMatchingOfferingTag;
            } else {
                if (hasSvcOfferingTag) {
                    clusterHosts = hostsMatchingOfferingTag;
                } else {
                    clusterHosts = hostsMatchingTemplateTag;
                }
            }
        }
    }
    if (isVMDeployedWithUefi) {
        clusterHosts.retainAll(hostsMatchingUefiTag);
    }
    // add all hosts that we are not considering to the avoid list
    List<HostVO> allhostsInCluster = _hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null);
    allhostsInCluster.removeAll(clusterHosts);
    for (HostVO host : allhostsInCluster) {
        avoid.addHost(host.getId());
    }
    return allocateTo(plan, offering, template, avoid, clusterHosts, returnUpTo, considerReservedCapacity, account);
}
Also used : UserVmDetailVO(com.cloud.vm.UserVmDetailVO) Account(com.cloud.user.Account) ServiceOffering(com.cloud.offering.ServiceOffering) VMTemplateVO(com.cloud.storage.VMTemplateVO) ArrayList(java.util.ArrayList) HostVO(com.cloud.host.HostVO)

Aggregations

ServiceOffering (com.cloud.offering.ServiceOffering)103 ArrayList (java.util.ArrayList)34 Account (com.cloud.user.Account)30 DataCenter (com.cloud.dc.DataCenter)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)20 HashMap (java.util.HashMap)18 VirtualMachine (com.cloud.vm.VirtualMachine)17 VMTemplateVO (com.cloud.storage.VMTemplateVO)14 UserVm (com.cloud.uservm.UserVm)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 Map (java.util.Map)14 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)13 ServerApiException (com.cloud.api.ServerApiException)12 HostVO (com.cloud.host.HostVO)12 DiskOffering (com.cloud.offering.DiskOffering)11 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)11 Host (com.cloud.host.Host)10 Network (com.cloud.network.Network)10 List (java.util.List)10 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)9