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");
}
}
}
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());
}
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);
}
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;
}
Aggregations