Skip to content

Commit ff85d3d

Browse files
authored
Nsx add lb rule (#8161)
* NSX: Create and delete NSX Static Nat rules * fix issues with static nat * add static nat * Support to add and delete Port forward rules * add license * fix adding multiple pf rules * cleanup * NSX: Add support to create and delete Load balancer rules * fix deletion of lb rules * add header file and update protocol detail
1 parent ce1659e commit ff85d3d

File tree

11 files changed

+579
-15
lines changed

11 files changed

+579
-15
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.agent.api;
18+
19+
import org.apache.cloudstack.resource.NsxLoadBalancerMember;
20+
21+
import java.util.List;
22+
23+
public class CreateNsxLoadBalancerRuleCommand extends NsxNetworkCommand {
24+
25+
private final String publicPort;
26+
private final String algorithm;
27+
private final String protocol;
28+
List<NsxLoadBalancerMember> memberList;
29+
30+
private final long lbId;
31+
public CreateNsxLoadBalancerRuleCommand(long domainId, long accountId, long zoneId, Long networkResourceId,
32+
String networkResourceName, boolean isResourceVpc,
33+
List<NsxLoadBalancerMember> memberList, long lbId, String publicPort,
34+
String algorithm, String protocol) {
35+
super(domainId, accountId, zoneId, networkResourceId, networkResourceName, isResourceVpc);
36+
this.lbId = lbId;
37+
this.memberList = memberList;
38+
this.publicPort = publicPort;
39+
this.algorithm = algorithm;
40+
this.protocol = protocol;
41+
}
42+
43+
44+
public long getLbId() {
45+
return lbId;
46+
}
47+
48+
public String getPublicPort() {
49+
return publicPort;
50+
}
51+
52+
public List<NsxLoadBalancerMember> getMemberList() {
53+
return memberList;
54+
}
55+
56+
public String getAlgorithm() {
57+
return algorithm;
58+
}
59+
60+
public String getProtocol() {
61+
return protocol;
62+
}
63+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.agent.api;
18+
19+
import org.apache.cloudstack.resource.NsxLoadBalancerMember;
20+
21+
import java.util.List;
22+
23+
public class DeleteNsxLoadBalancerRuleCommand extends NsxNetworkCommand {
24+
private long lbId;
25+
List<NsxLoadBalancerMember> memberList;
26+
27+
public DeleteNsxLoadBalancerRuleCommand(long domainId, long accountId, long zoneId, Long networkResourceId,
28+
String networkResourceName, boolean isResourceVpc,
29+
List<NsxLoadBalancerMember> memberList, long lbId, long vmId) {
30+
super(domainId, accountId, zoneId, networkResourceId, networkResourceName, isResourceVpc, vmId);
31+
this.lbId = lbId;
32+
this.memberList = memberList;
33+
}
34+
35+
public long getLbId() {
36+
return lbId;
37+
}
38+
39+
public List<NsxLoadBalancerMember> getMemberList() { return memberList; }
40+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ public NsxNetworkCommand(long domainId, long accountId, long zoneId, Long networ
3838
}
3939

4040
public NsxNetworkCommand(long domainId, long accountId, long zoneId, Long networkResourceId, String networkResourceName,
41-
boolean isResourceVpc, Long vmId) {
41+
boolean isResourceVpc) {
4242
super(domainId, accountId, zoneId);
4343
this.networkResourceId = networkResourceId;
4444
this.networkResourceName = networkResourceName;
4545
this.isResourceVpc = isResourceVpc;
46+
}
47+
48+
public NsxNetworkCommand(long domainId, long accountId, long zoneId, Long networkResourceId, String networkResourceName,
49+
boolean isResourceVpc, Long vmId) {
50+
this(domainId, accountId, zoneId, networkResourceId, networkResourceName, isResourceVpc);
4651
this.vmId = vmId;
4752
}
4853

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.resource;
18+
19+
public class NsxLoadBalancerMember {
20+
private long vmId;
21+
private String vmIp;
22+
private int port;
23+
24+
public NsxLoadBalancerMember(long vmId, String vmIp, int port) {
25+
this.vmId = vmId;
26+
this.vmIp = vmIp;
27+
this.port = port;
28+
}
29+
30+
public long getVmId() {
31+
return vmId;
32+
}
33+
34+
public String getVmIp() {
35+
return vmIp;
36+
}
37+
38+
public int getPort() {
39+
return port;
40+
}
41+
}

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

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

19+
import java.util.List;
20+
1921
public class NsxNetworkRule {
2022
private long domainId;
2123
private long accountId;
@@ -30,6 +32,8 @@ public class NsxNetworkRule {
3032
private String publicPort;
3133
private String privatePort;
3234
private String protocol;
35+
private String algorithm;
36+
private List<NsxLoadBalancerMember> memberList;
3337

3438
public long getDomainId() {
3539
return domainId;
@@ -135,6 +139,22 @@ public void setProtocol(String protocol) {
135139
this.protocol = protocol;
136140
}
137141

142+
public void setAlgorithm(String algorithm) {
143+
this.algorithm = algorithm;
144+
}
145+
146+
public String getAlgorithm() {
147+
return algorithm;
148+
}
149+
150+
public List<NsxLoadBalancerMember> getMemberList() {
151+
return memberList;
152+
}
153+
154+
public void setMemberList(List<NsxLoadBalancerMember> memberList) {
155+
this.memberList = memberList;
156+
}
157+
138158
public static final class Builder {
139159
private long domainId;
140160
private long accountId;
@@ -150,6 +170,8 @@ public static final class Builder {
150170
private String publicPort;
151171
private String privatePort;
152172
private String protocol;
173+
private String algorithm;
174+
private List<NsxLoadBalancerMember> memberList;
153175

154176
public Builder() {
155177
}
@@ -220,6 +242,16 @@ public Builder setProtocol(String protocol) {
220242
return this;
221243
}
222244

245+
public Builder setAlgorithm(String algorithm) {
246+
this.algorithm = algorithm;
247+
return this;
248+
}
249+
250+
public Builder setMemberList(List<NsxLoadBalancerMember> memberList) {
251+
this.memberList = memberList;
252+
return this;
253+
}
254+
223255
public NsxNetworkRule build() {
224256
NsxNetworkRule rule = new NsxNetworkRule();
225257
rule.setDomainId(this.domainId);
@@ -235,6 +267,8 @@ public NsxNetworkRule build() {
235267
rule.setPrivatePort(this.privatePort);
236268
rule.setProtocol(this.protocol);
237269
rule.setRuleId(this.ruleId);
270+
rule.setAlgorithm(this.algorithm);
271+
rule.setMemberList(this.memberList);
238272
return rule;
239273
}
240274
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@
3636
import org.apache.cloudstack.NsxAnswer;
3737
import org.apache.cloudstack.StartupNsxCommand;
3838
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
39+
import org.apache.cloudstack.agent.api.CreateNsxLoadBalancerRuleCommand;
3940
import org.apache.cloudstack.agent.api.CreateNsxPortForwardRuleCommand;
4041
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
4142
import org.apache.cloudstack.agent.api.CreateNsxStaticNatCommand;
4243
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
44+
import org.apache.cloudstack.agent.api.DeleteNsxLoadBalancerRuleCommand;
4345
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
4446
import org.apache.cloudstack.agent.api.DeleteNsxNatRuleCommand;
4547
import org.apache.cloudstack.agent.api.DeleteNsxTier1GatewayCommand;
@@ -114,6 +116,10 @@ public Answer executeRequest(Command cmd) {
114116
return executeRequest((DeleteNsxNatRuleCommand) cmd);
115117
} else if (cmd instanceof CreateNsxPortForwardRuleCommand) {
116118
return executeRequest((CreateNsxPortForwardRuleCommand) cmd);
119+
} else if (cmd instanceof CreateNsxLoadBalancerRuleCommand) {
120+
return executeRequest((CreateNsxLoadBalancerRuleCommand) cmd);
121+
} else if (cmd instanceof DeleteNsxLoadBalancerRuleCommand) {
122+
return executeRequest((DeleteNsxLoadBalancerRuleCommand) cmd);
117123
} else {
118124
return Answer.createUnsupportedCommandAnswer(cmd);
119125
}
@@ -401,6 +407,33 @@ private NsxAnswer executeRequest(DeleteNsxNatRuleCommand cmd) {
401407
return new NsxAnswer(cmd, true, null);
402408
}
403409

410+
private NsxAnswer executeRequest(CreateNsxLoadBalancerRuleCommand cmd) {
411+
String tier1GatewayName = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(),
412+
cmd.getNetworkResourceId(), cmd.isResourceVpc());
413+
String ruleName = NsxControllerUtils.getLoadBalancerRuleName(tier1GatewayName, cmd.getLbId());
414+
try {
415+
nsxApiClient.createAndAddNsxLbVirtualServer(tier1GatewayName, cmd.getLbId(), cmd.getPublicIp(), cmd.getPublicPort(),
416+
cmd.getMemberList(), cmd.getAlgorithm(), cmd.getProtocol());
417+
} catch (Exception e) {
418+
LOGGER.error(String.format("Failed to add NSX load balancer rule %s for network: %s", ruleName, cmd.getNetworkResourceName()));
419+
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));
420+
}
421+
return new NsxAnswer(cmd, true, null);
422+
}
423+
424+
private NsxAnswer executeRequest(DeleteNsxLoadBalancerRuleCommand cmd) {
425+
String tier1GatewayName = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(),
426+
cmd.getZoneId(), cmd.getNetworkResourceId(), cmd.isResourceVpc());
427+
String ruleName = NsxControllerUtils.getLoadBalancerRuleName(tier1GatewayName, cmd.getLbId());
428+
try {
429+
nsxApiClient.deleteNsxLbResources(tier1GatewayName, cmd.getLbId(), cmd.getVmId());
430+
} catch (Exception e) {
431+
LOGGER.error(String.format("Failed to add NSX load balancer rule %s for network: %s", ruleName, cmd.getNetworkResourceName()));
432+
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));
433+
}
434+
return new NsxAnswer(cmd, true, null);
435+
}
436+
404437
@Override
405438
public boolean start() {
406439
return true;

0 commit comments

Comments
 (0)