diff --git a/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java b/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java index 0e89e9a5187..6007f34d603 100644 --- a/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java +++ b/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java @@ -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> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.VIRTUAL_MACHINE_ID, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); - s_properties.add(new Pair(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> getProperties() { - return s_properties; - } - @Override - public List> execute(Map 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 offerings = (List)getResponseObject(); + + List response = new ArrayList(); + 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 offerings = getManagementServer().searchForServiceOfferings(c); - if (offerings == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "unable to find service offerings"); - } - List> offeringTags = new ArrayList>(); - Object[] soTag = new Object[offerings.size()]; - int i = 0; - for (ServiceOfferingVO offering : offerings) - { - List> offeringData = new ArrayList>(); - - offeringData.add(new Pair(BaseCmd.Properties.ID.getName(), Long.toString(offering.getId()))); - offeringData.add(new Pair(BaseCmd.Properties.NAME.getName(), offering.getName())); - offeringData.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT.getName(), offering.getDisplayText())); - offeringData.add(new Pair(BaseCmd.Properties.CPU_NUMBER.getName(), Integer.valueOf(offering.getCpu()).toString())); - offeringData.add(new Pair(BaseCmd.Properties.CPU_SPEED.getName(), Integer.valueOf(offering.getSpeed()).toString())); - offeringData.add(new Pair(BaseCmd.Properties.MEMORY.getName(), Integer.valueOf(offering.getRamSize()).toString())); - offeringData.add(new Pair(BaseCmd.Properties.CREATED.getName(), getDateString(offering.getCreated()))); - String storageType = offering.getUseLocalStorage() ? "local" : "shared"; - offeringData.add(new Pair(BaseCmd.Properties.STORAGE_TYPE.getName(), storageType)); - offeringData.add(new Pair(BaseCmd.Properties.OFFER_HA.getName(), offering.getOfferHA())); - offeringData.add(new Pair(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName(), (offering.getGuestIpType().equals(GuestIpType.Virtualized)))); - offeringData.add(new Pair(BaseCmd.Properties.TAGS.getName(), (offering.getTags()))); - - soTag[i++] = offeringData; - } - Pair offeringTag = new Pair("serviceoffering", soTag); - offeringTags.add(offeringTag); - return offeringTags; + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 46bab3b43c9..6a713278380 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -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 searchForServiceOfferings(Criteria c); + List searchForServiceOfferings(ListServiceOfferingsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; /** * Searches for Clusters by the specified search criteria diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 9ac74651f58..7963f2caf8b 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -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 searchForServiceOfferings(Criteria c) { - Filter searchFilter = new Filter(ServiceOfferingVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit()); + public List searchForServiceOfferings(ListServiceOfferingsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { + Filter searchFilter = new Filter(ServiceOfferingVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal()); SearchCriteria 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 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 searchForClusters(ListClustersCmd cmd) { Filter searchFilter = new Filter(ClusterVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());