Skip to content

Commit fec31d5

Browse files
committed
CLOUDSTACK-8064: UpdatePortForwardingRuleCmd implementation
1 parent dcb7fcc commit fec31d5

4 files changed

Lines changed: 91 additions & 45 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, String customId, Boolean forDisplay);
84+
PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay);
8585

8686
}

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

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.cloudstack.api.BaseAsyncCustomIdCmd;
2424
import org.apache.cloudstack.api.Parameter;
2525
import org.apache.cloudstack.api.response.FirewallRuleResponse;
26-
import org.apache.cloudstack.api.response.IPAddressResponse;
2726
import org.apache.cloudstack.api.response.UserVmResponse;
2827
import org.apache.log4j.Logger;
2928

@@ -32,6 +31,7 @@
3231
import com.cloud.network.rules.FirewallRule;
3332
import com.cloud.network.rules.PortForwardingRule;
3433
import com.cloud.user.Account;
34+
import com.cloud.utils.net.Ip;
3535

3636
@APICommand(name = "updatePortForwardingRule",
3737
responseObject = FirewallRuleResponse.class,
@@ -47,65 +47,47 @@ 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_IP, type = CommandType.STRING, description = "the private IP address of the port forwarding rule")
51-
private String privateIp;
52-
53-
@Parameter(name = ApiConstants.PRIVATE_PORT, type = CommandType.STRING, description = "the private port of the port forwarding rule")
54-
private String privatePort;
55-
56-
@Parameter(name = ApiConstants.PROTOCOL,
57-
type = CommandType.STRING,
58-
description = "the protocol for the port fowarding rule. Valid values are TCP or UDP.")
59-
private String protocol;
60-
61-
@Parameter(name = ApiConstants.IP_ADDRESS_ID,
62-
type = CommandType.UUID,
63-
entityType = IPAddressResponse.class,
64-
description = "the IP address id of the port forwarding rule")
65-
private Long publicIpId;
66-
67-
@Parameter(name = ApiConstants.PUBLIC_PORT, type = CommandType.STRING, description = "the public port of the port forwarding rule")
68-
private String publicPort;
50+
@Parameter(name=ApiConstants.PRIVATE_PORT, type=CommandType.INTEGER, description="the private port of the port forwarding rule")
51+
private Integer privatePort;
6952

7053
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID,
7154
type = CommandType.UUID,
7255
entityType = UserVmResponse.class,
7356
description = "the ID of the virtual machine for the port forwarding rule")
7457
private Long virtualMachineId;
7558

59+
@Parameter(name = ApiConstants.VM_GUEST_IP, type = CommandType.STRING, required = false, description = "VM guest nic Secondary ip address for the port forwarding rule", since = "4.5")
60+
private String vmGuestIp;
61+
7662
@Parameter(name = ApiConstants.FOR_DISPLAY, type = CommandType.BOOLEAN, description = "an optional field, whether to the display the rule to the end user or not", since = "4.4", authorized = {RoleType.Admin})
7763
private Boolean display;
7864

7965
/////////////////////////////////////////////////////
8066
/////////////////// Accessors ///////////////////////
8167
/////////////////////////////////////////////////////
8268

83-
public String getPrivateIp() {
84-
return privateIp;
69+
public Long getId() {
70+
return id;
8571
}
8672

87-
public String getPrivatePort() {
73+
public Integer getPrivatePort() {
8874
return privatePort;
8975
}
9076

91-
public String getProtocol() {
92-
return protocol;
93-
}
94-
95-
public String getPublicPort() {
96-
return publicPort;
97-
}
98-
9977
public Long getVirtualMachineId() {
10078
return virtualMachineId;
10179
}
10280

81+
public Ip getVmGuestIp() {
82+
if (vmGuestIp == null) {
83+
return null;
84+
}
85+
return new Ip(vmGuestIp);
86+
}
87+
10388
public Boolean getDisplay() {
10489
return display;
10590
}
106-
public Long getId() {
107-
return id;
108-
}
10991

11092
/////////////////////////////////////////////////////
11193
/////////////// API Implementation///////////////////
@@ -148,21 +130,13 @@ public void checkUuid() {
148130

149131
@Override
150132
public void execute() {
151-
PortForwardingRule rule = _rulesService.updatePortForwardingRule(id, getCustomId(), getDisplay());
133+
PortForwardingRule rule = _rulesService.updatePortForwardingRule(id, getPrivatePort(), getVirtualMachineId(), getVmGuestIp(), getCustomId(), getDisplay());
152134
FirewallRuleResponse fwResponse = new FirewallRuleResponse();
153135
if (rule != null) {
154136
fwResponse = _responseGenerator.createPortForwardingRuleResponse(rule);
155137
setResponseObject(fwResponse);
156138
}
157139
fwResponse.setResponseName(getCommandName());
158-
//FIXME: PortForwardingRule result = _mgr.updatePortForwardingRule(this);
159-
// if (result != null) {
160-
// FirewallRuleResponse response = _responseGenerator.createFirewallRuleResponse(result);
161-
// response.setResponseName(getName());
162-
// this.setResponseObject(response);
163-
// } else {
164-
// throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update port forwarding rule");
165-
// }
166140
}
167141

168142
@Override

‎engine/schema/src/com/cloud/network/rules/PortForwardingRuleVO.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,28 @@ public int getDestinationPortStart() {
7979
return destinationPortStart;
8080
}
8181

82+
public void setDestinationPortStart(int destinationPortStart) {
83+
this.destinationPortStart = destinationPortStart;
84+
}
85+
8286
@Override
8387
public int getDestinationPortEnd() {
8488
return destinationPortEnd;
8589
}
8690

91+
public void setDestinationPortEnd(int destinationPortEnd) {
92+
this.destinationPortEnd = destinationPortEnd;
93+
}
94+
8795
@Override
8896
public long getVirtualMachineId() {
8997
return virtualMachineId;
9098
}
9199

100+
public void setVirtualMachineId(long virtualMachineId) {
101+
this.virtualMachineId = virtualMachineId;
102+
}
103+
92104
@Override
93105
public Long getRelated() {
94106
return null;

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.cloud.network.dao.LoadBalancerVMMapVO;
5656
import com.cloud.network.rules.FirewallRule.FirewallRuleType;
5757
import com.cloud.network.rules.FirewallRule.Purpose;
58+
import com.cloud.network.rules.FirewallRule.State;
5859
import com.cloud.network.rules.dao.PortForwardingRulesDao;
5960
import com.cloud.network.vpc.VpcManager;
6061
import com.cloud.network.vpc.VpcService;
@@ -1510,7 +1511,7 @@ public List<FirewallRuleVO> listAssociatedRulesForGuestNic(Nic nic) {
15101511

15111512
@Override
15121513
@ActionEvent(eventType = EventTypes.EVENT_NET_RULE_MODIFY, eventDescription = "updating forwarding rule", async = true)
1513-
public PortForwardingRule updatePortForwardingRule(long id, String customId, Boolean forDisplay) {
1514+
public PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay) {
15141515
Account caller = CallContext.current().getCallingAccount();
15151516
PortForwardingRuleVO rule = _portForwardingDao.findById(id);
15161517
if (rule == null) {
@@ -1526,7 +1527,66 @@ public PortForwardingRule updatePortForwardingRule(long id, String customId, Boo
15261527
rule.setDisplay(forDisplay);
15271528
}
15281529

1530+
if (rule.getSourcePortStart() != rule.getSourcePortEnd() && privatePort != null) {
1531+
throw new InvalidParameterValueException("Unable to update the private port of port forwarding rule as the rule has port range : " + rule.getSourcePortStart() + " to " + rule.getSourcePortEnd());
1532+
}
1533+
if (virtualMachineId == null && vmGuestIp != null) {
1534+
throw new InvalidParameterValueException("vmguestip should be set along with virtualmachineid");
1535+
}
1536+
Ip dstIp = rule.getDestinationIpAddress();
1537+
if (virtualMachineId != null) {
1538+
// Verify that vm has nic in the network
1539+
Nic guestNic = _networkModel.getNicInNetwork(virtualMachineId, rule.getNetworkId());
1540+
if (guestNic == null || guestNic.getIp4Address() == null) {
1541+
throw new InvalidParameterValueException("Vm doesn't belong to network associated with ipAddress");
1542+
} else {
1543+
dstIp = new Ip(guestNic.getIp4Address());
1544+
}
1545+
1546+
if (vmGuestIp != null) {
1547+
//vm ip is passed so it can be primary or secondary ip addreess.
1548+
if (!dstIp.equals(vmGuestIp)) {
1549+
//the vm ip is secondary ip to the nic.
1550+
// is vmIp is secondary ip or not
1551+
NicSecondaryIp secondaryIp = _nicSecondaryDao.findByIp4AddressAndNicId(vmGuestIp.toString(), guestNic.getId());
1552+
if (secondaryIp == null) {
1553+
throw new InvalidParameterValueException("IP Address is not in the VM nic's network ");
1554+
}
1555+
dstIp = vmGuestIp;
1556+
}
1557+
}
1558+
}
1559+
1560+
// revoke old rules at first
1561+
List<PortForwardingRuleVO> rules = new ArrayList<PortForwardingRuleVO>();
1562+
rule.setState(State.Revoke);
15291563
_portForwardingDao.update(id, rule);
1564+
rules.add(rule);
1565+
try {
1566+
if (!_firewallMgr.applyRules(rules, true, false)) {
1567+
throw new CloudRuntimeException("Failed to revoke the existing port forwarding rule:" + id);
1568+
}
1569+
} catch (ResourceUnavailableException ex) {
1570+
throw new CloudRuntimeException("Failed to revoke the existing port forwarding rule:" + id + " due to ", ex);
1571+
}
1572+
1573+
rule = _portForwardingDao.findById(id);
1574+
rule.setState(State.Add);
1575+
if (privatePort != null) {
1576+
rule.setDestinationPortStart(privatePort.intValue());
1577+
rule.setDestinationPortEnd(privatePort.intValue());
1578+
}
1579+
if (virtualMachineId != null) {
1580+
rule.setVirtualMachineId(virtualMachineId);
1581+
rule.setDestinationIpAddress(dstIp);
1582+
}
1583+
_portForwardingDao.update(id, rule);
1584+
1585+
//apply new rules
1586+
if (!applyPortForwardingRules(rule.getSourceIpAddressId(), false, caller)) {
1587+
throw new CloudRuntimeException("Failed to apply the new port forwarding rule:" + id);
1588+
}
1589+
15301590
return _portForwardingDao.findById(id);
15311591
}
15321592
}

0 commit comments

Comments
 (0)