Skip to content

Commit 2e2ee2f

Browse files
ustcweizhouPrasanna Santhanam
authored andcommitted
CLOUDSTACK-1088: EnableStaticNat error will clear the data in database
The issue occur in two conditions (1) If I use two sessions or browsers to EnableStaticNat on CloudStack UI. one is successful, the other is failed. However, there is no ip in database. (2) If I use API call EnableStaticNat several times The first time succeed, the second failed, the third succeed. the result is success-fail-success-fail-success-fail, which it is not correct. Reported-by: Wei Zhou <w.zhou@leaseweb.com> Reviewed-by: https://reviews.apache.org/r/9254/ Signed-off-by: Prasanna Santhanam <tsp@apache.org>
1 parent 8234dfa commit 2e2ee2f

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ public boolean enableStaticNat(long ipId, long vmId, long networkId, boolean isS
412412

413413
// Verify input parameters
414414
boolean performedIpAssoc = false;
415-
boolean result = false;
415+
boolean isOneToOneNat = ipAddress.isOneToOneNat();
416+
Long associatedWithVmId = ipAddress.getAssociatedWithVmId();
416417
try {
417418
Network network = _networkModel.getNetwork(networkId);
418419
if (network == null) {
@@ -476,28 +477,26 @@ public boolean enableStaticNat(long ipId, long vmId, long networkId, boolean isS
476477
// enable static nat on the backend
477478
s_logger.trace("Enabling static nat for ip address " + ipAddress + " and vm id=" + vmId + " on the backend");
478479
if (applyStaticNatForIp(ipId, false, caller, false)) {
479-
result = true;
480+
performedIpAssoc = false; // ignor unassignIPFromVpcNetwork in finally block
481+
return true;
480482
} else {
481483
s_logger.warn("Failed to enable static nat rule for ip address " + ipId + " on the backend");
484+
ipAddress.setOneToOneNat(isOneToOneNat);
485+
ipAddress.setAssociatedWithVmId(associatedWithVmId);
486+
_ipAddressDao.update(ipAddress.getId(), ipAddress);
482487
}
483488
} else {
484489
s_logger.warn("Failed to update ip address " + ipAddress + " in the DB as a part of enableStaticNat");
485490

486491
}
487492
} finally {
488-
if (!result) {
489-
ipAddress.setOneToOneNat(false);
490-
ipAddress.setAssociatedWithVmId(null);
491-
_ipAddressDao.update(ipAddress.getId(), ipAddress);
492-
493-
if (performedIpAssoc) {
494-
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
495-
IpAddress ip = _ipAddressDao.findById(ipAddress.getId());
496-
_vpcMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
497-
}
493+
if (performedIpAssoc) {
494+
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
495+
IpAddress ip = _ipAddressDao.findById(ipAddress.getId());
496+
_vpcMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
498497
}
499498
}
500-
return result;
499+
return false;
501500
}
502501

503502
protected void isIpReadyForStaticNat(long vmId, IPAddressVO ipAddress, Account caller, long callerUserId)

0 commit comments

Comments
 (0)