mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Refactoring listServiceOfferings to new API framework.
This commit is contained in:
parent
a905442b2e
commit
e6ebe3ac77
@ -20,36 +20,22 @@ package com.cloud.api.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.BaseListCmd;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.response.ServiceOfferingResponse;
|
||||
import com.cloud.offering.ServiceOffering.GuestIpType;
|
||||
import com.cloud.server.Criteria;
|
||||
import com.cloud.serializer.SerializerHelper;
|
||||
import com.cloud.service.ServiceOfferingVO;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.UserVmVO;
|
||||
|
||||
public class ListServiceOfferingsCmd extends BaseCmd {
|
||||
@Implementation(method="searchForServiceOfferings")
|
||||
public class ListServiceOfferingsCmd extends BaseListCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(ListServiceOfferingsCmd.class.getName());
|
||||
|
||||
private static final String s_name = "listserviceofferingsresponse";
|
||||
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
|
||||
|
||||
static {
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.VIRTUAL_MACHINE_ID, Boolean.FALSE));
|
||||
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.KEYWORD, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGE, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
@ -88,83 +74,29 @@ public class ListServiceOfferingsCmd extends BaseCmd {
|
||||
public String getName() {
|
||||
return s_name;
|
||||
}
|
||||
@Override
|
||||
public List<Pair<Enum, Boolean>> getProperties() {
|
||||
return s_properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||
String name = (String)params.get(BaseCmd.Properties.NAME.getName());
|
||||
Long id = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
Long vmId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName());
|
||||
String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName());
|
||||
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
|
||||
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
|
||||
|
||||
Long startIndex = Long.valueOf(0);
|
||||
int pageSizeNum = 50;
|
||||
if (pageSize != null) {
|
||||
pageSizeNum = pageSize.intValue();
|
||||
}
|
||||
if (page != null) {
|
||||
int pageNum = page.intValue();
|
||||
if (pageNum > 0) {
|
||||
startIndex = Long.valueOf(pageSizeNum * (pageNum-1));
|
||||
}
|
||||
}
|
||||
Criteria c = new Criteria("created", Boolean.FALSE, startIndex, Long.valueOf(pageSizeNum));
|
||||
if (keyword != null) {
|
||||
c.addCriteria(Criteria.KEYWORD, keyword);
|
||||
} else {
|
||||
c.addCriteria(Criteria.ID, id);
|
||||
c.addCriteria(Criteria.NAME, name);
|
||||
}
|
||||
|
||||
//If vmId is present in the list of parameters, verify it
|
||||
if (vmId != null) {
|
||||
UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue());
|
||||
if ((vmInstance == null) || (vmInstance.getRemoved() != null)) {
|
||||
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId);
|
||||
}
|
||||
if ((account != null) && !isAdmin(account.getType())) {
|
||||
if (account.getId().longValue() != vmInstance.getAccountId()) {
|
||||
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account");
|
||||
}
|
||||
}
|
||||
if (keyword == null)
|
||||
c.addCriteria(Criteria.INSTANCEID, vmId);
|
||||
@Override @SuppressWarnings("unchecked")
|
||||
public String getResponse() {
|
||||
List<ServiceOfferingVO> offerings = (List<ServiceOfferingVO>)getResponseObject();
|
||||
|
||||
List<ServiceOfferingResponse> response = new ArrayList<ServiceOfferingResponse>();
|
||||
for (ServiceOfferingVO offering : offerings) {
|
||||
ServiceOfferingResponse offeringResponse = new ServiceOfferingResponse();
|
||||
offeringResponse.setId(offering.getId());
|
||||
offeringResponse.setName(offering.getName());
|
||||
offeringResponse.setDisplayText(offering.getDisplayText());
|
||||
offeringResponse.setCpuNumber(offering.getCpu());
|
||||
offeringResponse.setCpuSpeed(offering.getSpeed());
|
||||
offeringResponse.setMemory(offering.getRamSize());
|
||||
offeringResponse.setCreated(offering.getCreated());
|
||||
offeringResponse.setStorageType(offering.getUseLocalStorage() ? "local" : "shared");
|
||||
offeringResponse.setOfferHa(offering.getOfferHA());
|
||||
offeringResponse.setUseVirtualNetwork(offering.getGuestIpType().equals(GuestIpType.Virtualized));
|
||||
offeringResponse.setTags(offering.getTags());
|
||||
|
||||
response.add(offeringResponse);
|
||||
}
|
||||
|
||||
List<ServiceOfferingVO> offerings = getManagementServer().searchForServiceOfferings(c);
|
||||
if (offerings == null) {
|
||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "unable to find service offerings");
|
||||
}
|
||||
List<Pair<String, Object>> offeringTags = new ArrayList<Pair<String, Object>>();
|
||||
Object[] soTag = new Object[offerings.size()];
|
||||
int i = 0;
|
||||
for (ServiceOfferingVO offering : offerings)
|
||||
{
|
||||
List<Pair<String, Object>> offeringData = new ArrayList<Pair<String, Object>>();
|
||||
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), Long.toString(offering.getId())));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), offering.getName()));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), offering.getDisplayText()));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.CPU_NUMBER.getName(), Integer.valueOf(offering.getCpu()).toString()));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.CPU_SPEED.getName(), Integer.valueOf(offering.getSpeed()).toString()));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.MEMORY.getName(), Integer.valueOf(offering.getRamSize()).toString()));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(offering.getCreated())));
|
||||
String storageType = offering.getUseLocalStorage() ? "local" : "shared";
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.STORAGE_TYPE.getName(), storageType));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.OFFER_HA.getName(), offering.getOfferHA()));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName(), (offering.getGuestIpType().equals(GuestIpType.Virtualized))));
|
||||
offeringData.add(new Pair<String, Object>(BaseCmd.Properties.TAGS.getName(), (offering.getTags())));
|
||||
|
||||
soTag[i++] = offeringData;
|
||||
}
|
||||
Pair<String, Object> offeringTag = new Pair<String, Object>("serviceoffering", soTag);
|
||||
offeringTags.add(offeringTag);
|
||||
return offeringTags;
|
||||
return SerializerHelper.toSerializedString(response);
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,6 +53,7 @@ import com.cloud.api.commands.ListPortForwardingServicesCmd;
|
||||
import com.cloud.api.commands.ListPreallocatedLunsCmd;
|
||||
import com.cloud.api.commands.ListPublicIpAddressesCmd;
|
||||
import com.cloud.api.commands.ListRoutersCmd;
|
||||
import com.cloud.api.commands.ListServiceOfferingsCmd;
|
||||
import com.cloud.api.commands.ListTemplatesCmd;
|
||||
import com.cloud.api.commands.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
@ -873,10 +874,10 @@ public interface ManagementServer {
|
||||
/**
|
||||
* Searches for Service Offerings by the specified search criteria
|
||||
* Can search by: "name"
|
||||
* @param c
|
||||
* @param cmd
|
||||
* @return List of ServiceOfferings
|
||||
*/
|
||||
List<ServiceOfferingVO> searchForServiceOfferings(Criteria c);
|
||||
List<ServiceOfferingVO> searchForServiceOfferings(ListServiceOfferingsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
|
||||
|
||||
/**
|
||||
* Searches for Clusters by the specified search criteria
|
||||
|
||||
@ -90,6 +90,7 @@ import com.cloud.api.commands.ListPortForwardingServicesCmd;
|
||||
import com.cloud.api.commands.ListPreallocatedLunsCmd;
|
||||
import com.cloud.api.commands.ListPublicIpAddressesCmd;
|
||||
import com.cloud.api.commands.ListRoutersCmd;
|
||||
import com.cloud.api.commands.ListServiceOfferingsCmd;
|
||||
import com.cloud.api.commands.ListTemplatesCmd;
|
||||
import com.cloud.api.commands.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
@ -3833,14 +3834,14 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceOfferingVO> searchForServiceOfferings(Criteria c) {
|
||||
Filter searchFilter = new Filter(ServiceOfferingVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
|
||||
public List<ServiceOfferingVO> searchForServiceOfferings(ListServiceOfferingsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
|
||||
Filter searchFilter = new Filter(ServiceOfferingVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
|
||||
SearchCriteria<ServiceOfferingVO> sc = _offeringsDao.createSearchCriteria();
|
||||
|
||||
Object name = c.getCriteria(Criteria.NAME);
|
||||
Object vmIdObj = c.getCriteria(Criteria.INSTANCEID);
|
||||
Object id = c.getCriteria(Criteria.ID);
|
||||
Object keyword = c.getCriteria(Criteria.KEYWORD);
|
||||
Object name = cmd.getServiceOfferingName();
|
||||
Object id = cmd.getId();
|
||||
Object keyword = cmd.getKeyword();
|
||||
Long vmId = cmd.getVirtualMachineId();
|
||||
|
||||
if (keyword != null) {
|
||||
SearchCriteria<ServiceOfferingVO> ssc = _offeringsDao.createSearchCriteria();
|
||||
@ -3848,6 +3849,25 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
|
||||
|
||||
sc.addAnd("name", SearchCriteria.Op.SC, ssc);
|
||||
} else if (vmId != null) {
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
|
||||
UserVmVO vmInstance = _userVmDao.findById(vmId);
|
||||
if ((vmInstance == null) || (vmInstance.getRemoved() != null)) {
|
||||
throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
|
||||
}
|
||||
if ((account != null) && !isAdmin(account.getType())) {
|
||||
if (account.getId().longValue() != vmInstance.getAccountId()) {
|
||||
throw new PermissionDeniedException("unable to find a virtual machine with id " + vmId + " for this account");
|
||||
}
|
||||
}
|
||||
|
||||
ServiceOfferingVO offering = _offeringsDao.findById(vmInstance.getServiceOfferingId());
|
||||
sc.addAnd("id", SearchCriteria.Op.NEQ, offering.getId());
|
||||
|
||||
// Only return offerings with the same Guest IP type and storage pool preference
|
||||
sc.addAnd("guestIpType", SearchCriteria.Op.EQ, offering.getGuestIpType());
|
||||
sc.addAnd("useLocalStorage", SearchCriteria.Op.EQ, offering.getUseLocalStorage());
|
||||
}
|
||||
|
||||
if (id != null) {
|
||||
@ -3858,21 +3878,9 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
sc.addAnd("name", SearchCriteria.Op.LIKE, "%" + name + "%");
|
||||
}
|
||||
|
||||
if (vmIdObj != null) {
|
||||
UserVmVO vm = _userVmDao.findById((Long) vmIdObj);
|
||||
if (vm != null) {
|
||||
ServiceOfferingVO offering = _offeringsDao.findById(vm.getServiceOfferingId());
|
||||
sc.addAnd("id", SearchCriteria.Op.NEQ, offering.getId());
|
||||
|
||||
// Only return offerings with the same Guest IP type and storage pool preference
|
||||
sc.addAnd("guestIpType", SearchCriteria.Op.EQ, offering.getGuestIpType());
|
||||
sc.addAnd("useLocalStorage", SearchCriteria.Op.EQ, offering.getUseLocalStorage());
|
||||
}
|
||||
}
|
||||
|
||||
return _offeringsDao.search(sc, searchFilter);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<ClusterVO> searchForClusters(ListClustersCmd cmd) {
|
||||
Filter searchFilter = new Filter(ClusterVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user