diff --git a/api/src/com/cloud/api/commands/CreateIpForwardingRuleCmd.java b/api/src/com/cloud/api/commands/CreateIpForwardingRuleCmd.java index 859144f775f..1d3d38c86c6 100644 --- a/api/src/com/cloud/api/commands/CreateIpForwardingRuleCmd.java +++ b/api/src/com/cloud/api/commands/CreateIpForwardingRuleCmd.java @@ -124,7 +124,7 @@ public class CreateIpForwardingRuleCmd extends BaseAsyncCreateCmd implements Sta result = result && _rulesService.applyStaticNatRules(ipAddressId, UserContext.current().getCaller()); rule = _entityMgr.findById(FirewallRule.class, getEntityId()); - StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule); + StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule, false); IpForwardingRuleResponse fwResponse = _responseGenerator.createIpForwardingRuleResponse(staticNatRule); fwResponse.setResponseName(getCommandName()); this.setResponseObject(fwResponse); diff --git a/api/src/com/cloud/api/commands/ListIpForwardingRulesCmd.java b/api/src/com/cloud/api/commands/ListIpForwardingRulesCmd.java index e064c5bdec4..692f341845a 100644 --- a/api/src/com/cloud/api/commands/ListIpForwardingRulesCmd.java +++ b/api/src/com/cloud/api/commands/ListIpForwardingRulesCmd.java @@ -87,7 +87,7 @@ public class ListIpForwardingRulesCmd extends BaseListProjectAndAccountResources ListResponse response = new ListResponse(); List ipForwardingResponses = new ArrayList(); for (FirewallRule rule : result) { - StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule); + StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule, false); IpForwardingRuleResponse resp = _responseGenerator.createIpForwardingRuleResponse(staticNatRule); if (resp != null) { ipForwardingResponses.add(resp); diff --git a/api/src/com/cloud/network/rules/RulesService.java b/api/src/com/cloud/network/rules/RulesService.java index 55439632be3..c91b1263bbe 100644 --- a/api/src/com/cloud/network/rules/RulesService.java +++ b/api/src/com/cloud/network/rules/RulesService.java @@ -77,7 +77,7 @@ public interface RulesService { boolean applyStaticNatRules(long ipAdddressId, Account caller) throws ResourceUnavailableException; - StaticNatRule buildStaticNatRule(FirewallRule rule); + StaticNatRule buildStaticNatRule(FirewallRule rule, boolean forRevoke); List getSourceCidrs(long ruleId); diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index 070e6a2b611..f0a4ae690bb 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -1882,7 +1882,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (!staticNatFirewallRules.isEmpty()) { List staticNatRules = new ArrayList(); for (FirewallRule rule : staticNatFirewallRules) { - staticNatRules.add(_rulesMgr.buildStaticNatRule(rule)); + staticNatRules.add(_rulesMgr.buildStaticNatRule(rule, false)); } createApplyStaticNatRulesCommands(staticNatRules, router, cmds); } diff --git a/server/src/com/cloud/network/rules/RulesManager.java b/server/src/com/cloud/network/rules/RulesManager.java index 4d1cb113f35..3b3e6a1277b 100644 --- a/server/src/com/cloud/network/rules/RulesManager.java +++ b/server/src/com/cloud/network/rules/RulesManager.java @@ -33,7 +33,7 @@ public interface RulesManager extends RulesService { boolean applyPortForwardingRules(long ipAddressId, boolean continueOnError, Account caller); - boolean applyStaticNatRules(long sourceIpId, boolean continueOnError, Account caller); + boolean applyStaticNatRulesForIp(long sourceIpId, boolean continueOnError, Account caller, boolean forRevoke); boolean applyPortForwardingRulesForNetwork(long networkId, boolean continueOnError, Account caller); diff --git a/server/src/com/cloud/network/rules/RulesManagerImpl.java b/server/src/com/cloud/network/rules/RulesManagerImpl.java index 38458404842..a8fb6894d61 100755 --- a/server/src/com/cloud/network/rules/RulesManagerImpl.java +++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java @@ -495,7 +495,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { boolean success = false; if (apply) { - success = applyStaticNatRules(rule.getSourceIpAddressId(), true, caller); + success = applyStaticNatRulesForIp(rule.getSourceIpAddressId(), true, caller, true); } else { success = true; } @@ -563,7 +563,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { // apply rules for all ip addresses for (Long ipId : ipsToReprogram) { s_logger.debug("Applying static nat rules for ip address id=" + ipId + " as a part of vm expunge"); - if (!applyStaticNatRules(ipId, true, _accountMgr.getSystemAccount())) { + if (!applyStaticNatRulesForIp(ipId, true, _accountMgr.getSystemAccount(), true)) { success = false; s_logger.warn("Failed to apply static nat rules for ip id=" + ipId); } @@ -654,7 +654,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } @Override - public boolean applyStaticNatRules(long sourceIpId, boolean continueOnError, Account caller) { + public boolean applyStaticNatRulesForIp(long sourceIpId, boolean continueOnError, Account caller, boolean forRevoke) { List rules = _firewallDao.listByIpAndPurpose(sourceIpId, Purpose.StaticNat); List staticNatRules = new ArrayList(); @@ -664,7 +664,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } for (FirewallRule rule : rules) { - staticNatRules.add(buildStaticNatRule(rule)); + staticNatRules.add(buildStaticNatRule(rule, forRevoke)); } if (caller != null) { @@ -722,7 +722,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } for (FirewallRuleVO rule : rules) { - staticNatRules.add(buildStaticNatRule(rule)); + staticNatRules.add(buildStaticNatRule(rule, false)); } try { @@ -833,7 +833,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { @Override @ActionEvent(eventType = EventTypes.EVENT_NET_RULE_ADD, eventDescription = "applying static nat rule", async = true) public boolean applyStaticNatRules(long ipId, Account caller) throws ResourceUnavailableException { - if (!applyStaticNatRules(ipId, false, caller)) { + if (!applyStaticNatRulesForIp(ipId, false, caller, false)) { throw new CloudRuntimeException("Failed to apply static nat rule"); } return true; @@ -869,7 +869,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { success = success && applyPortForwardingRules(ipId, true, caller); // revoke all all static nat rules - success = success && applyStaticNatRules(ipId, true, caller); + success = success && applyStaticNatRulesForIp(ipId, true, caller, true); // revoke static nat for the ip address success = success && applyStaticNatForIp(ipId, false, caller, true); @@ -1112,15 +1112,20 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager { } @Override - public StaticNatRule buildStaticNatRule(FirewallRule rule) { + public StaticNatRule buildStaticNatRule(FirewallRule rule, boolean forRevoke) { IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId()); FirewallRuleVO ruleVO = _firewallDao.findById(rule.getId()); if (ip == null || !ip.isOneToOneNat() || ip.getAssociatedWithVmId() == null) { throw new InvalidParameterValueException("Source ip address of the rule id=" + rule.getId() + " is not static nat enabled"); } - - String dstIp = _networkMgr.getIpInNetwork(ip.getAssociatedWithVmId(), rule.getNetworkId()); + + String dstIp; + if (forRevoke) { + dstIp = _networkMgr.getIpInNetworkIncludingRemoved(ip.getAssociatedWithVmId(), rule.getNetworkId()); + } else { + dstIp = _networkMgr.getIpInNetwork(ip.getAssociatedWithVmId(), rule.getNetworkId()); + } return new StaticNatRuleImpl(ruleVO, dstIp); }