Skip to content

Commit 228376f

Browse files
author
Andrey Volchkov
committed
fix: NsxResource.executeRequest DeleteNsxNatRuleCommand comparison bug
Fixes an issue in NsxResource.executeRequest where Network.Service comparison failed when DeleteNsxNatRuleCommand was executed in a different process. Due to serialization/deserialization, the deserialized Network.Service instance was not equal to the static instances Network.Service.StaticNat and Network.Service.PortForwarding, causing the comparison to always return false. fix
1 parent eb93f01 commit 228376f

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/DeleteNsxNatRuleCommand.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public String getProtocol() {
5454
return protocol;
5555
}
5656

57+
public String getNetworkServiceName() {
58+
if (service != null) {
59+
return service.getName();
60+
}
61+
return null;
62+
}
63+
5764
@Override
5865
public boolean equals(Object o) {
5966
if (this == o) {

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/resource/NsxResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,10 @@ private NsxAnswer executeRequest(CreateNsxPortForwardRuleCommand cmd) {
415415

416416
private NsxAnswer executeRequest(DeleteNsxNatRuleCommand cmd) {
417417
String ruleName = null;
418-
if (cmd.getService() == Network.Service.StaticNat) {
418+
if (Network.Service.StaticNat.getName().equals(cmd.getNetworkServiceName())) {
419419
ruleName = NsxControllerUtils.getStaticNatRuleName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(),
420420
cmd.getNetworkResourceId(), cmd.isResourceVpc());
421-
} else if (cmd.getService() == Network.Service.PortForwarding) {
421+
} else if (Network.Service.PortForwarding.getName().equals(cmd.getNetworkServiceName())) {
422422
ruleName = NsxControllerUtils.getPortForwardRuleName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(),
423423
cmd.getNetworkResourceId(), cmd.getRuleId(), cmd.isResourceVpc());
424424
}

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/resource/NsxResourceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
// under the License.
1717
package org.apache.cloudstack.resource;
1818

19+
import com.cloud.network.Network;
1920
import com.cloud.network.dao.NetworkVO;
2021
import com.cloud.utils.exception.CloudRuntimeException;
22+
import com.fasterxml.jackson.core.JsonProcessingException;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
2124
import com.vmware.nsx.model.TransportZone;
2225
import com.vmware.nsx.model.TransportZoneListResult;
2326
import com.vmware.nsx_policy.model.EnforcementPoint;
@@ -61,6 +64,7 @@
6164
import static org.mockito.ArgumentMatchers.anyString;
6265
import static org.mockito.Mockito.doThrow;
6366
import static org.mockito.Mockito.mock;
67+
import static org.mockito.Mockito.verify;
6468
import static org.mockito.Mockito.when;
6569

6670
@RunWith(MockitoJUnitRunner.class)
@@ -247,8 +251,11 @@ public void testCreatePortForwardRule() {
247251
@Test
248252
public void testDeleteNsxNatRule() {
249253
DeleteNsxNatRuleCommand cmd = new DeleteNsxNatRuleCommand(domainId, accountId, zoneId, 3L, "VPC01", true, 2L, 5L, "22", "tcp");
254+
Network.Service service = new Network.Service("PortForwarding");
255+
cmd.setService(service);
250256
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
251257
assertTrue(answer.getResult());
258+
verify(nsxApi).deleteNatRule(service, "22", "tcp", "VPC01", "D1-A2-Z1-V3", "D1-A2-Z1-V3-PF5");
252259
}
253260

254261
@Test

0 commit comments

Comments
 (0)