Search in sources :

Example 11 with ListResponse

use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.

the class QuotaTariffListCmd method execute.

@Override
public void execute() {
    final List<QuotaTariffVO> result = _responseBuilder.listQuotaTariffPlans(this);
    final List<QuotaTariffResponse> responses = new ArrayList<QuotaTariffResponse>();
    for (final QuotaTariffVO resource : result) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Result desc=" + resource.getDescription() + " date=" + resource.getEffectiveOn() + " val=" + resource.getCurrencyValue());
        }
        responses.add(_responseBuilder.createQuotaTariffResponse(resource));
    }
    final ListResponse<QuotaTariffResponse> response = new ListResponse<QuotaTariffResponse>();
    response.setResponses(responses);
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) QuotaTariffVO(org.apache.cloudstack.quota.vo.QuotaTariffVO) ArrayList(java.util.ArrayList) QuotaTariffResponse(org.apache.cloudstack.api.response.QuotaTariffResponse)

Example 12 with ListResponse

use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.

the class ListVmwareDcsCmd method execute.

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    List<? extends VmwareDatacenter> vmwareDcList = null;
    try {
        vmwareDcList = _vmwareDatacenterService.listVmwareDatacenters(this);
    } catch (InvalidParameterValueException ie) {
        throw new InvalidParameterValueException("Invalid zone id " + getZoneId());
    } catch (Exception e) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to find associated VMware DCs associated with zone " + getZoneId());
    }
    ListResponse<VmwareDatacenterResponse> response = new ListResponse<VmwareDatacenterResponse>();
    List<VmwareDatacenterResponse> vmwareDcResponses = new ArrayList<VmwareDatacenterResponse>();
    if (vmwareDcList != null && vmwareDcList.size() > 0) {
        for (VmwareDatacenter vmwareDc : vmwareDcList) {
            VmwareDatacenterResponse vmwareDcResponse = new VmwareDatacenterResponse();
            vmwareDcResponse.setId(vmwareDc.getUuid());
            vmwareDcResponse.setVcenter(vmwareDc.getVcenterHost());
            vmwareDcResponse.setName(vmwareDc.getVmwareDatacenterName());
            vmwareDcResponse.setZoneId(getZoneId());
            vmwareDcResponse.setObjectName("VMwareDC");
            vmwareDcResponses.add(vmwareDcResponse);
        }
    }
    response.setResponses(vmwareDcResponses);
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : VmwareDatacenterResponse(org.apache.cloudstack.api.response.VmwareDatacenterResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ListResponse(org.apache.cloudstack.api.response.ListResponse) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) VmwareDatacenter(com.cloud.hypervisor.vmware.VmwareDatacenter) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException)

Example 13 with ListResponse

use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.

the class ListSrxFirewallNetworksCmd method execute.

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
    try {
        List<? extends Network> networks = _srxFwService.listNetworks(this);
        ListResponse<NetworkResponse> response = new ListResponse<NetworkResponse>();
        List<NetworkResponse> networkResponses = new ArrayList<NetworkResponse>();
        if (networks != null && !networks.isEmpty()) {
            for (Network network : networks) {
                NetworkResponse networkResponse = _responseGenerator.createNetworkResponse(ResponseView.Full, network);
                networkResponses.add(networkResponse);
            }
        }
        response.setResponses(networkResponses);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } catch (InvalidParameterValueException invalidParamExcp) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, invalidParamExcp.getMessage());
    } catch (CloudRuntimeException runtimeExcp) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, runtimeExcp.getMessage());
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) NetworkResponse(org.apache.cloudstack.api.response.NetworkResponse) ArrayList(java.util.ArrayList)

Example 14 with ListResponse

use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.

the class ApiResponseSerializer method toXMLSerializedString.

private static String toXMLSerializedString(ResponseObject result, StringBuilder log) {
    if (result != null && log != null) {
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.append("<").append(result.getResponseName()).append(" cloud-stack-version=\"").append(ApiDBUtils.getVersion()).append("\">");
        log.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        log.append("<").append(result.getResponseName()).append(" cloud-stack-version=\"").append(ApiDBUtils.getVersion()).append("\">");
        if (result instanceof ListResponse) {
            Integer count = ((ListResponse) result).getCount();
            if (count != null && count != 0) {
                sb.append("<").append(ApiConstants.COUNT).append(">").append(((ListResponse) result).getCount()).append("</").append(ApiConstants.COUNT).append(">");
                log.append("<").append(ApiConstants.COUNT).append(">").append(((ListResponse) result).getCount()).append("</").append(ApiConstants.COUNT).append(">");
            }
            List<? extends ResponseObject> responses = ((ListResponse) result).getResponses();
            if ((responses != null) && !responses.isEmpty()) {
                for (ResponseObject obj : responses) {
                    serializeResponseObjXML(sb, log, obj);
                }
            }
        } else {
            if (result instanceof CreateCmdResponse || result instanceof AsyncJobResponse || result instanceof AuthenticationCmdResponse) {
                serializeResponseObjFieldsXML(sb, log, result);
            } else {
                serializeResponseObjXML(sb, log, result);
            }
        }
        sb.append("</").append(result.getResponseName()).append(">");
        log.append("</").append(result.getResponseName()).append(">");
        return sb.toString();
    }
    return null;
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) ResponseObject(org.apache.cloudstack.api.ResponseObject) CreateCmdResponse(org.apache.cloudstack.api.response.CreateCmdResponse) AuthenticationCmdResponse(org.apache.cloudstack.api.response.AuthenticationCmdResponse)

Example 15 with ListResponse

use of org.apache.cloudstack.api.response.ListResponse in project cloudstack by apache.

the class ApiResponseSerializer method toJSONSerializedString.

public static String toJSONSerializedString(ResponseObject result, StringBuilder log) {
    if (result != null && log != null) {
        Gson responseBuilder = ApiResponseGsonHelper.getBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create();
        Gson logBuilder = ApiResponseGsonHelper.getLogBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create();
        StringBuilder sb = new StringBuilder();
        sb.append("{\"").append(result.getResponseName()).append("\":");
        log.append("{\"").append(result.getResponseName()).append("\":");
        if (result instanceof ListResponse) {
            List<? extends ResponseObject> responses = ((ListResponse) result).getResponses();
            Integer count = ((ListResponse) result).getCount();
            boolean nonZeroCount = (count != null && count.longValue() != 0);
            if (nonZeroCount) {
                sb.append("{\"").append(ApiConstants.COUNT).append("\":").append(count);
                log.append("{\"").append(ApiConstants.COUNT).append("\":").append(count);
            }
            if ((responses != null) && !responses.isEmpty()) {
                String jsonStr = responseBuilder.toJson(responses.get(0));
                jsonStr = unescape(jsonStr);
                String logStr = logBuilder.toJson(responses.get(0));
                logStr = unescape(logStr);
                if (nonZeroCount) {
                    sb.append(",\"").append(responses.get(0).getObjectName()).append("\":[").append(jsonStr);
                    log.append(",\"").append(responses.get(0).getObjectName()).append("\":[").append(logStr);
                }
                for (int i = 1; i < ((ListResponse) result).getResponses().size(); i++) {
                    jsonStr = responseBuilder.toJson(responses.get(i));
                    jsonStr = unescape(jsonStr);
                    logStr = logBuilder.toJson(responses.get(i));
                    logStr = unescape(logStr);
                    sb.append(",").append(jsonStr);
                    log.append(",").append(logStr);
                }
                sb.append("]}");
                log.append("]}");
            } else {
                if (!nonZeroCount) {
                    sb.append("{");
                    log.append("{");
                }
                sb.append("}");
                log.append("}");
            }
        } else if (result instanceof SuccessResponse) {
            sb.append("{\"success\":\"").append(((SuccessResponse) result).getSuccess()).append("\"}");
            log.append("{\"success\":\"").append(((SuccessResponse) result).getSuccess()).append("\"}");
        } else if (result instanceof ExceptionResponse) {
            String jsonErrorText = responseBuilder.toJson(result);
            jsonErrorText = unescape(jsonErrorText);
            sb.append(jsonErrorText);
            log.append(jsonErrorText);
        } else {
            String jsonStr = responseBuilder.toJson(result);
            if (jsonStr != null && !jsonStr.isEmpty()) {
                jsonStr = unescape(jsonStr);
                if (result instanceof AsyncJobResponse || result instanceof CreateCmdResponse || result instanceof AuthenticationCmdResponse) {
                    sb.append(jsonStr);
                } else {
                    sb.append("{\"").append(result.getObjectName()).append("\":").append(jsonStr).append("}");
                }
            } else {
                sb.append("{}");
            }
            String logStr = logBuilder.toJson(result);
            if (logStr != null && !logStr.isEmpty()) {
                logStr = unescape(logStr);
                if (result instanceof AsyncJobResponse || result instanceof CreateCmdResponse || result instanceof AuthenticationCmdResponse) {
                    log.append(logStr);
                } else {
                    log.append("{\"").append(result.getObjectName()).append("\":").append(logStr).append("}");
                }
            } else {
                log.append("{}");
            }
        }
        sb.append("}");
        log.append("}");
        return sb.toString();
    }
    return null;
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ExceptionResponse(org.apache.cloudstack.api.response.ExceptionResponse) ListResponse(org.apache.cloudstack.api.response.ListResponse) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) Gson(com.google.gson.Gson) CreateCmdResponse(org.apache.cloudstack.api.response.CreateCmdResponse) AuthenticationCmdResponse(org.apache.cloudstack.api.response.AuthenticationCmdResponse)

Aggregations

ListResponse (org.apache.cloudstack.api.response.ListResponse)149 ArrayList (java.util.ArrayList)134 List (java.util.List)62 ServerApiException (org.apache.cloudstack.api.ServerApiException)44 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)29 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)21 Network (com.cloud.network.Network)9 DedicatedResources (com.cloud.dc.DedicatedResources)8 NetworkResponse (org.apache.cloudstack.api.response.NetworkResponse)8 ResponseView (org.apache.cloudstack.api.ResponseObject.ResponseView)7 TemplateResponse (org.apache.cloudstack.api.response.TemplateResponse)7 Host (com.cloud.host.Host)6 Domain (com.cloud.domain.Domain)5 VirtualMachineTemplate (com.cloud.template.VirtualMachineTemplate)5 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)4 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 Account (com.cloud.user.Account)4