Skip to content

Commit 8ef5232

Browse files
committed
CLOUDSTACK-9317: When there 1 static nat removing PF rules is handled
There is 1 static nat rule and 2 PF rule. Removing 2 PF rules was deleting static nat rule. Fixed this issue in this commit.
1 parent c20e0ef commit 8ef5232

7 files changed

Lines changed: 41 additions & 9 deletions

File tree

api/src/com/cloud/network/IpAddress.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ enum Purpose {
9292

9393
public Date getCreated();
9494

95+
State getRuleState();
96+
97+
void setRuleState(State ruleState);
98+
9599
}

core/src/com/cloud/agent/api/routing/NetworkElementCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class NetworkElementCommand extends Command {
3838
public static final String VPC_PRIVATE_GATEWAY = "vpc.gateway.private";
3939
public static final String FIREWALL_EGRESS_DEFAULT = "firewall.egress.default";
4040
public static final String ROUTER_MONITORING_ENABLE = "router.monitor.enable";
41-
public static final String NETWORK_PUB_LAST_IP = "newtork.public.last.ip";
41+
public static final String NETWORK_PUB_LAST_IP = "network.public.last.ip";
4242

4343
private String routerAccessIp;
4444

engine/components-api/src/com/cloud/network/addr/PublicIp.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,13 @@ public Class<?> getEntityType()
255255
return IpAddress.class;
256256
}
257257

258+
@Override
259+
public State getRuleState() {
260+
return _addr.getRuleState();
261+
}
262+
263+
@Override
264+
public void setRuleState(State ruleState) {
265+
_addr.setRuleState(ruleState);
266+
}
258267
}

engine/schema/src/com/cloud/network/dao/IPAddressVO.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public class IPAddressVO implements IpAddress {
117117
@Column(name = "display", updatable = true, nullable = false)
118118
protected boolean display = true;
119119

120+
@Enumerated(value = EnumType.STRING)
121+
@Column(name = "rule_state")
122+
State ruleState;
123+
120124
@Column(name= GenericDao.REMOVED_COLUMN)
121125
private Date removed;
122126

@@ -367,4 +371,14 @@ public Date getRemoved() {
367371
public Date getCreated() {
368372
return created;
369373
}
374+
375+
@Override
376+
public State getRuleState() {
377+
return ruleState;
378+
}
379+
380+
@Override
381+
public void setRuleState(State ruleState) {
382+
this.ruleState = ruleState;
383+
}
370384
}

server/src/com/cloud/network/router/CommandSetupHelper.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,8 @@ public int compare(final PublicIpAddress o1, final PublicIpAddress o2) {
858858
ipsWithrules++;
859859
}
860860

861-
if (ip.isOneToOneNat()) {
861+
// check onetoonenat and also check if the ip "add":false. If there are 2 PF remove 1 static nat add
862+
if (ip.isOneToOneNat() && ip.getRuleState() == null) {
862863
ipsStaticNat++;
863864
}
864865
}
@@ -870,13 +871,8 @@ public int compare(final PublicIpAddress o1, final PublicIpAddress o2) {
870871
final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId());
871872
cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString());
872873

873-
boolean remove = false;
874-
// if there is only one static nat then it will be checked for remove at the resource
875-
if (ipsWithrules == 0 && (ipsStaticNat == 0 || ipsStaticNat == 1)) {
876-
remove = true;
877-
}
878-
879-
if (remove) {
874+
// if there 1 static nat then it will be checked for remove at the resource
875+
if (ipsWithrules == 0 && ipsStaticNat == 0 ) {
880876
// there is only one ip address for the network.
881877
cmd.setAccessDetail(NetworkElementCommand.NETWORK_PUB_LAST_IP, "true");
882878
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,10 @@ public boolean disableStaticNat(long ipId, Account caller, long callerUserId, bo
12591259
throw ex;
12601260
}
12611261

1262+
ipAddress.setRuleState(IpAddress.State.Releasing);
1263+
_ipAddressDao.update(ipAddress.getId(), ipAddress);
1264+
ipAddress = _ipAddressDao.findById(ipId);
1265+
12621266
// Revoke all firewall rules for the ip
12631267
try {
12641268
s_logger.debug("Revoking all " + Purpose.Firewall + "rules as a part of disabling static nat for public IP id=" + ipId);
@@ -1280,6 +1284,7 @@ public boolean disableStaticNat(long ipId, Account caller, long callerUserId, bo
12801284
boolean isIpSystem = ipAddress.getSystem();
12811285
ipAddress.setOneToOneNat(false);
12821286
ipAddress.setAssociatedWithVmId(null);
1287+
ipAddress.setRuleState(null);
12831288
ipAddress.setVmIp(null);
12841289
if (isIpSystem && !releaseIpIfElastic) {
12851290
ipAddress.setSystem(false);
@@ -1295,6 +1300,9 @@ public boolean disableStaticNat(long ipId, Account caller, long callerUserId, bo
12951300
return true;
12961301
} else {
12971302
s_logger.warn("Failed to disable one to one nat for the ip address id" + ipId);
1303+
ipAddress = _ipAddressDao.findById(ipId);
1304+
ipAddress.setRuleState(null);
1305+
_ipAddressDao.update(ipAddress.getId(), ipAddress);
12981306
return false;
12991307
}
13001308
}

setup/db/db/schema-4920to41000.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,4 @@ CREATE TABLE `cloud`.`guest_os_details` (
245245
CONSTRAINT `fk_guest_os_details__guest_os_id` FOREIGN KEY `fk_guest_os_details__guest_os_id`(`guest_os_id`) REFERENCES `guest_os`(`id`) ON DELETE CASCADE
246246
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
247247

248+
ALTER TABLE `user_ip_address` ADD COLUMN `rule_state` VARCHAR(32) COMMENT 'static rule state while removing';

0 commit comments

Comments
 (0)