Search in sources :

Example 36 with ServiceOffering

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

the class AutoScaleManagerImpl method createAutoScaleVmProfile.

@Override
@ActionEvent(eventType = EventTypes.EVENT_AUTOSCALEVMPROFILE_CREATE, eventDescription = "creating autoscale vm profile", create = true)
public AutoScaleVmProfile createAutoScaleVmProfile(CreateAutoScaleVmProfileCmd cmd) {
    Account owner = _accountDao.findById(cmd.getAccountId());
    Account caller = CallContext.current().getCallingAccount();
    _accountMgr.checkAccess(caller, null, true, owner);
    long zoneId = cmd.getZoneId();
    long serviceOfferingId = cmd.getServiceOfferingId();
    long autoscaleUserId = cmd.getAutoscaleUserId();
    DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("Unable to find zone by id");
    }
    ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
    if (serviceOffering == null) {
        throw new InvalidParameterValueException("Unable to find service offering by id");
    }
    // validations
    HashMap<String, String> deployParams = cmd.getDeployParamMap();
    if (deployParams.containsKey("networks") && deployParams.get("networks").length() > 0) {
        throw new InvalidParameterValueException("'networks' is not a valid parameter, network for an AutoScaled VM is chosen automatically. An autoscaled VM is deployed in the loadbalancer's network");
    }
    /*
         * Just for making sure the values are right in other deploy params.
         * For ex. if projectId is given as a string instead of an long value, this
         * will be throwing an error.
         */
    dispatchChainFactory.getStandardDispatchChain().dispatch(new DispatchTask(ComponentContext.inject(DeployVMCmd.class), deployParams));
    AutoScaleVmProfileVO profileVO = new AutoScaleVmProfileVO(cmd.getZoneId(), cmd.getDomainId(), cmd.getAccountId(), cmd.getServiceOfferingId(), cmd.getTemplateId(), cmd.getOtherDeployParams(), cmd.getCounterParamList(), cmd.getDestroyVmGraceperiod(), autoscaleUserId);
    if (cmd.getDisplay() != null) {
        profileVO.setDisplay(cmd.getDisplay());
    }
    profileVO = checkValidityAndPersist(profileVO);
    s_logger.info("Successfully create AutoScale Vm Profile with Id: " + profileVO.getId());
    return profileVO;
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ServiceOffering(com.cloud.offering.ServiceOffering) DispatchTask(com.cloud.api.dispatch.DispatchTask) ActionEvent(com.cloud.event.ActionEvent)

Example 37 with ServiceOffering

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

the class UserVmManagerTest method testScaleVMF3.

// Test scaleVm for Stopped vm.
//@Test(expected=InvalidParameterValueException.class)
public void testScaleVMF3() 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);
    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.Stopped).when(_vmInstance).getState();
    when(_vmDao.findById(anyLong())).thenReturn(null);
    doReturn(true).when(_itMgr).upgradeVmDb(anyLong(), anyLong());
    //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) AccountVO(com.cloud.user.AccountVO)

Example 38 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(), so1, false)).thenReturn(_vmInstance);
    doReturn(true).when(_itMgr).upgradeVmDb(anyLong(), anyLong());
    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) AccountVO(com.cloud.user.AccountVO)

Example 39 with ServiceOffering

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

the class UserVmManagerTest method testScaleVMF2.

// Test scaleVm on equal service offerings.
@Test(expected = InvalidParameterValueException.class)
public void testScaleVMF2() 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);
    when(_vmInstanceDao.findById(anyLong())).thenReturn(_vmInstance);
    doReturn(Hypervisor.HypervisorType.XenServer).when(_vmInstance).getHypervisorType();
    doReturn(VirtualMachine.State.Running).when(_vmInstance).getState();
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _templateMock);
    doNothing().when(_itMgr).checkIfCanUpgrade(_vmMock, _offeringVo);
    ServiceOffering so1 = getSvcoffering(512);
    ServiceOffering so2 = getSvcoffering(256);
    when(_offeringDao.findById(anyLong())).thenReturn((ServiceOfferingVO) so1);
    when(_offeringDao.findByIdIncludingRemoved(anyLong(), anyLong())).thenReturn((ServiceOfferingVO) so1);
    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) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 40 with ServiceOffering

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

the class RandomAllocator method allocateTo.

@Override
public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan plan, Type type, ExcludeList avoid, List<? extends Host> hosts, int returnUpTo, boolean considerReservedCapacity) {
    long dcId = plan.getDataCenterId();
    Long podId = plan.getPodId();
    Long clusterId = plan.getClusterId();
    ServiceOffering offering = vmProfile.getServiceOffering();
    List<Host> suitableHosts = new ArrayList<Host>();
    List<Host> hostsCopy = new ArrayList<Host>(hosts);
    if (type == Host.Type.Storage) {
        return suitableHosts;
    }
    String hostTag = offering.getHostTag();
    if (hostTag != null) {
        s_logger.debug("Looking for hosts in dc: " + dcId + "  pod:" + podId + "  cluster:" + clusterId + " having host tag:" + hostTag);
    } else {
        s_logger.debug("Looking for hosts in dc: " + dcId + "  pod:" + podId + "  cluster:" + clusterId);
    }
    // list all computing hosts, regardless of whether they support routing...it's random after all
    if (hostTag != null) {
        hostsCopy.retainAll(_hostDao.listByHostTag(type, clusterId, podId, dcId, hostTag));
    } else {
        hostsCopy.retainAll(_resourceMgr.listAllUpAndEnabledHosts(type, clusterId, podId, dcId));
    }
    s_logger.debug("Random Allocator found " + hostsCopy.size() + "  hosts");
    if (hostsCopy.size() == 0) {
        return suitableHosts;
    }
    Collections.shuffle(hostsCopy);
    for (Host host : hostsCopy) {
        if (suitableHosts.size() == returnUpTo) {
            break;
        }
        if (!avoid.shouldAvoid(host)) {
            suitableHosts.add(host);
        } else {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Host name: " + host.getName() + ", hostId: " + host.getId() + " is in avoid set, " + "skipping this and trying other available hosts");
            }
        }
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Random Host Allocator returning " + suitableHosts.size() + " suitable hosts");
    }
    return suitableHosts;
}
Also used : ServiceOffering(com.cloud.offering.ServiceOffering) ArrayList(java.util.ArrayList) Host(com.cloud.host.Host)

Aggregations

ServiceOffering (com.cloud.offering.ServiceOffering)46 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)18 Account (com.cloud.user.Account)14 ArrayList (java.util.ArrayList)13 DataCenter (com.cloud.dc.DataCenter)11 VirtualMachine (com.cloud.vm.VirtualMachine)9 ServerApiException (org.apache.cloudstack.api.ServerApiException)8 ServerApiException (com.cloud.api.ServerApiException)5 VirtualMachineTemplate (com.cloud.template.VirtualMachineTemplate)5 UserVm (com.cloud.uservm.UserVm)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 HashMap (java.util.HashMap)5 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 Host (com.cloud.host.Host)4 HostVO (com.cloud.host.HostVO)4 DiskOffering (com.cloud.offering.DiskOffering)4 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)4 VMTemplateVO (com.cloud.storage.VMTemplateVO)4 ServiceOfferingResponse (com.cloud.api.response.ServiceOfferingResponse)3