mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
bug 10731: adding optional paramter for cidr
This commit is contained in:
parent
978b2640e2
commit
24510cd5fb
@ -18,6 +18,8 @@
|
||||
|
||||
package com.cloud.api.commands;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.api.ApiConstants;
|
||||
@ -25,6 +27,7 @@ import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.BaseCmd.CommandType;
|
||||
import com.cloud.api.response.LoadBalancerResponse;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.NetworkRuleConflictException;
|
||||
@ -60,6 +63,9 @@ public class CreateLoadBalancerRuleCmd extends BaseCmd implements LoadBalancer
|
||||
@Parameter(name=ApiConstants.PUBLIC_PORT, type=CommandType.INTEGER, required=true, description="the public port from where the network traffic will be load balanced from")
|
||||
private Integer publicPort;
|
||||
|
||||
@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the cidr list to forward traffic from")
|
||||
private List<String> cidrlist;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/////////////////// Accessors ///////////////////////
|
||||
@ -100,6 +106,10 @@ public class CreateLoadBalancerRuleCmd extends BaseCmd implements LoadBalancer
|
||||
return loadBalancerRuleName;
|
||||
}
|
||||
|
||||
public List<String> getSourceCidrList() {
|
||||
return cidrlist;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/////////////// API Implementation///////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
@ -111,6 +121,14 @@ public class CreateLoadBalancerRuleCmd extends BaseCmd implements LoadBalancer
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
if (cidrlist != null){
|
||||
for (String cidr: cidrlist){
|
||||
if (!NetUtils.isValidCIDR(cidr)){
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Source cidrs formatting error " + cidr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LoadBalancer result = null;
|
||||
try {
|
||||
result = _lbService.createLoadBalancerRule(this);
|
||||
|
||||
@ -53,6 +53,9 @@ public class LoadBalancerResponse extends BaseResponse {
|
||||
@SerializedName("algorithm")
|
||||
@Param(description = "the load balancer algorithm (source, roundrobin, leastconn)")
|
||||
private String algorithm;
|
||||
|
||||
@SerializedName(ApiConstants.CIDR_LIST) @Param(description="the cidr list to forward traffic from")
|
||||
private String cidrList;
|
||||
|
||||
@SerializedName("account")
|
||||
@Param(description = "the account of the load balancer rule")
|
||||
@ -122,6 +125,14 @@ public class LoadBalancerResponse extends BaseResponse {
|
||||
this.privatePort = privatePort;
|
||||
}
|
||||
|
||||
public String getCidrList() {
|
||||
return cidrList;
|
||||
}
|
||||
|
||||
public void setCidrList(String cidrs) {
|
||||
this.cidrList = cidrs;
|
||||
}
|
||||
|
||||
public String getAlgorithm() {
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
@ -66,6 +66,12 @@ public class LoadBalancingRule implements FirewallRule, LoadBalancer{
|
||||
return lb.getDefaultPortEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSourceCidrList() {
|
||||
return lb.getSourceCidrList();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getAlgorithm() {
|
||||
return lb.getAlgorithm();
|
||||
|
||||
@ -17,6 +17,8 @@
|
||||
*/
|
||||
package com.cloud.network.rules;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Definition for a LoadBalancer
|
||||
@ -33,4 +35,6 @@ public interface LoadBalancer extends FirewallRule {
|
||||
|
||||
String getAlgorithm();
|
||||
|
||||
public List<String> getSourceCidrList();
|
||||
|
||||
}
|
||||
|
||||
@ -718,9 +718,10 @@ public class ApiResponseHelper implements ResponseGenerator {
|
||||
lbResponse.setId(loadBalancer.getId());
|
||||
lbResponse.setName(loadBalancer.getName());
|
||||
lbResponse.setDescription(loadBalancer.getDescription());
|
||||
List<String> cidrs = ApiDBUtils.findPortForwardingSourceCidrs(loadBalancer.getId());
|
||||
lbResponse.setCidrList(StringUtils.join(cidrs, ","));
|
||||
|
||||
IPAddressVO publicIp = ApiDBUtils.findIpAddressById(loadBalancer.getSourceIpAddressId());
|
||||
|
||||
lbResponse.setPublicIpId(publicIp.getId());
|
||||
lbResponse.setPublicIp(publicIp.getAddress().addr());
|
||||
lbResponse.setPublicPort(Integer.toString(loadBalancer.getSourcePortStart()));
|
||||
|
||||
@ -18,11 +18,14 @@
|
||||
|
||||
package com.cloud.network;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.PrimaryKeyJoinColumn;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import com.cloud.network.rules.FirewallRuleVO;
|
||||
import com.cloud.network.rules.LoadBalancer;
|
||||
@ -49,16 +52,20 @@ public class LoadBalancerVO extends FirewallRuleVO implements LoadBalancer {
|
||||
@Column(name="default_port_end")
|
||||
private int defaultPortEnd;
|
||||
|
||||
@Transient
|
||||
List<String> sourceCidrs;
|
||||
|
||||
public LoadBalancerVO() {
|
||||
}
|
||||
|
||||
public LoadBalancerVO(String xId, String name, String description, long srcIpId, int srcPort, int dstPort, String algorithm, long networkId, long accountId, long domainId) {
|
||||
public LoadBalancerVO(String xId, String name, String description, long srcIpId, int srcPort, int dstPort, List<String> sourceCidrs, String algorithm, long networkId, long accountId, long domainId) {
|
||||
super(xId, srcIpId, srcPort, NetUtils.TCP_PROTO, networkId, accountId, domainId, Purpose.LoadBalancing);
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.algorithm = algorithm;
|
||||
this.defaultPortStart = dstPort;
|
||||
this.defaultPortEnd = dstPort;
|
||||
this.sourceCidrs = sourceCidrs;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -69,6 +76,15 @@ public class LoadBalancerVO extends FirewallRuleVO implements LoadBalancer {
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setSourceCidrList(List<String> sourceCidrs) {
|
||||
this.sourceCidrs=sourceCidrs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSourceCidrList() {
|
||||
return sourceCidrs;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -28,6 +28,9 @@ import javax.ejb.Local;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.network.LoadBalancerVO;
|
||||
import com.cloud.network.rules.PortForwardingRuleVO;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.db.DB;
|
||||
import com.cloud.utils.db.GenericDaoBase;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
@ -47,6 +50,8 @@ public class LoadBalancerDaoImpl extends GenericDaoBase<LoadBalancerVO, Long> im
|
||||
private final SearchBuilder<LoadBalancerVO> ListByIp;
|
||||
private final SearchBuilder<LoadBalancerVO> IpAndPublicPortSearch;
|
||||
private final SearchBuilder<LoadBalancerVO> AccountAndNameSearch;
|
||||
|
||||
protected final FirewallRulesCidrsDaoImpl _portForwardingRulesCidrsDao = ComponentLocator.inject(FirewallRulesCidrsDaoImpl.class);
|
||||
|
||||
protected LoadBalancerDaoImpl() {
|
||||
ListByIp = createSearchBuilder();
|
||||
@ -114,5 +119,53 @@ public class LoadBalancerDaoImpl extends GenericDaoBase<LoadBalancerVO, Long> im
|
||||
sc.setParameters("accountId", accountId);
|
||||
sc.setParameters("name", name);
|
||||
return findOneBy(sc);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveSourceCidrs(LoadBalancerVO loadBalancerRule) {
|
||||
List<String> cidrlist = loadBalancerRule.getSourceCidrList();
|
||||
if (cidrlist == null) {
|
||||
return;
|
||||
}
|
||||
_portForwardingRulesCidrsDao.persist(loadBalancerRule.getId(), cidrlist);
|
||||
}
|
||||
|
||||
|
||||
public void loadSourceCidrs(LoadBalancerVO loadBalancerRule){
|
||||
List<String> sourceCidrs = _portForwardingRulesCidrsDao.getSourceCidrs(loadBalancerRule.getId());
|
||||
loadBalancerRule.setSourceCidrList(sourceCidrs);
|
||||
}
|
||||
|
||||
|
||||
@Override @DB
|
||||
public LoadBalancerVO persist(LoadBalancerVO loadBalancerRule) {
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
|
||||
LoadBalancerVO dbfirewallRule = super.persist(loadBalancerRule);
|
||||
|
||||
saveSourceCidrs(loadBalancerRule);
|
||||
loadSourceCidrs(dbfirewallRule);
|
||||
|
||||
txn.commit();
|
||||
|
||||
return dbfirewallRule;
|
||||
}
|
||||
|
||||
|
||||
@Override @DB
|
||||
public boolean update(Long loadBalancerRuleId, LoadBalancerVO loadBalancerRule) {
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
|
||||
boolean persisted = super.update(loadBalancerRuleId, loadBalancerRule);
|
||||
if (!persisted) {
|
||||
return persisted;
|
||||
}
|
||||
|
||||
saveSourceCidrs(loadBalancerRule);
|
||||
txn.commit();
|
||||
|
||||
return persisted;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -394,8 +394,8 @@ public class LoadBalancingRulesManagerImpl implements LoadBalancingRulesManager,
|
||||
throw new InvalidParameterValueException("LB service is not supported in network id=" + networkId);
|
||||
}
|
||||
|
||||
LoadBalancerVO newRule = new LoadBalancerVO(lb.getXid(), lb.getName(), lb.getDescription(), lb.getSourceIpAddressId(), lb.getSourcePortEnd(), lb.getDefaultPortStart(), lb.getAlgorithm(),
|
||||
networkId, ipAddr.getAccountId(), ipAddr.getDomainId());
|
||||
LoadBalancerVO newRule = new LoadBalancerVO(lb.getXid(), lb.getName(), lb.getDescription(), lb.getSourceIpAddressId(), lb.getSourcePortEnd(), lb.getDefaultPortStart(),
|
||||
lb.getSourceCidrList(), lb.getAlgorithm(), networkId, ipAddr.getAccountId(), ipAddr.getDomainId());
|
||||
|
||||
newRule = _lbDao.persist(newRule);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user