Search in sources :

Example 6 with LoadBalancerResponse

use of com.cloud.api.response.LoadBalancerResponse in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createLoadBalancerResponse.

@Override
public LoadBalancerResponse createLoadBalancerResponse(final LoadBalancer loadBalancer) {
    final LoadBalancerResponse lbResponse = new LoadBalancerResponse();
    lbResponse.setId(loadBalancer.getUuid());
    lbResponse.setName(loadBalancer.getName());
    lbResponse.setDescription(loadBalancer.getDescription());
    final List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(loadBalancer.getId());
    lbResponse.setCidrList(StringUtils.join(cidrs, ","));
    final IPAddressVO publicIp = ApiDBUtils.findIpAddressById(loadBalancer.getSourceIpAddressId());
    lbResponse.setPublicIpId(publicIp.getUuid());
    lbResponse.setPublicIp(publicIp.getAddress().addr());
    lbResponse.setPublicPort(Integer.toString(loadBalancer.getSourcePortStart()));
    lbResponse.setPrivatePort(Integer.toString(loadBalancer.getDefaultPortStart()));
    lbResponse.setAlgorithm(loadBalancer.getAlgorithm());
    lbResponse.setLbProtocol(loadBalancer.getLbProtocol());
    lbResponse.setForDisplay(loadBalancer.isDisplay());
    lbResponse.setClientTimeout(loadBalancer.getClientTimeout());
    lbResponse.setServerTimeout(loadBalancer.getServerTimeout());
    final FirewallRule.State state = loadBalancer.getState();
    String stateToSet = state.toString();
    if (state.equals(FirewallRule.State.Revoke)) {
        stateToSet = "Deleting";
    }
    lbResponse.setState(stateToSet);
    populateOwner(lbResponse, loadBalancer);
    final DataCenter zone = ApiDBUtils.findZoneById(publicIp.getDataCenterId());
    if (zone != null) {
        lbResponse.setZoneId(zone.getUuid());
    }
    // set tag information
    final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.LoadBalancer, loadBalancer.getId());
    final List<ResourceTagResponse> tagResponses = new ArrayList<>();
    for (final ResourceTag tag : tags) {
        final ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        if (tagResponse != null) {
            tagResponses.add(tagResponse);
        }
    }
    lbResponse.setTags(tagResponses);
    final Network ntwk = ApiDBUtils.findNetworkById(loadBalancer.getNetworkId());
    lbResponse.setNetworkId(ntwk.getUuid());
    lbResponse.setObjectName("loadbalancer");
    return lbResponse;
}
Also used : ArrayList(java.util.ArrayList) LoadBalancerResponse(com.cloud.api.response.LoadBalancerResponse) DataCenter(com.cloud.legacymodel.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) PhysicalNetwork(com.cloud.network.PhysicalNetwork) Network(com.cloud.legacymodel.network.Network) ResourceTagResponse(com.cloud.api.response.ResourceTagResponse) IPAddressVO(com.cloud.network.dao.IPAddressVO) FirewallRule(com.cloud.legacymodel.network.FirewallRule)

Example 7 with LoadBalancerResponse

use of com.cloud.api.response.LoadBalancerResponse in project CloudStack-archive by CloudStack-extras.

the class CreateLoadBalancerRuleCmd method execute.

@Override
public void execute() throws ResourceAllocationException, ResourceUnavailableException {
    UserContext callerContext = UserContext.current();
    boolean success = true;
    LoadBalancer rule = null;
    try {
        UserContext.current().setEventDetails("Rule Id: " + getEntityId());
        if (getOpenFirewall()) {
            success = success && _firewallService.applyFirewallRules(getSourceIpAddressId(), callerContext.getCaller());
        }
        // State might be different after the rule is applied, so get new object here
        rule = _entityMgr.findById(LoadBalancer.class, getEntityId());
        LoadBalancerResponse lbResponse = new LoadBalancerResponse();
        if (rule != null) {
            lbResponse = _responseGenerator.createLoadBalancerResponse(rule);
            setResponseObject(lbResponse);
        }
        lbResponse.setResponseName(getCommandName());
    } catch (Exception ex) {
        s_logger.warn("Failed to create LB rule due to exception ", ex);
    } finally {
        if (!success || rule == null) {
            if (getOpenFirewall()) {
                _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
            }
            // no need to apply the rule on the backend as it exists in the db only
            _lbService.deleteLoadBalancerRule(getEntityId(), false);
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create load balancer rule");
        }
    }
}
Also used : LoadBalancerResponse(com.cloud.api.response.LoadBalancerResponse) ServerApiException(com.cloud.api.ServerApiException) UserContext(com.cloud.user.UserContext) LoadBalancer(com.cloud.network.rules.LoadBalancer) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ServerApiException(com.cloud.api.ServerApiException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException)

Aggregations

LoadBalancerResponse (com.cloud.api.response.LoadBalancerResponse)7 ServerApiException (com.cloud.api.ServerApiException)4 LoadBalancer (com.cloud.legacymodel.network.LoadBalancer)3 LoadBalancer (com.cloud.network.rules.LoadBalancer)3 ArrayList (java.util.ArrayList)3 ListResponse (com.cloud.api.response.ListResponse)2 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)2 ResourceTagResponse (com.cloud.api.response.ResourceTagResponse)1 CallContext (com.cloud.context.CallContext)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 DataCenter (com.cloud.legacymodel.dc.DataCenter)1 InsufficientAddressCapacityException (com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException)1 NetworkRuleConflictException (com.cloud.legacymodel.exceptions.NetworkRuleConflictException)1 ResourceAllocationException (com.cloud.legacymodel.exceptions.ResourceAllocationException)1 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)1 FirewallRule (com.cloud.legacymodel.network.FirewallRule)1