Skip to content

Commit 273ae03

Browse files
authored
Allow updating of Load Balancer source CIDR list (#11568)
1 parent 5730931 commit 273ae03

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.cloud.network.rules.FirewallRule;
3434
import com.cloud.network.rules.LoadBalancer;
3535
import com.cloud.user.Account;
36+
import java.util.List;
3637

3738
@APICommand(name = "updateLoadBalancerRule", description = "Updates load balancer", responseObject = LoadBalancerResponse.class,
3839
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
@@ -64,6 +65,9 @@ public class UpdateLoadBalancerRuleCmd extends BaseAsyncCustomIdCmd {
6465
@Parameter(name = ApiConstants.PROTOCOL, type = CommandType.STRING, description = "The protocol for the LB")
6566
private String lbProtocol;
6667

68+
@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the cidr list to forward traffic from", since = "4.22")
69+
private List<String> cidrList;
70+
6771
/////////////////////////////////////////////////////
6872
/////////////////// Accessors ///////////////////////
6973
/////////////////////////////////////////////////////
@@ -92,6 +96,9 @@ public String getLbProtocol() {
9296
return lbProtocol;
9397
}
9498

99+
public List<String> getCidrList() {
100+
return cidrList;
101+
}
95102
/////////////////////////////////////////////////////
96103
/////////////// API Implementation///////////////////
97104
/////////////////////////////////////////////////////

engine/schema/src/main/java/com/cloud/network/dao/LoadBalancerVO.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,13 @@ public String toString() {
144144
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(
145145
this, "id", "uuid", "name", "purpose", "state"));
146146
}
147+
148+
/**
149+
* Sets the CIDR list associated with this load balancer rule.
150+
*
151+
* @param cidrList a comma-separated list of CIDR strings, e.g. "1.2.3.4/24,1.2.3.5/24" or an empty string e.g. "" to clear the restrictions
152+
*/
153+
public void setCidrList(String cidrList) {
154+
this.cidrList = cidrList;
155+
}
147156
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 com.cloud.network.dao;
18+
19+
import org.junit.Test;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNull;
22+
23+
public class LoadBalancerVOTest {
24+
@Test
25+
public void testSetCidrList() {
26+
LoadBalancerVO loadBalancer = new LoadBalancerVO();
27+
String cidrList = "192.168.1.0/24,10.0.0.0/16";
28+
loadBalancer.setCidrList(cidrList);
29+
assertEquals(cidrList, loadBalancer.getCidrList());
30+
}
31+
32+
@Test
33+
public void testSetCidrListEmpty() {
34+
LoadBalancerVO loadBalancer = new LoadBalancerVO();
35+
loadBalancer.setCidrList("");
36+
assertEquals("", loadBalancer.getCidrList());
37+
}
38+
39+
@Test
40+
public void testSetCidrListNull() {
41+
LoadBalancerVO loadBalancer = new LoadBalancerVO();
42+
loadBalancer.setCidrList(null);
43+
assertNull(loadBalancer.getCidrList());
44+
}
45+
}

server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,17 @@ public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) {
22572257
}
22582258

22592259
validateInputsForExternalNetworkProvider(lb, algorithm, lbProtocol);
2260+
2261+
List<String> cidrList = cmd.getCidrList();
2262+
2263+
if (cidrList != null) {
2264+
String cidrListStr = StringUtils.join(cidrList, ",");
2265+
if (!cidrListStr.isEmpty() && !NetUtils.isValidCidrList(cidrListStr)) {
2266+
throw new InvalidParameterValueException("Invalid CIDR list: " + cidrListStr);
2267+
}
2268+
lb.setCidrList(cidrListStr);
2269+
}
2270+
22602271
// Validate rule in LB provider
22612272
LoadBalancingRule rule = getLoadBalancerRuleToApply(lb);
22622273
if (!validateLbRule(rule)) {
@@ -2266,10 +2277,12 @@ public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) {
22662277
LoadBalancerVO tmplbVo = _lbDao.findById(lbRuleId);
22672278
boolean success = _lbDao.update(lbRuleId, lb);
22682279

2269-
// If algorithm or lb protocol is changed, have to reapply the lb config
2270-
boolean needToReApplyRule = (algorithm != null && !algorithm.equals(tmplbVo.getAlgorithm()))
2271-
|| (lbProtocol != null && !lbProtocol.equals(tmplbVo.getLbProtocol()));
2272-
if (needToReApplyRule) {
2280+
// Check if algorithm, lb protocol, or cidrlist has changed, and reapply the lb config if needed
2281+
boolean algorithmChanged = !Objects.equals(algorithm, tmplbVo.getAlgorithm());
2282+
boolean protocolChanged = !Objects.equals(lbProtocol, tmplbVo.getLbProtocol());
2283+
boolean cidrListChanged = !Objects.equals(tmplbVo.getCidrList(), lb.getCidrList());
2284+
2285+
if (algorithmChanged || protocolChanged || cidrListChanged) {
22732286
try {
22742287
lb.setState(FirewallRule.State.Add);
22752288
_lbDao.persist(lb);
@@ -2293,9 +2306,8 @@ public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) {
22932306
if (lbBackup.getAlgorithm() != null) {
22942307
lb.setAlgorithm(lbBackup.getAlgorithm());
22952308
}
2296-
if (lbBackup.getLbProtocol() != null) {
2297-
lb.setLbProtocol(lbBackup.getLbProtocol());
2298-
}
2309+
2310+
lb.setCidrList(lbBackup.getCidrList());
22992311
lb.setState(lbBackup.getState());
23002312
_lbDao.update(lb.getId(), lb);
23012313
_lbDao.persist(lb);

0 commit comments

Comments
 (0)