Search in sources :

Example 1 with IpForwardingRuleResponse

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

the class CreateIpForwardingRuleCmd method execute.

@Override
public void execute() throws ResourceUnavailableException {
    boolean result = true;
    FirewallRule rule = null;
    try {
        CallContext.current().setEventDetails("Rule ID: " + getEntityId());
        if (getOpenFirewall()) {
            result = result && _firewallService.applyIngressFirewallRules(ipAddressId, CallContext.current().getCallingAccount());
        }
        result = result && _rulesService.applyStaticNatRules(ipAddressId, CallContext.current().getCallingAccount());
        rule = _entityMgr.findById(FirewallRule.class, getEntityId());
        StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule, false);
        IpForwardingRuleResponse fwResponse = _responseGenerator.createIpForwardingRuleResponse(staticNatRule);
        fwResponse.setResponseName(getCommandName());
        setResponseObject(fwResponse);
    } finally {
        if (!result || rule == null) {
            if (getOpenFirewall()) {
                _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
            }
            _rulesService.revokeStaticNatRule(getEntityId(), true);
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Error in creating IP forwarding rule on the domr");
        }
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) StaticNatRule(com.cloud.network.rules.StaticNatRule) FirewallRule(com.cloud.network.rules.FirewallRule) IpForwardingRuleResponse(org.apache.cloudstack.api.response.IpForwardingRuleResponse)

Example 2 with IpForwardingRuleResponse

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

the class ListIpForwardingRulesCmd method execute.

@Override
public void execute() {
    Pair<List<? extends FirewallRule>, Integer> result = _rulesService.searchStaticNatRules(publicIpAddressId, id, vmId, this.getStartIndex(), this.getPageSizeVal(), this.getAccountName(), this.getDomainId(), this.getProjectId(), this.isRecursive(), this.listAll());
    ListResponse<IpForwardingRuleResponse> response = new ListResponse<IpForwardingRuleResponse>();
    List<IpForwardingRuleResponse> ipForwardingResponses = new ArrayList<IpForwardingRuleResponse>();
    for (FirewallRule rule : result.first()) {
        StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule, false);
        IpForwardingRuleResponse resp = _responseGenerator.createIpForwardingRuleResponse(staticNatRule);
        if (resp != null) {
            ipForwardingResponses.add(resp);
        }
    }
    response.setResponses(ipForwardingResponses, result.second());
    response.setResponseName(getCommandName());
    this.setResponseObject(response);
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StaticNatRule(com.cloud.network.rules.StaticNatRule) FirewallRule(com.cloud.network.rules.FirewallRule) IpForwardingRuleResponse(org.apache.cloudstack.api.response.IpForwardingRuleResponse)

Example 3 with IpForwardingRuleResponse

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

the class ApiResponseHelper method createIpForwardingRuleResponse.

@Override
public IpForwardingRuleResponse createIpForwardingRuleResponse(StaticNatRule fwRule) {
    IpForwardingRuleResponse response = new IpForwardingRuleResponse();
    response.setId(fwRule.getUuid());
    response.setProtocol(fwRule.getProtocol());
    IpAddress ip = ApiDBUtils.findIpAddressById(fwRule.getSourceIpAddressId());
    if (ip != null) {
        response.setPublicIpAddressId(ip.getId());
        response.setPublicIpAddress(ip.getAddress().addr());
        if (fwRule.getDestIpAddress() != null) {
            UserVm vm = ApiDBUtils.findUserVmById(ip.getAssociatedWithVmId());
            if (vm != null) {
                // vm might be destroyed
                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";
    }
    response.setStartPort(fwRule.getSourcePortStart());
    response.setEndPort(fwRule.getSourcePortEnd());
    response.setProtocol(fwRule.getProtocol());
    response.setState(stateToSet);
    response.setObjectName("ipforwardingrule");
    return response;
}
Also used : UserVm(com.cloud.uservm.UserVm) IpAddress(com.cloud.network.IpAddress) FirewallRule(com.cloud.network.rules.FirewallRule) IpForwardingRuleResponse(org.apache.cloudstack.api.response.IpForwardingRuleResponse)

Aggregations

FirewallRule (com.cloud.network.rules.FirewallRule)3 IpForwardingRuleResponse (org.apache.cloudstack.api.response.IpForwardingRuleResponse)3 StaticNatRule (com.cloud.network.rules.StaticNatRule)2 IpAddress (com.cloud.network.IpAddress)1 UserVm (com.cloud.uservm.UserVm)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ServerApiException (org.apache.cloudstack.api.ServerApiException)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1