Search in sources :

Example 1 with FirewallRuleResponse

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

the class CreatePortForwardingRuleCmd method execute.

@Override
public void execute() throws ResourceUnavailableException {
    CallContext callerContext = CallContext.current();
    boolean success = true;
    PortForwardingRule rule = null;
    try {
        CallContext.current().setEventDetails("Rule Id: " + getEntityId());
        if (getOpenFirewall()) {
            success = success && _firewallService.applyIngressFirewallRules(ipAddressId, callerContext.getCallingAccount());
        }
        success = success && _rulesService.applyPortForwardingRules(ipAddressId, callerContext.getCallingAccount());
        // State is different after the rule is applied, so get new object here
        rule = _entityMgr.findById(PortForwardingRule.class, getEntityId());
        FirewallRuleResponse fwResponse = new FirewallRuleResponse();
        if (rule != null) {
            fwResponse = _responseGenerator.createPortForwardingRuleResponse(rule);
            setResponseObject(fwResponse);
        }
        fwResponse.setResponseName(getCommandName());
    } finally {
        if (!success || rule == null) {
            if (getOpenFirewall()) {
                _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
            }
            try {
                _rulesService.revokePortForwardingRule(getEntityId(), true);
            } catch (Exception ex) {
            //Ignore e.g. failed to apply rules to device error
            }
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to apply port forwarding rule");
        }
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) CallContext(org.apache.cloudstack.context.CallContext) PortForwardingRule(com.cloud.network.rules.PortForwardingRule) FirewallRuleResponse(org.apache.cloudstack.api.response.FirewallRuleResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException)

Example 2 with FirewallRuleResponse

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

the class UpdatePortForwardingRuleCmd method execute.

@Override
public void execute() {
    PortForwardingRule rule = _rulesService.updatePortForwardingRule(id, getPrivatePort(), getVirtualMachineId(), getVmGuestIp(), getCustomId(), getDisplay());
    FirewallRuleResponse fwResponse = new FirewallRuleResponse();
    if (rule != null) {
        fwResponse = _responseGenerator.createPortForwardingRuleResponse(rule);
        setResponseObject(fwResponse);
    }
    fwResponse.setResponseName(getCommandName());
}
Also used : PortForwardingRule(com.cloud.network.rules.PortForwardingRule) FirewallRuleResponse(org.apache.cloudstack.api.response.FirewallRuleResponse)

Example 3 with FirewallRuleResponse

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

the class ListPortForwardingRulesCmd method execute.

@Override
public void execute() {
    Pair<List<? extends PortForwardingRule>, Integer> result = _rulesService.listPortForwardingRules(this);
    ListResponse<FirewallRuleResponse> response = new ListResponse<FirewallRuleResponse>();
    List<FirewallRuleResponse> fwResponses = new ArrayList<FirewallRuleResponse>();
    for (PortForwardingRule fwRule : result.first()) {
        FirewallRuleResponse ruleData = _responseGenerator.createPortForwardingRuleResponse(fwRule);
        ruleData.setObjectName("portforwardingrule");
        fwResponses.add(ruleData);
    }
    response.setResponses(fwResponses, result.second());
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PortForwardingRule(com.cloud.network.rules.PortForwardingRule) FirewallRuleResponse(org.apache.cloudstack.api.response.FirewallRuleResponse)

Example 4 with FirewallRuleResponse

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

the class ApiResponseHelper method createPortForwardingRuleResponse.

@Override
public FirewallRuleResponse createPortForwardingRuleResponse(PortForwardingRule fwRule) {
    FirewallRuleResponse response = new FirewallRuleResponse();
    response.setId(fwRule.getUuid());
    response.setPrivateStartPort(Integer.toString(fwRule.getDestinationPortStart()));
    response.setPrivateEndPort(Integer.toString(fwRule.getDestinationPortEnd()));
    response.setProtocol(fwRule.getProtocol());
    response.setPublicStartPort(Integer.toString(fwRule.getSourcePortStart()));
    response.setPublicEndPort(Integer.toString(fwRule.getSourcePortEnd()));
    List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(fwRule.getId());
    response.setCidrList(StringUtils.join(cidrs, ","));
    Network guestNtwk = ApiDBUtils.findNetworkById(fwRule.getNetworkId());
    response.setNetworkId(guestNtwk.getUuid());
    IpAddress ip = ApiDBUtils.findIpAddressById(fwRule.getSourceIpAddressId());
    if (ip != null) {
        response.setPublicIpAddressId(ip.getUuid());
        response.setPublicIpAddress(ip.getAddress().addr());
        if (fwRule.getDestinationIpAddress() != null) {
            response.setDestNatVmIp(fwRule.getDestinationIpAddress().toString());
            UserVm vm = ApiDBUtils.findUserVmById(fwRule.getVirtualMachineId());
            if (vm != null) {
                response.setVirtualMachineId(vm.getUuid());
                response.setVirtualMachineName(vm.getHostName());
                if (vm.getDisplayName() != null) {
                    response.setVirtualMachineDisplayName(vm.getDisplayName());
                } else {
                    response.setVirtualMachineDisplayName(vm.getHostName());
                }
            }
        }
    }
    FirewallRule.State state = fwRule.getState();
    String stateToSet = state.toString();
    if (state.equals(FirewallRule.State.Revoke)) {
        stateToSet = "Deleting";
    }
    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.PortForwardingRule, fwRule.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    response.setTags(tagResponses);
    response.setState(stateToSet);
    response.setForDisplay(fwRule.isDisplay());
    response.setObjectName("portforwardingrule");
    return response;
}
Also used : ArrayList(java.util.ArrayList) UserVm(com.cloud.uservm.UserVm) ResourceTag(com.cloud.server.ResourceTag) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) IpAddress(com.cloud.network.IpAddress) FirewallRuleResponse(org.apache.cloudstack.api.response.FirewallRuleResponse) FirewallRule(com.cloud.network.rules.FirewallRule)

Aggregations

FirewallRuleResponse (org.apache.cloudstack.api.response.FirewallRuleResponse)4 PortForwardingRule (com.cloud.network.rules.PortForwardingRule)3 ArrayList (java.util.ArrayList)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 IpAddress (com.cloud.network.IpAddress)1 Network (com.cloud.network.Network)1 PhysicalNetwork (com.cloud.network.PhysicalNetwork)1 FirewallRule (com.cloud.network.rules.FirewallRule)1 ResourceTag (com.cloud.server.ResourceTag)1 UserVm (com.cloud.uservm.UserVm)1 List (java.util.List)1 ServerApiException (org.apache.cloudstack.api.ServerApiException)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1 ResourceTagResponse (org.apache.cloudstack.api.response.ResourceTagResponse)1 CallContext (org.apache.cloudstack.context.CallContext)1