Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDSTACK-9812:Update "updatePortForwardingRule" api to include additional parameter to update the end port in case of port range #1985

Merged
merged 1 commit into from Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/com/cloud/network/rules/RulesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ Pair<List<? extends FirewallRule>, Integer> searchStaticNatRules(Long ipId, Long

boolean disableStaticNat(long ipId) throws ResourceUnavailableException, NetworkRuleConflictException, InsufficientAddressCapacityException;

PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay);
PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Integer privateEndPort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay);

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public class UpdatePortForwardingRuleCmd extends BaseAsyncCustomIdCmd {
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = FirewallRuleResponse.class, required = true, description = "the ID of the port forwarding rule", since = "4.4")
private Long id;

@Parameter(name=ApiConstants.PRIVATE_PORT, type=CommandType.INTEGER, description="the private port of the port forwarding rule")
@Parameter(name=ApiConstants.PRIVATE_START_PORT, type=CommandType.INTEGER, description="the private start port of the port forwarding rule")
private Integer privatePort;


@Parameter(name=ApiConstants.PRIVATE_END_PORT, type=CommandType.INTEGER, description="the private end port of the port forwarding rule")
private Integer privateEndPort;

@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID,
type = CommandType.UUID,
entityType = UserVmResponse.class,
Expand All @@ -74,6 +78,10 @@ public Integer getPrivatePort() {
return privatePort;
}

public Integer getPrivateEndPort() {
return privateEndPort;
}

public Long getVirtualMachineId() {
return virtualMachineId;
}
Expand Down Expand Up @@ -130,7 +138,7 @@ public void checkUuid() {

@Override
public void execute() {
PortForwardingRule rule = _rulesService.updatePortForwardingRule(id, getPrivatePort(), getVirtualMachineId(), getVmGuestIp(), getCustomId(), getDisplay());
PortForwardingRule rule = _rulesService.updatePortForwardingRule(getId(), getPrivatePort(), getPrivateEndPort(), getVirtualMachineId(), getVmGuestIp(), getCustomId(), getDisplay());
FirewallRuleResponse fwResponse = new FirewallRuleResponse();
if (rule != null) {
fwResponse = _responseGenerator.createPortForwardingRuleResponse(rule);
Expand Down
32 changes: 28 additions & 4 deletions server/src/com/cloud/network/rules/RulesManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ public List<FirewallRuleVO> listAssociatedRulesForGuestNic(Nic nic) {

@Override
@ActionEvent(eventType = EventTypes.EVENT_NET_RULE_MODIFY, eventDescription = "updating forwarding rule", async = true)
public PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay) {
public PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Integer privateEndPort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay) {
Account caller = CallContext.current().getCallingAccount();
PortForwardingRuleVO rule = _portForwardingDao.findById(id);
if (rule == null) {
Expand All @@ -1541,9 +1541,29 @@ public PortForwardingRule updatePortForwardingRule(long id, Integer privatePort,
rule.setDisplay(forDisplay);
}

if (!rule.getSourcePortStart().equals(rule.getSourcePortEnd()) && privatePort != null) {
throw new InvalidParameterValueException("Unable to update the private port of port forwarding rule as the rule has port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
if (privatePort != null && !NetUtils.isValidPort(privatePort)) {
throw new InvalidParameterValueException("privatePort is an invalid value: " + privatePort);
}
if (privateEndPort != null && !NetUtils.isValidPort(privateEndPort)) {
throw new InvalidParameterValueException("PrivateEndPort has an invalid value: " + privateEndPort);
}

if (privatePort != null && privateEndPort != null && ((privateEndPort - privatePort) != (rule.getSourcePortEnd() - rule.getSourcePortStart())))
{
throw new InvalidParameterValueException("Unable to update the private port range of port forwarding rule as " +
"the provided port range is not consistent with the port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
}

//in case of port range
if (!rule.getSourcePortStart().equals(rule.getSourcePortEnd())) {
if ((privatePort == null || privateEndPort == null) && !(privatePort == null && privateEndPort == null)) {
throw new InvalidParameterValueException("Unable to update the private port range of port forwarding rule as " +
"the provided port range is not consistent with the port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
}
}



if (virtualMachineId == null && vmGuestIp != null) {
throw new InvalidParameterValueException("vmguestip should be set along with virtualmachineid");
}
Expand Down Expand Up @@ -1588,8 +1608,12 @@ public PortForwardingRule updatePortForwardingRule(long id, Integer privatePort,
rule.setState(State.Add);
if (privatePort != null) {
rule.setDestinationPortStart(privatePort.intValue());
rule.setDestinationPortEnd(privatePort.intValue());
rule.setDestinationPortEnd((privateEndPort == null) ? privatePort.intValue() : privateEndPort.intValue());
} else if (privateEndPort != null) {
rule.setDestinationPortStart(privateEndPort.intValue());
rule.setDestinationPortEnd(privateEndPort);
}

if (virtualMachineId != null) {
rule.setVirtualMachineId(virtualMachineId);
rule.setDestinationIpAddress(dstIp);
Expand Down
Loading