Skip to content

Commit d1e61eb

Browse files
mrunalinikankariyayadvr
authored andcommitted
CLOUDSTACK-9812: Update 'updatePortForwardingRule' api to include additional parameter end port (apache#1985)
Configure a PF rule Private port : Start port ; 20 ENd POrt 25 || Public Port : Start port 20 ; ENd Port : 25. Trigger UpdatePortForwardingRule api ApI fails with following error : " Unable to update the private port of port forwarding rule as the rule has port range " Solution- Port range gets modified
1 parent bf35aef commit d1e61eb

6 files changed

Lines changed: 503 additions & 8 deletions

File tree

api/src/com/cloud/network/rules/RulesService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ Pair<List<? extends FirewallRule>, Integer> searchStaticNatRules(Long ipId, Long
8181

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

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

8686
}

api/src/org/apache/cloudstack/api/command/user/firewall/UpdatePortForwardingRuleCmd.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ public class UpdatePortForwardingRuleCmd extends BaseAsyncCustomIdCmd {
4747
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = FirewallRuleResponse.class, required = true, description = "the ID of the port forwarding rule", since = "4.4")
4848
private Long id;
4949

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

53+
54+
@Parameter(name=ApiConstants.PRIVATE_END_PORT, type=CommandType.INTEGER, description="the private end port of the port forwarding rule")
55+
private Integer privateEndPort;
56+
5357
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID,
5458
type = CommandType.UUID,
5559
entityType = UserVmResponse.class,
@@ -74,6 +78,10 @@ public Integer getPrivatePort() {
7478
return privatePort;
7579
}
7680

81+
public Integer getPrivateEndPort() {
82+
return privateEndPort;
83+
}
84+
7785
public Long getVirtualMachineId() {
7886
return virtualMachineId;
7987
}
@@ -130,7 +138,7 @@ public void checkUuid() {
130138

131139
@Override
132140
public void execute() {
133-
PortForwardingRule rule = _rulesService.updatePortForwardingRule(id, getPrivatePort(), getVirtualMachineId(), getVmGuestIp(), getCustomId(), getDisplay());
141+
PortForwardingRule rule = _rulesService.updatePortForwardingRule(getId(), getPrivatePort(), getPrivateEndPort(), getVirtualMachineId(), getVmGuestIp(), getCustomId(), getDisplay());
134142
FirewallRuleResponse fwResponse = new FirewallRuleResponse();
135143
if (rule != null) {
136144
fwResponse = _responseGenerator.createPortForwardingRuleResponse(rule);

server/src/com/cloud/network/rules/RulesManagerImpl.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ public List<FirewallRuleVO> listAssociatedRulesForGuestNic(Nic nic) {
15251525

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

1544-
if (!rule.getSourcePortStart().equals(rule.getSourcePortEnd()) && privatePort != null) {
1545-
throw new InvalidParameterValueException("Unable to update the private port of port forwarding rule as the rule has port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
1544+
if (privatePort != null && !NetUtils.isValidPort(privatePort)) {
1545+
throw new InvalidParameterValueException("privatePort is an invalid value: " + privatePort);
15461546
}
1547+
if (privateEndPort != null && !NetUtils.isValidPort(privateEndPort)) {
1548+
throw new InvalidParameterValueException("PrivateEndPort has an invalid value: " + privateEndPort);
1549+
}
1550+
1551+
if (privatePort != null && privateEndPort != null && ((privateEndPort - privatePort) != (rule.getSourcePortEnd() - rule.getSourcePortStart())))
1552+
{
1553+
throw new InvalidParameterValueException("Unable to update the private port range of port forwarding rule as " +
1554+
"the provided port range is not consistent with the port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
1555+
}
1556+
1557+
//in case of port range
1558+
if (!rule.getSourcePortStart().equals(rule.getSourcePortEnd())) {
1559+
if ((privatePort == null || privateEndPort == null) && !(privatePort == null && privateEndPort == null)) {
1560+
throw new InvalidParameterValueException("Unable to update the private port range of port forwarding rule as " +
1561+
"the provided port range is not consistent with the port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
1562+
}
1563+
}
1564+
1565+
1566+
15471567
if (virtualMachineId == null && vmGuestIp != null) {
15481568
throw new InvalidParameterValueException("vmguestip should be set along with virtualmachineid");
15491569
}
@@ -1588,8 +1608,12 @@ public PortForwardingRule updatePortForwardingRule(long id, Integer privatePort,
15881608
rule.setState(State.Add);
15891609
if (privatePort != null) {
15901610
rule.setDestinationPortStart(privatePort.intValue());
1591-
rule.setDestinationPortEnd(privatePort.intValue());
1611+
rule.setDestinationPortEnd((privateEndPort == null) ? privatePort.intValue() : privateEndPort.intValue());
1612+
} else if (privateEndPort != null) {
1613+
rule.setDestinationPortStart(privateEndPort.intValue());
1614+
rule.setDestinationPortEnd(privateEndPort);
15921615
}
1616+
15931617
if (virtualMachineId != null) {
15941618
rule.setVirtualMachineId(virtualMachineId);
15951619
rule.setDestinationIpAddress(dstIp);

0 commit comments

Comments
 (0)