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();
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations