Skip to content

Commit 270a440

Browse files
committed
nsx patches
1 parent 71f47d6 commit 270a440

7 files changed

Lines changed: 741 additions & 60 deletions

File tree

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: 3 additions & 3 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
}
@@ -456,7 +456,7 @@ private NsxAnswer executeRequest(DeleteNsxLoadBalancerRuleCommand cmd) {
456456
try {
457457
nsxApiClient.deleteNsxLbResources(tier1GatewayName, cmd.getLbId());
458458
} catch (Exception e) {
459-
logger.error(String.format("Failed to add NSX load balancer rule %s for network: %s", ruleName, cmd.getNetworkResourceName()));
459+
logger.error(String.format("Failed to delete NSX load balancer rule %s for network: %s", ruleName, cmd.getNetworkResourceName()));
460460
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));
461461
}
462462
return new NsxAnswer(cmd, true, null);

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxApiClient.java

Lines changed: 176 additions & 57 deletions
Large diffs are not rendered by default.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.service;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.Objects;
22+
import java.util.function.BiConsumer;
23+
import java.util.function.Function;
24+
25+
class PagedFetcher<R, T> {
26+
27+
private final Function<String, R> fetchPage;
28+
private Function<R, String> cursorExtractor;
29+
private Function<R, List<T>> itemsExtractor;
30+
private BiConsumer<R, List<T>> itemsSetter;
31+
32+
static <R, T> PagedFetcher<R, T> withPageFetcher(Function<String, R> pageFetcher) {
33+
return new PagedFetcher<>(pageFetcher);
34+
}
35+
36+
PagedFetcher<R, T> cursorExtractor(Function<R, String> cursorProvider) {
37+
this.cursorExtractor = cursorProvider;
38+
return this;
39+
}
40+
41+
PagedFetcher<R, T> itemsExtractor(Function<R, List<T>> resultsProvider) {
42+
this.itemsExtractor = resultsProvider;
43+
return this;
44+
}
45+
46+
PagedFetcher<R, T> itemsSetter(BiConsumer<R, List<T>> resultsSetter) {
47+
this.itemsSetter = resultsSetter;
48+
return this;
49+
}
50+
51+
private PagedFetcher(Function<String, R> pageFetcher) {
52+
this.fetchPage = pageFetcher;
53+
}
54+
55+
R fetchAll() {
56+
Objects.requireNonNull(cursorExtractor, "Cursor extractor must be set");
57+
Objects.requireNonNull(itemsExtractor, "Items extractor must be set");
58+
Objects.requireNonNull(itemsSetter, "Items setter must be set");
59+
60+
R firstPage = fetchPage.apply(null);
61+
String cursor = cursorExtractor.apply(firstPage);
62+
if (cursor == null || cursor.isEmpty()) {
63+
return firstPage;
64+
}
65+
66+
List<T> firstResults = itemsExtractor.apply(firstPage);
67+
List<T> allItems = firstResults != null
68+
? new ArrayList<>(firstResults)
69+
: new ArrayList<>();
70+
while (cursor != null && !cursor.isEmpty()) {
71+
R nextPage = fetchPage.apply(cursor);
72+
List<T> nextItems = itemsExtractor.apply(nextPage);
73+
if (nextItems != null && !nextItems.isEmpty()) {
74+
allItems.addAll(nextItems);
75+
}
76+
cursor = cursorExtractor.apply(nextPage);
77+
}
78+
79+
itemsSetter.accept(firstPage, allItems);
80+
return firstPage;
81+
}
82+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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;
2122
import com.vmware.nsx.model.TransportZone;
@@ -61,6 +62,7 @@
6162
import static org.mockito.ArgumentMatchers.anyString;
6263
import static org.mockito.Mockito.doThrow;
6364
import static org.mockito.Mockito.mock;
65+
import static org.mockito.Mockito.verify;
6466
import static org.mockito.Mockito.when;
6567

6668
@RunWith(MockitoJUnitRunner.class)
@@ -247,8 +249,12 @@ public void testCreatePortForwardRule() {
247249
@Test
248250
public void testDeleteNsxNatRule() {
249251
DeleteNsxNatRuleCommand cmd = new DeleteNsxNatRuleCommand(domainId, accountId, zoneId, 3L, "VPC01", true, 2L, 5L, "22", "tcp");
252+
Network.Service service = mock(Network.Service.class);
253+
when(service.getName()).thenReturn("PortForwarding");
254+
cmd.setService(service);
250255
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
251256
assertTrue(answer.getResult());
257+
verify(nsxApi).deleteNatRule(service, "22", "tcp", "VPC01", "D1-A2-Z1-V3", "D1-A2-Z1-V3-PF5");
252258
}
253259

254260
@Test

0 commit comments

Comments
 (0)